@augment-vir/common 28.0.2 → 28.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.clamp = exports.round = exports.wrapNumber = exports.toEnsuredNumber = exports.ensureMinAndMax = exports.doesRequireScientificNotation = exports.convertIntoNumber = exports.addCommasToNumber = exports.NaNString = void 0;
3
+ exports.clamp = exports.round = exports.wrapNumber = exports.toMaybeNumber = exports.toEnsuredNumber = exports.convertIntoNumber = exports.toNumber = exports.ensureMinAndMax = exports.doesRequireScientificNotation = exports.addCommasToNumber = exports.NaNString = void 0;
4
4
  const common_string_1 = require("./common-string");
5
5
  const regexp_1 = require("./regexp");
6
6
  exports.NaNString = String(NaN);
@@ -25,18 +25,6 @@ function addCommasToNumber(input) {
25
25
  ].join('');
26
26
  }
27
27
  exports.addCommasToNumber = addCommasToNumber;
28
- function convertIntoNumber(input) {
29
- if (typeof input === 'number') {
30
- return input;
31
- }
32
- else if (typeof input === 'string') {
33
- return Number((0, common_string_1.removeCommasFromNumberString)(input));
34
- }
35
- else {
36
- return Number(input);
37
- }
38
- }
39
- exports.convertIntoNumber = convertIntoNumber;
40
28
  function doesRequireScientificNotation(input) {
41
29
  return String(input).includes('e');
42
30
  }
@@ -54,16 +42,46 @@ function ensureMinAndMax({ min, max }) {
54
42
  }
55
43
  }
56
44
  exports.ensureMinAndMax = ensureMinAndMax;
45
+ /**
46
+ * Tries to convert the input into a number. Handles strings with commas. Note: this might return
47
+ * `NaN`.
48
+ */
49
+ function toNumber(input) {
50
+ if (typeof input === 'number') {
51
+ return input;
52
+ }
53
+ else if (typeof input === 'string') {
54
+ return Number((0, common_string_1.removeCommasFromNumberString)(input));
55
+ }
56
+ else {
57
+ return Number(input);
58
+ }
59
+ }
60
+ exports.toNumber = toNumber;
61
+ /** @deprecated Use {@link toNumber} instead. */
62
+ exports.convertIntoNumber = toNumber;
63
+ /** Tries to convert the input into a number and throws an error if `NaN` is created. */
57
64
  function toEnsuredNumber(input) {
58
- const numeric = Number(input);
59
- if (isNaN(numeric)) {
60
- throw new Error(`Cannot convert given input to a number: ${input}`);
65
+ const numeric = toMaybeNumber(input);
66
+ if (numeric == undefined) {
67
+ throw new Error(`Cannot convert to a number: ${input}`);
61
68
  }
62
69
  else {
63
70
  return numeric;
64
71
  }
65
72
  }
66
73
  exports.toEnsuredNumber = toEnsuredNumber;
74
+ /** Tries to convert the input into a number and returns `undefined` if `NaN` is created. */
75
+ function toMaybeNumber(input) {
76
+ const numeric = toNumber(input);
77
+ if (isNaN(numeric)) {
78
+ return undefined;
79
+ }
80
+ else {
81
+ return numeric;
82
+ }
83
+ }
84
+ exports.toMaybeNumber = toMaybeNumber;
67
85
  /**
68
86
  * If the given value is outside the given min/max bounds, instead of clamping the number (as the
69
87
  * `clamp` function does), this function wraps the value around to the next bound.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.typedSplit = exports.getAllIndexesOf = exports.escapeStringForRegExp = exports.replaceStringAtIndex = exports.camelCaseToKebabCase = exports.isCase = exports.hasCase = exports.StringCaseEnum = exports.kebabCaseToCamelCase = exports.capitalizeFirstLetter = exports.splitIncludeSplit = exports.collapseWhiteSpace = exports.removeCommasFromNumberString = exports.removeColor = exports.removeAnsiEscapeCodes = exports.joinWithFinalConjunction = void 0;
3
+ exports.typedSplit = exports.getAllIndexesOf = exports.escapeStringForRegExp = exports.replaceStringAtIndex = exports.camelCaseToKebabCase = exports.isCase = exports.hasCase = exports.StringCaseEnum = exports.kebabCaseToCamelCase = exports.capitalizeFirstLetter = exports.splitIncludeSplit = exports.collapseWhiteSpace = exports.removeCommasFromNumberString = exports.removeColor = exports.removeAnsiEscapeCodes = exports.wrapString = exports.joinWithFinalConjunction = void 0;
4
4
  const ansi_1 = require("./ansi");
5
5
  const regexp_1 = require("./regexp");
6
6
  /**
@@ -28,6 +28,14 @@ function joinWithFinalConjunction(list, conjunction = 'and') {
28
28
  return fullyJoined;
29
29
  }
30
30
  exports.joinWithFinalConjunction = joinWithFinalConjunction;
31
+ /** Wraps `value` on both sides with `wrapper`. */
32
+ function wrapString({ value, wrapper }) {
33
+ return [
34
+ wrapper,
35
+ wrapper,
36
+ ].join(value);
37
+ }
38
+ exports.wrapString = wrapString;
31
39
  function removeAnsiEscapeCodes(input) {
32
40
  return input.replace(ansi_1.ansiRegex, '');
33
41
  }
@@ -21,17 +21,6 @@ export function addCommasToNumber(input) {
21
21
  decimalString,
22
22
  ].join('');
23
23
  }
