Skip to content

Commit

Permalink
[minor] add Dimensions type
Browse files Browse the repository at this point in the history
  • Loading branch information
electrovir committed Nov 8, 2023
1 parent a344dc7 commit 390a630
Show file tree
Hide file tree
Showing 18 changed files with 495 additions and 676 deletions.
833 changes: 321 additions & 512 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "augment-vir",
"version": "21.5.1",
"version": "21.6.0",
"private": true,
"homepage": "https://github.com/electrovir/augment-vir",
"bugs": {
Expand Down
6 changes: 3 additions & 3 deletions packages/browser-testing/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/browser-testing",
"version": "21.5.1",
"version": "21.6.0",
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
"bugs": {
"url": "https://github.com/electrovir/augment-vir/issues"
Expand All @@ -23,8 +23,8 @@
"test:coverage": "npm test"
},
"dependencies": {
"@augment-vir/common": "^21.5.1",
"@augment-vir/testing": "^21.5.1",
"@augment-vir/common": "^21.6.0",
"@augment-vir/testing": "^21.6.0",
"@open-wc/testing": "^3.2.2",
"@types/mocha": "^10.0.3",
"@web/test-runner-commands": "^0.9.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/browser",
"version": "21.5.1",
"version": "21.6.0",
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/browser",
"bugs": {
"url": "https://github.com/electrovir/augment-vir/issues"
Expand All @@ -24,12 +24,12 @@
"test:watch": "web-test-runner --color --config configs/web-test-runner.config.mjs --watch"
},
"dependencies": {
"@augment-vir/common": "^21.5.1",
"@augment-vir/common": "^21.6.0",
"element-vir": "^16.5.2",
"html-spec-tags": "^1.0.0"
},
"devDependencies": {
"@augment-vir/browser-testing": "^21.5.1",
"@augment-vir/browser-testing": "^21.6.0",
"@open-wc/testing": "^3.2.2",
"@types/chai": "^4.3.9",
"@types/mocha": "^10.0.3",
Expand Down
6 changes: 3 additions & 3 deletions packages/chai/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/chai",
"version": "21.5.1",
"version": "21.6.0",
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
"bugs": {
"url": "https://github.com/electrovir/augment-vir/issues"
Expand All @@ -23,8 +23,8 @@
"test:coverage": "npm test"
},
"dependencies": {
"@augment-vir/common": "^21.5.1",
"@augment-vir/testing": "^21.5.1",
"@augment-vir/common": "^21.6.0",
"@augment-vir/testing": "^21.6.0",
"type-fest": "^4.6.0"
},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions packages/common-tests/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/common-tests",
"version": "21.5.1",
"version": "21.6.0",
"private": true,
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common-tests",
"bugs": {
Expand All @@ -22,9 +22,9 @@
"test:types": "tsc --noEmit"
},
"devDependencies": {
"@augment-vir/chai": "^21.5.1",
"@augment-vir/common": "^21.5.1",
"@augment-vir/node-js": "^21.5.1",
"@augment-vir/chai": "^21.6.0",
"@augment-vir/common": "^21.6.0",
"@augment-vir/node-js": "^21.6.0",
"@electrovir/nyc": "^15.1.0-fix0",
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@types/chai": "^4.3.9",
Expand Down
133 changes: 103 additions & 30 deletions packages/common-tests/src/tests/common-number.test.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
import {itCases} from '@augment-vir/chai';
import {addCommasToNumber, clamp, convertIntoNumber, ensureMinAndMax} from '@augment-vir/common';
import {expect} from 'chai';
import {
Dimensions,
addCommasToNumber,
clamp,
convertIntoNumber,
ensureMinAndMax,
round,
toEnsuredNumber,
} from '@augment-vir/common';
import {describe, it} from 'mocha';

describe(clamp.name, () => {
it('should successfully clamp downwards', () => {
expect(
clamp({
max: 45,
min: 31,
value: 150,
}),
).to.equal(45);
});

it('should successfully clamp upwards', () => {
expect(
clamp({
max: 45,
min: 31,
value: 13,
}),
).to.equal(31);
});

it("shouldn't change values in the middle", () => {
expect(
clamp({
max: 45,
min: 31,
itCases(clamp, [
{
it: 'does not alter a within-range value',
input: {
max: 50,
min: 40,
value: 42,
}),
).to.equal(42);
});
},
expect: 42,
},
{
it: 'clamps a too high number',
input: {
max: 50,
min: 40,
value: 1_000,
},
expect: 50,
},
{
it: 'clamps a too low number',
input: {
max: 50,
min: 40,
value: 20,
},
expect: 40,
},
]);
});

describe(convertIntoNumber.name, () => {
Expand Down Expand Up @@ -183,3 +190,69 @@ describe(ensureMinAndMax.name, () => {
},
]);
});

describe(toEnsuredNumber.name, () => {
itCases(toEnsuredNumber, [
{
it: 'converts a string to a number',
input: '5',
expect: 5,
},
{
it: 'errors on invalid number string',
input: '5-3',
throws: Error,
},
{
it: 'errors on object input',
input: {},
throws: Error,
},
]);
});

describe(round.name, () => {
itCases(round, [
{
it: 'rounds down with 2 decimals',
input: {
digits: 2,
number: 1.123456,
},
expect: 1.12,
},
{
it: 'rounds up with 2 decimals',
input: {
digits: 2,
number: 1.125456,
},
expect: 1.13,
},
{
it: 'rounds up with 3 decimals and a carry',
input: {
digits: 3,
number: 1.129556,
},
expect: 1.13,
},
{
it: 'rounds up with 0 decimals',
input: {
digits: 0,
number: 1.564123,
},
expect: 2,
},
]);
});

describe('Dimensions', () => {
it('is assignable to from expected object shape', () => {
const testDimensions: Dimensions = {
width: 0,
height: Infinity,
};
});
});
59 changes: 0 additions & 59 deletions packages/common-tests/src/tests/number.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/common",
"version": "21.5.1",
"version": "21.6.0",
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
"bugs": {
"url": "https://github.com/electrovir/augment-vir/issues"
Expand Down
59 changes: 41 additions & 18 deletions packages/common/src/augments/common-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,6 @@ export function addCommasToNumber(input: number | string): string {
].join('');
}

export function clamp(
/**
* This uses a destructured object so that consumers cannot get confused as to which input is
* which (which would be easy to do since they're all of the same type).
*/
{
value,
min,
max,
}: {
value: number;
min: number;
max: number;
},
): number {
return Math.max(Math.min(value, max), min);
}

export function convertIntoNumber(input: unknown): number {
if (typeof input === 'number') {
return input;
Expand Down Expand Up @@ -78,3 +60,44 @@ export function ensureMinAndMax({min, max}: {min: number; max: number}): {
return {min, max};
}
}

export function toEnsuredNumber(input: any): number {
const numeric = Number(input);

if (isNaN(numeric)) {
throw new Error(`Cannot convert given input to a number: ${input}`);
} else {
return numeric;
}
}
/**
* If the given value is outside the given min/max bounds, instead of clamping the number (as the
* `clamp` function does), this function wraps the value around to the next bound.
*
* @example
* wrapNumber({min: 0, max: 100, value: 101}) == 0;
*/
export function wrapNumber({max, min, value}: {value: number; max: number; min: number}): number {
if (value > max) {
return min;
} else if (value < min) {
return max;
}

return value;
}

export function round(inputs: {number: number; digits: number}): number {
const digitFactor = Math.pow(10, inputs.digits);
const multiplied = inputs.number * digitFactor;

return Number((Math.round(multiplied) / digitFactor).toFixed(inputs.digits));
}

/** Clamp's the given value to within the min and max bounds, inclusive. */
export function clamp({value, min, max}: {value: number; min: number; max: number}) {
return Math.min(Math.max(value, min), max);
}

/** Standard box dimensions. */
export type Dimensions = {width: number; height: number};
Loading

0 comments on commit 390a630

Please sign in to comment.