complete-common 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +3 -3
  3. package/dist/constants.d.cts +7 -0
  4. package/dist/constants.d.mts +7 -0
  5. package/dist/constants.d.ts +7 -0
  6. package/dist/constants.d.ts.map +1 -0
  7. package/dist/functions/array.d.cts +83 -0
  8. package/dist/functions/array.d.mts +83 -0
  9. package/dist/functions/array.d.ts +83 -0
  10. package/dist/functions/array.d.ts.map +1 -0
  11. package/dist/functions/assert.d.cts +45 -0
  12. package/dist/functions/assert.d.mts +45 -0
  13. package/dist/functions/assert.d.ts +45 -0
  14. package/dist/functions/assert.d.ts.map +1 -0
  15. package/dist/functions/enums.d.cts +73 -0
  16. package/dist/functions/enums.d.mts +73 -0
  17. package/dist/functions/enums.d.ts +73 -0
  18. package/dist/functions/enums.d.ts.map +1 -0
  19. package/dist/functions/map.d.cts +24 -0
  20. package/dist/functions/map.d.mts +24 -0
  21. package/dist/functions/map.d.ts +24 -0
  22. package/dist/functions/map.d.ts.map +1 -0
  23. package/dist/functions/math.d.cts +13 -0
  24. package/dist/functions/math.d.mts +13 -0
  25. package/dist/functions/math.d.ts +13 -0
  26. package/dist/functions/math.d.ts.map +1 -0
  27. package/dist/functions/object.d.cts +60 -0
  28. package/dist/functions/object.d.mts +60 -0
  29. package/dist/functions/object.d.ts +60 -0
  30. package/dist/functions/object.d.ts.map +1 -0
  31. package/dist/functions/random.d.cts +23 -0
  32. package/dist/functions/random.d.mts +23 -0
  33. package/dist/functions/random.d.ts +23 -0
  34. package/dist/functions/random.d.ts.map +1 -0
  35. package/dist/functions/set.d.cts +61 -0
  36. package/dist/functions/set.d.mts +61 -0
  37. package/dist/functions/set.d.ts +61 -0
  38. package/dist/functions/set.d.ts.map +1 -0
  39. package/dist/functions/sort.d.cts +14 -0
  40. package/dist/functions/sort.d.mts +14 -0
  41. package/dist/functions/sort.d.ts +14 -0
  42. package/dist/functions/sort.d.ts.map +1 -0
  43. package/dist/functions/string.d.cts +122 -0
  44. package/dist/functions/string.d.mts +122 -0
  45. package/dist/functions/string.d.ts +122 -0
  46. package/dist/functions/string.d.ts.map +1 -0
  47. package/dist/functions/string.test.d.cts +2 -0
  48. package/dist/functions/string.test.d.mts +2 -0
  49. package/dist/functions/string.test.d.ts +2 -0
  50. package/dist/functions/string.test.d.ts.map +1 -0
  51. package/dist/functions/time.d.cts +20 -0
  52. package/dist/functions/time.d.mts +20 -0
  53. package/dist/functions/time.d.ts +20 -0
  54. package/dist/functions/time.d.ts.map +1 -0
  55. package/dist/functions/tuple.d.cts +27 -0
  56. package/dist/functions/tuple.d.mts +27 -0
  57. package/dist/functions/tuple.d.ts +27 -0
  58. package/dist/functions/tuple.d.ts.map +1 -0
  59. package/dist/functions/types.d.cts +13 -0
  60. package/dist/functions/types.d.mts +13 -0
  61. package/dist/functions/types.d.ts +13 -0
  62. package/dist/functions/types.d.ts.map +1 -0
  63. package/dist/functions/utils.d.cts +131 -0
  64. package/dist/functions/utils.d.mts +131 -0
  65. package/dist/functions/utils.d.ts +131 -0
  66. package/dist/functions/utils.d.ts.map +1 -0
  67. package/dist/functions/utils.test.d.cts +2 -0
  68. package/dist/functions/utils.test.d.mts +2 -0
  69. package/dist/functions/utils.test.d.ts +2 -0
  70. package/dist/functions/utils.test.d.ts.map +1 -0
  71. package/dist/index.cjs +122 -77
  72. package/dist/index.d.cts +30 -705
  73. package/dist/index.d.mts +30 -705
  74. package/dist/index.d.ts +30 -705
  75. package/dist/index.d.ts.map +1 -0
  76. package/dist/index.mjs +118 -78
  77. package/dist/types/AddSubtract.d.cts +23 -0
  78. package/dist/types/AddSubtract.d.mts +23 -0
  79. package/dist/types/AddSubtract.d.ts +23 -0
  80. package/dist/types/AddSubtract.d.ts.map +1 -0
  81. package/dist/types/CompositionTypeSatisfiesEnum.d.cts +35 -0
  82. package/dist/types/CompositionTypeSatisfiesEnum.d.mts +35 -0
  83. package/dist/types/CompositionTypeSatisfiesEnum.d.ts +35 -0
  84. package/dist/types/CompositionTypeSatisfiesEnum.d.ts.map +1 -0
  85. package/dist/types/ERange.d.cts +12 -0
  86. package/dist/types/ERange.d.mts +12 -0
  87. package/dist/types/ERange.d.ts +12 -0
  88. package/dist/types/ERange.d.ts.map +1 -0
  89. package/dist/types/IRange.d.cts +13 -0
  90. package/dist/types/IRange.d.mts +13 -0
  91. package/dist/types/IRange.d.ts +13 -0
  92. package/dist/types/IRange.d.ts.map +1 -0
  93. package/dist/types/Immutable.d.cts +17 -0
  94. package/dist/types/Immutable.d.mts +17 -0
  95. package/dist/types/Immutable.d.ts +17 -0
  96. package/dist/types/Immutable.d.ts.map +1 -0
  97. package/dist/types/NaturalNumbersLessThan.d.cts +8 -0
  98. package/dist/types/NaturalNumbersLessThan.d.mts +8 -0
  99. package/dist/types/NaturalNumbersLessThan.d.ts +8 -0
  100. package/dist/types/NaturalNumbersLessThan.d.ts.map +1 -0
  101. package/dist/types/NaturalNumbersLessThanOrEqualTo.d.cts +8 -0
  102. package/dist/types/NaturalNumbersLessThanOrEqualTo.d.mts +8 -0
  103. package/dist/types/NaturalNumbersLessThanOrEqualTo.d.ts +8 -0
  104. package/dist/types/NaturalNumbersLessThanOrEqualTo.d.ts.map +1 -0
  105. package/dist/types/ObjectValues.d.cts +3 -0
  106. package/dist/types/ObjectValues.d.mts +3 -0
  107. package/dist/types/ObjectValues.d.ts +3 -0
  108. package/dist/types/ObjectValues.d.ts.map +1 -0
  109. package/dist/types/ReadonlyMap.d.cts +9 -0
  110. package/dist/types/ReadonlyMap.d.mts +9 -0
  111. package/dist/types/ReadonlyMap.d.ts +9 -0
  112. package/dist/types/ReadonlyMap.d.ts.map +1 -0
  113. package/dist/types/ReadonlyRecord.d.cts +7 -0
  114. package/dist/types/ReadonlyRecord.d.mts +7 -0
  115. package/dist/types/ReadonlyRecord.d.ts +7 -0
  116. package/dist/types/ReadonlyRecord.d.ts.map +1 -0
  117. package/dist/types/ReadonlySet.d.cts +8 -0
  118. package/dist/types/ReadonlySet.d.mts +8 -0
  119. package/dist/types/ReadonlySet.d.ts +8 -0
  120. package/dist/types/ReadonlySet.d.ts.map +1 -0
  121. package/dist/types/Tuple.d.cts +10 -0
  122. package/dist/types/Tuple.d.mts +10 -0
  123. package/dist/types/Tuple.d.ts +10 -0
  124. package/dist/types/Tuple.d.ts.map +1 -0
  125. package/dist/types/WidenLiteral.d.cts +3 -0
  126. package/dist/types/WidenLiteral.d.mts +3 -0
  127. package/dist/types/WidenLiteral.d.ts +3 -0
  128. package/dist/types/WidenLiteral.d.ts.map +1 -0
  129. package/dist/types/Writeable.d.cts +9 -0
  130. package/dist/types/Writeable.d.mts +9 -0
  131. package/dist/types/Writeable.d.ts +9 -0
  132. package/dist/types/Writeable.d.ts.map +1 -0
  133. package/package.json +13 -2
  134. package/src/constants.ts +5 -0
  135. package/src/functions/array.ts +8 -2
  136. package/src/functions/assert.ts +86 -0
  137. package/src/functions/enums.ts +6 -0
  138. package/src/functions/map.ts +7 -45
  139. package/src/functions/math.ts +6 -0
  140. package/src/functions/object.ts +90 -3
  141. package/src/functions/random.ts +6 -0
  142. package/src/functions/set.ts +7 -0
  143. package/src/functions/sort.ts +6 -0
  144. package/src/functions/string.ts +25 -0
  145. package/src/functions/time.ts +26 -0
  146. package/src/functions/tuple.ts +6 -0
  147. package/src/functions/types.ts +6 -0
  148. package/src/functions/utils.test.ts +2 -8
  149. package/src/functions/utils.ts +7 -37
  150. package/src/index.ts +14 -12
  151. package/src/types/AddSubtract.ts +10 -2
  152. package/src/types/CompositionTypeSatisfiesEnum.ts +5 -3
  153. package/src/types/ObjectValues.ts +1 -0
  154. package/src/types/ReadonlyRecord.ts +5 -0
  155. package/src/types/Tuple.ts +1 -0
  156. package/src/types/WidenLiteral.ts +1 -0
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Helper type to get a range of integers between 0 and N.
3
+ *
4
+ * From:
5
+ * https://stackoverflow.com/questions/39494689/is-it-possible-to-restrict-number-to-a-certain-range
6
+ */
7
+ export type NaturalNumbersLessThanOrEqualTo<N extends number, T extends number[] = []> = T extends [unknown, ...infer Tail] ? Tail["length"] extends N ? T[number] : NaturalNumbersLessThanOrEqualTo<N, [...T, T["length"]]> : NaturalNumbersLessThanOrEqualTo<N, [...T, T["length"]]>;
8
+ //# sourceMappingURL=NaturalNumbersLessThanOrEqualTo.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NaturalNumbersLessThanOrEqualTo.d.ts","sourceRoot":"","sources":["../../src/types/NaturalNumbersLessThanOrEqualTo.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,MAAM,+BAA+B,CACzC,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,IACrB,CAAC,SAAS,CAAC,OAAO,EAAE,GAAG,MAAM,IAAI,CAAC,GAClC,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,GACtB,CAAC,CAAC,MAAM,CAAC,GACT,+BAA+B,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,GACzD,+BAA+B,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ /** Helper type to capture just the values of an object/record. */
2
+ export type ObjectValues<T> = T[keyof T];
3
+ //# sourceMappingURL=ObjectValues.d.ts.map
@@ -0,0 +1,3 @@
1
+ /** Helper type to capture just the values of an object/record. */
2
+ export type ObjectValues<T> = T[keyof T];
3
+ //# sourceMappingURL=ObjectValues.d.ts.map
@@ -0,0 +1,3 @@
1
+ /** Helper type to capture just the values of an object/record. */
2
+ export type ObjectValues<T> = T[keyof T];
3
+ //# sourceMappingURL=ObjectValues.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ObjectValues.d.ts","sourceRoot":"","sources":["../../src/types/ObjectValues.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC"}
@@ -0,0 +1,9 @@
1
+ interface ReadonlyMapConstructor {
2
+ new (): ReadonlyMap<any, any>;
3
+ new <K, V>(entries?: ReadonlyArray<readonly [K, V]> | Iterable<readonly [K, V]> | null): ReadonlyMap<K, V>;
4
+ readonly prototype: ReadonlyMap<any, any>;
5
+ }
6
+ /** An alias for the `Map` constructor that returns a read-only map. */
7
+ export declare const ReadonlyMap: ReadonlyMapConstructor;
8
+ export {};
9
+ //# sourceMappingURL=ReadonlyMap.d.ts.map
@@ -0,0 +1,9 @@
1
+ interface ReadonlyMapConstructor {
2
+ new (): ReadonlyMap<any, any>;
3
+ new <K, V>(entries?: ReadonlyArray<readonly [K, V]> | Iterable<readonly [K, V]> | null): ReadonlyMap<K, V>;
4
+ readonly prototype: ReadonlyMap<any, any>;
5
+ }
6
+ /** An alias for the `Map` constructor that returns a read-only map. */
7
+ export declare const ReadonlyMap: ReadonlyMapConstructor;
8
+ export {};
9
+ //# sourceMappingURL=ReadonlyMap.d.ts.map
@@ -0,0 +1,9 @@
1
+ interface ReadonlyMapConstructor {
2
+ new (): ReadonlyMap<any, any>;
3
+ new <K, V>(entries?: ReadonlyArray<readonly [K, V]> | Iterable<readonly [K, V]> | null): ReadonlyMap<K, V>;
4
+ readonly prototype: ReadonlyMap<any, any>;
5
+ }
6
+ /** An alias for the `Map` constructor that returns a read-only map. */
7
+ export declare const ReadonlyMap: ReadonlyMapConstructor;
8
+ export {};
9
+ //# sourceMappingURL=ReadonlyMap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ReadonlyMap.d.ts","sourceRoot":"","sources":["../../src/types/ReadonlyMap.ts"],"names":[],"mappings":"AAEA,UAAU,sBAAsB;IAC9B,QAAQ,WAAW,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IAC9B,KAAK,CAAC,EAAE,CAAC,EACP,OAAO,CAAC,EAAE,aAAa,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,IAAI,GAC1E,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACrB,QAAQ,CAAC,SAAS,EAAE,WAAW,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;CAC3C;AAED,uEAAuE;AACvE,eAAO,MAAM,WAAW,EAAU,sBAAsB,CAAC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Helper type to specify that a record should be read-only.
3
+ *
4
+ * This is the same thing as `Readonly<Record<K, V>>`.
5
+ */
6
+ export type ReadonlyRecord<K extends string | number | symbol, V> = Readonly<Record<K, V>>;
7
+ //# sourceMappingURL=ReadonlyRecord.d.ts.map
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Helper type to specify that a record should be read-only.
3
+ *
4
+ * This is the same thing as `Readonly<Record<K, V>>`.
5
+ */
6
+ export type ReadonlyRecord<K extends string | number | symbol, V> = Readonly<Record<K, V>>;
7
+ //# sourceMappingURL=ReadonlyRecord.d.ts.map
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Helper type to specify that a record should be read-only.
3
+ *
4
+ * This is the same thing as `Readonly<Record<K, V>>`.
5
+ */
6
+ export type ReadonlyRecord<K extends string | number | symbol, V> = Readonly<Record<K, V>>;
7
+ //# sourceMappingURL=ReadonlyRecord.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ReadonlyRecord.d.ts","sourceRoot":"","sources":["../../src/types/ReadonlyRecord.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,MAAM,cAAc,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,IAAI,QAAQ,CAC1E,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CACb,CAAC"}
@@ -0,0 +1,8 @@
1
+ interface ReadonlySetConstructor {
2
+ new <T = any>(values?: readonly T[] | Iterable<T> | null): ReadonlySet<T>;
3
+ readonly prototype: ReadonlySet<any>;
4
+ }
5
+ /** An alias for the `Set` constructor that returns a read-only set. */
6
+ export declare const ReadonlySet: ReadonlySetConstructor;
7
+ export {};
8
+ //# sourceMappingURL=ReadonlySet.d.ts.map
@@ -0,0 +1,8 @@
1
+ interface ReadonlySetConstructor {
2
+ new <T = any>(values?: readonly T[] | Iterable<T> | null): ReadonlySet<T>;
3
+ readonly prototype: ReadonlySet<any>;
4
+ }
5
+ /** An alias for the `Set` constructor that returns a read-only set. */
6
+ export declare const ReadonlySet: ReadonlySetConstructor;
7
+ export {};
8
+ //# sourceMappingURL=ReadonlySet.d.ts.map
@@ -0,0 +1,8 @@
1
+ interface ReadonlySetConstructor {
2
+ new <T = any>(values?: readonly T[] | Iterable<T> | null): ReadonlySet<T>;
3
+ readonly prototype: ReadonlySet<any>;
4
+ }
5
+ /** An alias for the `Set` constructor that returns a read-only set. */
6
+ export declare const ReadonlySet: ReadonlySetConstructor;
7
+ export {};
8
+ //# sourceMappingURL=ReadonlySet.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ReadonlySet.d.ts","sourceRoot":"","sources":["../../src/types/ReadonlySet.ts"],"names":[],"mappings":"AAEA,UAAU,sBAAsB;IAC9B,KAAK,CAAC,GAAG,GAAG,EAAE,MAAM,CAAC,EAAE,SAAS,CAAC,EAAE,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;IAC1E,QAAQ,CAAC,SAAS,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC;CACtC;AAED,uEAAuE;AACvE,eAAO,MAAM,WAAW,EAAU,sBAAsB,CAAC"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Helper type to represent a tuple of length N.
3
+ *
4
+ * From:
5
+ * https://stackoverflow.com/questions/52489261/typescript-can-i-define-an-n-length-tuple-type/52490977#52490977
6
+ */
7
+ export type Tuple<T, N extends number> = N extends N ? number extends N ? T[] : _TupleOf<T, N, []> : never;
8
+ type _TupleOf<T, N extends number, R extends unknown[]> = R["length"] extends N ? R : _TupleOf<T, N, [T, ...R]>;
9
+ export {};
10
+ //# sourceMappingURL=Tuple.d.ts.map
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Helper type to represent a tuple of length N.
3
+ *
4
+ * From:
5
+ * https://stackoverflow.com/questions/52489261/typescript-can-i-define-an-n-length-tuple-type/52490977#52490977
6
+ */
7
+ export type Tuple<T, N extends number> = N extends N ? number extends N ? T[] : _TupleOf<T, N, []> : never;
8
+ type _TupleOf<T, N extends number, R extends unknown[]> = R["length"] extends N ? R : _TupleOf<T, N, [T, ...R]>;
9
+ export {};
10
+ //# sourceMappingURL=Tuple.d.ts.map
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Helper type to represent a tuple of length N.
3
+ *
4
+ * From:
5
+ * https://stackoverflow.com/questions/52489261/typescript-can-i-define-an-n-length-tuple-type/52490977#52490977
6
+ */
7
+ export type Tuple<T, N extends number> = N extends N ? number extends N ? T[] : _TupleOf<T, N, []> : never;
8
+ type _TupleOf<T, N extends number, R extends unknown[]> = R["length"] extends N ? R : _TupleOf<T, N, [T, ...R]>;
9
+ export {};
10
+ //# sourceMappingURL=Tuple.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Tuple.d.ts","sourceRoot":"","sources":["../../src/types/Tuple.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,IAAI,CAAC,SAAS,CAAC,GAChD,MAAM,SAAS,CAAC,GACd,CAAC,EAAE,GACH,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,GACpB,KAAK,CAAC;AAEV,KAAK,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,OAAO,EAAE,IAAI,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,GAC3E,CAAC,GACD,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ /** Helper type used to widen a const array to the corresponding base type. */
2
+ export type WidenLiteral<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T extends bigint ? bigint : T extends symbol ? symbol : T;
3
+ //# sourceMappingURL=WidenLiteral.d.ts.map
@@ -0,0 +1,3 @@
1
+ /** Helper type used to widen a const array to the corresponding base type. */
2
+ export type WidenLiteral<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T extends bigint ? bigint : T extends symbol ? symbol : T;
3
+ //# sourceMappingURL=WidenLiteral.d.ts.map
@@ -0,0 +1,3 @@
1
+ /** Helper type used to widen a const array to the corresponding base type. */
2
+ export type WidenLiteral<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T extends bigint ? bigint : T extends symbol ? symbol : T;
3
+ //# sourceMappingURL=WidenLiteral.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WidenLiteral.d.ts","sourceRoot":"","sources":["../../src/types/WidenLiteral.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,SAAS,MAAM,GAC1C,MAAM,GACN,CAAC,SAAS,MAAM,GACd,MAAM,GACN,CAAC,SAAS,OAAO,GACf,OAAO,GACP,CAAC,SAAS,MAAM,GACd,MAAM,GACN,CAAC,SAAS,MAAM,GACd,MAAM,GACN,CAAC,CAAC"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Helper type to convert a read-only object into a writable object.
3
+ *
4
+ * This is the opposite of the built-in `Readonly` utility type.
5
+ */
6
+ export type Writeable<T> = {
7
+ -readonly [P in keyof T]: T[P];
8
+ };
9
+ //# sourceMappingURL=Writeable.d.ts.map
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Helper type to convert a read-only object into a writable object.
3
+ *
4
+ * This is the opposite of the built-in `Readonly` utility type.
5
+ */
6
+ export type Writeable<T> = {
7
+ -readonly [P in keyof T]: T[P];
8
+ };
9
+ //# sourceMappingURL=Writeable.d.ts.map
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Helper type to convert a read-only object into a writable object.
3
+ *
4
+ * This is the opposite of the built-in `Readonly` utility type.
5
+ */
6
+ export type Writeable<T> = {
7
+ -readonly [P in keyof T]: T[P];
8
+ };
9
+ //# sourceMappingURL=Writeable.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Writeable.d.ts","sourceRoot":"","sources":["../../src/types/Writeable.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI;IAAE,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,CAAC"}
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "complete-common",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Helper functions for TypeScript projects.",
5
5
  "keywords": [],
