@augment-vir/common 31.71.4 → 31.72.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.
@@ -0,0 +1,67 @@
1
+ /**
2
+ * A suite of functions that each convert an `unknown` value into a specific type, throwing an error
3
+ * if the conversion is not possible.
4
+ *
5
+ * @category Convert
6
+ * @category Package : @augment-vir/common
7
+ * @example
8
+ *
9
+ * ```ts
10
+ * import {convertTo} from '@augment-vir/common';
11
+ *
12
+ * convertTo.string(42); // `'42'`
13
+ * convertTo.string(undefined); // throws an error
14
+ * ```
15
+ *
16
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
17
+ */
18
+ export declare const convertTo: {
19
+ /**
20
+ * Converts anything into a string.
21
+ *
22
+ * @example
23
+ *
24
+ * ```ts
25
+ * import {convertTo} from '@augment-vir/common';
26
+ *
27
+ * convertTo.string('hi'); // `'hi'`
28
+ * convertTo.string(42); // `'42'`
29
+ * convertTo.string(null); // `'hi'`
30
+ * convertTo.string({}); // `'{}'`
31
+ * ```
32
+ */
33
+ string(this: void, value: unknown): string;
34
+ /**
35
+ * Converts almost anything into a string but maps `null`, `undefined`, and empty string results
36
+ * to `undefined`.
37
+ *
38
+ * @example
39
+ *
40
+ * ```ts
41
+ * import {convertTo} from '@augment-vir/common';
42
+ *
43
+ * convertTo.string('hi'); // `'hi'`
44
+ * convertTo.string(42); // `'42'`
45
+ * convertTo.string(null); // `undefined`
46
+ * convertTo.string(''); // `undefined`
47
+ * convertTo.string({}); // `'{}'`
48
+ * ```
49
+ */
50
+ maybeString(this: void, value: unknown): string | undefined;
51
+ /**
52
+ * Converts almost anything into a number but maps `null`, `undefined`, and any conversion that
53
+ * results in `NaN` to `undefined`.
54
+ *
55
+ * @example
56
+ *
57
+ * ```ts
58
+ * import {convertTo} from '@augment-vir/common';
59
+ *
60
+ * convertTo.maybeNumber('42'); // `42`
61
+ * convertTo.maybeNumber(42); // `42`
62
+ * convertTo.maybeNumber(null); // `undefined`
63
+ * convertTo.maybeNumber('hello'); // `undefined`
64
+ * ```
65
+ */
66
+ maybeNumber(this: void, value: unknown): number | undefined;
67
+ };
@@ -0,0 +1,90 @@
1
+ import { isPrimitive, stringify } from '@augment-vir/core';
2
+ /**
3
+ * A suite of functions that each convert an `unknown` value into a specific type, throwing an error
4
+ * if the conversion is not possible.
5
+ *
6
+ * @category Convert
7
+ * @category Package : @augment-vir/common
8
+ * @example
9
+ *
10
+ * ```ts
11
+ * import {convertTo} from '@augment-vir/common';
12
+ *
13
+ * convertTo.string(42); // `'42'`
14
+ * convertTo.string(undefined); // throws an error
15
+ * ```
16
+ *
17
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
18
+ */
19
+ export const convertTo = {
20
+ /**
21
+ * Converts anything into a string.
22
+ *
23
+ * @example
24
+ *
25
+ * ```ts
26
+ * import {convertTo} from '@augment-vir/common';
27
+ *
28
+ * convertTo.string('hi'); // `'hi'`
29
+ * convertTo.string(42); // `'42'`
30
+ * convertTo.string(null); // `'hi'`
31
+ * convertTo.string({}); // `'{}'`
32
+ * ```
33
+ */
34
+ string(value) {
35
+ if (isPrimitive(value)) {
36
+ return String(value);
37
+ }
38
+ else {
39
+ return stringify(value);
40
+ }
41
+ },
42
+ /**
43
+ * Converts almost anything into a string but maps `null`, `undefined`, and empty string results
44
+ * to `undefined`.
45
+ *
46
+ * @example
47
+ *
48
+ * ```ts
49
+ * import {convertTo} from '@augment-vir/common';
50
+ *
51
+ * convertTo.string('hi'); // `'hi'`
52
+ * convertTo.string(42); // `'42'`
53
+ * convertTo.string(null); // `undefined`
54
+ * convertTo.string(''); // `undefined`
55
+ * convertTo.string({}); // `'{}'`
56
+ * ```
57
+ */
58
+ maybeString(value) {
59
+ if (value == undefined) {
60
+ return undefined;
61
+ }
62
+ else {
63
+ return convertTo.string(value) || undefined;
64
+ }
65
+ },
66
+ /**
67
+ * Converts almost anything into a number but maps `null`, `undefined`, and any conversion that
68
+ * results in `NaN` to `undefined`.
69
+ *
70
+ * @example
71
+ *
72
+ * ```ts
73
+ * import {convertTo} from '@augment-vir/common';
74
+ *
75
+ * convertTo.maybeNumber('42'); // `42`
76
+ * convertTo.maybeNumber(42); // `42`
77
+ * convertTo.maybeNumber(null); // `undefined`
78
+ * convertTo.maybeNumber('hello'); // `undefined`
79
+ * ```
80
+ */
81
+ maybeNumber(value) {
82
+ if (value == undefined || value === '') {
83
+ return undefined;
84
+ }
85
+ else {
86
+ const converted = Number(value);
87
+ return Number.isNaN(converted) ? undefined : converted;
88
+ }
89
+ },
90
+ };
package/dist/index.d.ts CHANGED
@@ -14,6 +14,7 @@ export * from './augments/array/from-async-iterable.js';
14
14
  export * from './augments/array/repeat-array.js';
15
15
  export * from './augments/array/shuffle-array.js';
16
16
  export * from './augments/array/string-array.js';
17
+ export * from './augments/convert/convert-to.js';
17
18
  export * from './augments/core-exports.js';
18
19
  export * from './augments/enum/enum-value-check.js';
19
20
  export * from './augments/error/combine-errors.js';
package/dist/index.js CHANGED
@@ -14,6 +14,7 @@ export * from './augments/array/from-async-iterable.js';
14
14
  export * from './augments/array/repeat-array.js';
15
15
  export * from './augments/array/shuffle-array.js';
16
16
  export * from './augments/array/string-array.js';
17
+ export * from './augments/convert/convert-to.js';
17
18
  export * from './augments/core-exports.js';
18
19
  export * from './augments/enum/enum-value-check.js';
19
20
  export * from './augments/error/combine-errors.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/common",
3
- "version": "31.71.4",
3
+ "version": "31.72.0",
4
4
  "description": "A collection of augments, helpers types, functions, and classes for any JavaScript environment.",
5
5
  "keywords": [
6
6
  "augment",
@@ -40,8 +40,8 @@
40
40
  "test:web": "virmator --no-deps test web"
41
41
  },
42
42
  "dependencies": {
43
- "@augment-vir/assert": "^31.71.4",
44
- "@augment-vir/core": "^31.71.4",
43
+ "@augment-vir/assert": "^31.72.0",
44
+ "@augment-vir/core": "^31.72.0",
45
45
  "@date-vir/duration": "^8.3.2",
46
46
  "@paralleldrive/cuid2": "^3.3.0",
47
47
  "ansi-styles": "^6.2.3",