Skip to content

Commit

Permalink
[minor] add toNumber, toMaybeNumber to @augment-vir/common
Browse files Browse the repository at this point in the history
  • Loading branch information
electrovir committed May 31, 2024
1 parent 501d7e3 commit 55f4cf4
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 74 deletions.
60 changes: 30 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "augment-vir",
"version": "28.0.2",
"version": "28.1.0",
"private": true,
"homepage": "https://github.com/electrovir/augment-vir",
"bugs": {
Expand All @@ -15,8 +15,6 @@
"name": "electrovir",
"url": "https://github.com/electrovir"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"workspaces": [
"./packages/*"
],
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": "28.0.2",
"version": "28.1.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": "^28.0.2",
"@augment-vir/testing": "^28.0.2",
"@augment-vir/common": "^28.1.0",
"@augment-vir/testing": "^28.1.0",
"@open-wc/testing": "^4.0.0",
"@types/mocha": "^10.0.6",
"@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": "28.0.2",
"version": "28.1.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": "^28.0.2",
"@augment-vir/common": "^28.1.0",
"html-spec-tags": "^2.2.0",
"run-time-assertions": "^1.0.0"
},
"devDependencies": {
"@augment-vir/browser-testing": "^28.0.2",
"@augment-vir/browser-testing": "^28.1.0",
"@open-wc/testing": "^4.0.0",
"@types/chai": "^4.3.12",
"@types/mocha": "^10.0.6",
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": "28.0.2",
"version": "28.1.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": "^28.0.2",
"@augment-vir/testing": "^28.0.2",
"@augment-vir/common": "^28.1.0",
"@augment-vir/testing": "^28.1.0",
"type-fest": "^4.12.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": "28.0.2",
"version": "28.1.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": "^28.0.2",
"@augment-vir/common": "^28.0.2",
"@augment-vir/node-js": "^28.0.2",
"@augment-vir/chai": "^28.1.0",
"@augment-vir/common": "^28.1.0",
"@augment-vir/node-js": "^28.1.0",
"@electrovir/nyc": "^15.1.0-fix0",
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@types/chai": "^4.3.14",
Expand Down
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": "28.0.2",
"version": "28.1.0",
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
"bugs": {
"url": "https://github.com/electrovir/augment-vir/issues"
Expand Down
45 changes: 32 additions & 13 deletions packages/common/src/augments/common-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@ export function addCommasToNumber(input: number | string): string {
].join('');
}

export function convertIntoNumber(input: unknown): number {
if (typeof input === 'number') {
return input;
} else if (typeof input === 'string') {
return Number(removeCommasFromNumberString(input));
} else {
return Number(input);
}
}

export function doesRequireScientificNotation(input: number): boolean {
return String(input).includes('e');
}
Expand All @@ -61,11 +51,40 @@ export function ensureMinAndMax({min, max}: {min: number; max: number}): {
}
}

export function toEnsuredNumber(input: any): number {
const numeric = Number(input);
/**
* Tries to convert the input into a number. Handles strings with commas. Note: this might return
* `NaN`.
*/
export function toNumber(input: unknown): number {
if (typeof input === 'number') {
return input;
} else if (typeof input === 'string') {
return Number(removeCommasFromNumberString(input));
} else {
return Number(input);
}
}

/** @deprecated Use {@link toNumber} instead. */
export const convertIntoNumber = toNumber;

/** Tries to convert the input into a number and throws an error if `NaN` is created. */
export function toEnsuredNumber(input: unknown): number {
const numeric = toMaybeNumber(input);

if (numeric == undefined) {
throw new Error(`Cannot convert to a number: ${input}`);
} else {
return numeric;
}
}

/** Tries to convert the input into a number and returns `undefined` if `NaN` is created. */
export function toMaybeNumber(input: unknown): number | undefined {
const numeric = toNumber(input);

if (isNaN(numeric)) {
throw new Error(`Cannot convert given input to a number: ${input}`);
return undefined;
} else {
return numeric;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/docker/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/docker",
"version": "28.0.2",
"version": "28.1.0",
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/docker",
"bugs": {
"url": "https://github.com/electrovir/augment-vir/issues"
Expand All @@ -22,8 +22,8 @@
"test:coverage": "npm test"
},
"dependencies": {
"@augment-vir/common": "^28.0.2",
"@augment-vir/node-js": "^28.0.2"
"@augment-vir/common": "^28.1.0",
"@augment-vir/node-js": "^28.1.0"
},
"devDependencies": {
"typescript": "5.3.3"
Expand Down
6 changes: 3 additions & 3 deletions packages/node-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/node-js",
"version": "28.0.2",
"version": "28.1.0",
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/node-js",
"bugs": {
"url": "https://github.com/electrovir/augment-vir/issues"
Expand All @@ -22,7 +22,7 @@
"test:coverage": "npm test coverage"
},
"dependencies": {
"@augment-vir/common": "^28.0.2",
"@augment-vir/common": "^28.1.0",
"ansi-colors": "^4.1.3",
"axios": "^1.6.8",
"fs-extra": "^11.2.0",
Expand All @@ -31,7 +31,7 @@
"type-fest": "^4.12.0"
},
"devDependencies": {
"@augment-vir/chai": "^28.0.2",
"@augment-vir/chai": "^28.1.0",
"@electrovir/nyc": "^15.1.0-fix0",
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@types/chai": "^4.3.14",
Expand Down
Loading

0 comments on commit 55f4cf4

Please sign in to comment.