@silvermine/toolbox 0.5.1 → 0.7.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.
Files changed (131) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/commonjs/index.js +3 -0
  3. package/dist/commonjs/index.js.map +1 -1
  4. package/dist/commonjs/types/KeyValueStringObject.js +3 -4
  5. package/dist/commonjs/types/KeyValueStringObject.js.map +1 -1
  6. package/dist/commonjs/types/StringArrayOfStringsMap.js +3 -4
  7. package/dist/commonjs/types/StringArrayOfStringsMap.js.map +1 -1
  8. package/dist/commonjs/types/StringMap.js +3 -4
  9. package/dist/commonjs/types/StringMap.js.map +1 -1
  10. package/dist/commonjs/types/StringUnknownMap.js +2 -3
  11. package/dist/commonjs/types/StringUnknownMap.js.map +1 -1
  12. package/dist/commonjs/utils/chunk.js +1 -2
  13. package/dist/commonjs/utils/chunk.js.map +1 -1
  14. package/dist/commonjs/utils/compact.js +1 -2
  15. package/dist/commonjs/utils/compact.js.map +1 -1
  16. package/dist/commonjs/utils/delay.js +1 -2
  17. package/dist/commonjs/utils/delay.js.map +1 -1
  18. package/dist/commonjs/utils/escape-html.js +3 -4
  19. package/dist/commonjs/utils/escape-html.js.map +1 -1
  20. package/dist/commonjs/utils/flatten.js +1 -2
  21. package/dist/commonjs/utils/flatten.js.map +1 -1
  22. package/dist/commonjs/utils/get-tag-string.js +1 -2
  23. package/dist/commonjs/utils/get-tag-string.js.map +1 -1
  24. package/dist/commonjs/utils/get.js +3 -4
  25. package/dist/commonjs/utils/get.js.map +1 -1
  26. package/dist/commonjs/utils/group-by.js +30 -0
  27. package/dist/commonjs/utils/group-by.js.map +1 -0
  28. package/dist/commonjs/utils/has-defined.js +2 -3
  29. package/dist/commonjs/utils/has-defined.js.map +1 -1
  30. package/dist/commonjs/utils/is-arguments.js +2 -3
  31. package/dist/commonjs/utils/is-arguments.js.map +1 -1
  32. package/dist/commonjs/utils/is-array-of-strings.js +3 -4
  33. package/dist/commonjs/utils/is-array-of-strings.js.map +1 -1
  34. package/dist/commonjs/utils/is-array.js +1 -2
  35. package/dist/commonjs/utils/is-array.js.map +1 -1
  36. package/dist/commonjs/utils/is-boolean.js +1 -2
  37. package/dist/commonjs/utils/is-boolean.js.map +1 -1
  38. package/dist/commonjs/utils/is-empty.js +5 -6
  39. package/dist/commonjs/utils/is-empty.js.map +1 -1
  40. package/dist/commonjs/utils/is-enum-value.js +1 -2
  41. package/dist/commonjs/utils/is-enum-value.js.map +1 -1
  42. package/dist/commonjs/utils/is-function.js +12 -0
  43. package/dist/commonjs/utils/is-function.js.map +1 -0
  44. package/dist/commonjs/utils/is-map-with-values-of-type.js +2 -3
  45. package/dist/commonjs/utils/is-map-with-values-of-type.js.map +1 -1
  46. package/dist/commonjs/utils/is-not-null-or-undefined.js +2 -3
  47. package/dist/commonjs/utils/is-not-null-or-undefined.js.map +1 -1
  48. package/dist/commonjs/utils/is-null.js +1 -2
  49. package/dist/commonjs/utils/is-null.js.map +1 -1
  50. package/dist/commonjs/utils/is-number.js +2 -3
  51. package/dist/commonjs/utils/is-number.js.map +1 -1
  52. package/dist/commonjs/utils/is-object.js +1 -2
  53. package/dist/commonjs/utils/is-object.js.map +1 -1
  54. package/dist/commonjs/utils/is-promise-like.js +2 -3
  55. package/dist/commonjs/utils/is-promise-like.js.map +1 -1
  56. package/dist/commonjs/utils/is-promise.js +2 -3
  57. package/dist/commonjs/utils/is-promise.js.map +1 -1
  58. package/dist/commonjs/utils/is-set.js +1 -2
  59. package/dist/commonjs/utils/is-set.js.map +1 -1
  60. package/dist/commonjs/utils/is-string.js +2 -3
  61. package/dist/commonjs/utils/is-string.js.map +1 -1
  62. package/dist/commonjs/utils/is-undefined.js +1 -2
  63. package/dist/commonjs/utils/is-undefined.js.map +1 -1
  64. package/dist/commonjs/utils/make-template.js +3 -4
  65. package/dist/commonjs/utils/make-template.js.map +1 -1
  66. package/dist/commonjs/utils/pick.js +2 -3
  67. package/dist/commonjs/utils/pick.js.map +1 -1
  68. package/dist/commonjs/utils/pluck.js +1 -2
  69. package/dist/commonjs/utils/pluck.js.map +1 -1
  70. package/dist/commonjs/utils/range.js +1 -2
  71. package/dist/commonjs/utils/range.js.map +1 -1
  72. package/dist/commonjs/utils/sort-keys-replacer.js +23 -0
  73. package/dist/commonjs/utils/sort-keys-replacer.js.map +1 -0
  74. package/dist/commonjs/utils/uniq.js +2 -1
  75. package/dist/commonjs/utils/uniq.js.map +1 -1
  76. package/dist/esm/index.js +3 -0
  77. package/dist/esm/index.js.map +1 -1
  78. package/dist/esm/types/KeyValueStringObject.js.map +1 -1
  79. package/dist/esm/types/Omit.js +1 -0
  80. package/dist/esm/types/Optional.js +1 -0
  81. package/dist/esm/types/PropsWithType.js +1 -0
  82. package/dist/esm/types/RequireDefined.js +1 -0
  83. package/dist/esm/types/RequireOptional.js +1 -0
  84. package/dist/esm/types/StrictUnion.js +1 -0
  85. package/dist/esm/types/StringArrayOfStringsMap.js.map +1 -1
  86. package/dist/esm/types/StringMap.js.map +1 -1
  87. package/dist/esm/types/UnionKeys.js +1 -0
  88. package/dist/esm/types/Writable.js +1 -0
  89. package/dist/esm/utils/chunk.js.map +1 -1
  90. package/dist/esm/utils/delay.js.map +1 -1
  91. package/dist/esm/utils/escape-html.js +1 -1
  92. package/dist/esm/utils/escape-html.js.map +1 -1
  93. package/dist/esm/utils/get.js.map +1 -1
  94. package/dist/esm/utils/group-by.js +27 -0
  95. package/dist/esm/utils/group-by.js.map +1 -0
  96. package/dist/esm/utils/is-array-of-strings.js.map +1 -1
  97. package/dist/esm/utils/is-empty.js.map +1 -1
  98. package/dist/esm/utils/is-enum-value.js.map +1 -1
  99. package/dist/esm/utils/is-function.js +9 -0
  100. package/dist/esm/utils/is-function.js.map +1 -0
  101. package/dist/esm/utils/is-map-with-values-of-type.js.map +1 -1
  102. package/dist/esm/utils/make-template.js.map +1 -1
  103. package/dist/esm/utils/pick.js.map +1 -1
  104. package/dist/esm/utils/range.js.map +1 -1
  105. package/dist/esm/utils/sort-keys-replacer.js +20 -0
  106. package/dist/esm/utils/sort-keys-replacer.js.map +1 -0
  107. package/dist/esm/utils/uniq.js.map +1 -1
  108. package/dist/types/index.d.ts +3 -0
  109. package/dist/types/types/Omit.d.ts +1 -1
  110. package/dist/types/types/Optional.d.ts +1 -1
  111. package/dist/types/types/PropsWithType.d.ts +1 -1
  112. package/dist/types/types/RequireDefined.d.ts +1 -1
  113. package/dist/types/types/RequireOptional.d.ts +1 -1
  114. package/dist/types/types/StrictUnion.d.ts +3 -3
  115. package/dist/types/types/UnionKeys.d.ts +1 -1
  116. package/dist/types/types/Writable.d.ts +1 -1
  117. package/dist/types/utils/compact.d.ts +1 -1
  118. package/dist/types/utils/delay.d.ts +2 -1
  119. package/dist/types/utils/group-by.d.ts +7 -0
  120. package/dist/types/utils/is-empty.d.ts +1 -1
  121. package/dist/types/utils/is-enum-value.d.ts +1 -1
  122. package/dist/types/utils/is-function.d.ts +6 -0
  123. package/dist/types/utils/sort-keys-replacer.d.ts +9 -0
  124. package/dist/types/utils/uniq.d.ts +1 -1
  125. package/package.json +3 -3
  126. package/src/index.ts +3 -0
  127. package/src/utils/delay.ts +3 -1
  128. package/src/utils/group-by.ts +32 -0
  129. package/src/utils/is-enum-value.ts +1 -1
  130. package/src/utils/is-function.ts +8 -0
  131. package/src/utils/sort-keys-replacer.ts +20 -0
