@develia/commons 0.4.16 → 0.4.18

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 (207) hide show
  1. package/dist/cjs/array.js +22 -2
  2. package/dist/cjs/array.js.map +1 -1
  3. package/dist/cjs/async-pool-executor.js +26 -11
  4. package/dist/cjs/async-pool-executor.js.map +1 -1
  5. package/dist/cjs/browser/form-utilities.js +35 -34
  6. package/dist/cjs/browser/form-utilities.js.map +1 -1
  7. package/dist/cjs/browser/geo-location.js +1 -1
  8. package/dist/cjs/browser/geo-location.js.map +1 -1
  9. package/dist/cjs/browser/native-bridge.js +3 -1
  10. package/dist/cjs/browser/native-bridge.js.map +1 -1
  11. package/dist/cjs/cancellable-promise.js +34 -13
  12. package/dist/cjs/cancellable-promise.js.map +1 -1
  13. package/dist/cjs/clean-url-search-params.js +26 -3
  14. package/dist/cjs/clean-url-search-params.js.map +1 -1
  15. package/dist/cjs/collections/cache-dictionary.js +3 -3
  16. package/dist/cjs/collections/cache-dictionary.js.map +1 -1
  17. package/dist/cjs/collections/default-map.js.map +1 -1
  18. package/dist/cjs/collections/default-weak-map.js.map +1 -1
  19. package/dist/cjs/collections/from.js +3 -3
  20. package/dist/cjs/collections/from.js.map +1 -1
  21. package/dist/cjs/collections/index.js.map +1 -1
  22. package/dist/cjs/draft.js +46 -11
  23. package/dist/cjs/draft.js.map +1 -1
  24. package/dist/cjs/event-bus.js +1 -1
  25. package/dist/cjs/event-bus.js.map +1 -1
  26. package/dist/cjs/functions.js +348 -0
  27. package/dist/cjs/functions.js.map +1 -0
  28. package/dist/cjs/index.js +1 -0
  29. package/dist/cjs/index.js.map +1 -1
  30. package/dist/cjs/key-value-pair.js.map +1 -1
  31. package/dist/cjs/lazy.js.map +1 -1
  32. package/dist/cjs/math.js +3 -0
  33. package/dist/cjs/math.js.map +1 -1
  34. package/dist/cjs/order.js.map +1 -1
  35. package/dist/cjs/parse.js +10 -1
  36. package/dist/cjs/parse.js.map +1 -1
  37. package/dist/cjs/patch.js.map +1 -1
  38. package/dist/cjs/stopwatch.js.map +1 -1
  39. package/dist/cjs/string.js +8 -5
  40. package/dist/cjs/string.js.map +1 -1
  41. package/dist/cjs/timer.js +1 -1
  42. package/dist/cjs/timer.js.map +1 -1
  43. package/dist/cjs/timespan.js +21 -8
  44. package/dist/cjs/timespan.js.map +1 -1
  45. package/dist/cjs/type-code.js.map +1 -1
  46. package/dist/cjs/type.js +20 -19
  47. package/dist/cjs/type.js.map +1 -1
  48. package/dist/cjs/types.js.map +1 -1
  49. package/dist/cjs/utilities.js +175 -39
  50. package/dist/cjs/utilities.js.map +1 -1
  51. package/dist/esm/array.js +21 -2
  52. package/dist/esm/array.js.map +1 -1
  53. package/dist/esm/async-pool-executor.js +26 -11
  54. package/dist/esm/async-pool-executor.js.map +1 -1
  55. package/dist/esm/browser/form-utilities.js +35 -34
  56. package/dist/esm/browser/form-utilities.js.map +1 -1
  57. package/dist/esm/browser/geo-location.js +1 -1
  58. package/dist/esm/browser/geo-location.js.map +1 -1
  59. package/dist/esm/browser/native-bridge.js +3 -1
  60. package/dist/esm/browser/native-bridge.js.map +1 -1
  61. package/dist/esm/cancellable-promise.js +34 -13
  62. package/dist/esm/cancellable-promise.js.map +1 -1
  63. package/dist/esm/clean-url-search-params.js +26 -3
  64. package/dist/esm/clean-url-search-params.js.map +1 -1
  65. package/dist/esm/collections/cache-dictionary.js +3 -3
  66. package/dist/esm/collections/cache-dictionary.js.map +1 -1
  67. package/dist/esm/collections/default-map.js.map +1 -1
  68. package/dist/esm/collections/default-weak-map.js.map +1 -1
  69. package/dist/esm/collections/from.js +3 -3
  70. package/dist/esm/collections/from.js.map +1 -1
  71. package/dist/esm/collections/index.js.map +1 -1
  72. package/dist/esm/draft.js +46 -11
  73. package/dist/esm/draft.js.map +1 -1
  74. package/dist/esm/event-bus.js +1 -1
  75. package/dist/esm/event-bus.js.map +1 -1
  76. package/dist/esm/functions.js +336 -0
  77. package/dist/esm/functions.js.map +1 -0
  78. package/dist/esm/index.js +1 -0
  79. package/dist/esm/index.js.map +1 -1
  80. package/dist/esm/key-value-pair.js.map +1 -1
  81. package/dist/esm/lazy.js.map +1 -1
  82. package/dist/esm/math.js +3 -0
  83. package/dist/esm/math.js.map +1 -1
  84. package/dist/esm/order.js.map +1 -1
  85. package/dist/esm/parse.js +10 -1
  86. package/dist/esm/parse.js.map +1 -1
  87. package/dist/esm/patch.js.map +1 -1
  88. package/dist/esm/stopwatch.js.map +1 -1
  89. package/dist/esm/string.js +8 -5
  90. package/dist/esm/string.js.map +1 -1
  91. package/dist/esm/timer.js +1 -1
  92. package/dist/esm/timer.js.map +1 -1
  93. package/dist/esm/timespan.js +21 -8
  94. package/dist/esm/timespan.js.map +1 -1
  95. package/dist/esm/type-code.js.map +1 -1
  96. package/dist/esm/type.js +20 -19
  97. package/dist/esm/type.js.map +1 -1
  98. package/dist/esm/types.js.map +1 -1
  99. package/dist/esm/utilities.js +174 -39
  100. package/dist/esm/utilities.js.map +1 -1
  101. package/dist/types/array.d.ts +20 -0
  102. package/dist/types/array.d.ts.map +1 -1
  103. package/dist/types/async-pool-executor.d.ts +2 -2
  104. package/dist/types/async-pool-executor.d.ts.map +1 -1
  105. package/dist/types/browser/form-utilities.d.ts.map +1 -1
  106. package/dist/types/browser/native-bridge.d.ts.map +1 -1
  107. package/dist/types/cancellable-promise.d.ts.map +1 -1
  108. package/dist/types/clean-url-search-params.d.ts +3 -1
  109. package/dist/types/clean-url-search-params.d.ts.map +1 -1
  110. package/dist/types/collections/from.d.ts +1 -1
  111. package/dist/types/draft.d.ts.map +1 -1
  112. package/dist/types/functions.d.ts +231 -0
  113. package/dist/types/functions.d.ts.map +1 -0
  114. package/dist/types/index.d.ts +1 -0
  115. package/dist/types/index.d.ts.map +1 -1
  116. package/dist/types/math.d.ts.map +1 -1
  117. package/dist/types/parse.d.ts.map +1 -1
  118. package/dist/types/string.d.ts.map +1 -1
  119. package/dist/types/timer.d.ts +1 -1
  120. package/dist/types/timespan.d.ts.map +1 -1
  121. package/dist/types/type.d.ts +2 -0
  122. package/dist/types/type.d.ts.map +1 -1
  123. package/dist/types/types.d.ts +1 -1
  124. package/dist/types/types.d.ts.map +1 -1
  125. package/dist/types/utilities.d.ts +21 -3
  126. package/dist/types/utilities.d.ts.map +1 -1
  127. package/dist/web/array.js +21 -2
  128. package/dist/web/array.js.map +1 -1
  129. package/dist/web/async-pool-executor.js +26 -11
  130. package/dist/web/async-pool-executor.js.map +1 -1
  131. package/dist/web/browser/form-utilities.js +35 -34
  132. package/dist/web/browser/form-utilities.js.map +1 -1
  133. package/dist/web/browser/geo-location.js +1 -1
  134. package/dist/web/browser/geo-location.js.map +1 -1
  135. package/dist/web/browser/native-bridge.js +3 -1
  136. package/dist/web/browser/native-bridge.js.map +1 -1
  137. package/dist/web/cancellable-promise.js +34 -13
  138. package/dist/web/cancellable-promise.js.map +1 -1
  139. package/dist/web/clean-url-search-params.js +26 -3
  140. package/dist/web/clean-url-search-params.js.map +1 -1
  141. package/dist/web/collections/cache-dictionary.js +3 -3
  142. package/dist/web/collections/cache-dictionary.js.map +1 -1
  143. package/dist/web/collections/default-map.js.map +1 -1
  144. package/dist/web/collections/default-weak-map.js.map +1 -1
  145. package/dist/web/collections/from.js +3 -3
  146. package/dist/web/collections/from.js.map +1 -1
  147. package/dist/web/collections/index.js.map +1 -1
  148. package/dist/web/draft.js +46 -11
  149. package/dist/web/draft.js.map +1 -1
  150. package/dist/web/event-bus.js +1 -1
  151. package/dist/web/event-bus.js.map +1 -1
  152. package/dist/web/functions.js +336 -0
  153. package/dist/web/functions.js.map +1 -0
  154. package/dist/web/index.js +1 -0
  155. package/dist/web/index.js.map +1 -1
  156. package/dist/web/key-value-pair.js.map +1 -1
  157. package/dist/web/lazy.js.map +1 -1
  158. package/dist/web/math.js +3 -0
  159. package/dist/web/math.js.map +1 -1
  160. package/dist/web/order.js.map +1 -1
  161. package/dist/web/parse.js +10 -1
  162. package/dist/web/parse.js.map +1 -1
  163. package/dist/web/patch.js.map +1 -1
  164. package/dist/web/stopwatch.js.map +1 -1
  165. package/dist/web/string.js +8 -5
  166. package/dist/web/string.js.map +1 -1
  167. package/dist/web/timer.js +1 -1
  168. package/dist/web/timer.js.map +1 -1
  169. package/dist/web/timespan.js +21 -8
  170. package/dist/web/timespan.js.map +1 -1
  171. package/dist/web/type-code.js.map +1 -1
  172. package/dist/web/type.js +20 -19
  173. package/dist/web/type.js.map +1 -1
  174. package/dist/web/types.js.map +1 -1
  175. package/dist/web/utilities.js +174 -39
  176. package/dist/web/utilities.js.map +1 -1
  177. package/package.json +66 -60
  178. package/src/array.ts +116 -84
  179. package/src/async-pool-executor.ts +107 -85
  180. package/src/browser/form-utilities.ts +32 -31
  181. package/src/browser/geo-location.ts +113 -113
  182. package/src/browser/native-bridge.ts +118 -116
  183. package/src/cancellable-promise.ts +102 -76
  184. package/src/clean-url-search-params.ts +36 -11
  185. package/src/collections/cache-dictionary.ts +115 -115
  186. package/src/collections/default-map.ts +48 -48
  187. package/src/collections/default-weak-map.ts +51 -51
  188. package/src/collections/from.ts +859 -859
  189. package/src/collections/index.ts +3 -3
  190. package/src/draft.ts +55 -16
  191. package/src/event-bus.ts +83 -83
  192. package/src/functions.ts +496 -0
  193. package/src/index.ts +1 -0
  194. package/src/key-value-pair.ts +24 -24
  195. package/src/lazy.ts +32 -32
  196. package/src/math.ts +33 -30
  197. package/src/order.ts +5 -5
  198. package/src/parse.ts +251 -240
  199. package/src/patch.ts +79 -79
  200. package/src/stopwatch.ts +43 -43
  201. package/src/string.ts +8 -5
  202. package/src/timer.ts +67 -67
  203. package/src/timespan.ts +412 -398
  204. package/src/type-code.ts +15 -15
  205. package/src/type.ts +372 -371
  206. package/src/types.ts +30 -30
  207. package/src/utilities.ts +653 -480