6
- "homepage": "https://complete-js.github.io/",
6
+ "homepage": "https://complete-ts.github.io/",
7
7
  "bugs": {
8
8
  "url": "https://github.com/complete-ts/complete/issues"
9
9
  },
@@ -30,7 +30,18 @@
30
30
  ],
31
31
  "scripts": {
32
32
  "build": "tsx ./scripts/build.ts",
33
+ "docs": "typedoc",
33
34
  "lint": "tsx ./scripts/lint.ts",
34
35
  "test": "glob \"./src/**/*.test.ts\" --cmd=\"node --import tsx --test --test-reporter spec\""
36
+ },
37
+ "devDependencies": {
38
+ "@types/node": "^22.13.4",
39
+ "complete-node": "^1.7.4",
40
+ "eslint-plugin-sort-exports": "^0.9.1",
41
+ "execa": "^9.5.2",
42
+ "glob": "^11.0.1",
43
+ "typescript": "^5.7.3",
44
+ "typescript-eslint": "^8.24.1",
45
+ "unbuild": "^3.3.1"
35
46
  }
36
47
  }
package/src/constants.ts CHANGED
@@ -1,3 +1,8 @@
1
+ /** Equal to 1000. */
1
2
  export const SECOND_IN_MILLISECONDS = 1000;
