complete-common 1.3.0 → 2.0.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 (48) hide show
  1. package/dist/functions/array.d.cts +7 -7
  2. package/dist/functions/array.d.mts +7 -7
  3. package/dist/functions/array.d.ts +7 -7
  4. package/dist/functions/array.d.ts.map +1 -1
  5. package/dist/functions/assert.d.cts +12 -0
  6. package/dist/functions/assert.d.mts +12 -0
  7. package/dist/functions/assert.d.ts +12 -0
  8. package/dist/functions/assert.d.ts.map +1 -1
  9. package/dist/functions/enums.d.cts +1 -2
  10. package/dist/functions/enums.d.mts +1 -2
  11. package/dist/functions/enums.d.ts +1 -2
  12. package/dist/functions/enums.d.ts.map +1 -1
  13. package/dist/functions/map.d.cts +6 -2
  14. package/dist/functions/map.d.mts +6 -2
  15. package/dist/functions/map.d.ts +6 -2
  16. package/dist/functions/map.d.ts.map +1 -1
  17. package/dist/functions/object.d.cts +3 -30
  18. package/dist/functions/object.d.mts +3 -30
  19. package/dist/functions/object.d.ts +3 -30
  20. package/dist/functions/object.d.ts.map +1 -1
  21. package/dist/functions/set.d.cts +6 -26
  22. package/dist/functions/set.d.mts +6 -26
  23. package/dist/functions/set.d.ts +6 -26
  24. package/dist/functions/set.d.ts.map +1 -1
  25. package/dist/functions/sort.d.cts +1 -1
  26. package/dist/functions/sort.d.mts +1 -1
  27. package/dist/functions/sort.d.ts +1 -1
  28. package/dist/functions/sort.d.ts.map +1 -1
  29. package/dist/index.cjs +34 -44
  30. package/dist/index.d.cts +1 -0
  31. package/dist/index.d.mts +1 -0
  32. package/dist/index.d.ts +1 -0
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.mjs +34 -41
  35. package/dist/types/TranspiledEnum.d.cts +3 -0
  36. package/dist/types/TranspiledEnum.d.mts +3 -0
  37. package/dist/types/TranspiledEnum.d.ts +3 -0
  38. package/dist/types/TranspiledEnum.d.ts.map +1 -0
  39. package/package.json +5 -5
  40. package/src/functions/array.ts +8 -13
  41. package/src/functions/assert.ts +23 -0
  42. package/src/functions/enums.ts +1 -1
  43. package/src/functions/map.ts +8 -5
  44. package/src/functions/object.ts +3 -44
  45. package/src/functions/set.ts +8 -41
  46. package/src/functions/sort.ts +3 -2
  47. package/src/index.ts +1 -0
  48. package/src/types/TranspiledEnum.ts +2 -0
