complete-common 1.0.1 → 1.1.1
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.
- package/dist/functions/assert.d.cts +23 -0
- package/dist/functions/assert.d.mts +23 -0
- package/dist/functions/assert.d.ts +23 -0
- package/dist/functions/assert.d.ts.map +1 -1
- package/dist/functions/map.d.cts +0 -28
- package/dist/functions/map.d.mts +0 -28
- package/dist/functions/map.d.ts +0 -28
- package/dist/functions/map.d.ts.map +1 -1
- package/dist/functions/object.d.cts +46 -0
- package/dist/functions/object.d.mts +46 -0
- package/dist/functions/object.d.ts +46 -0
- package/dist/functions/object.d.ts.map +1 -1
- package/dist/functions/time.d.cts +20 -0
- package/dist/functions/time.d.mts +20 -0
- package/dist/functions/time.d.ts +20 -0
- package/dist/functions/time.d.ts.map +1 -0
- package/dist/index.cjs +58 -14
- package/dist/index.d.cts +1 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +54 -15
- package/package.json +8 -8
- package/src/functions/assert.ts +45 -0
- package/src/functions/map.ts +0 -45
- package/src/functions/object.ts +82 -0
- package/src/functions/time.ts +26 -0
- package/src/index.ts +1 -0
|
@@ -3,6 +3,16 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
|
+
declare const TYPES_TO_VALIDATION_FUNCTIONS: {
|
|
7
|
+
readonly string: (val: unknown) => val is string;
|
|
8
|
+
readonly number: (val: unknown) => val is number;
|
|
9
|
+
readonly boolean: (val: unknown) => val is boolean;
|
|
10
|
+
readonly object: (val: unknown) => val is object;
|
|
11
|
+
readonly array: (val: unknown) => val is unknown[];
|
|
12
|
+
readonly null: (val: unknown) => val is null;
|
|
13
|
+
readonly undefined: (val: unknown) => val is undefined;
|
|
14
|
+
readonly function: (val: unknown) => val is Function;
|
|
15
|
+
};
|
|
6
16
|
/**
|
|
7
17
|
* Helper function to throw an error if the provided value is equal to `undefined`.
|
|
8
18
|
*
|
|
@@ -19,4 +29,17 @@ export declare function assertDefined<T>(value: T, ...[msg]: [undefined] extends
|
|
|
19
29
|
export declare function assertNotNull<T>(value: T, ...[msg]: [null] extends [T] ? [string] : [
|
|
20
30
|
"The assertion is useless because the provided value does not contain null."
|
|
21
31
|
]): asserts value is Exclude<T, null>;
|
|
32
|
+
/**
|
|
33
|
+
* Helper function to throw an error if the provided value is not an object (i.e. a TypeScript
|
|
34
|
+
* record).
|
|
35
|
+
*
|
|
36
|
+
* This is useful to have TypeScript narrow a `Record<string, unknown> | undefined` value to
|
|
37
|
+
* `Record<string, unknown>` in a concise way.
|
|
38
|
+
*
|
|
39
|
+
* Under the hood, this function uses the `isObject` helper function.
|
|
40
|
+
*/
|
|
41
|
+
export declare function assertObject(value: unknown, msg: string): asserts value is Record<string, unknown>;
|
|
42
|
+
/** Helper function to throw an error if the provided value is not of the provided type. */
|
|
43
|
+
export declare function assertType(value: unknown, type: keyof typeof TYPES_TO_VALIDATION_FUNCTIONS, msg: string): asserts value is unknown;
|
|
44
|
+
export {};
|
|
22
45
|
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -3,6 +3,16 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
|
+
declare const TYPES_TO_VALIDATION_FUNCTIONS: {
|
|
7
|
+
readonly string: (val: unknown) => val is string;
|
|
8
|
+
readonly number: (val: unknown) => val is number;
|
|
9
|
+
readonly boolean: (val: unknown) => val is boolean;
|
|
10
|
+
readonly object: (val: unknown) => val is object;
|
|
11
|
+
readonly array: (val: unknown) => val is unknown[];
|
|
12
|
+
readonly null: (val: unknown) => val is null;
|
|
13
|
+
readonly undefined: (val: unknown) => val is undefined;
|
|
14
|
+
readonly function: (val: unknown) => val is Function;
|
|
15
|
+
};
|
|
6
16
|
/**
|
|
7
17
|
* Helper function to throw an error if the provided value is equal to `undefined`.
|
|
8
18
|
*
|
|
@@ -19,4 +29,17 @@ export declare function assertDefined<T>(value: T, ...[msg]: [undefined] extends
|
|
|
19
29
|
export declare function assertNotNull<T>(value: T, ...[msg]: [null] extends [T] ? [string] : [
|
|
20
30
|
"The assertion is useless because the provided value does not contain null."
|
|
21
31
|
]): asserts value is Exclude<T, null>;
|
|
32
|
+
/**
|
|
33
|
+
* Helper function to throw an error if the provided value is not an object (i.e. a TypeScript
|
|
34
|
+
* record).
|
|
35
|
+
*
|
|
36
|
+
* This is useful to have TypeScript narrow a `Record<string, unknown> | undefined` value to
|
|
37
|
+
* `Record<string, unknown>` in a concise way.
|
|
38
|
+
*
|
|
39
|
+
* Under the hood, this function uses the `isObject` helper function.
|
|
40
|
+
*/
|
|
41
|
+
export declare function assertObject(value: unknown, msg: string): asserts value is Record<string, unknown>;
|
|
42
|
+
/** Helper function to throw an error if the provided value is not of the provided type. */
|
|
43
|
+
export declare function assertType(value: unknown, type: keyof typeof TYPES_TO_VALIDATION_FUNCTIONS, msg: string): asserts value is unknown;
|
|
44
|
+
export {};
|
|
22
45
|
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -3,6 +3,16 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
|
+
declare const TYPES_TO_VALIDATION_FUNCTIONS: {
|
|
7
|
+
readonly string: (val: unknown) => val is string;
|
|
8
|
+
readonly number: (val: unknown) => val is number;
|
|
9
|
+
readonly boolean: (val: unknown) => val is boolean;
|
|
10
|
+
readonly object: (val: unknown) => val is object;
|
|
11
|
+
readonly array: (val: unknown) => val is unknown[];
|
|
12
|
+
readonly null: (val: unknown) => val is null;
|
|
13
|
+
readonly undefined: (val: unknown) => val is undefined;
|
|
14
|
+
readonly function: (val: unknown) => val is Function;
|
|
15
|
+
};
|
|
6
16
|
/**
|
|
7
17
|
* Helper function to throw an error if the provided value is equal to `undefined`.
|
|
8
18
|
*
|
|
@@ -19,4 +29,17 @@ export declare function assertDefined<T>(value: T, ...[msg]: [undefined] extends
|
|
|
19
29
|
export declare function assertNotNull<T>(value: T, ...[msg]: [null] extends [T] ? [string] : [
|
|
20
30
|
"The assertion is useless because the provided value does not contain null."
|
|
21
31
|
]): asserts value is Exclude<T, null>;
|
|
32
|
+
/**
|
|
33
|
+
* Helper function to throw an error if the provided value is not an object (i.e. a TypeScript
|
|
34
|
+
* record).
|
|
35
|
+
*
|
|
36
|
+
* This is useful to have TypeScript narrow a `Record<string, unknown> | undefined` value to
|
|
37
|
+
* `Record<string, unknown>` in a concise way.
|
|
38
|
+
*
|
|
39
|
+
* Under the hood, this function uses the `isObject` helper function.
|
|
40
|
+
*/
|
|
41
|
+
export declare function assertObject(value: unknown, msg: string): asserts value is Record<string, unknown>;
|
|
42
|
+
/** Helper function to throw an error if the provided value is not of the provided type. */
|
|
43
|
+
export declare function assertType(value: unknown, type: keyof typeof TYPES_TO_VALIDATION_FUNCTIONS, msg: string): asserts value is unknown;
|
|
44
|
+
export {};
|
|
22
45
|
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../src/functions/assert.ts"],"names":[],"mappings":"AAAA;;;;GAIG;
|
|
1
|
+
{"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../src/functions/assert.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,QAAA,MAAM,6BAA6B;2BACnB,OAAO,KAAG,GAAG,IAAI,MAAM;2BACvB,OAAO,KAAG,GAAG,IAAI,MAAM;4BACtB,OAAO,KAAG,GAAG,IAAI,OAAO;2BACzB,OAAO,KAAG,GAAG,IAAI,MAAM;0BAExB,OAAO,KAAG,GAAG,IAAI,OAAO,EAAE;yBAC3B,OAAO,KAAG,GAAG,IAAI,IAAI;8BAChB,OAAO,KAAG,GAAG,IAAI,SAAS;6BAE3B,OAAO,KAAG,GAAG,IAAI,QAAQ;CACjC,CAAC;AAEX;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAC7B,KAAK,EAAE,CAAC,EACR,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,CAAC,GAC7B,CAAC,MAAM,CAAC,GACR;IACE,iFAAiF;CAClF,GACJ,OAAO,CAAC,KAAK,IAAI,OAAO,CAAC,CAAC,EAAE,SAAS,CAAC,CAIxC;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAC7B,KAAK,EAAE,CAAC,EACR,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,GACxB,CAAC,MAAM,CAAC,GACR;IACE,4EAA4E;CAC7E,GACJ,OAAO,CAAC,KAAK,IAAI,OAAO,CAAC,CAAC,EAAE,IAAI,CAAC,CAInC;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAI1C;AAED,2FAA2F;AAC3F,wBAAgB,UAAU,CACxB,KAAK,EAAE,OAAO,EACd,IAAI,EAAE,MAAM,OAAO,6BAA6B,EAChD,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,KAAK,IAAI,OAAO,CAK1B"}
|
package/dist/functions/map.d.cts
CHANGED
|
@@ -21,32 +21,4 @@ export declare function mapFilter<K, V>(map: ReadonlyMap<K, V>, predicate: (valu
|
|
|
21
21
|
* This is efficient such that it avoids converting the map values into an array.
|
|
22
22
|
*/
|
|
23
23
|
export declare function mapFind<K, V>(map: ReadonlyMap<K, V>, predicate: (value: V) => boolean): V | undefined;
|
|
24
|
-
/**
|
|
25
|
-
* Helper function to convert an object to a map.
|
|
26
|
-
*
|
|
27
|
-
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
28
|
-
* then later on you need to query it in a way where you expect the return value to be T or
|
|
29
|
-
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
30
|
-
* assertions.
|
|
31
|
-
*
|
|
32
|
-
* Note that the map values will be inserted in a random order, due to how `pairs` works under the
|
|
33
|
-
* hood.
|
|
34
|
-
*
|
|
35
|
-
* Also see the `objectToReadonlyMap` function.
|
|
36
|
-
*/
|
|
37
|
-
export declare function objectToMap<K extends string | number | symbol, V>(object: Record<K, V>): Map<K, V>;
|
|
38
|
-
/**
|
|
39
|
-
* Helper function to convert an object to a read-only map.
|
|
40
|
-
*
|
|
41
|
-
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
42
|
-
* then later on you need to query it in a way where you expect the return value to be T or
|
|
43
|
-
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
44
|
-
* assertions.
|
|
45
|
-
*
|
|
46
|
-
* Note that the map values will be inserted in a random order, due to how `pairs` works under the
|
|
47
|
-
* hood.
|
|
48
|
-
*
|
|
49
|
-
* Also see the `objectToMap` function.
|
|
50
|
-
*/
|
|
51
|
-
export declare function objectToReadonlyMap<K extends string | number | symbol, V>(object: Record<K, V>): ReadonlyMap<K, V>;
|
|
52
24
|
//# sourceMappingURL=map.d.ts.map
|
package/dist/functions/map.d.mts
CHANGED
|
@@ -21,32 +21,4 @@ export declare function mapFilter<K, V>(map: ReadonlyMap<K, V>, predicate: (valu
|
|
|
21
21
|
* This is efficient such that it avoids converting the map values into an array.
|
|
22
22
|
*/
|
|
23
23
|
export declare function mapFind<K, V>(map: ReadonlyMap<K, V>, predicate: (value: V) => boolean): V | undefined;
|
|
24
|
-
/**
|
|
25
|
-
* Helper function to convert an object to a map.
|
|
26
|
-
*
|
|
27
|
-
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
28
|
-
* then later on you need to query it in a way where you expect the return value to be T or
|
|
29
|
-
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
30
|
-
* assertions.
|
|
31
|
-
*
|
|
32
|
-
* Note that the map values will be inserted in a random order, due to how `pairs` works under the
|
|
33
|
-
* hood.
|
|
34
|
-
*
|
|
35
|
-
* Also see the `objectToReadonlyMap` function.
|
|
36
|
-
*/
|
|
37
|
-
export declare function objectToMap<K extends string | number | symbol, V>(object: Record<K, V>): Map<K, V>;
|
|
38
|
-
/**
|
|
39
|
-
* Helper function to convert an object to a read-only map.
|
|
40
|
-
*
|
|
41
|
-
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
42
|
-
* then later on you need to query it in a way where you expect the return value to be T or
|
|
43
|
-
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
44
|
-
* assertions.
|
|
45
|
-
*
|
|
46
|
-
* Note that the map values will be inserted in a random order, due to how `pairs` works under the
|
|
47
|
-
* hood.
|
|
48
|
-
*
|
|
49
|
-
* Also see the `objectToMap` function.
|
|
50
|
-
*/
|
|
51
|
-
export declare function objectToReadonlyMap<K extends string | number | symbol, V>(object: Record<K, V>): ReadonlyMap<K, V>;
|
|
52
24
|
//# sourceMappingURL=map.d.ts.map
|
package/dist/functions/map.d.ts
CHANGED
|
@@ -21,32 +21,4 @@ export declare function mapFilter<K, V>(map: ReadonlyMap<K, V>, predicate: (valu
|
|
|
21
21
|
* This is efficient such that it avoids converting the map values into an array.
|
|
22
22
|
*/
|
|
23
23
|
export declare function mapFind<K, V>(map: ReadonlyMap<K, V>, predicate: (value: V) => boolean): V | undefined;
|
|
24
|
-
/**
|
|
25
|
-
* Helper function to convert an object to a map.
|
|
26
|
-
*
|
|
27
|
-
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
28
|
-
* then later on you need to query it in a way where you expect the return value to be T or
|
|
29
|
-
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
30
|
-
* assertions.
|
|
31
|
-
*
|
|
32
|
-
* Note that the map values will be inserted in a random order, due to how `pairs` works under the
|
|
33
|
-
* hood.
|
|
34
|
-
*
|
|
35
|
-
* Also see the `objectToReadonlyMap` function.
|
|
36
|
-
*/
|
|
37
|
-
export declare function objectToMap<K extends string | number | symbol, V>(object: Record<K, V>): Map<K, V>;
|
|
38
|
-
/**
|
|
39
|
-
* Helper function to convert an object to a read-only map.
|
|
40
|
-
*
|
|
41
|
-
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
42
|
-
* then later on you need to query it in a way where you expect the return value to be T or
|
|
43
|
-
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
44
|
-
* assertions.
|
|
45
|
-
*
|
|
46
|
-
* Note that the map values will be inserted in a random order, due to how `pairs` works under the
|
|
47
|
-
* hood.
|
|
48
|
-
*
|
|
49
|
-
* Also see the `objectToMap` function.
|
|
50
|
-
*/
|
|
51
|
-
export declare function objectToReadonlyMap<K extends string | number | symbol, V>(object: Record<K, V>): ReadonlyMap<K, V>;
|
|
52
24
|
//# sourceMappingURL=map.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"map.d.ts","sourceRoot":"","sources":["../../src/functions/map.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;;;;GAQG;AAEH,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,EAC5B,GAAG,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,EACtB,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,GAC/B,CAAC,EAAE,CAWL;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,EAC1B,GAAG,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,EACtB,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,GAC/B,CAAC,GAAG,SAAS,CASf
|
|
1
|
+
{"version":3,"file":"map.d.ts","sourceRoot":"","sources":["../../src/functions/map.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;;;;GAQG;AAEH,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,EAC5B,GAAG,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,EACtB,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,GAC/B,CAAC,EAAE,CAWL;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,EAC1B,GAAG,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,EACtB,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,GAC/B,CAAC,GAAG,SAAS,CASf"}
|
|
@@ -11,4 +11,50 @@ import type { ReadonlyRecord } from "../types/ReadonlyRecord.js";
|
|
|
11
11
|
* This is efficient such that it avoids converting the object values into an array.
|
|
12
12
|
*/
|
|
13
13
|
export declare function objectFilter<K extends string | number | symbol, V>(object: ReadonlyRecord<K, V>, predicate: (value: V) => boolean): V[];
|
|
14
|
+
/**
|
|
15
|
+
* Helper function to convert an object to a map.
|
|
16
|
+
*
|
|
17
|
+
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
18
|
+
* then later on you need to query it in a way where you expect the return value to be T or
|
|
19
|
+
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
20
|
+
* assertions.
|
|
21
|
+
*
|
|
22
|
+
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
23
|
+
* only having string keys under the hood.
|
|
24
|
+
*
|
|
25
|
+
* Also see the `objectToReadonlyMap` function.
|
|
26
|
+
*/
|
|
27
|
+
export declare function objectToMap<K extends string | number | symbol, V>(object: Record<K, V>): Map<K, V>;
|
|
28
|
+
/**
|
|
29
|
+
* Helper function to convert an object to a read-only map.
|
|
30
|
+
*
|
|
31
|
+
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
32
|
+
* then later on you need to query it in a way where you expect the return value to be T or
|
|
33
|
+
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
34
|
+
* assertions.
|
|
35
|
+
*
|
|
36
|
+
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
37
|
+
* only having string keys under the hood.
|
|
38
|
+
*
|
|
39
|
+
* Also see the `objectToMap` function.
|
|
40
|
+
*/
|
|
41
|
+
export declare function objectToReadonlyMap<K extends string | number | symbol, V>(object: Record<K, V>): ReadonlyMap<K, V>;
|
|
42
|
+
/**
|
|
43
|
+
* Helper function to convert an object to a reverse map.
|
|
44
|
+
*
|
|
45
|
+
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
46
|
+
* only having string keys under the hood.
|
|
47
|
+
*
|
|
48
|
+
* Also see the `objectToReverseReadonlyMap` function.
|
|
49
|
+
*/
|
|
50
|
+
export declare function objectToReverseMap<K extends string | number | symbol, V extends string | number | symbol>(object: Record<K, V>): Map<V, K>;
|
|
51
|
+
/**
|
|
52
|
+
* Helper function to convert an object to a reverse read-only map.
|
|
53
|
+
*
|
|
54
|
+
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
55
|
+
* only having string keys under the hood.
|
|
56
|
+
*
|
|
57
|
+
* Also see the `objectToReverseMap` function.
|
|
58
|
+
*/
|
|
59
|
+
export declare function objectToReverseReadonlyMap<K extends string | number | symbol, V extends string | number | symbol>(object: Record<K, V>): ReadonlyMap<V, K>;
|
|
14
60
|
//# sourceMappingURL=object.d.ts.map
|
|
@@ -11,4 +11,50 @@ import type { ReadonlyRecord } from "../types/ReadonlyRecord.js";
|
|
|
11
11
|
* This is efficient such that it avoids converting the object values into an array.
|
|
12
12
|
*/
|
|
13
13
|
export declare function objectFilter<K extends string | number | symbol, V>(object: ReadonlyRecord<K, V>, predicate: (value: V) => boolean): V[];
|
|
14
|
+
/**
|
|
15
|
+
* Helper function to convert an object to a map.
|
|
16
|
+
*
|
|
17
|
+
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
18
|
+
* then later on you need to query it in a way where you expect the return value to be T or
|
|
19
|
+
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
20
|
+
* assertions.
|
|
21
|
+
*
|
|
22
|
+
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
23
|
+
* only having string keys under the hood.
|
|
24
|
+
*
|
|
25
|
+
* Also see the `objectToReadonlyMap` function.
|
|
26
|
+
*/
|
|
27
|
+
export declare function objectToMap<K extends string | number | symbol, V>(object: Record<K, V>): Map<K, V>;
|
|
28
|
+
/**
|
|
29
|
+
* Helper function to convert an object to a read-only map.
|
|
30
|
+
*
|
|
31
|
+
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
32
|
+
* then later on you need to query it in a way where you expect the return value to be T or
|
|
33
|
+
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
34
|
+
* assertions.
|
|
35
|
+
*
|
|
36
|
+
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
37
|
+
* only having string keys under the hood.
|
|
38
|
+
*
|
|
39
|
+
* Also see the `objectToMap` function.
|
|
40
|
+
*/
|
|
41
|
+
export declare function objectToReadonlyMap<K extends string | number | symbol, V>(object: Record<K, V>): ReadonlyMap<K, V>;
|
|
42
|
+
/**
|
|
43
|
+
* Helper function to convert an object to a reverse map.
|
|
44
|
+
*
|
|
45
|
+
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
46
|
+
* only having string keys under the hood.
|
|
47
|
+
*
|
|
48
|
+
* Also see the `objectToReverseReadonlyMap` function.
|
|
49
|
+
*/
|
|
50
|
+
export declare function objectToReverseMap<K extends string | number | symbol, V extends string | number | symbol>(object: Record<K, V>): Map<V, K>;
|
|
51
|
+
/**
|
|
52
|
+
* Helper function to convert an object to a reverse read-only map.
|
|
53
|
+
*
|
|
54
|
+
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
55
|
+
* only having string keys under the hood.
|
|
56
|
+
*
|
|
57
|
+
* Also see the `objectToReverseMap` function.
|
|
58
|
+
*/
|
|
59
|
+
export declare function objectToReverseReadonlyMap<K extends string | number | symbol, V extends string | number | symbol>(object: Record<K, V>): ReadonlyMap<V, K>;
|
|
14
60
|
//# sourceMappingURL=object.d.ts.map
|
|
@@ -11,4 +11,50 @@ import type { ReadonlyRecord } from "../types/ReadonlyRecord.js";
|
|
|
11
11
|
* This is efficient such that it avoids converting the object values into an array.
|
|
12
12
|
*/
|
|
13
13
|
export declare function objectFilter<K extends string | number | symbol, V>(object: ReadonlyRecord<K, V>, predicate: (value: V) => boolean): V[];
|
|
14
|
+
/**
|
|
15
|
+
* Helper function to convert an object to a map.
|
|
16
|
+
*
|
|
17
|
+
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
18
|
+
* then later on you need to query it in a way where you expect the return value to be T or
|
|
19
|
+
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
20
|
+
* assertions.
|
|
21
|
+
*
|
|
22
|
+
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
23
|
+
* only having string keys under the hood.
|
|
24
|
+
*
|
|
25
|
+
* Also see the `objectToReadonlyMap` function.
|
|
26
|
+
*/
|
|
27
|
+
export declare function objectToMap<K extends string | number | symbol, V>(object: Record<K, V>): Map<K, V>;
|
|
28
|
+
/**
|
|
29
|
+
* Helper function to convert an object to a read-only map.
|
|
30
|
+
*
|
|
31
|
+
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
32
|
+
* then later on you need to query it in a way where you expect the return value to be T or
|
|
33
|
+
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
34
|
+
* assertions.
|
|
35
|
+
*
|
|
36
|
+
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
37
|
+
* only having string keys under the hood.
|
|
38
|
+
*
|
|
39
|
+
* Also see the `objectToMap` function.
|
|
40
|
+
*/
|
|
41
|
+
export declare function objectToReadonlyMap<K extends string | number | symbol, V>(object: Record<K, V>): ReadonlyMap<K, V>;
|
|
42
|
+
/**
|
|
43
|
+
* Helper function to convert an object to a reverse map.
|
|
44
|
+
*
|
|
45
|
+
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
46
|
+
* only having string keys under the hood.
|
|
47
|
+
*
|
|
48
|
+
* Also see the `objectToReverseReadonlyMap` function.
|
|
49
|
+
*/
|
|
50
|
+
export declare function objectToReverseMap<K extends string | number | symbol, V extends string | number | symbol>(object: Record<K, V>): Map<V, K>;
|
|
51
|
+
/**
|
|
52
|
+
* Helper function to convert an object to a reverse read-only map.
|
|
53
|
+
*
|
|
54
|
+
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
55
|
+
* only having string keys under the hood.
|
|
56
|
+
*
|
|
57
|
+
* Also see the `objectToReverseMap` function.
|
|
58
|
+
*/
|
|
59
|
+
export declare function objectToReverseReadonlyMap<K extends string | number | symbol, V extends string | number | symbol>(object: Record<K, V>): ReadonlyMap<V, K>;
|
|
14
60
|
//# sourceMappingURL=object.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../../src/functions/object.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAEjE;;;;;GAKG;AAEH,wBAAgB,YAAY,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,EAChE,MAAM,EAAE,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC,EAC5B,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,GAC/B,CAAC,EAAE,CAaL"}
|
|
1
|
+
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../../src/functions/object.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAEjE;;;;;GAKG;AAEH,wBAAgB,YAAY,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,EAChE,MAAM,EAAE,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC,EAC5B,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,GAC/B,CAAC,EAAE,CAaL;AAED;;;;;;;;;;;;GAYG;AAEH,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,EAC/D,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GACnB,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAQX;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,EACvE,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GACnB,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAEnB;AAED;;;;;;;GAOG;AAEH,wBAAgB,kBAAkB,CAChC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,EAClC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,EAClC,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAQjC;AAED;;;;;;;GAOG;AACH,wBAAgB,0BAA0B,CACxC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,EAClC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,EAClC,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAEzC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper functions having to do with time.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Helper function to get the number of elapsed seconds since a starting time.
|
|
8
|
+
*
|
|
9
|
+
* This function always returns a whole number (using `Math.floor` on the result).
|
|
10
|
+
*
|
|
11
|
+
* For example:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const startTime = Date.now();
|
|
15
|
+
* doSomething();
|
|
16
|
+
* const elapsedSeconds = getElapsedSeconds(startTime);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function getElapsedSeconds(startTime: number): number;
|
|
20
|
+
//# sourceMappingURL=time.d.ts.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper functions having to do with time.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Helper function to get the number of elapsed seconds since a starting time.
|
|
8
|
+
*
|
|
9
|
+
* This function always returns a whole number (using `Math.floor` on the result).
|
|
10
|
+
*
|
|
11
|
+
* For example:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const startTime = Date.now();
|
|
15
|
+
* doSomething();
|
|
16
|
+
* const elapsedSeconds = getElapsedSeconds(startTime);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function getElapsedSeconds(startTime: number): number;
|
|
20
|
+
//# sourceMappingURL=time.d.ts.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper functions having to do with time.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Helper function to get the number of elapsed seconds since a starting time.
|
|
8
|
+
*
|
|
9
|
+
* This function always returns a whole number (using `Math.floor` on the result).
|
|
10
|
+
*
|
|
11
|
+
* For example:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const startTime = Date.now();
|
|
15
|
+
* doSomething();
|
|
16
|
+
* const elapsedSeconds = getElapsedSeconds(startTime);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function getElapsedSeconds(startTime: number): number;
|
|
20
|
+
//# sourceMappingURL=time.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"time.d.ts","sourceRoot":"","sources":["../../src/functions/time.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAM3D"}
|
package/dist/index.cjs
CHANGED
|
@@ -6,6 +6,21 @@ const HOUR_IN_MILLISECONDS = 60 * MINUTE_IN_MILLISECONDS;
|
|
|
6
6
|
|
|
7
7
|
const ReadonlySet = Set;
|
|
8
8
|
|
|
9
|
+
function isObject(variable) {
|
|
10
|
+
return typeof variable === "object" && variable !== null && !Array.isArray(variable);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const TYPES_TO_VALIDATION_FUNCTIONS = {
|
|
14
|
+
string: (val) => typeof val === "string",
|
|
15
|
+
number: (val) => typeof val === "number",
|
|
16
|
+
boolean: (val) => typeof val === "boolean",
|
|
17
|
+
object: (val) => val !== null && typeof val === "object",
|
|
18
|
+
array: (val) => Array.isArray(val),
|
|
19
|
+
null: (val) => val === null,
|
|
20
|
+
undefined: (val) => val === void 0,
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
22
|
+
function: (val) => typeof val === "function"
|
|
23
|
+
};
|
|
9
24
|
function assertDefined(value, ...[msg]) {
|
|
10
25
|
if (value === void 0) {
|
|
11
26
|
throw new TypeError(msg);
|
|
@@ -16,6 +31,17 @@ function assertNotNull(value, ...[msg]) {
|
|
|
16
31
|
throw new TypeError(msg);
|
|
17
32
|
}
|
|
18
33
|
}
|
|
34
|
+
function assertObject(value, msg) {
|
|
35
|
+
if (!isObject(value)) {
|
|
36
|
+
throw new TypeError(msg);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function assertType(value, type, msg) {
|
|
40
|
+
const validationFunction = TYPES_TO_VALIDATION_FUNCTIONS[type];
|
|
41
|
+
if (!validationFunction(value)) {
|
|
42
|
+
throw new TypeError(msg);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
19
45
|
|
|
20
46
|
function getRandomInt(min, max, exceptions = []) {
|
|
21
47
|
min = Math.ceil(min);
|
|
@@ -166,16 +192,6 @@ function mapFind(map, predicate) {
|
|
|
166
192
|
}
|
|
167
193
|
return void 0;
|
|
168
194
|
}
|
|
169
|
-
function objectToMap(object) {
|
|
170
|
-
const map = /* @__PURE__ */ new Map();
|
|
171
|
-
for (const [key, value] of Object.entries(object)) {
|
|
172
|
-
map.set(key, value);
|
|
173
|
-
}
|
|
174
|
-
return map;
|
|
175
|
-
}
|
|
176
|
-
function objectToReadonlyMap(object) {
|
|
177
|
-
return objectToMap(object);
|
|
178
|
-
}
|
|
179
195
|
|
|
180
196
|
function clamp(num, min, max) {
|
|
181
197
|
return Math.max(min, Math.min(num, max));
|
|
@@ -192,6 +208,26 @@ function objectFilter(object, predicate) {
|
|
|
192
208
|
}
|
|
193
209
|
return array;
|
|
194
210
|
}
|
|
211
|
+
function objectToMap(object) {
|
|
212
|
+
const map = /* @__PURE__ */ new Map();
|
|
213
|
+
for (const [key, value] of Object.entries(object)) {
|
|
214
|
+
map.set(key, value);
|
|
215
|
+
}
|
|
216
|
+
return map;
|
|
217
|
+
}
|
|
218
|
+
function objectToReadonlyMap(object) {
|
|
219
|
+
return objectToMap(object);
|
|
220
|
+
}
|
|
221
|
+
function objectToReverseMap(object) {
|
|
222
|
+
const map = /* @__PURE__ */ new Map();
|
|
223
|
+
for (const [key, value] of Object.entries(object)) {
|
|
224
|
+
map.set(value, key);
|
|
225
|
+
}
|
|
226
|
+
return map;
|
|
227
|
+
}
|
|
228
|
+
function objectToReverseReadonlyMap(object) {
|
|
229
|
+
return objectToReverseMap(object);
|
|
230
|
+
}
|
|
195
231
|
|
|
196
232
|
function addSetsToSet(mainSet, ...setsToAdd) {
|
|
197
233
|
for (const set of setsToAdd) {
|
|
@@ -450,6 +486,13 @@ function truncateString(string, maxLength) {
|
|
|
450
486
|
return string.slice(0, maxLength);
|
|
451
487
|
}
|
|
452
488
|
|
|
489
|
+
function getElapsedSeconds(startTime) {
|
|
490
|
+
const endTime = Date.now();
|
|
491
|
+
const elapsedMilliseconds = endTime - startTime;
|
|
492
|
+
const elapsedSeconds = elapsedMilliseconds / 1e3;
|
|
493
|
+
return Math.floor(elapsedSeconds);
|
|
494
|
+
}
|
|
495
|
+
|
|
453
496
|
function* tupleEntries(tuple) {
|
|
454
497
|
yield* tuple.entries();
|
|
455
498
|
}
|
|
@@ -457,10 +500,6 @@ function* tupleKeys(tuple) {
|
|
|
457
500
|
yield* tuple.keys();
|
|
458
501
|
}
|
|
459
502
|
|
|
460
|
-
function isObject(variable) {
|
|
461
|
-
return typeof variable === "object" && variable !== null && !Array.isArray(variable);
|
|
462
|
-
}
|
|
463
|
-
|
|
464
503
|
const ReadonlyMap = Map;
|
|
465
504
|
|
|
466
505
|
exports.HOUR_IN_MILLISECONDS = HOUR_IN_MILLISECONDS;
|
|
@@ -475,6 +514,8 @@ exports.arrayRemove = arrayRemove;
|
|
|
475
514
|
exports.arrayRemoveInPlace = arrayRemoveInPlace;
|
|
476
515
|
exports.assertDefined = assertDefined;
|
|
477
516
|
exports.assertNotNull = assertNotNull;
|
|
517
|
+
exports.assertObject = assertObject;
|
|
518
|
+
exports.assertType = assertType;
|
|
478
519
|
exports.capitalizeFirstLetter = capitalizeFirstLetter;
|
|
479
520
|
exports.clamp = clamp;
|
|
480
521
|
exports.combineSets = combineSets;
|
|
@@ -483,6 +524,7 @@ exports.eRange = eRange;
|
|
|
483
524
|
exports.emptyArray = emptyArray;
|
|
484
525
|
exports.escapeHTMLCharacters = escapeHTMLCharacters;
|
|
485
526
|
exports.filterMap = filterMap;
|
|
527
|
+
exports.getElapsedSeconds = getElapsedSeconds;
|
|
486
528
|
exports.getEnumEntries = getEnumEntries;
|
|
487
529
|
exports.getEnumKeys = getEnumKeys;
|
|
488
530
|
exports.getEnumValues = getEnumValues;
|
|
@@ -514,6 +556,8 @@ exports.objectKeysToReadonlySet = objectKeysToReadonlySet;
|
|
|
514
556
|
exports.objectKeysToSet = objectKeysToSet;
|
|
515
557
|
exports.objectToMap = objectToMap;
|
|
516
558
|
exports.objectToReadonlyMap = objectToReadonlyMap;
|
|
559
|
+
exports.objectToReverseMap = objectToReverseMap;
|
|
560
|
+
exports.objectToReverseReadonlyMap = objectToReverseReadonlyMap;
|
|
517
561
|
exports.objectValuesToReadonlySet = objectValuesToReadonlySet;
|
|
518
562
|
exports.objectValuesToSet = objectValuesToSet;
|
|
519
563
|
exports.parseFloatSafe = parseFloatSafe;
|
package/dist/index.d.cts
CHANGED
|
@@ -9,6 +9,7 @@ export * from "./functions/random.js";
|
|
|
9
9
|
export * from "./functions/set.js";
|
|
10
10
|
export * from "./functions/sort.js";
|
|
11
11
|
export * from "./functions/string.js";
|
|
12
|
+
export * from "./functions/time.js";
|
|
12
13
|
export * from "./functions/tuple.js";
|
|
13
14
|
export * from "./functions/types.js";
|
|
14
15
|
export * from "./functions/utils.js";
|
package/dist/index.d.mts
CHANGED
|
@@ -9,6 +9,7 @@ export * from "./functions/random.js";
|
|
|
9
9
|
export * from "./functions/set.js";
|
|
10
10
|
export * from "./functions/sort.js";
|
|
11
11
|
export * from "./functions/string.js";
|
|
12
|
+
export * from "./functions/time.js";
|
|
12
13
|
export * from "./functions/tuple.js";
|
|
13
14
|
export * from "./functions/types.js";
|
|
14
15
|
export * from "./functions/utils.js";
|
package/dist/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export * from "./functions/random.js";
|
|
|
9
9
|
export * from "./functions/set.js";
|
|
10
10
|
export * from "./functions/sort.js";
|
|
11
11
|
export * from "./functions/string.js";
|
|
12
|
+
export * from "./functions/time.js";
|
|
12
13
|
export * from "./functions/tuple.js";
|
|
13
14
|
export * from "./functions/types.js";
|
|
14
15
|
export * from "./functions/utils.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;AACtC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,mBAAmB,wBAAwB,CAAC;AAC5C,mBAAmB,yCAAyC,CAAC;AAC7D,mBAAmB,mBAAmB,CAAC;AACvC,mBAAmB,sBAAsB,CAAC;AAC1C,mBAAmB,mBAAmB,CAAC;AACvC,mBAAmB,mCAAmC,CAAC;AACvD,mBAAmB,4CAA4C,CAAC;AAChE,mBAAmB,yBAAyB,CAAC;AAC7C,cAAc,wBAAwB,CAAC;AACvC,mBAAmB,2BAA2B,CAAC;AAC/C,cAAc,wBAAwB,CAAC;AACvC,mBAAmB,kBAAkB,CAAC;AACtC,mBAAmB,yBAAyB,CAAC;AAC7C,mBAAmB,sBAAsB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;AACtC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,mBAAmB,wBAAwB,CAAC;AAC5C,mBAAmB,yCAAyC,CAAC;AAC7D,mBAAmB,mBAAmB,CAAC;AACvC,mBAAmB,sBAAsB,CAAC;AAC1C,mBAAmB,mBAAmB,CAAC;AACvC,mBAAmB,mCAAmC,CAAC;AACvD,mBAAmB,4CAA4C,CAAC;AAChE,mBAAmB,yBAAyB,CAAC;AAC7C,cAAc,wBAAwB,CAAC;AACvC,mBAAmB,2BAA2B,CAAC;AAC/C,cAAc,wBAAwB,CAAC;AACvC,mBAAmB,kBAAkB,CAAC;AACtC,mBAAmB,yBAAyB,CAAC;AAC7C,mBAAmB,sBAAsB,CAAC"}
|
package/dist/index.mjs
CHANGED
|
@@ -4,6 +4,21 @@ const HOUR_IN_MILLISECONDS = 60 * MINUTE_IN_MILLISECONDS;
|
|
|
4
4
|
|
|
5
5
|
const ReadonlySet = Set;
|
|
6
6
|
|
|
7
|
+
function isObject(variable) {
|
|
8
|
+
return typeof variable === "object" && variable !== null && !Array.isArray(variable);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const TYPES_TO_VALIDATION_FUNCTIONS = {
|
|
12
|
+
string: (val) => typeof val === "string",
|
|
13
|
+
number: (val) => typeof val === "number",
|
|
14
|
+
boolean: (val) => typeof val === "boolean",
|
|
15
|
+
object: (val) => val !== null && typeof val === "object",
|
|
16
|
+
array: (val) => Array.isArray(val),
|
|
17
|
+
null: (val) => val === null,
|
|
18
|
+
undefined: (val) => val === void 0,
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
20
|
+
function: (val) => typeof val === "function"
|
|
21
|
+
};
|
|
7
22
|
function assertDefined(value, ...[msg]) {
|
|
8
23
|
if (value === void 0) {
|
|
9
24
|
throw new TypeError(msg);
|
|
@@ -14,6 +29,17 @@ function assertNotNull(value, ...[msg]) {
|
|
|
14
29
|
throw new TypeError(msg);
|
|
15
30
|
}
|
|
16
31
|
}
|
|
32
|
+
function assertObject(value, msg) {
|
|
33
|
+
if (!isObject(value)) {
|
|
34
|
+
throw new TypeError(msg);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function assertType(value, type, msg) {
|
|
38
|
+
const validationFunction = TYPES_TO_VALIDATION_FUNCTIONS[type];
|
|
39
|
+
if (!validationFunction(value)) {
|
|
40
|
+
throw new TypeError(msg);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
17
43
|
|
|
18
44
|
function getRandomInt(min, max, exceptions = []) {
|
|
19
45
|
min = Math.ceil(min);
|
|
@@ -164,16 +190,6 @@ function mapFind(map, predicate) {
|
|
|
164
190
|
}
|
|
165
191
|
return void 0;
|
|
166
192
|
}
|
|
167
|
-
function objectToMap(object) {
|
|
168
|
-
const map = /* @__PURE__ */ new Map();
|
|
169
|
-
for (const [key, value] of Object.entries(object)) {
|
|
170
|
-
map.set(key, value);
|
|
171
|
-
}
|
|
172
|
-
return map;
|
|
173
|
-
}
|
|
174
|
-
function objectToReadonlyMap(object) {
|
|
175
|
-
return objectToMap(object);
|
|
176
|
-
}
|
|
177
193
|
|
|
178
194
|
function clamp(num, min, max) {
|
|
179
195
|
return Math.max(min, Math.min(num, max));
|
|
@@ -190,6 +206,26 @@ function objectFilter(object, predicate) {
|
|
|
190
206
|
}
|
|
191
207
|
return array;
|
|
192
208
|
}
|
|
209
|
+
function objectToMap(object) {
|
|
210
|
+
const map = /* @__PURE__ */ new Map();
|
|
211
|
+
for (const [key, value] of Object.entries(object)) {
|
|
212
|
+
map.set(key, value);
|
|
213
|
+
}
|
|
214
|
+
return map;
|
|
215
|
+
}
|
|
216
|
+
function objectToReadonlyMap(object) {
|
|
217
|
+
return objectToMap(object);
|
|
218
|
+
}
|
|
219
|
+
function objectToReverseMap(object) {
|
|
220
|
+
const map = /* @__PURE__ */ new Map();
|
|
221
|
+
for (const [key, value] of Object.entries(object)) {
|
|
222
|
+
map.set(value, key);
|
|
223
|
+
}
|
|
224
|
+
return map;
|
|
225
|
+
}
|
|
226
|
+
function objectToReverseReadonlyMap(object) {
|
|
227
|
+
return objectToReverseMap(object);
|
|
228
|
+
}
|
|
193
229
|
|
|
194
230
|
function addSetsToSet(mainSet, ...setsToAdd) {
|
|
195
231
|
for (const set of setsToAdd) {
|
|
@@ -448,6 +484,13 @@ function truncateString(string, maxLength) {
|
|
|
448
484
|
return string.slice(0, maxLength);
|
|
449
485
|
}
|
|
450
486
|
|
|
487
|
+
function getElapsedSeconds(startTime) {
|
|
488
|
+
const endTime = Date.now();
|
|
489
|
+
const elapsedMilliseconds = endTime - startTime;
|
|
490
|
+
const elapsedSeconds = elapsedMilliseconds / 1e3;
|
|
491
|
+
return Math.floor(elapsedSeconds);
|
|
492
|
+
}
|
|
493
|
+
|
|
451
494
|
function* tupleEntries(tuple) {
|
|
452
495
|
yield* tuple.entries();
|
|
453
496
|
}
|
|
@@ -455,10 +498,6 @@ function* tupleKeys(tuple) {
|
|
|
455
498
|
yield* tuple.keys();
|
|
456
499
|
}
|
|
457
500
|
|
|
458
|
-
function isObject(variable) {
|
|
459
|
-
return typeof variable === "object" && variable !== null && !Array.isArray(variable);
|
|
460
|
-
}
|
|
461
|
-
|
|
462
501
|
const ReadonlyMap = Map;
|
|
463
502
|
|
|
464
|
-
export { HOUR_IN_MILLISECONDS, MINUTE_IN_MILLISECONDS, ReadonlyMap, ReadonlySet, SECOND_IN_MILLISECONDS, addSetsToSet, arrayCopyTwoDimensional, arrayEquals, arrayRemove, arrayRemoveInPlace, assertDefined, assertNotNull, capitalizeFirstLetter, clamp, combineSets, copySet, eRange, emptyArray, escapeHTMLCharacters, filterMap, getEnumEntries, getEnumKeys, getEnumValues, getNumConsecutiveDiacritics, getRandomArrayElement, getRandomArrayIndex, getRandomInt, hasDiacritic, hasEmoji, hasWhitespace, iRange, includes, interfaceSatisfiesEnum, isArray, isEnumValue, isFirstLetterCapitalized, isKebabCase, isKeyOf, isObject, isSemanticVersion, kebabCaseToCamelCase, mapFilter, mapFind, newArray, noop, normalizeString, objectFilter, objectKeysToReadonlySet, objectKeysToSet, objectToMap, objectToReadonlyMap, objectValuesToReadonlySet, objectValuesToSet, parseFloatSafe, parseIntSafe, parseSemanticVersion, removeLinesBetweenMarkers, removeLinesMatching, removeNonPrintableCharacters, removeWhitespace, repeat, setAdd, setHas, sortCaseInsensitive, sumArray, todo, trimPrefix, trimSuffix, truncateString, tupleEntries, tupleKeys };
|
|
503
|
+
export { HOUR_IN_MILLISECONDS, MINUTE_IN_MILLISECONDS, ReadonlyMap, ReadonlySet, SECOND_IN_MILLISECONDS, addSetsToSet, arrayCopyTwoDimensional, arrayEquals, arrayRemove, arrayRemoveInPlace, assertDefined, assertNotNull, assertObject, assertType, capitalizeFirstLetter, clamp, combineSets, copySet, eRange, emptyArray, escapeHTMLCharacters, filterMap, getElapsedSeconds, getEnumEntries, getEnumKeys, getEnumValues, getNumConsecutiveDiacritics, getRandomArrayElement, getRandomArrayIndex, getRandomInt, hasDiacritic, hasEmoji, hasWhitespace, iRange, includes, interfaceSatisfiesEnum, isArray, isEnumValue, isFirstLetterCapitalized, isKebabCase, isKeyOf, isObject, isSemanticVersion, kebabCaseToCamelCase, mapFilter, mapFind, newArray, noop, normalizeString, objectFilter, objectKeysToReadonlySet, objectKeysToSet, objectToMap, objectToReadonlyMap, objectToReverseMap, objectToReverseReadonlyMap, objectValuesToReadonlySet, objectValuesToSet, parseFloatSafe, parseIntSafe, parseSemanticVersion, removeLinesBetweenMarkers, removeLinesMatching, removeNonPrintableCharacters, removeWhitespace, repeat, setAdd, setHas, sortCaseInsensitive, sumArray, todo, trimPrefix, trimSuffix, truncateString, tupleEntries, tupleKeys };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "complete-common",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Helper functions for TypeScript projects.",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"homepage": "https://complete-ts.github.io/",
|
|
@@ -35,12 +35,12 @@
|
|
|
35
35
|
"test": "glob \"./src/**/*.test.ts\" --cmd=\"node --import tsx --test --test-reporter spec\""
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@types/node": "
|
|
39
|
-
"complete-node": "
|
|
40
|
-
"eslint-plugin-sort-exports": "
|
|
41
|
-
"glob": "
|
|
42
|
-
"typescript": "
|
|
43
|
-
"typescript-eslint": "
|
|
44
|
-
"unbuild": "
|
|
38
|
+
"@types/node": "22.13.5",
|
|
39
|
+
"complete-node": "3.0.0",
|
|
40
|
+
"eslint-plugin-sort-exports": "0.9.1",
|
|
41
|
+
"glob": "11.0.1",
|
|
42
|
+
"typescript": "5.7.3",
|
|
43
|
+
"typescript-eslint": "8.24.1",
|
|
44
|
+
"unbuild": "3.3.1"
|
|
45
45
|
}
|
|
46
46
|
}
|
package/src/functions/assert.ts
CHANGED
|
@@ -4,6 +4,21 @@
|
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
import { isObject } from "./types.js";
|
|
8
|
+
|
|
9
|
+
const TYPES_TO_VALIDATION_FUNCTIONS = {
|
|
10
|
+
string: (val: unknown): val is string => typeof val === "string",
|
|
11
|
+
number: (val: unknown): val is number => typeof val === "number",
|
|
12
|
+
boolean: (val: unknown): val is boolean => typeof val === "boolean",
|
|
13
|
+
object: (val: unknown): val is object =>
|
|
14
|
+
val !== null && typeof val === "object",
|
|
15
|
+
array: (val: unknown): val is unknown[] => Array.isArray(val),
|
|
16
|
+
null: (val: unknown): val is null => val === null,
|
|
17
|
+
undefined: (val: unknown): val is undefined => val === undefined,
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
19
|
+
function: (val: unknown): val is Function => typeof val === "function",
|
|
20
|
+
} as const;
|
|
21
|
+
|
|
7
22
|
/**
|
|
8
23
|
* Helper function to throw an error if the provided value is equal to `undefined`.
|
|
9
24
|
*
|
|
@@ -39,3 +54,33 @@ export function assertNotNull<T>(
|
|
|
39
54
|
throw new TypeError(msg);
|
|
40
55
|
}
|
|
41
56
|
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Helper function to throw an error if the provided value is not an object (i.e. a TypeScript
|
|
60
|
+
* record).
|
|
61
|
+
*
|
|
62
|
+
* This is useful to have TypeScript narrow a `Record<string, unknown> | undefined` value to
|
|
63
|
+
* `Record<string, unknown>` in a concise way.
|
|
64
|
+
*
|
|
65
|
+
* Under the hood, this function uses the `isObject` helper function.
|
|
66
|
+
*/
|
|
67
|
+
export function assertObject(
|
|
68
|
+
value: unknown,
|
|
69
|
+
msg: string,
|
|
70
|
+
): asserts value is Record<string, unknown> {
|
|
71
|
+
if (!isObject(value)) {
|
|
72
|
+
throw new TypeError(msg);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Helper function to throw an error if the provided value is not of the provided type. */
|
|
77
|
+
export function assertType(
|
|
78
|
+
value: unknown,
|
|
79
|
+
type: keyof typeof TYPES_TO_VALIDATION_FUNCTIONS,
|
|
80
|
+
msg: string,
|
|
81
|
+
): asserts value is unknown {
|
|
82
|
+
const validationFunction = TYPES_TO_VALIDATION_FUNCTIONS[type];
|
|
83
|
+
if (!validationFunction(value)) {
|
|
84
|
+
throw new TypeError(msg);
|
|
85
|
+
}
|
|
86
|
+
}
|
package/src/functions/map.ts
CHANGED
|
@@ -50,48 +50,3 @@ export function mapFind<K, V>(
|
|
|
50
50
|
|
|
51
51
|
return undefined;
|
|
52
52
|
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Helper function to convert an object to a map.
|
|
56
|
-
*
|
|
57
|
-
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
58
|
-
* then later on you need to query it in a way where you expect the return value to be T or
|
|
59
|
-
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
60
|
-
* assertions.
|
|
61
|
-
*
|
|
62
|
-
* Note that the map values will be inserted in a random order, due to how `pairs` works under the
|
|
63
|
-
* hood.
|
|
64
|
-
*
|
|
65
|
-
* Also see the `objectToReadonlyMap` function.
|
|
66
|
-
*/
|
|
67
|
-
// eslint-disable-next-line complete/no-mutable-return
|
|
68
|
-
export function objectToMap<K extends string | number | symbol, V>(
|
|
69
|
-
object: Record<K, V>,
|
|
70
|
-
): Map<K, V> {
|
|
71
|
-
const map = new Map<K, V>();
|
|
72
|
-
|
|
73
|
-
for (const [key, value] of Object.entries(object)) {
|
|
74
|
-
map.set(key as K, value as V);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return map;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Helper function to convert an object to a read-only map.
|
|
82
|
-
*
|
|
83
|
-
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
84
|
-
* then later on you need to query it in a way where you expect the return value to be T or
|
|
85
|
-
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
86
|
-
* assertions.
|
|
87
|
-
*
|
|
88
|
-
* Note that the map values will be inserted in a random order, due to how `pairs` works under the
|
|
89
|
-
* hood.
|
|
90
|
-
*
|
|
91
|
-
* Also see the `objectToMap` function.
|
|
92
|
-
*/
|
|
93
|
-
export function objectToReadonlyMap<K extends string | number | symbol, V>(
|
|
94
|
-
object: Record<K, V>,
|
|
95
|
-
): ReadonlyMap<K, V> {
|
|
96
|
-
return objectToMap(object);
|
|
97
|
-
}
|
package/src/functions/object.ts
CHANGED
|
@@ -30,3 +30,85 @@ export function objectFilter<K extends string | number | symbol, V>(
|
|
|
30
30
|
|
|
31
31
|
return array;
|
|
32
32
|
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Helper function to convert an object to a map.
|
|
36
|
+
*
|
|
37
|
+
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
38
|
+
* then later on you need to query it in a way where you expect the return value to be T or
|
|
39
|
+
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
40
|
+
* assertions.
|
|
41
|
+
*
|
|
42
|
+
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
43
|
+
* only having string keys under the hood.
|
|
44
|
+
*
|
|
45
|
+
* Also see the `objectToReadonlyMap` function.
|
|
46
|
+
*/
|
|
47
|
+
// eslint-disable-next-line complete/no-mutable-return
|
|
48
|
+
export function objectToMap<K extends string | number | symbol, V>(
|
|
49
|
+
object: Record<K, V>,
|
|
50
|
+
): Map<K, V> {
|
|
51
|
+
const map = new Map<K, V>();
|
|
52
|
+
|
|
53
|
+
for (const [key, value] of Object.entries(object)) {
|
|
54
|
+
map.set(key as K, value as V);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return map;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Helper function to convert an object to a read-only map.
|
|
62
|
+
*
|
|
63
|
+
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
64
|
+
* then later on you need to query it in a way where you expect the return value to be T or
|
|
65
|
+
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
66
|
+
* assertions.
|
|
67
|
+
*
|
|
68
|
+
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
69
|
+
* only having string keys under the hood.
|
|
70
|
+
*
|
|
71
|
+
* Also see the `objectToMap` function.
|
|
72
|
+
*/
|
|
73
|
+
export function objectToReadonlyMap<K extends string | number | symbol, V>(
|
|
74
|
+
object: Record<K, V>,
|
|
75
|
+
): ReadonlyMap<K, V> {
|
|
76
|
+
return objectToMap(object);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Helper function to convert an object to a reverse map.
|
|
81
|
+
*
|
|
82
|
+
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
83
|
+
* only having string keys under the hood.
|
|
84
|
+
*
|
|
85
|
+
* Also see the `objectToReverseReadonlyMap` function.
|
|
86
|
+
*/
|
|
87
|
+
// eslint-disable-next-line complete/no-mutable-return
|
|
88
|
+
export function objectToReverseMap<
|
|
89
|
+
K extends string | number | symbol,
|
|
90
|
+
V extends string | number | symbol,
|
|
91
|
+
>(object: Record<K, V>): Map<V, K> {
|
|
92
|
+
const map = new Map<V, K>();
|
|
93
|
+
|
|
94
|
+
for (const [key, value] of Object.entries(object)) {
|
|
95
|
+
map.set(value as V, key as K);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return map;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Helper function to convert an object to a reverse read-only map.
|
|
103
|
+
*
|
|
104
|
+
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
105
|
+
* only having string keys under the hood.
|
|
106
|
+
*
|
|
107
|
+
* Also see the `objectToReverseMap` function.
|
|
108
|
+
*/
|
|
109
|
+
export function objectToReverseReadonlyMap<
|
|
110
|
+
K extends string | number | symbol,
|
|
111
|
+
V extends string | number | symbol,
|
|
112
|
+
>(object: Record<K, V>): ReadonlyMap<V, K> {
|
|
113
|
+
return objectToReverseMap(object);
|
|
114
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper functions having to do with time.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Helper function to get the number of elapsed seconds since a starting time.
|
|
9
|
+
*
|
|
10
|
+
* This function always returns a whole number (using `Math.floor` on the result).
|
|
11
|
+
*
|
|
12
|
+
* For example:
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* const startTime = Date.now();
|
|
16
|
+
* doSomething();
|
|
17
|
+
* const elapsedSeconds = getElapsedSeconds(startTime);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export function getElapsedSeconds(startTime: number): number {
|
|
21
|
+
const endTime = Date.now();
|
|
22
|
+
const elapsedMilliseconds = endTime - startTime;
|
|
23
|
+
const elapsedSeconds = elapsedMilliseconds / 1000;
|
|
24
|
+
|
|
25
|
+
return Math.floor(elapsedSeconds);
|
|
26
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -9,6 +9,7 @@ export * from "./functions/random.js";
|
|
|
9
9
|
export * from "./functions/set.js";
|
|
10
10
|
export * from "./functions/sort.js";
|
|
11
11
|
export * from "./functions/string.js";
|
|
12
|
+
export * from "./functions/time.js";
|
|
12
13
|
export * from "./functions/tuple.js";
|
|
13
14
|
export * from "./functions/types.js";
|
|
14
15
|
export * from "./functions/utils.js";
|