3
+
4
+ /** Equal to 60,000. */
2
5
  export const MINUTE_IN_MILLISECONDS = 60 * SECOND_IN_MILLISECONDS;
6
+
7
+ /** Equal to 3,600,000. */
3
8
  export const HOUR_IN_MILLISECONDS = 60 * MINUTE_IN_MILLISECONDS;
@@ -1,7 +1,13 @@
1
+ /**
2
+ * Helper functions that have to do with arrays.
3
+ *
4
+ * @module
5
+ */
6
+
1
7
  import type { WidenLiteral } from "../index.js";
2
8
  import { ReadonlySet } from "../types/ReadonlySet.js";
9
+ import { assertDefined } from "./assert.js";
3
10
  import { getRandomInt } from "./random.js";
4
- import { assertDefined } from "./utils.js";
5
11
 
6
12
  /**
7
13
  * Helper function to copy a two-dimensional array. Note that the sub-arrays will only be shallow
@@ -84,7 +90,7 @@ export function arrayRemoveInPlace<T>(
84
90
 
85
91
  for (const element of elementsToRemove) {
86
92
  const index = array.indexOf(element);
87
- if (index > -1) {
93
+ if (index !== -1) {
88
94
  const removedElement = array.splice(index, 1);
89
95
  removedElements.push(...removedElement);
90
96
  }
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Helper functions that have to do with asserting.
3
+ *
4
+ * @module
5
+ */
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
+
22
+ /**
23
+ * Helper function to throw an error if the provided value is equal to `undefined`.
24
+ *
25
+ * This is useful to have TypeScript narrow a `T | undefined` value to `T` in a concise way.
26
+ */
27
+ export function assertDefined<T>(
28
+ value: T,
29
+ ...[msg]: [undefined] extends [T]
30
+ ? [string]
31
+ : [
32
+ "The assertion is useless because the provided value does not contain undefined.",
33
+ ]
34
+ ): asserts value is Exclude<T, undefined> {
35
+ if (value === undefined) {
36
+ throw new TypeError(msg);
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Helper function to throw an error if the provided value is equal to `null`.
42
+ *
43
+ * This is useful to have TypeScript narrow a `T | null` value to `T` in a concise way.
44
+ */
45
+ export function assertNotNull<T>(
46
+ value: T,
47
+ ...[msg]: [null] extends [T]
48
+ ? [string]
49
+ : [
50
+ "The assertion is useless because the provided value does not contain null.",
51
+ ]
52
+ ): asserts value is Exclude<T, null> {
53
+ if (value === null) {
54
+ throw new TypeError(msg);
55
+ }
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
+ }
@@ -1,3 +1,9 @@
1
+ /**
2
+ * Helper functions that have to do with TypeScript enums.
3
+ *
4
+ * @module
5
+ */
6
+
1
7
  type TranspiledEnum = Record<string, string | number>;
2
8
 
3
9
  /**
@@ -1,3 +1,10 @@
1
+ /**
2
+ * Helper functions that have to do with
3
+ * [maps](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map).
4
+ *
5
+ * @module
6
+ */
7
+
1
8
  /**
2
9
  * Helper function to get the values in a `Map` that match an arbitrary condition. Similar to the
3
10
  * `Array.map` method, but works for maps.
@@ -43,48 +50,3 @@ export function mapFind<K, V>(
43
50
 
44
51
  return undefined;
45
52
  }
46
-
47
- /**
48
- * Helper function to convert an object to a map.
49
- *
50
- * This is useful when you need to construct a type safe object with the `satisfies` operator, but
51
- * then later on you need to query it in a way where you expect the return value to be T or
52
- * undefined. In this situation, by converting the object to a map, you can avoid unsafe type
53
- * assertions.
54
- *
55
- * Note that the map values will be inserted in a random order, due to how `pairs` works under the
56
- * hood.
57
- *
58
- * Also see the `objectToReadonlyMap` function.
59
- */
60
- // eslint-disable-next-line complete/no-mutable-return
61
- export function objectToMap<K extends string | number | symbol, V>(
62
- object: Record<K, V>,
63
- ): Map<K, V> {
64
- const map = new Map<K, V>();
65
-
66
- for (const [key, value] of Object.entries(object)) {
67
- map.set(key as K, value as V);
68
- }
69
-
70
- return map;
71
- }
72
-
73
- /**
74
- * Helper function to convert an object to a read-only map.
75
- *
76
- * This is useful when you need to construct a type safe object with the `satisfies` operator, but
77
- * then later on you need to query it in a way where you expect the return value to be T or
78
- * undefined. In this situation, by converting the object to a map, you can avoid unsafe type
79
- * assertions.
80
- *
81
- * Note that the map values will be inserted in a random order, due to how `pairs` works under the
82
- * hood.
83
- *
84
- * Also see the `objectToMap` function.
85
- */
86
- export function objectToReadonlyMap<K extends string | number | symbol, V>(
87
- object: Record<K, V>,
88
- ): ReadonlyMap<K, V> {
89
- return objectToMap(object);
90
- }
@@ -1,3 +1,9 @@
1
+ /**
2
+ * Helper functions that have to do with math.
3
+ *
4
+ * @module
5
+ */
6
+
1
7
  /**
2
8
  * Helper function to normalize a number, ensuring that it is within a certain range.
3
9
  *
@@ -1,12 +1,17 @@
1
1
  /**
2
- * Helper function to get the values in an object that match an arbitrary condition. Similar to the
3
- * `Array.map` method, but works for objects.
2
+ * Helper functions that have to do with objects.
4
3
  *
5
- * This is efficient such that it avoids converting the object values into an array.
4
+ * @module
6
5
  */
7
6
 
8
7
  import type { ReadonlyRecord } from "../types/ReadonlyRecord.js";
9
8
 
9
+ /**
10
+ * Helper function to get the values in an object that match an arbitrary condition. Similar to the
11
+ * `Array.map` method, but works for objects.
12
+ *
13
+ * This is efficient such that it avoids converting the object values into an array.
14
+ */
10
15
  // eslint-disable-next-line complete/no-mutable-return
11
16
  export function objectFilter<K extends string | number | symbol, V>(
12
17
  object: ReadonlyRecord<K, V>,
@@ -25,3 +30,85 @@ export function objectFilter<K extends string | number | symbol, V>(
25
30
 
26
31
  return array;
27
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
+ }
@@ -1,3 +1,9 @@
1
+ /**
2
+ * Helper functions that have to do with getting random values.
3
+ *
4
+ * @module
5
+ */
6
+
1
7
  import { ReadonlySet } from "../types/ReadonlySet.js";
2
8
 
3
9
  /**
@@ -1,3 +1,10 @@
1
+ /**
2
+ * Helper functions that have to do with
3
+ * [sets](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
4
+ *
5
+ * @module
6
+ */
7
+
1
8
  /**
2
9
  * Helper function to add all of the values in one set to another set. The first set passed will be
3
10
  * modified in place.
@@ -1,3 +1,9 @@
1
+ /**
2
+ * Helper functions that have to do with sorting.
3
+ *
4
+ * @module
5
+ */
6
+
1
7
  /**
2
8
  * Helper function to perform a case insensitive sort. This will copy the provided array and return
3
9
  * the sorted copy.