24
- export function convertIntoNumber(input) {
25
- if (typeof input === 'number') {
26
- return input;
27
- }
28
- else if (typeof input === 'string') {
29
- return Number(removeCommasFromNumberString(input));
30
- }
31
- else {
32
- return Number(input);
33
- }
34
- }
35
24
  export function doesRequireScientificNotation(input) {
36
25
  return String(input).includes('e');
37
26
  }
@@ -47,10 +36,38 @@ export function ensureMinAndMax({ min, max }) {
47
36
  return { min, max };
48
37
  }
49
38
  }
39
+ /**
40
+ * Tries to convert the input into a number. Handles strings with commas. Note: this might return
41
+ * `NaN`.
42
+ */
43
+ export function toNumber(input) {
44
+ if (typeof input === 'number') {
45
+ return input;
46
+ }
47
+ else if (typeof input === 'string') {
48
+ return Number(removeCommasFromNumberString(input));
49
+ }
50
+ else {
51
+ return Number(input);
52
+ }
53
+ }
54
+ /** @deprecated Use {@link toNumber} instead. */
55
+ export const convertIntoNumber = toNumber;
56
+ /** Tries to convert the input into a number and throws an error if `NaN` is created. */
50
57
  export function toEnsuredNumber(input) {
51
- const numeric = Number(input);
58
+ const numeric = toMaybeNumber(input);
59
+ if (numeric == undefined) {
60
+ throw new Error(`Cannot convert to a number: ${input}`);
61
+ }
62
+ else {
63
+ return numeric;
64
+ }
65
+ }
66
+ /** Tries to convert the input into a number and returns `undefined` if `NaN` is created. */
67
+ export function toMaybeNumber(input) {
68
+ const numeric = toNumber(input);
52
69
  if (isNaN(numeric)) {
53
- throw new Error(`Cannot convert given input to a number: ${input}`);
70
+ return undefined;
54
71
  }
55
72
  else {
56
73
  return numeric;
@@ -24,6 +24,13 @@ export function joinWithFinalConjunction(list, conjunction = 'and') {
24
24
  const fullyJoined = `${commaJoined}${commaSep}${conjunction} ${list[list.length - 1]}`;
25
25
  return fullyJoined;
26
26
  }
27
+ /** Wraps `value` on both sides with `wrapper`. */
28
+ export function wrapString({ value, wrapper }) {
29
+ return [
30
+ wrapper,
31
+ wrapper,
32
+ ].join(value);
33
+ }
27
34
  export function removeAnsiEscapeCodes(input) {
28
35
  return input.replace(ansiRegex, '');
29
36
  }
@@ -1,6 +1,5 @@
1
1
  export declare const NaNString: string;
2
2
  export declare function addCommasToNumber(input: number | string): string;
3
- export declare function convertIntoNumber(input: unknown): number;
4
3
  export declare function doesRequireScientificNotation(input: number): boolean;
5
4
  /**
6
5
  * Given a min and max, ensures that they are in correct order. Meaning, min is less than max. If
@@ -13,7 +12,17 @@ export declare function ensureMinAndMax({ min, max }: {
13
12
  min: number;
14
13
  max: number;
15
14
  };
16
- export declare function toEnsuredNumber(input: any): number;
15
+ /**
16
+ * Tries to convert the input into a number. Handles strings with commas. Note: this might return
17
+ * `NaN`.
18
+ */
19
+ export declare function toNumber(input: unknown): number;
20
+ /** @deprecated Use {@link toNumber} instead. */
21
+ export declare const convertIntoNumber: typeof toNumber;
22
+ /** Tries to convert the input into a number and throws an error if `NaN` is created. */
23
+ export declare function toEnsuredNumber(input: unknown): number;
24
+ /** Tries to convert the input into a number and returns `undefined` if `NaN` is created. */
25
+ export declare function toMaybeNumber(input: unknown): number | undefined;
17
26
  /**
18
27
  * If the given value is outside the given min/max bounds, instead of clamping the number (as the
19
28
  * `clamp` function does), this function wraps the value around to the next bound.
@@ -10,6 +10,11 @@ import { AtLeastTuple } from './tuple';
10
10
  * @param conjunction Defaults to 'and'. The conjunction to be used before the final element.
11
11
  */
12
12
  export declare function joinWithFinalConjunction(list: ReadonlyArray<any>, conjunction?: string): string;
13
+ /** Wraps `value` on both sides with `wrapper`. */
14
+ export declare function wrapString({ value, wrapper }: {
15
+ value: string;
16
+ wrapper: string;
17
+ }): string;
13
18
  export declare function removeAnsiEscapeCodes(input: string): string;
14
19
  export declare const removeColor: typeof removeAnsiEscapeCodes;
15
20
  export declare function removeCommasFromNumberString(numberString: string): string;
@@ -13,11 +13,11 @@ export type WrapInTryOptions<FallbackValue> = PartialAndUndefined<{
13
13
  fallbackValue: FallbackValue;
14
14
  }>;
15
15
  export declare function wrapInTry<Value extends Promise<any>>(callback: NoInputsFunction<Value>, options?: undefined | {
16
- handleError?: undefined;
16
+ handleError?: never;
17
17
  fallbackValue?: never;
18
18
  }): Promise<Error | Awaited<Value>>;
19
19
  export declare function wrapInTry<Value>(callback: NoInputsFunction<Value>, options?: undefined | {
20
- handleError?: undefined;
20
+ handleError?: never;
21
21
  fallbackValue?: never;
22
22
  }): Error | Value;
23
23
  export declare function wrapInTry<Value extends Promise<any>, FallbackValue = undefined>(callback: NoInputsFunction<Value>, options: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/common",
3
- "version": "28.0.2",
3
+ "version": "28.1.0",
4
4
  "homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
5
5
  "bugs": {
6
6
  "url": "https://github.com/electrovir/augment-vir/issues"