package/src/parse.ts CHANGED
@@ -1,241 +1,252 @@
1
- // parse.ts
2
- /**
3
- * Options for {@link parseBoolean}.
4
- */
5
- export interface ParseBooleanOptions {
6
- /**
7
- * Additional strings to consider as `true`.
8
- * These are matched case-insensitively.
9
- * @example ["oui", "ja"]
10
- */
11
- extraTruthy?: string[];
12
-
13
- /**
14
- * Additional strings to consider as `false`.
15
- * These are matched case-insensitively.
16
- * @example ["non", "nein"]
17
- */
18
- extraFalsy?: string[];
19
- }
20
-
21
- /**
22
- * Options for {@link parseInteger}.
23
- */
24
- export interface ParseIntegerOptions {
25
- /**
26
- * Numeric base for conversion.
27
- * @default 10
28
- */
29
- radix?: number;
30
-
31
- /**
32
- * Thousands separator to strip before parsing.
33
- * @example "." for "1.234.567" → 1234567
34
- * @example "," for "1,234,567" → 1234567
35
- */
36
- thousandsSeparator?: string;
37
- }
38
-
39
- /**
40
- * Options for {@link parseDecimal}.
41
- */
42
- export interface ParseDecimalOptions {
43
- /**
44
- * Character used as decimal point.
45
- * @default "."
46
- */
47
- decimalSeparator?: "." | ",";
48
-
49
- /**
50
- * Thousands separator to strip before parsing.
51
- * @example "." for "1.234,56" (EU format)
52
- * @example "," for "1,234.56" (US format)
53
- */
54
- thousandsSeparator?: string;
55
- }
56
-
57
- // ============================================
58
- // HELPERS
59
- // ============================================
60
-
61
- function stripThousands(value: string, separator?: string): string {
62
- if (!separator) return value;
63
- return value.split(separator).join("");
64
- }
65
-
66
- // ============================================
67
- // parseInteger
68
- // ============================================
69
-
70
- /**
71
- * Safely parses a value into an integer.
72
- * Returns `null` if the value cannot be parsed.
73
- *
74
- * - `null` or `undefined` → `null`
75
- * - `number` → returned as-is (floats are truncated)
76
- * - `string` → parsed with the given options
77
- *
78
- * @param value - The value to parse.
79
- * @param options - Optional parsing configuration.
80
- * @returns The parsed integer, or `null` on failure.
81
- *
82
- * @example
83
- * ```ts
84
- * parseInteger(42) // 42
85
- * parseInteger(3.7) // 3
86
- * parseInteger(null) // null
87
- * parseInteger(undefined) // null
88
- * parseInteger("42") // 42
89
- * parseInteger("1.234.567", { thousandsSeparator: "." }) // 1234567
90
- * parseInteger("FF", { radix: 16 }) // 255
91
- * parseInteger("abc") // null
92
- * ```
93
- */
94
- export function parseInteger(
95
- value: string | number | null | undefined,
96
- options: ParseIntegerOptions = {}
97
- ): number | null {
98
- if (value === null || value === undefined) return null;
99
-
100
- if (typeof value === "number") {
101
- return Number.isFinite(value) ? Math.trunc(value) : null;
102
- }
103
-
104
- const { radix = 10, thousandsSeparator } = options;
105
-
106
- const cleaned = stripThousands(value.trim(), thousandsSeparator);
107
-
108
- if (cleaned === "") return null;
109
- if (radix === 10 && !/^[+-]?\d+$/.test(cleaned)) return null;
110
-
111
- const parsed = Number.parseInt(cleaned, radix);
112
-
113
- return Number.isNaN(parsed) ? null : parsed;
114
- }
115
-
116
- // ============================================
117
- // parseDecimal
118
- // ============================================
119
-
120
- /**
121
- * Safely parses a value into a floating-point number.
122
- * Returns `null` if the value cannot be parsed.
123
- *
124
- * - `null` or `undefined` → `null`
125
- * - `number` → returned as-is (if finite)
126
- * - `string` → parsed with the given options
127
- *
128
- * Supports custom decimal and thousands separators, making it suitable
129
- * for both US (`1,234.56`) and EU (`1.234,56`) number formats.
130
- *
131
- * @param value - The value to parse.
132
- * @param options - Optional parsing configuration.
133
- * @returns The parsed float, or `null` on failure.
134
- *
135
- * @example
136
- * ```ts
137
- * parseDecimal(3.14) // 3.14
138
- * parseDecimal(null) // null
139
- * parseDecimal(undefined) // null
140
- * parseDecimal("3.14") // 3.14
141
- * parseDecimal("3,14", { decimalSeparator: "," }) // 3.14
142
- * parseDecimal("1.234,56", { decimalSeparator: ",", thousandsSeparator: "." }) // 1234.56
143
- * parseDecimal("abc") // null
144
- * ```
145
- */
146
- export function parseDecimal(
147
- value: string | number | null | undefined,
148
- options: ParseDecimalOptions = {}
149
- ): number | null {
150
- if (value === null || value === undefined) return null;
151
-
152
- if (typeof value === "number") {
153
- return Number.isFinite(value) ? value : null;
154
- }
155
-
156
- const { decimalSeparator = ".", thousandsSeparator } = options;
157
-
158
- let cleaned = stripThousands(value.trim(), thousandsSeparator);
159
-
160
- if (cleaned === "") return null;
161
-
162
- if (decimalSeparator === ",") {
163
- const commaCount = (cleaned.match(/,/g) || []).length;
164
- if (commaCount > 1) return null;
165
- cleaned = cleaned.replace(",", ".");
166
- }
167
-
168
- if (!/^[+-]?\d+(\.\d+)?$/.test(cleaned)) return null;
169
-
170
- const parsed = Number.parseFloat(cleaned);
171
-
172
- return Number.isNaN(parsed) || !Number.isFinite(parsed) ? null : parsed;
173
- }
174
-
175
- // ============================================
176
- // parseBoolean
177
- // ============================================
178
-
179
- const DEFAULT_TRUTHY = ["true", "1", "yes", "si", "sí", "on"];
180
- const DEFAULT_FALSY = ["false", "0", "no", "off"];
181
-
182
- /**
183
- * Safely parses a value into a boolean.
184
- * Returns `null` if the value cannot be interpreted as boolean.
185
- *
186
- * - `null` or `undefined` → `null`
187
- * - `boolean` → returned as-is
188
- * - `number` → `0` is `false`, any other finite number is `true`
189
- * - `string` → matched case-insensitively against known truthy/falsy values
190
- *
191
- * Default truthy: `"true"`, `"1"`, `"yes"`, `"si"`, `"sí"`, `"on"`
192
- * Default falsy: `"false"`, `"0"`, `"no"`, `"off"`
193
- *
194
- * @param value - The value to parse.
195
- * @param options - Optional parsing configuration.
196
- * @returns The parsed boolean, or `null` on failure.
197
- *
198
- * @example
199
- * ```ts
200
- * parseBoolean(true) // true
201
- * parseBoolean(false) // false
202
- * parseBoolean(1) // true
203
- * parseBoolean(0) // false
204
- * parseBoolean(null) // null
205
- * parseBoolean(undefined) // null
206
- * parseBoolean("yes") // true
207
- * parseBoolean("no") // false
208
- * parseBoolean("sí") // true
209
- * parseBoolean("TRUE") // true
210
- * parseBoolean("oui") // null
211
- * parseBoolean("oui", { extraTruthy: ["oui"] }) // true
212
- * parseBoolean("nein", { extraFalsy: ["nein"] }) // false
213
- * parseBoolean("whatever") // null
214
- * ```
215
- */
216
- export function parseBoolean(
217
- value: string | number | boolean | null | undefined,
218
- options: ParseBooleanOptions = {}
219
- ): boolean | null {
220
- if (value === null || value === undefined) return null;
221
-
222
- if (typeof value === "boolean") return value;
223
-
224
- if (typeof value === "number") {
225
- return Number.isFinite(value) ? value !== 0 : null;
226
- }
227
-
228
- const { extraTruthy = [], extraFalsy = [] } = options;
229
-
230
- const normalized = value.trim().toLowerCase();
231
-
232
- if (normalized === "") return null;
233
-
234
- const truthy = [...DEFAULT_TRUTHY, ...extraTruthy.map((s) => s.toLowerCase())];
235
- const falsy = [...DEFAULT_FALSY, ...extraFalsy.map((s) => s.toLowerCase())];
236
-
237
- if (truthy.includes(normalized)) return true;
238
- if (falsy.includes(normalized)) return false;
239
-
240
- return null;
1
+ // parse.ts
2
+ /**
3
+ * Options for {@link parseBoolean}.
4
+ */
5
+ export interface ParseBooleanOptions {
6
+ /**
7
+ * Additional strings to consider as `true`.
8
+ * These are matched case-insensitively.
9
+ * @example ["oui", "ja"]
10
+ */
11
+ extraTruthy?: string[];
12
+
13
+ /**
14
+ * Additional strings to consider as `false`.
15
+ * These are matched case-insensitively.
16
+ * @example ["non", "nein"]
17
+ */
18
+ extraFalsy?: string[];
19
+ }
20
+
21
+ /**
22
+ * Options for {@link parseInteger}.
23
+ */
24
+ export interface ParseIntegerOptions {
25
+ /**
26
+ * Numeric base for conversion.
27
+ * @default 10
28
+ */
29
+ radix?: number;
30
+
31
+ /**
32
+ * Thousands separator to strip before parsing.
33
+ * @example "." for "1.234.567" → 1234567
34
+ * @example "," for "1,234,567" → 1234567
35
+ */
36
+ thousandsSeparator?: string;
37
+ }
38
+
39
+ /**
40
+ * Options for {@link parseDecimal}.
41
+ */
42
+ export interface ParseDecimalOptions {
43
+ /**
44
+ * Character used as decimal point.
45
+ * @default "."
46
+ */
47
+ decimalSeparator?: "." | ",";
48
+
49
+ /**
50
+ * Thousands separator to strip before parsing.
51
+ * @example "." for "1.234,56" (EU format)
52
+ * @example "," for "1,234.56" (US format)
53
+ */
54
+ thousandsSeparator?: string;
55
+ }
56
+
57
+ // ============================================
58
+ // HELPERS
59
+ // ============================================
60
+
61
+ function stripThousands(value: string, separator?: string): string {
62
+ if (!separator) return value;
63
+ return value.split(separator).join("");
64
+ }
65
+
66
+ function isValidInteger(value: string, radix: number): boolean {
67
+ if (!Number.isInteger(radix) || radix < 2 || radix > 36) return false;
68
+
69
+ const signless = value.replace(/^[+-]/, "");
70
+ if (signless === "") return false;
71
+
72
+ const digits = "0123456789abcdefghijklmnopqrstuvwxyz".slice(0, radix);
73
+
74
+ return [...signless.toLowerCase()].every((char) => digits.includes(char));
75
+ }
76
+
77
+ // ============================================
78
+ // parseInteger
79
+ // ============================================
80
+
81
+ /**
82
+ * Safely parses a value into an integer.
83
+ * Returns `null` if the value cannot be parsed.
84
+ *
85
+ * - `null` or `undefined` → `null`
86
+ * - `number` → returned as-is (floats are truncated)
87
+ * - `string` → parsed with the given options
88
+ *
89
+ * @param value - The value to parse.
90
+ * @param options - Optional parsing configuration.
91
+ * @returns The parsed integer, or `null` on failure.
92
+ *
93
+ * @example
94
+ * ```ts
95
+ * parseInteger(42) // 42
96
+ * parseInteger(3.7) // 3
97
+ * parseInteger(null) // null
98
+ * parseInteger(undefined) // null
99
+ * parseInteger("42") // 42
100
+ * parseInteger("1.234.567", { thousandsSeparator: "." }) // 1234567
101
+ * parseInteger("FF", { radix: 16 }) // 255
102
+ * parseInteger("abc") // null
103
+ * ```
104
+ */
105
+ export function parseInteger(
106
+ value: string | number | null | undefined,
107
+ options: ParseIntegerOptions = {}
108
+ ): number | null {
109
+ if (value === null || value === undefined) return null;
110
+
111
+ if (typeof value === "number") {
112
+ return Number.isFinite(value) ? Math.trunc(value) : null;
113
+ }
114
+
115
+ const { radix = 10, thousandsSeparator } = options;
116
+
117
+ const cleaned = stripThousands(value.trim(), thousandsSeparator);
118
+
119
+ if (cleaned === "") return null;
120
+ if (!isValidInteger(cleaned, radix)) return null;
121
+
122
+ const parsed = Number.parseInt(cleaned, radix);
123
+
124
+ return Number.isNaN(parsed) ? null : parsed;
125
+ }
126
+
127
+ // ============================================
128
+ // parseDecimal
129
+ // ============================================
130
+
131
+ /**
132
+ * Safely parses a value into a floating-point number.
133
+ * Returns `null` if the value cannot be parsed.
134
+ *
135
+ * - `null` or `undefined` → `null`
136
+ * - `number` → returned as-is (if finite)
137
+ * - `string` → parsed with the given options
138
+ *
139
+ * Supports custom decimal and thousands separators, making it suitable
140
+ * for both US (`1,234.56`) and EU (`1.234,56`) number formats.
141
+ *
142
+ * @param value - The value to parse.
143
+ * @param options - Optional parsing configuration.
144
+ * @returns The parsed float, or `null` on failure.
145
+ *
146
+ * @example
147
+ * ```ts
148
+ * parseDecimal(3.14) // 3.14
149
+ * parseDecimal(null) // null
150
+ * parseDecimal(undefined) // null
151
+ * parseDecimal("3.14") // 3.14
152
+ * parseDecimal("3,14", { decimalSeparator: "," }) // 3.14
153
+ * parseDecimal("1.234,56", { decimalSeparator: ",", thousandsSeparator: "." }) // 1234.56
154
+ * parseDecimal("abc") // null
155
+ * ```
156
+ */
157
+ export function parseDecimal(
158
+ value: string | number | null | undefined,
159
+ options: ParseDecimalOptions = {}
160
+ ): number | null {
161
+ if (value === null || value === undefined) return null;
162
+
163
+ if (typeof value === "number") {
164
+ return Number.isFinite(value) ? value : null;
165
+ }
166
+
167
+ const { decimalSeparator = ".", thousandsSeparator } = options;
168
+
169
+ let cleaned = stripThousands(value.trim(), thousandsSeparator);
170
+
171
+ if (cleaned === "") return null;
172
+
173
+ if (decimalSeparator === ",") {
174
+ const commaCount = (cleaned.match(/,/g) || []).length;
175
+ if (commaCount > 1) return null;
176
+ cleaned = cleaned.replace(",", ".");
177
+ }
178
+
179
+ if (!/^[+-]?\d+(\.\d+)?$/.test(cleaned)) return null;
180
+
181
+ const parsed = Number.parseFloat(cleaned);
182
+
183
+ return Number.isNaN(parsed) || !Number.isFinite(parsed) ? null : parsed;
184
+ }
185
+
186
+ // ============================================
187
+ // parseBoolean
188
+ // ============================================
189
+
190
+ const DEFAULT_TRUTHY = ["true", "1", "yes", "si", "sí", "on"];
191
+ const DEFAULT_FALSY = ["false", "0", "no", "off"];
192
+
193
+ /**
194
+ * Safely parses a value into a boolean.
195
+ * Returns `null` if the value cannot be interpreted as boolean.
196
+ *
197
+ * - `null` or `undefined` → `null`
198
+ * - `boolean` → returned as-is
199
+ * - `number` → `0` is `false`, any other finite number is `true`
200
+ * - `string` → matched case-insensitively against known truthy/falsy values
201
+ *
202
+ * Default truthy: `"true"`, `"1"`, `"yes"`, `"si"`, `"sí"`, `"on"`
203
+ * Default falsy: `"false"`, `"0"`, `"no"`, `"off"`
204
+ *
205
+ * @param value - The value to parse.
206
+ * @param options - Optional parsing configuration.
207
+ * @returns The parsed boolean, or `null` on failure.
208
+ *
209
+ * @example
210
+ * ```ts
211
+ * parseBoolean(true) // true
212
+ * parseBoolean(false) // false
213
+ * parseBoolean(1) // true
214
+ * parseBoolean(0) // false
215
+ * parseBoolean(null) // null
216
+ * parseBoolean(undefined) // null
217
+ * parseBoolean("yes") // true
218
+ * parseBoolean("no") // false
219
+ * parseBoolean("sí") // true
220
+ * parseBoolean("TRUE") // true
221
+ * parseBoolean("oui") // null
222
+ * parseBoolean("oui", { extraTruthy: ["oui"] }) // true
223
+ * parseBoolean("nein", { extraFalsy: ["nein"] }) // false
224
+ * parseBoolean("whatever") // null
225
+ * ```
226
+ */
227
+ export function parseBoolean(
228
+ value: string | number | boolean | null | undefined,
229
+ options: ParseBooleanOptions = {}
230
+ ): boolean | null {
231
+ if (value === null || value === undefined) return null;
232
+
233
+ if (typeof value === "boolean") return value;
234
+
235
+ if (typeof value === "number") {
236
+ return Number.isFinite(value) ? value !== 0 : null;
237
+ }
238
+
239
+ const { extraTruthy = [], extraFalsy = [] } = options;
240
+
241
+ const normalized = value.trim().toLowerCase();
242
+
243
+ if (normalized === "") return null;
244
+
245
+ const truthy = [...DEFAULT_TRUTHY, ...extraTruthy.map((s) => s.toLowerCase())];
246
+ const falsy = [...DEFAULT_FALSY, ...extraFalsy.map((s) => s.toLowerCase())];
247
+
248
+ if (truthy.includes(normalized)) return true;
249
+ if (falsy.includes(normalized)) return false;
250
+
251
+ return null;
241
252
  }
package/src/patch.ts CHANGED
@@ -1,80 +1,80 @@
1
- import type {Constructor} from "./types.js";
2
-
3
- export type PureMethod<T extends object, A extends any[] = any[], R = any> =
4
- (obj: T, ...args: A) => R;
5
-
6
- export type PatchSpec<T extends object> =
7
- | Record<string, PureMethod<T, any[], any>>
8
- | Map<PropertyKey, PureMethod<T, any[], any>>;
9
-
10
-
11
- export function patch<C extends Constructor<any>>(
12
- targetOrCtor: C,
13
- fns: PatchSpec<InstanceType<C>>
14
- ): Map<PropertyKey, PropertyDescriptor | null>;
15
-
16
- export function patch<T extends object>(
17
- targetOrCtor: T,
18
- fns: PatchSpec<T>
19
- ): Map<PropertyKey, PropertyDescriptor | null>;
20
-
21
- /**
22
- * Devuelve un Map donde:
23
- * - clave -> null (método añadido nuevo)
24
- * - clave -> descriptorOriginal (método sustituido)
25
- */
26
- export function patch(
27
- targetOrCtor: any,
28
- fns: PatchSpec<any>
29
- ): Map<PropertyKey, PropertyDescriptor | null> {
30
- const target = typeof targetOrCtor === "function" ? targetOrCtor.prototype : targetOrCtor;
31
-
32
- const result = new Map<PropertyKey, PropertyDescriptor | null>();
33
- const entries: Array<[PropertyKey, PureMethod<any>]> =
34
- fns instanceof Map ? [...fns.entries()] : Object.entries(fns) as any;
35
-
36
- for (const [key, fn] of entries) {
37
- const prev = Object.getOwnPropertyDescriptor(target, key);
38
- const method = function (this: unknown, ...args: unknown[]) {
39
- return (fn as any)(this, ...args);
40
- };
41
- try {
42
- Object.defineProperty(target, key, {
43
- value: method,
44
- writable: true,
45
- configurable: true,
46
- enumerable: false,
47
- });
48
- result.set(key, prev ?? null);
49
- } catch { /* ignorar */
50
- }
51
- }
52
- return result;
53
- }
54
-
55
- // Overloads unpatch
56
- export function unpatch<C extends Constructor<any>>(
57
- targetOrCtor: C,
58
- changes: Map<PropertyKey, PropertyDescriptor | null>
59
- ): void;
60
- export function unpatch<T extends object>(
61
- targetOrCtor: T,
62
- changes: Map<PropertyKey, PropertyDescriptor | null>
63
- ): void;
64
-
65
- /**
66
- * Revierte un patch a partir del mapa devuelto por `patch`.
67
- */
68
- export function unpatch(targetOrCtor: any, changes: Map<PropertyKey, PropertyDescriptor | null>): void {
69
- const target = typeof targetOrCtor === "function" ? targetOrCtor.prototype : targetOrCtor;
70
- for (const [key, prev] of changes) {
71
- try {
72
- if (prev === null) {
73
- if (Object.prototype.hasOwnProperty.call(target, key)) delete target[key];
74
- } else {
75
- Object.defineProperty(target, key, prev);
76
- }
77
- } catch { /* ignorar */
78
- }
79
- }
1
+ import type {Constructor} from "./types.js";
2
+
3
+ export type PureMethod<T extends object, A extends any[] = any[], R = any> =
4
+ (obj: T, ...args: A) => R;
5
+
6
+ export type PatchSpec<T extends object> =
7
+ | Record<string, PureMethod<T, any[], any>>
8
+ | Map<PropertyKey, PureMethod<T, any[], any>>;
9
+
10
+
11
+ export function patch<C extends Constructor<any>>(
12
+ targetOrCtor: C,
13
+ fns: PatchSpec<InstanceType<C>>
14
+ ): Map<PropertyKey, PropertyDescriptor | null>;
15
+
16
+ export function patch<T extends object>(
17
+ targetOrCtor: T,
18
+ fns: PatchSpec<T>
19
+ ): Map<PropertyKey, PropertyDescriptor | null>;
20
+
21
+ /**
22
+ * Devuelve un Map donde:
23
+ * - clave -> null (método añadido nuevo)
24
+ * - clave -> descriptorOriginal (método sustituido)
25
+ */
26
+ export function patch(
27
+ targetOrCtor: any,
28
+ fns: PatchSpec<any>
29
+ ): Map<PropertyKey, PropertyDescriptor | null> {
30
+ const target = typeof targetOrCtor === "function" ? targetOrCtor.prototype : targetOrCtor;
31
+
32
+ const result = new Map<PropertyKey, PropertyDescriptor | null>();
33
+ const entries: Array<[PropertyKey, PureMethod<any>]> =
34
+ fns instanceof Map ? [...fns.entries()] : Object.entries(fns) as any;
35
+
36
+ for (const [key, fn] of entries) {
37
+ const prev = Object.getOwnPropertyDescriptor(target, key);
38
+ const method = function (this: unknown, ...args: unknown[]) {
39
+ return (fn as any)(this, ...args);
40
+ };
41
+ try {
42
+ Object.defineProperty(target, key, {
43
+ value: method,
44
+ writable: true,
45
+ configurable: true,
46
+ enumerable: false,
47
+ });
48
+ result.set(key, prev ?? null);
49
+ } catch { /* ignorar */
50
+ }
51
+ }
52
+ return result;
53
+ }
54
+
55
+ // Overloads unpatch
56
+ export function unpatch<C extends Constructor<any>>(
57
+ targetOrCtor: C,
58
+ changes: Map<PropertyKey, PropertyDescriptor | null>
59
+ ): void;
60
+ export function unpatch<T extends object>(
61
+ targetOrCtor: T,
62
+ changes: Map<PropertyKey, PropertyDescriptor | null>
63
+ ): void;
64
+
65
+ /**
66
+ * Revierte un patch a partir del mapa devuelto por `patch`.
67
+ */
68
+ export function unpatch(targetOrCtor: any, changes: Map<PropertyKey, PropertyDescriptor | null>): void {
69
+ const target = typeof targetOrCtor === "function" ? targetOrCtor.prototype : targetOrCtor;
70
+ for (const [key, prev] of changes) {
71
+ try {
72
+ if (prev === null) {
73
+ if (Object.prototype.hasOwnProperty.call(target, key)) delete target[key];
74
+ } else {
75
+ Object.defineProperty(target, key, prev);
76
+ }
77
+ } catch { /* ignorar */
78
+ }
79
+ }
80
80
  }