@@ -0,0 +1,20 @@
1
+ import { isStringUnknownMap } from '../types/StringUnknownMap';
2
+ import { isFunction } from './is-function';
3
+ /**
4
+ * JSON replacer that recursively sorts object keys to ensure consistent serialization.
5
+ *
6
+ * @example
7
+ * ```
8
+ * JSON.stringify(query, sortKeysReplacer);
9
+ * ```
10
+ */
11
+ export function sortKeysReplacer(_key, value) {
12
+ if (isStringUnknownMap(value) && !isFunction(value)) {
13
+ return Object.keys(value).sort().reduce((memo, k) => {
14
+ memo[k] = value[k];
15
+ return memo;
16
+ }, {});
17
+ }
18
+ return value;
19
+ }
20
+ //# sourceMappingURL=sort-keys-replacer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sort-keys-replacer.js","sourceRoot":"","sources":["../../../src/utils/sort-keys-replacer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAoB,MAAM,2BAA2B,CAAC;AACjF,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAY,EAAE,KAAc;IAC1D,IAAI,kBAAkB,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;QACnD,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE;YACjD,IAAI,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;YACnB,OAAO,IAAI,CAAC;QACf,CAAC,EAAE,EAAsB,CAAC,CAAC;IAC9B,CAAC;IACD,OAAO,KAAK,CAAC;AAChB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"uniq.js","sourceRoot":"","sources":["../../../src/utils/uniq.ts"],"names":[],"mappings":"AAAA,MAAM,YAAY,GAAG,CAAO,GAAQ,EAAE,QAA8C,EAAO,EAAE;IAC1F,MAAM,MAAM,GAAQ,EAAE,EAChB,IAAI,GAAQ,EAAE,CAAC;IAErB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE;QACnD,MAAM,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,EACd,QAAQ,GAAG,QAAQ,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC;QAEzC,IAAI,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE;YAChC,IAAI,CA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IACvC,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;QAC9C,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,KAAK,CAAC;IACxC,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AACjB,CAAC,CAAC;AAGF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,CAAO,GAAQ,EAAE,QAAkB,EAAE,QAA+C,EAAO,EAAE;IAC9G,IAAI,QAAQ,EAAE,CAAC;QACZ,OAAO,YAAY,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;IACtC,CAAC;IACD,IAAI,QAAQ,EAAE,CAAC;QACZ,OAAO,UAAU,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;IACD,OAAO,YAAY,CAAC,GAAG,CAAC,CAAC;AAC5B,CAAC,CAAC"}
@@ -16,6 +16,7 @@ export * from './utils/is-array-of-strings';
16
16
  export * from './utils/is-boolean';