@@ -16,33 +16,13 @@ export declare function addSetsToSet<T>(mainSet: Set<T>, ...setsToAdd: ReadonlyA
16
16
  *
17
17
  * This function is variadic, meaning that you can specify N sets.
18
18
  */
19
- export declare function combineSets<T>(...sets: ReadonlyArray<ReadonlySet<T>>): Set<T>;
19
+ export declare function combineSets<T>(...sets: ReadonlyArray<ReadonlySet<T>>): ReadonlySet<T>;
20
20
  /** Helper function to copy a set. (You can also use a Set constructor to accomplish this task.) */
21
- export declare function copySet<T>(oldSet: ReadonlySet<T>): Set<T>;
22
- /**
23
- * Helper function to convert the keys of an object to a read-only set.
24
- *
25
- * Also see the `objectKeysToSet` function.
26
- */
27
- export declare function objectKeysToReadonlySet<K extends string | number | symbol, V>(object: Record<K, V>): ReadonlySet<K>;
28
- /**
29
- * Helper function to convert the keys of an object to a set.
30
- *
31
- * Also see the `objectKeysToReadonlySet` function.
32
- */
33
- export declare function objectKeysToSet<K extends string | number | symbol, V>(object: Record<K, V>): Set<K>;
34
- /**
35
- * Helper function to convert the values of an object to a read-only set.
36
- *
37
- * Also see the `objectValuesToSet` function.
38
- */
39
- export declare function objectValuesToReadonlySet<K extends string | number | symbol, V>(object: Record<K, V>): ReadonlySet<V>;
40
- /**
41
- * Helper function to convert the values of an object to a set.
42
- *
43
- * Also see the `objectValuesToReadonlySet` function.
44
- */
45
- export declare function objectValuesToSet<K extends string | number | symbol, V>(object: Record<K, V>): Set<V>;
21
+ export declare function copySet<T>(oldSet: ReadonlySet<T>): ReadonlySet<T>;
22
+ /** Helper function to convert the keys of an object to a set. */
23
+ export declare function objectKeysToSet<K extends string | number | symbol, V>(object: Record<K, V>): ReadonlySet<K>;
24
+ /** Helper function to convert the values of an object to a set. */
25
+ export declare function objectValuesToSet<K extends string | number | symbol, V>(object: Record<K, V>): ReadonlySet<V>;
46
26
  /**
47
27
  * Helper function to add one or more elements to a set at once without having to repeatedly call
48
28
  * the `Set.add` method.
@@ -16,33 +16,13 @@ export declare function addSetsToSet<T>(mainSet: Set<T>, ...setsToAdd: ReadonlyA
16
16
  *
17
17
  * This function is variadic, meaning that you can specify N sets.
18
18
  */
19
- export declare function combineSets<T>(...sets: ReadonlyArray<ReadonlySet<T>>): Set<T>;
19
+ export declare function combineSets<T>(...sets: ReadonlyArray<ReadonlySet<T>>): ReadonlySet<T>;
20
20
  /** Helper function to copy a set. (You can also use a Set constructor to accomplish this task.) */
21
- export declare function copySet<T>(oldSet: ReadonlySet<T>): Set<T>;
22
- /**
23
- * Helper function to convert the keys of an object to a read-only set.
24
- *
25
- * Also see the `objectKeysToSet` function.
26
- */
27
- export declare function objectKeysToReadonlySet<K extends string | number | symbol, V>(object: Record<K, V>): ReadonlySet<K>;
28
- /**
29
- * Helper function to convert the keys of an object to a set.
30
- *
31
- * Also see the `objectKeysToReadonlySet` function.
32
- */
33
- export declare function objectKeysToSet<K extends string | number | symbol, V>(object: Record<K, V>): Set<K>;
34
- /**
35
- * Helper function to convert the values of an object to a read-only set.
36
- *
37
- * Also see the `objectValuesToSet` function.
38
- */
39
- export declare function objectValuesToReadonlySet<K extends string | number | symbol, V>(object: Record<K, V>): ReadonlySet<V>;
40
- /**
41
- * Helper function to convert the values of an object to a set.
42
- *
43
- * Also see the `objectValuesToReadonlySet` function.
44
- */
45
- export declare function objectValuesToSet<K extends string | number | symbol, V>(object: Record<K, V>): Set<V>;
21
+ export declare function copySet<T>(oldSet: ReadonlySet<T>): ReadonlySet<T>;
22
+ /** Helper function to convert the keys of an object to a set. */
23
+ export declare function objectKeysToSet<K extends string | number | symbol, V>(object: Record<K, V>): ReadonlySet<K>;
24
+ /** Helper function to convert the values of an object to a set. */
25
+ export declare function objectValuesToSet<K extends string | number | symbol, V>(object: Record<K, V>): ReadonlySet<V>;
46
26
  /**
47
27
  * Helper function to add one or more elements to a set at once without having to repeatedly call
48
28
  * the `Set.add` method.
@@ -1 +1 @@
1
- {"version":3,"file":"set.d.ts","sourceRoot":"","sources":["../../src/functions/set.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAE5B,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC,EACf,GAAG,SAAS,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,GAC1C,IAAI,CAMN;AAED;;;;GAIG;AAEH,wBAAgB,WAAW,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAS7E;AAED,mGAAmG;AAEnG,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAOzD;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,EAC3E,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GACnB,WAAW,CAAC,CAAC,CAAC,CAEhB;AAED;;;;GAIG;AAEH,wBAAgB,eAAe,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,EACnE,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GACnB,GAAG,CAAC,CAAC,CAAC,CAQR;AAED;;;;GAIG;AACH,wBAAgB,yBAAyB,CACvC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,EAClC,CAAC,EACD,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAEtC;AAED;;;;GAIG;AAEH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,EACrE,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GACnB,GAAG,CAAC,CAAC,CAAC,CAQR;AAED;;;;;GAKG;AAEH,wBAAgB,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,GAAG,QAAQ,EAAE,SAAS,CAAC,EAAE,GAAG,IAAI,CAItE;AAED;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,CAAC,EACtB,GAAG,EAAE,WAAW,CAAC,CAAC,CAAC,EACnB,GAAG,QAAQ,EAAE,SAAS,CAAC,EAAE,GACxB,OAAO,CAET"}
1
+ {"version":3,"file":"set.d.ts","sourceRoot":"","sources":["../../src/functions/set.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAE5B,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC,EACf,GAAG,SAAS,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,GAC1C,IAAI,CAMN;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAC3B,GAAG,IAAI,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,GACrC,WAAW,CAAC,CAAC,CAAC,CAShB;AAED,mGAAmG;AACnG,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAOjE;AAED,iEAAiE;AACjE,wBAAgB,eAAe,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,EACnE,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GACnB,WAAW,CAAC,CAAC,CAAC,CAQhB;AAED,mEAAmE;AACnE,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,EACrE,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GACnB,WAAW,CAAC,CAAC,CAAC,CAQhB;AAED;;;;;GAKG;AAEH,wBAAgB,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,GAAG,QAAQ,EAAE,SAAS,CAAC,EAAE,GAAG,IAAI,CAItE;AAED;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,CAAC,EACtB,GAAG,EAAE,WAAW,CAAC,CAAC,CAAC,EACnB,GAAG,QAAQ,EAAE,SAAS,CAAC,EAAE,GACxB,OAAO,CAET"}
@@ -10,5 +10,5 @@
10
10
  * From:
11
11
  * https://stackoverflow.com/questions/8996963/how-to-perform-case-insensitive-sorting-array-of-string-in-javascript
12
12
  */
13
- export declare function sortCaseInsensitive(array: readonly string[]): string[];
13
+ export declare function sortCaseInsensitive(array: readonly string[]): readonly string[];
14
14
  //# sourceMappingURL=sort.d.ts.map
@@ -10,5 +10,5 @@
10
10
  * From:
11
11
  * https://stackoverflow.com/questions/8996963/how-to-perform-case-insensitive-sorting-array-of-string-in-javascript
12
12
  */
13
- export declare function sortCaseInsensitive(array: readonly string[]): string[];
13
+ export declare function sortCaseInsensitive(array: readonly string[]): readonly string[];
14
14
  //# sourceMappingURL=sort.d.ts.map
@@ -10,5 +10,5 @@
10
10
  * From:
11
11
  * https://stackoverflow.com/questions/8996963/how-to-perform-case-insensitive-sorting-array-of-string-in-javascript
12
12
  */
13
- export declare function sortCaseInsensitive(array: readonly string[]): string[];
13
+ export declare function sortCaseInsensitive(array: readonly string[]): readonly string[];
14
14
  //# sourceMappingURL=sort.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sort.d.ts","sourceRoot":"","sources":["../../src/functions/sort.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;GAMG;AAEH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,EAAE,CAStE"}
1
+ {"version":3,"file":"sort.d.ts","sourceRoot":"","sources":["../../src/functions/sort.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,SAAS,MAAM,EAAE,GACvB,SAAS,MAAM,EAAE,CASnB"}
package/dist/index.cjs CHANGED
@@ -6,6 +6,32 @@ const HOUR_IN_MILLISECONDS = 60 * MINUTE_IN_MILLISECONDS;
6
6
 
7
7
  const ReadonlySet = Set;
8
8
 
9
+ function getEnumEntries(transpiledEnum) {
10
+ const entries = Object.entries(transpiledEnum);
11
+ const numberEntries = entries.filter(
12
+ ([_key, value]) => typeof value === "number"
13
+ );
14
+ const entriesToReturn = numberEntries.length > 0 ? numberEntries : entries;
15
+ return entriesToReturn;
16
+ }
17
+ function getEnumKeys(transpiledEnum) {
18
+ const enumEntries = getEnumEntries(transpiledEnum);
19
+ return enumEntries.map(([key, _value]) => key);
20
+ }
21
+ function getEnumValues(transpiledEnum) {
22
+ const enumEntries = getEnumEntries(transpiledEnum);
23
+ return enumEntries.map(([_key, value]) => value);
24
+ }
25
+ function interfaceSatisfiesEnum() {
26
+ }
27
+ function isEnumValue(value, transpiledEnum, set) {
28
+ if (set !== void 0) {
29
+ return set.has(value);
30
+ }
31
+ const enumValues = getEnumValues(transpiledEnum);
32
+ return enumValues.includes(value);
33
+ }
34
+
9
35
  function isObject(variable) {
10
36
  return typeof variable === "object" && variable !== null && !Array.isArray(variable);
11
37
  }
@@ -25,6 +51,11 @@ function assertDefined(value, ...[msg]) {
25
51
  throw new TypeError(msg);
26
52
  }
27
53
  }
54
+ function assertEnumValue(value, transpiledEnum, msg, set) {
55
+ if (!isEnumValue(value, transpiledEnum, set)) {
56
+ throw new TypeError(msg);
57
+ }
58
+ }
28
59
  function assertNotNull(value, ...[msg]) {
29
60
  if (value === null) {
30
61
  throw new TypeError(msg);
@@ -150,32 +181,6 @@ function sumArray(array) {
150
181
  return array.reduce((accumulator, element) => accumulator + element, 0);
151
182
  }
152
183
 
153
- function getEnumEntries(transpiledEnum) {
154
- const entries = Object.entries(transpiledEnum);
155
- const numberEntries = entries.filter(
156
- ([_key, value]) => typeof value === "number"
157
- );
158
- const entriesToReturn = numberEntries.length > 0 ? numberEntries : entries;
159
- return entriesToReturn;
160
- }
161
- function getEnumKeys(transpiledEnum) {
162
- const enumEntries = getEnumEntries(transpiledEnum);
163
- return enumEntries.map(([key, _value]) => key);
164
- }
165
- function getEnumValues(transpiledEnum) {
166
- const enumEntries = getEnumEntries(transpiledEnum);
167
- return enumEntries.map(([_key, value]) => value);
168
- }
169
- function interfaceSatisfiesEnum() {
170
- }
171
- function isEnumValue(value, transpiledEnum, set) {
172
- if (set !== void 0) {
173
- return set.has(value);
174
- }
175
- const enumValues = getEnumValues(transpiledEnum);
176
- return enumValues.includes(value);
177
- }
178
-
179
184
  function mapFilter(map, predicate) {
180
185
  const array = [];
181
186
  for (const value of map.values()) {
@@ -187,8 +192,8 @@ function mapFilter(map, predicate) {
187
192
  return array;
188
193
  }
189
194
  function mapFind(map, predicate) {
190
- for (const value of map.values()) {
191
- const match = predicate(value);
195
+ for (const [key, value] of map) {
196
+ const match = predicate(value, key, map);
192
197
  if (match) {
193
198
  return value;
194
199
  }
@@ -218,9 +223,6 @@ function objectToMap(object) {
218
223
  }
219
224
  return map;
220
225
  }
221
- function objectToReadonlyMap(object) {
222
- return objectToMap(object);
223
- }
224
226
  function objectToReverseMap(object) {
225
227
  const map = /* @__PURE__ */ new Map();
226
228
  for (const [key, value] of Object.entries(object)) {
@@ -228,9 +230,6 @@ function objectToReverseMap(object) {
228
230
  }
229
231
  return map;
230
232
  }
231
- function objectToReverseReadonlyMap(object) {
232
- return objectToReverseMap(object);
233
- }
234
233
 
235
234
  function addSetsToSet(mainSet, ...setsToAdd) {
236
235
  for (const set of setsToAdd) {
@@ -255,9 +254,6 @@ function copySet(oldSet) {
255
254
  }
256
255
  return newSet;
257
256
  }
258
- function objectKeysToReadonlySet(object) {
259
- return objectKeysToSet(object);
260
- }
261
257
  function objectKeysToSet(object) {
262
258
  const set = /* @__PURE__ */ new Set();
263
259
  for (const key of Object.keys(object)) {
@@ -265,9 +261,6 @@ function objectKeysToSet(object) {
265
261
  }
266
262
  return set;
267
263
  }
268
- function objectValuesToReadonlySet(object) {
269
- return objectValuesToSet(object);
270
- }
271
264
  function objectValuesToSet(object) {
272
265
  const set = /* @__PURE__ */ new Set();
273
266
  for (const key of Object.values(object)) {
@@ -518,6 +511,7 @@ exports.arrayRemoveInPlace = arrayRemoveInPlace;
518
511
  exports.assertArray = assertArray;
519
512
  exports.assertBoolean = assertBoolean;
520
513
  exports.assertDefined = assertDefined;
514
+ exports.assertEnumValue = assertEnumValue;
521
515
  exports.assertNotNull = assertNotNull;
522
516
  exports.assertNumber = assertNumber;
523
517
  exports.assertObject = assertObject;
@@ -558,13 +552,9 @@ exports.newArray = newArray;
558
552
  exports.noop = noop;
559
553
  exports.normalizeString = normalizeString;
560
554
  exports.objectFilter = objectFilter;
561
- exports.objectKeysToReadonlySet = objectKeysToReadonlySet;
562
555
  exports.objectKeysToSet = objectKeysToSet;
563
556
  exports.objectToMap = objectToMap;
564
- exports.objectToReadonlyMap = objectToReadonlyMap;
565
557
  exports.objectToReverseMap = objectToReverseMap;
566
- exports.objectToReverseReadonlyMap = objectToReverseReadonlyMap;
567
- exports.objectValuesToReadonlySet = objectValuesToReadonlySet;
568
558
  exports.objectValuesToSet = objectValuesToSet;
569
559
  exports.parseFloatSafe = parseFloatSafe;
570
560
  exports.parseIntSafe = parseIntSafe;
package/dist/index.d.cts CHANGED
@@ -24,6 +24,7 @@ export type * from "./types/ObjectValues.js";
24
24
  export * from "./types/ReadonlyMap.js";
25
25
  export type * from "./types/ReadonlyRecord.js";
26
26
  export * from "./types/ReadonlySet.js";
27
+ export type * from "./types/TranspiledEnum.js";
27
28
  export type * from "./types/Tuple.js";
28
29
  export type * from "./types/WidenLiteral.js";
29
30
  export type * from "./types/Writeable.js";
package/dist/index.d.mts CHANGED
@@ -24,6 +24,7 @@ export type * from "./types/ObjectValues.js";
24
24
  export * from "./types/ReadonlyMap.js";
25
25
  export type * from "./types/ReadonlyRecord.js";
26
26
  export * from "./types/ReadonlySet.js";
27
+ export type * from "./types/TranspiledEnum.js";
27
28
  export type * from "./types/Tuple.js";
28
29
  export type * from "./types/WidenLiteral.js";
29
30
  export type * from "./types/Writeable.js";
package/dist/index.d.ts CHANGED
@@ -24,6 +24,7 @@ export type * from "./types/ObjectValues.js";
24
24
  export * from "./types/ReadonlyMap.js";
25
25
  export type * from "./types/ReadonlyRecord.js";
26
26
  export * from "./types/ReadonlySet.js";
27
+ export type * from "./types/TranspiledEnum.js";
27
28
  export type * from "./types/Tuple.js";
28
29
  export type * from "./types/WidenLiteral.js";
29
30
  export type * from "./types/Writeable.js";
@@ -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,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"}
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,2BAA2B,CAAC;AAC/C,mBAAmB,kBAAkB,CAAC;AACtC,mBAAmB,yBAAyB,CAAC;AAC7C,mBAAmB,sBAAsB,CAAC"}
package/dist/index.mjs CHANGED
@@ -4,6 +4,32 @@ const HOUR_IN_MILLISECONDS = 60 * MINUTE_IN_MILLISECONDS;
4
4
 
5
5
  const ReadonlySet = Set;
6
6
 
7
+ function getEnumEntries(transpiledEnum) {
8
+ const entries = Object.entries(transpiledEnum);
9
+ const numberEntries = entries.filter(
10
+ ([_key, value]) => typeof value === "number"
11
+ );
12
+ const entriesToReturn = numberEntries.length > 0 ? numberEntries : entries;
13
+ return entriesToReturn;
14
+ }
15
+ function getEnumKeys(transpiledEnum) {
16
+ const enumEntries = getEnumEntries(transpiledEnum);
17
+ return enumEntries.map(([key, _value]) => key);
18
+ }
19
+ function getEnumValues(transpiledEnum) {
20
+ const enumEntries = getEnumEntries(transpiledEnum);
21
+ return enumEntries.map(([_key, value]) => value);
22
+ }
23
+ function interfaceSatisfiesEnum() {
24
+ }
25
+ function isEnumValue(value, transpiledEnum, set) {
26
+ if (set !== void 0) {
27
+ return set.has(value);
28
+ }
29
+ const enumValues = getEnumValues(transpiledEnum);
30
+ return enumValues.includes(value);
31
+ }
32
+
7
33
  function isObject(variable) {
8
34
  return typeof variable === "object" && variable !== null && !Array.isArray(variable);
9
35
  }
@@ -23,6 +49,11 @@ function assertDefined(value, ...[msg]) {
23
49
  throw new TypeError(msg);
24
50
  }
25
51
  }
52
+ function assertEnumValue(value, transpiledEnum, msg, set) {
53
+ if (!isEnumValue(value, transpiledEnum, set)) {
54
+ throw new TypeError(msg);
55
+ }
56
+ }
26
57
  function assertNotNull(value, ...[msg]) {
27
58
  if (value === null) {
28
59
  throw new TypeError(msg);
@@ -148,32 +179,6 @@ function sumArray(array) {
148
179
  return array.reduce((accumulator, element) => accumulator + element, 0);
149
180
  }
150
181
 
151
- function getEnumEntries(transpiledEnum) {
152
- const entries = Object.entries(transpiledEnum);
153
- const numberEntries = entries.filter(
154
- ([_key, value]) => typeof value === "number"
155
- );
156
- const entriesToReturn = numberEntries.length > 0 ? numberEntries : entries;
157
- return entriesToReturn;
158
- }
159
- function getEnumKeys(transpiledEnum) {
160
- const enumEntries = getEnumEntries(transpiledEnum);
161
- return enumEntries.map(([key, _value]) => key);
162
- }
163
- function getEnumValues(transpiledEnum) {
164
- const enumEntries = getEnumEntries(transpiledEnum);
165
- return enumEntries.map(([_key, value]) => value);
166
- }
167
- function interfaceSatisfiesEnum() {
168
- }
169
- function isEnumValue(value, transpiledEnum, set) {
170
- if (set !== void 0) {
171
- return set.has(value);
172
- }
173
- const enumValues = getEnumValues(transpiledEnum);
174
- return enumValues.includes(value);
175
- }
176
-
177
182
  function mapFilter(map, predicate) {
178
183
  const array = [];
179
184
  for (const value of map.values()) {
@@ -185,8 +190,8 @@ function mapFilter(map, predicate) {
185
190
  return array;
186
191
  }
187
192
  function mapFind(map, predicate) {
188
- for (const value of map.values()) {
189
- const match = predicate(value);
193
+ for (const [key, value] of map) {
194
+ const match = predicate(value, key, map);
190
195
  if (match) {
191
196
  return value;
192
197
  }
@@ -216,9 +221,6 @@ function objectToMap(object) {
216
221
  }
217
222
  return map;
218
223
  }
219
- function objectToReadonlyMap(object) {
220
- return objectToMap(object);
221
- }
222
224
  function objectToReverseMap(object) {
223
225
  const map = /* @__PURE__ */ new Map();
224
226
  for (const [key, value] of Object.entries(object)) {
@@ -226,9 +228,6 @@ function objectToReverseMap(object) {
226
228
  }
227
229
  return map;
228
230
  }
229
- function objectToReverseReadonlyMap(object) {
230
- return objectToReverseMap(object);
231
- }
232
231
 
233
232
  function addSetsToSet(mainSet, ...setsToAdd) {
234
233
  for (const set of setsToAdd) {
@@ -253,9 +252,6 @@ function copySet(oldSet) {
253
252
  }
254
253
  return newSet;
255
254
  }
256
- function objectKeysToReadonlySet(object) {
257
- return objectKeysToSet(object);
258
- }
259
255
  function objectKeysToSet(object) {
260
256
  const set = /* @__PURE__ */ new Set();
261
257
  for (const key of Object.keys(object)) {
@@ -263,9 +259,6 @@ function objectKeysToSet(object) {
263
259
  }
264
260
  return set;
265
261
  }
266
- function objectValuesToReadonlySet(object) {
267
- return objectValuesToSet(object);
268
- }
269
262
  function objectValuesToSet(object) {
270
263
  const set = /* @__PURE__ */ new Set();
271
264
  for (const key of Object.values(object)) {
@@ -503,4 +496,4 @@ function* tupleKeys(tuple) {
503
496
 
504
497
  const ReadonlyMap = Map;
505
498
 
506
- export { HOUR_IN_MILLISECONDS, MINUTE_IN_MILLISECONDS, ReadonlyMap, ReadonlySet, SECOND_IN_MILLISECONDS, addSetsToSet, arrayCopyTwoDimensional, arrayEquals, arrayRemove, arrayRemoveInPlace, assertArray, assertBoolean, assertDefined, assertNotNull, assertNumber, assertObject, assertString, 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 };
499
+ export { HOUR_IN_MILLISECONDS, MINUTE_IN_MILLISECONDS, ReadonlyMap, ReadonlySet, SECOND_IN_MILLISECONDS, addSetsToSet, arrayCopyTwoDimensional, arrayEquals, arrayRemove, arrayRemoveInPlace, assertArray, assertBoolean, assertDefined, assertEnumValue, assertNotNull, assertNumber, assertObject, assertString, 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, objectKeysToSet, objectToMap, objectToReverseMap, objectValuesToSet, parseFloatSafe, parseIntSafe, parseSemanticVersion, removeLinesBetweenMarkers, removeLinesMatching, removeNonPrintableCharacters, removeWhitespace, repeat, setAdd, setHas, sortCaseInsensitive, sumArray, todo, trimPrefix, trimSuffix, truncateString, tupleEntries, tupleKeys };
@@ -0,0 +1,3 @@
1
+ /** Helper type to represent a TypeScript `enum` at run-time. */
2
+ export type TranspiledEnum = Record<string, string | number>;
3
+ //# sourceMappingURL=TranspiledEnum.d.ts.map
@@ -0,0 +1,3 @@
1
+ /** Helper type to represent a TypeScript `enum` at run-time. */
2
+ export type TranspiledEnum = Record<string, string | number>;
3
+ //# sourceMappingURL=TranspiledEnum.d.ts.map
@@ -0,0 +1,3 @@
1
+ /** Helper type to represent a TypeScript `enum` at run-time. */
2
+ export type TranspiledEnum = Record<string, string | number>;
3
+ //# sourceMappingURL=TranspiledEnum.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TranspiledEnum.d.ts","sourceRoot":"","sources":["../../src/types/TranspiledEnum.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAChE,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "complete-common",
3
- "version": "1.3.0",
3
+ "version": "2.0.0",
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": "22.13.5",
39
- "complete-node": "3.1.6",
38
+ "@types/node": "22.13.10",
39
+ "complete-node": "5.1.1",
40
40
  "eslint-plugin-sort-exports": "0.9.1",
41
41
  "glob": "11.0.1",
42
- "typescript": "5.7.3",
43
- "typescript-eslint": "8.25.0",
42
+ "typescript": "5.8.2",
43
+ "typescript-eslint": "8.26.0",
44
44
  "unbuild": "3.5.0"
45
45
  }
46
46
  }
@@ -13,10 +13,9 @@ import { getRandomInt } from "./random.js";
13
13
  * Helper function to copy a two-dimensional array. Note that the sub-arrays will only be shallow
14
14
  * copied (using the spread operator).
15
15
  */
16
- // eslint-disable-next-line complete/no-mutable-return
17
16
  export function arrayCopyTwoDimensional<T>(
18
17
  array: ReadonlyArray<readonly T[]>,
19
- ): T[][] {
18
+ ): readonly T[][] {
20
19
  const copiedArray: T[][] = [];
21
20
 
22
21
  for (const subArray of array) {
@@ -51,11 +50,10 @@ export function arrayEquals<T>(
51
50
  *
52
51
  * This function is variadic, meaning that you can specify N arguments to remove N elements.
53
52
  */
54
- // eslint-disable-next-line complete/no-mutable-return
55
53
  export function arrayRemove<T>(
56
54
  originalArray: readonly T[],
57
55
  ...elementsToRemove: readonly T[]
58
- ): T[] {
56
+ ): readonly T[] {
59
57
  const elementsToRemoveSet = new ReadonlySet(elementsToRemove);
60
58
 
61
59
  const array: T[] = [];
@@ -80,12 +78,11 @@ export function arrayRemove<T>(
80
78
  *
81
79
  * @returns The removed elements. This will be an empty array if no elements were removed.
82
80
  */
83
- // eslint-disable-next-line complete/no-mutable-return
84
81
  export function arrayRemoveInPlace<T>(
85
82
  // eslint-disable-next-line complete/prefer-readonly-parameter-types
86
83
  array: T[],
87
84
  ...elementsToRemove: readonly T[]
88
- ): T[] {
85
+ ): readonly T[] {
89
86
  const removedElements: T[] = [];
90
87
 
91
88
  for (const element of elementsToRemove) {
@@ -101,7 +98,7 @@ export function arrayRemoveInPlace<T>(
101
98
 
102
99
  /** Helper function to remove all of the elements in an array in-place. */
103
100
  // eslint-disable-next-line complete/prefer-readonly-parameter-types
104
- export function emptyArray<T>(array: T[]): void {
101
+ export function emptyArray(array: unknown[]): void {
105
102
  array.splice(0, array.length);
106
103
  }
107
104
 
@@ -116,11 +113,10 @@ export function emptyArray<T>(array: T[]): void {
116
113
  * This is named `filterMap` after the Rust function:
117
114
  * https://doc.rust-lang.org/std/iter/struct.FilterMap.html
118
115
  */
119
- // eslint-disable-next-line complete/no-mutable-return
120
116
  export function filterMap<OldT, NewT>(
121
117
  array: readonly OldT[],
122
118
  func: (element: OldT) => NewT | undefined,
123
- ): NewT[] {
119
+ ): readonly NewT[] {
124
120
  const filteredArray: NewT[] = [];
125
121
 
126
122
  for (const element of array) {
@@ -171,8 +167,8 @@ export function getRandomArrayElement<T>(
171
167
  * @param exceptions Optional. An array of indexes that will be skipped over when getting the random
172
168
  * index. Default is an empty array.
173
169
  */
174
- export function getRandomArrayIndex<T>(
175
- array: readonly T[],
170
+ export function getRandomArrayIndex(
171
+ array: readonly unknown[],
176
172
  exceptions: readonly number[] = [],
177
173
  ): number {
178
174
  if (array.length === 0) {
@@ -204,8 +200,7 @@ export function isArray(arg: unknown): arg is unknown[] {
204
200
  }
205
201
 
206
202
  /** Initializes an array with all elements containing the specified default value. */
207
- // eslint-disable-next-line complete/no-mutable-return
208
- export function newArray<T>(length: number, value: T): T[] {
203
+ export function newArray<T>(length: number, value: T): readonly T[] {
209
204
  return Array.from({ length }, () => value);
210
205
  }
211
206
 
@@ -4,6 +4,8 @@
4
4
  * @module
5
5
  */
6
6
 
7
+ import type { TranspiledEnum } from "../types/TranspiledEnum.js";
8
+ import { isEnumValue } from "./enums.js";
7
9
  import { isObject } from "./types.js";
8
10
 
9
11
  /** Helper function to throw an error if the provided value is not an array. */
@@ -44,6 +46,27 @@ export function assertDefined<T>(
44
46
  }
45
47
  }
46
48
 
49
+ /**
50
+ * Helper function to throw an error if the provided value is not contained within an enum.
51
+ *
52
+ * @param value The value to check.
53
+ * @param transpiledEnum The enum to check against.
54
+ * @param msg The error message to throw if the check fails.
55
+ * @param set Optional. A set that contains all of the values of an enum. If provided, this function
56
+ * will check for existence using the set (instead of the enum itself). Using a set
57
+ * should be more performant for enums with around 52 or more elements.
58
+ */
59
+ export function assertEnumValue<T extends TranspiledEnum>(
60
+ value: number | string,
61
+ transpiledEnum: T,
62
+ msg: string,
63
+ set?: ReadonlySet<string | number>,
64
+ ): asserts value is T[keyof T] {
65
+ if (!isEnumValue(value, transpiledEnum, set)) {
66
+ throw new TypeError(msg);
67
+ }
68
+ }
69
+
47
70
  /**
48
71
  * Helper function to throw an error if the provided value is equal to `null`.
49
72
  *
@@ -4,7 +4,7 @@
4
4
  * @module
5
5
  */
6
6
 
7
- type TranspiledEnum = Record<string, string | number>;
7
+ import type { TranspiledEnum } from "../types/TranspiledEnum.js";
8
8
 
9
9
  /**
10
10
  * Helper function to get the entries of an enum.
@@ -14,11 +14,10 @@
14
14
  * If you want to perform a filter and a map at the same time on an array, use the `filterMap`
15
15
  * helper function instead.
16
16
  */
17
- // eslint-disable-next-line complete/no-mutable-return
18
17
  export function mapFilter<K, V>(
19
18
  map: ReadonlyMap<K, V>,
20
19
  predicate: (value: V) => boolean,
21
- ): V[] {
20
+ ): readonly V[] {
22
21
  const array: V[] = [];
23
22
 
24
23
  for (const value of map.values()) {
@@ -36,13 +35,17 @@ export function mapFilter<K, V>(
36
35
  * maps.
37
36
  *
38
37
  * This is efficient such that it avoids converting the map values into an array.
38
+ *
39
+ * @param map The map to search through.
40
+ * @param predicate Function that tests each value for a condition.
41
+ * @returns The first value that satisfies the predicate, or undefined if no values satisfy.
39
42
  */
40
43
  export function mapFind<K, V>(
41
44
  map: ReadonlyMap<K, V>,
42
- predicate: (value: V) => boolean,
45
+ predicate: (value: V, key: K, map: ReadonlyMap<K, V>) => boolean,
43
46
  ): V | undefined {
44
- for (const value of map.values()) {
45
- const match = predicate(value);
47
+ for (const [key, value] of map) {
48
+ const match = predicate(value, key, map);
46
49
  if (match) {
47
50
  return value;
48
51
  }