17
17
  export * from './utils/is-empty';
18
18
  export * from './utils/is-enum-value';
19
+ export * from './utils/is-function';
19
20
  export * from './utils/is-map-with-values-of-type';
20
21
  export * from './utils/is-number';
21
22
  export * from './utils/is-object';
@@ -33,7 +34,9 @@ export * from './utils/pick';
33
34
  export * from './utils/pluck';
34
35
  export * from './utils/delay';
35
36
  export * from './utils/get';
37
+ export * from './utils/group-by';
36
38
  export * from './utils/escape-html';
37
39
  export * from './utils/make-template';
38
40
  export * from './utils/uniq';
39
41
  export * from './utils/range';
42
+ export * from './utils/sort-keys-replacer';
@@ -5,4 +5,4 @@
5
5
  * @deprecated This will be removed once the TS version for this project has been upgraded
6
6
  * to TS 3.5 or greater
7
7
  */
8
- export declare type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
8
+ export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
@@ -22,4 +22,4 @@ import { Omit } from './Omit';
22
22
  * }
23
23
  * ```
24
24
  */
25
- export declare type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
25
+ export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
@@ -15,6 +15,6 @@
15
15
  * }
16
16
  * ```
17
17
  */
18
- export declare type PropsWithType<O, T> = Exclude<keyof Pick<O, {
18
+ export type PropsWithType<O, T> = Exclude<keyof Pick<O, {
19
19
  [K in keyof O]: O[K] extends T ? K : never;
20
20
  }[keyof O]>, undefined>;
@@ -18,6 +18,6 @@
18
18
  * }
19
19
  * ```
20
20
  */
21
- export declare type RequireDefined<T, K extends keyof T> = T & {
21
+ export type RequireDefined<T, K extends keyof T> = T & {
22
22
  [P in K]-?: Exclude<T[P], undefined>;
23
23
  };
@@ -18,6 +18,6 @@
18
18
  * }
19
19
  * ```
20
20
  */
21
- export declare type RequireOptional<T, K extends keyof T> = T & {
21
+ export type RequireOptional<T, K extends keyof T> = T & {
22
22
  [P in K]-?: T[P];
23
23
  };
@@ -1,8 +1,8 @@
1
1
  import { UnionKeys } from './UnionKeys';
2
- declare type InvalidKeys<K extends string | number | symbol> = {
2
+ type InvalidKeys<K extends string | number | symbol> = {
3
3
  [P in K]?: never;
4
4
  };
5
- declare type StrictUnionHelper<T, TAll> = T extends unknown ? (T & InvalidKeys<Exclude<UnionKeys<TAll>, keyof T>>) : never;
5
+ type StrictUnionHelper<T, TAll> = T extends unknown ? (T & InvalidKeys<Exclude<UnionKeys<TAll>, keyof T>>) : never;
6
6
  /**
7
7
  * A basic TypeScript union (e.g. A | B) results in a type containing the available
8
8
  * properties from the provided types. When StrictUnion is used (e.g. StrictUnion<A | B>),
@@ -11,5 +11,5 @@ declare type StrictUnionHelper<T, TAll> = T extends unknown ? (T & InvalidKeys<E
11
11
  *
12
12
  * See: https://github.com/microsoft/TypeScript/issues/20863#issuecomment-520551758
13
13
  */
14
- export declare type StrictUnion<T> = StrictUnionHelper<T, T>;
14
+ export type StrictUnion<T> = StrictUnionHelper<T, T>;
15
15
  export {};
@@ -6,4 +6,4 @@
6
6
  * type Keys = UnionKeys<Union>; // 'a' | 'b'
7
7
  * ```
8
8
  */
9
- export declare type UnionKeys<T> = T extends T ? keyof T : never;
9
+ export type UnionKeys<T> = T extends T ? keyof T : never;
@@ -19,6 +19,6 @@
19
19
  * }
20
20
  * ```
21
21
  */
22
- export declare type Writable<T> = {
22
+ export type Writable<T> = {
23
23
  -readonly [P in keyof T]: T[P];
24
24
  };
@@ -1,3 +1,3 @@
1
- declare type FalsyVals = '' | 0 | false | null | undefined;
1
+ type FalsyVals = '' | 0 | false | null | undefined;
2
2
  export declare function compact<T>(arr: (T | FalsyVals)[]): T[];
3
3
  export {};
@@ -1 +1,2 @@
1
- export declare function delay<T = undefined>(ms: number, value?: T): Promise<T>;
1
+ export declare function delay(ms: number): Promise<undefined>;
2
+ export declare function delay<T>(ms: number, value: T): Promise<T>;
@@ -0,0 +1,7 @@
1
+ import { PropsWithType } from '../types/PropsWithType';
2
+ /**
3
+ * Given an array of items and a function that returns a key, groups the items by the key.
4
+ * If the returned key is `undefined`, the item is not included in the result. If a string
5
+ * is passed as the second argument, the item is grouped by the values of that property.
6
+ */
7
+ export declare function groupBy<T, U extends string>(items: T[], fn: ((item: T) => string | undefined) | PropsWithType<T, string>): Record<U, T[] | undefined>;
@@ -4,7 +4,7 @@ interface IEmptyArguments extends IArguments {
4
4
  interface IEmptyObj {
5
5
  [s: string]: never;
6
6
  }
7
- declare type IEmptyTypes = (null | undefined | boolean | number | never[] | '' | IEmptyArguments | Set<never> | IEmptyObj);
7
+ type IEmptyTypes = (null | undefined | boolean | number | never[] | '' | IEmptyArguments | Set<never> | IEmptyObj);
8
8
  /**
9
9
  * Checks if `o` is an empty object. An object is "empty" if it:
10
10
  *
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * Type guard to check to see if the given value is a valid value for the enum.
3
3
  */
4
- export declare function isEnumValue<T>(enumType: T, value: unknown): value is T[keyof T];
4
+ export declare function isEnumValue<T extends object>(enumType: T, value: unknown): value is T[keyof T];
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Type guard for functions.
3
+ *
4
+ * @returns `true` if `o` is a `function`.
5
+ */
6
+ export declare function isFunction(o: unknown): o is Function;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * JSON replacer that recursively sorts object keys to ensure consistent serialization.
3
+ *
4
+ * @example
5
+ * ```
6
+ * JSON.stringify(query, sortKeysReplacer);
7
+ * ```
8
+ */
9
+ export declare function sortKeysReplacer(_key: string, value: unknown): unknown;
@@ -5,4 +5,4 @@
5
5
  * is not a one-to-one function, so providing an iteratee will disable
6
6
  * the faster algorithm.
7
7
  */
8
- export declare const uniq: <T, U>(arr: T[], isSorted?: boolean | undefined, iteratee?: ((value: T, i: number, arr: T[]) => U) | undefined) => T[];
8
+ export declare const uniq: <T, U>(arr: T[], isSorted?: boolean, iteratee?: (value: T, i: number, arr: T[]) => U) => T[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@silvermine/toolbox",
3
- "version": "0.5.1",
3
+ "version": "0.7.0",
4
4
  "description": "A library of common TypeScript types, custom type guards, and utility functions.",
5
5
  "main": "./dist/commonjs/index",
6
6
  "types": "./dist/types/index.d.ts",
@@ -42,7 +42,7 @@
42
42
  "@silvermine/typescript-config": "git+https://github.com/silvermine/typescript-config#23213e33077089e723629dead5342abe6f3b3c8c",
43
43
  "@types/chai": "4.3.20",
44
44
  "@types/mocha": "5.2.7",
45
- "@types/node": "12.20.45",
45
+ "@types/node": "20.12.2",
46
46
  "@types/sinon": "5.0.7",
47
47
  "chai": "4.5.0",
48
48
  "check-node-version": "4.2.1",
@@ -60,6 +60,6 @@
60
60
  "source-map-support": "0.5.21",
61
61
  "ts-node": "7.0.1",
62
62
  "tslib": "2.6.3",
63
- "typescript": "3.9.10"
63
+ "typescript": "5.7.2"
64
64
  }
65
65
  }
package/src/index.ts CHANGED
@@ -17,6 +17,7 @@ export * from './utils/is-array-of-strings';
17
17
  export * from './utils/is-boolean';
18
18
  export * from './utils/is-empty';
19
19
  export * from './utils/is-enum-value';
20
+ export * from './utils/is-function';
20
21
  export * from './utils/is-map-with-values-of-type';
21
22
  export * from './utils/is-number';
22
23
  export * from './utils/is-object';
@@ -35,7 +36,9 @@ export * from './utils/pick';
35
36
  export * from './utils/pluck';
36
37
  export * from './utils/delay';
37
38
  export * from './utils/get';
39
+ export * from './utils/group-by';
38
40
  export * from './utils/escape-html';
39
41
  export * from './utils/make-template';
40
42
  export * from './utils/uniq';
41
43
  export * from './utils/range';
44
+ export * from './utils/sort-keys-replacer';
@@ -1,4 +1,6 @@
1
- export function delay<T = undefined>(ms: number, value?: T): Promise<T> {
1
+ export function delay(ms: number): Promise<undefined>;
2
+ export function delay<T>(ms: number, value: T): Promise<T>;
3
+ export function delay<T>(ms: number, value?: T): Promise<T | undefined> {
2
4
  return new Promise((resolve) => {
3
5
  setTimeout(() => { resolve(value); }, ms);
4
6
  });
@@ -0,0 +1,32 @@
1
+ import { PropsWithType } from '../types/PropsWithType';
2
+ import { hasDefined } from './has-defined';
3
+ import { isString } from './is-string';
4
+
5
+ /**
6
+ * Given an array of items and a function that returns a key, groups the items by the key.
7
+ * If the returned key is `undefined`, the item is not included in the result. If a string
8
+ * is passed as the second argument, the item is grouped by the values of that property.
9
+ */
10
+ export function groupBy<T, U extends string>(items: T[], fn: ((item: T) => string | undefined) | PropsWithType<T, string>): Record<U, T[] | undefined> {
11
+ const keyFn = typeof fn === 'function' ? fn : (item: T) => {
12
+ const key = item[fn];
13
+
14
+ return isString(key) ? key : undefined;
15
+ };
16
+
17
+ return items.reduce((memo, item) => {
18
+ const key = keyFn(item);
19
+
20
+ if (key === undefined) {
21
+ return memo;
22
+ }
23
+
24
+ if (hasDefined(memo, key)) {
25
+ memo[key].push(item);
26
+ } else {
27
+ memo[key] = [ item ];
28
+ }
29
+
30
+ return memo;
31
+ }, {} as Record<string, T[] | undefined>);
32
+ }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Type guard to check to see if the given value is a valid value for the enum.
3
3
  */
4
- export function isEnumValue<T>(enumType: T, value: unknown): value is T[keyof T] {
4
+ export function isEnumValue<T extends object>(enumType: T, value: unknown): value is T[keyof T] {
5
5
  return (Object.keys(enumType) as Array<keyof T>)
6
6
  .map((key) => {
7
7
  return enumType[key];
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Type guard for functions.
3
+ *
4
+ * @returns `true` if `o` is a `function`.
5
+ */
6
+ export function isFunction(o: unknown): o is Function { // eslint-disable-line @typescript-eslint/ban-types
7
+ return typeof o === 'function';
8
+ }
@@ -0,0 +1,20 @@
1
+ import { isStringUnknownMap, StringUnknownMap } from '../types/StringUnknownMap';
2
+ import { isFunction } from './is-function';
3
+
4
+ /**
5
+ * JSON replacer that recursively sorts object keys to ensure consistent serialization.
6
+ *
7
+ * @example
8
+ * ```
9
+ * JSON.stringify(query, sortKeysReplacer);
10
+ * ```
11
+ */
12
+ export function sortKeysReplacer(_key: string, value: unknown): unknown {
13
+ if (isStringUnknownMap(value) && !isFunction(value)) {
14
+ return Object.keys(value).sort().reduce((memo, k) => {
15
+ memo[k] = value[k];
16
+ return memo;
17
+ }, {} as StringUnknownMap);
18
+ }
19
+ return value;
20
+ }