@develia/commons 0.4.16 → 0.4.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/array.js +22 -2
- package/dist/cjs/array.js.map +1 -1
- package/dist/cjs/async-pool-executor.js +26 -11
- package/dist/cjs/async-pool-executor.js.map +1 -1
- package/dist/cjs/browser/form-utilities.js +35 -34
- package/dist/cjs/browser/form-utilities.js.map +1 -1
- package/dist/cjs/browser/geo-location.js +1 -1
- package/dist/cjs/browser/geo-location.js.map +1 -1
- package/dist/cjs/browser/index.js +0 -0
- package/dist/cjs/browser/index.js.map +0 -0
- package/dist/cjs/browser/native-bridge.js +3 -1
- package/dist/cjs/browser/native-bridge.js.map +1 -1
- package/dist/cjs/cancellable-promise.js +34 -13
- package/dist/cjs/cancellable-promise.js.map +1 -1
- package/dist/cjs/clean-url-search-params.js +26 -3
- package/dist/cjs/clean-url-search-params.js.map +1 -1
- package/dist/cjs/collections/cache-dictionary.js +3 -3
- package/dist/cjs/collections/cache-dictionary.js.map +1 -1
- package/dist/cjs/collections/default-map.js +0 -0
- package/dist/cjs/collections/default-map.js.map +1 -1
- package/dist/cjs/collections/default-weak-map.js +0 -0
- package/dist/cjs/collections/default-weak-map.js.map +1 -1
- package/dist/cjs/collections/from.js +3 -3
- package/dist/cjs/collections/from.js.map +1 -1
- package/dist/cjs/collections/index.js +0 -0
- package/dist/cjs/collections/index.js.map +1 -1
- package/dist/cjs/draft.js +46 -11
- package/dist/cjs/draft.js.map +1 -1
- package/dist/cjs/event-bus.js +1 -1
- package/dist/cjs/event-bus.js.map +1 -1
- package/dist/cjs/functions.js +348 -0
- package/dist/cjs/functions.js.map +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/key-value-pair.js +0 -0
- package/dist/cjs/key-value-pair.js.map +1 -1
- package/dist/cjs/lazy.js +0 -0
- package/dist/cjs/lazy.js.map +1 -1
- package/dist/cjs/math.js +3 -0
- package/dist/cjs/math.js.map +1 -1
- package/dist/cjs/order.js +0 -0
- package/dist/cjs/order.js.map +1 -1
- package/dist/cjs/parse.js +10 -1
- package/dist/cjs/parse.js.map +1 -1
- package/dist/cjs/patch.js +0 -0
- package/dist/cjs/patch.js.map +1 -1
- package/dist/cjs/range.js +0 -0
- package/dist/cjs/range.js.map +0 -0
- package/dist/cjs/stopwatch.js +0 -0
- package/dist/cjs/stopwatch.js.map +1 -1
- package/dist/cjs/string.js +8 -5
- package/dist/cjs/string.js.map +1 -1
- package/dist/cjs/timer.js +1 -1
- package/dist/cjs/timer.js.map +1 -1
- package/dist/cjs/timespan.js +21 -8
- package/dist/cjs/timespan.js.map +1 -1
- package/dist/cjs/type-code.js +0 -0
- package/dist/cjs/type-code.js.map +1 -1
- package/dist/cjs/type.js +20 -19
- package/dist/cjs/type.js.map +1 -1
- package/dist/cjs/types.js +0 -0
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/utilities.js +175 -39
- package/dist/cjs/utilities.js.map +1 -1
- package/dist/esm/array.js +21 -2
- package/dist/esm/array.js.map +1 -1
- package/dist/esm/async-pool-executor.js +26 -11
- package/dist/esm/async-pool-executor.js.map +1 -1
- package/dist/esm/browser/form-utilities.js +35 -34
- package/dist/esm/browser/form-utilities.js.map +1 -1
- package/dist/esm/browser/geo-location.js +1 -1
- package/dist/esm/browser/geo-location.js.map +1 -1
- package/dist/esm/browser/index.js +0 -0
- package/dist/esm/browser/index.js.map +0 -0
- package/dist/esm/browser/native-bridge.js +3 -1
- package/dist/esm/browser/native-bridge.js.map +1 -1
- package/dist/esm/cancellable-promise.js +34 -13
- package/dist/esm/cancellable-promise.js.map +1 -1
- package/dist/esm/clean-url-search-params.js +26 -3
- package/dist/esm/clean-url-search-params.js.map +1 -1
- package/dist/esm/collections/cache-dictionary.js +3 -3
- package/dist/esm/collections/cache-dictionary.js.map +1 -1
- package/dist/esm/collections/default-map.js +0 -0
- package/dist/esm/collections/default-map.js.map +1 -1
- package/dist/esm/collections/default-weak-map.js +0 -0
- package/dist/esm/collections/default-weak-map.js.map +1 -1
- package/dist/esm/collections/from.js +3 -3
- package/dist/esm/collections/from.js.map +1 -1
- package/dist/esm/collections/index.js +0 -0
- package/dist/esm/collections/index.js.map +1 -1
- package/dist/esm/draft.js +46 -11
- package/dist/esm/draft.js.map +1 -1
- package/dist/esm/event-bus.js +1 -1
- package/dist/esm/event-bus.js.map +1 -1
- package/dist/esm/functions.js +336 -0
- package/dist/esm/functions.js.map +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/key-value-pair.js +0 -0
- package/dist/esm/key-value-pair.js.map +1 -1
- package/dist/esm/lazy.js +0 -0
- package/dist/esm/lazy.js.map +1 -1
- package/dist/esm/math.js +3 -0
- package/dist/esm/math.js.map +1 -1
- package/dist/esm/order.js +0 -0
- package/dist/esm/order.js.map +1 -1
- package/dist/esm/parse.js +10 -1
- package/dist/esm/parse.js.map +1 -1
- package/dist/esm/patch.js +0 -0
- package/dist/esm/patch.js.map +1 -1
- package/dist/esm/range.js +0 -0
- package/dist/esm/range.js.map +0 -0
- package/dist/esm/stopwatch.js +0 -0
- package/dist/esm/stopwatch.js.map +1 -1
- package/dist/esm/string.js +8 -5
- package/dist/esm/string.js.map +1 -1
- package/dist/esm/timer.js +1 -1
- package/dist/esm/timer.js.map +1 -1
- package/dist/esm/timespan.js +21 -8
- package/dist/esm/timespan.js.map +1 -1
- package/dist/esm/type-code.js +0 -0
- package/dist/esm/type-code.js.map +1 -1
- package/dist/esm/type.js +20 -19
- package/dist/esm/type.js.map +1 -1
- package/dist/esm/types.js +0 -0
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/utilities.js +174 -39
- package/dist/esm/utilities.js.map +1 -1
- package/dist/types/array.d.ts +20 -0
- package/dist/types/array.d.ts.map +1 -1
- package/dist/types/async-pool-executor.d.ts +2 -2
- package/dist/types/async-pool-executor.d.ts.map +1 -1
- package/dist/types/browser/form-utilities.d.ts +0 -0
- package/dist/types/browser/form-utilities.d.ts.map +1 -1
- package/dist/types/browser/geo-location.d.ts +0 -0
- package/dist/types/browser/geo-location.d.ts.map +0 -0
- package/dist/types/browser/index.d.ts +0 -0
- package/dist/types/browser/index.d.ts.map +0 -0
- package/dist/types/browser/native-bridge.d.ts +0 -0
- package/dist/types/browser/native-bridge.d.ts.map +1 -1
- package/dist/types/cancellable-promise.d.ts +0 -0
- package/dist/types/cancellable-promise.d.ts.map +1 -1
- package/dist/types/clean-url-search-params.d.ts +3 -1
- package/dist/types/clean-url-search-params.d.ts.map +1 -1
- package/dist/types/collections/cache-dictionary.d.ts +0 -0
- package/dist/types/collections/cache-dictionary.d.ts.map +0 -0
- package/dist/types/collections/default-map.d.ts +0 -0
- package/dist/types/collections/default-map.d.ts.map +0 -0
- package/dist/types/collections/default-weak-map.d.ts +0 -0
- package/dist/types/collections/default-weak-map.d.ts.map +0 -0
- package/dist/types/collections/from.d.ts +1 -1
- package/dist/types/collections/from.d.ts.map +0 -0
- package/dist/types/collections/index.d.ts +0 -0
- package/dist/types/collections/index.d.ts.map +0 -0
- package/dist/types/draft.d.ts +0 -0
- package/dist/types/draft.d.ts.map +1 -1
- package/dist/types/event-bus.d.ts +0 -0
- package/dist/types/event-bus.d.ts.map +0 -0
- package/dist/types/functions.d.ts +231 -0
- package/dist/types/functions.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/key-value-pair.d.ts +0 -0
- package/dist/types/key-value-pair.d.ts.map +0 -0
- package/dist/types/lazy.d.ts +0 -0
- package/dist/types/lazy.d.ts.map +0 -0
- package/dist/types/math.d.ts +0 -0
- package/dist/types/math.d.ts.map +1 -1
- package/dist/types/order.d.ts +0 -0
- package/dist/types/order.d.ts.map +0 -0
- package/dist/types/parse.d.ts +0 -0
- package/dist/types/parse.d.ts.map +1 -1
- package/dist/types/patch.d.ts +0 -0
- package/dist/types/patch.d.ts.map +0 -0
- package/dist/types/range.d.ts +0 -0
- package/dist/types/range.d.ts.map +0 -0
- package/dist/types/stopwatch.d.ts +0 -0
- package/dist/types/stopwatch.d.ts.map +0 -0
- package/dist/types/string.d.ts +0 -0
- package/dist/types/string.d.ts.map +1 -1
- package/dist/types/timer.d.ts +1 -1
- package/dist/types/timer.d.ts.map +0 -0
- package/dist/types/timespan.d.ts +0 -0
- package/dist/types/timespan.d.ts.map +1 -1
- package/dist/types/type-code.d.ts +0 -0
- package/dist/types/type-code.d.ts.map +0 -0
- package/dist/types/type.d.ts +2 -0
- package/dist/types/type.d.ts.map +1 -1
- package/dist/types/types.d.ts +1 -1
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/utilities.d.ts +21 -3
- package/dist/types/utilities.d.ts.map +1 -1
- package/dist/web/array.js +21 -2
- package/dist/web/array.js.map +1 -1
- package/dist/web/async-pool-executor.js +26 -11
- package/dist/web/async-pool-executor.js.map +1 -1
- package/dist/web/browser/form-utilities.js +35 -34
- package/dist/web/browser/form-utilities.js.map +1 -1
- package/dist/web/browser/geo-location.js +1 -1
- package/dist/web/browser/geo-location.js.map +1 -1
- package/dist/web/browser/index.js +0 -0
- package/dist/web/browser/index.js.map +0 -0
- package/dist/web/browser/native-bridge.js +3 -1
- package/dist/web/browser/native-bridge.js.map +1 -1
- package/dist/web/cancellable-promise.js +34 -13
- package/dist/web/cancellable-promise.js.map +1 -1
- package/dist/web/clean-url-search-params.js +26 -3
- package/dist/web/clean-url-search-params.js.map +1 -1
- package/dist/web/collections/cache-dictionary.js +3 -3
- package/dist/web/collections/cache-dictionary.js.map +1 -1
- package/dist/web/collections/default-map.js +0 -0
- package/dist/web/collections/default-map.js.map +1 -1
- package/dist/web/collections/default-weak-map.js +0 -0
- package/dist/web/collections/default-weak-map.js.map +1 -1
- package/dist/web/collections/from.js +3 -3
- package/dist/web/collections/from.js.map +1 -1
- package/dist/web/collections/index.js +0 -0
- package/dist/web/collections/index.js.map +1 -1
- package/dist/web/draft.js +46 -11
- package/dist/web/draft.js.map +1 -1
- package/dist/web/event-bus.js +1 -1
- package/dist/web/event-bus.js.map +1 -1
- package/dist/web/functions.js +336 -0
- package/dist/web/functions.js.map +1 -0
- package/dist/web/index.js +1 -0
- package/dist/web/index.js.map +1 -1
- package/dist/web/key-value-pair.js +0 -0
- package/dist/web/key-value-pair.js.map +1 -1
- package/dist/web/lazy.js +0 -0
- package/dist/web/lazy.js.map +1 -1
- package/dist/web/math.js +3 -0
- package/dist/web/math.js.map +1 -1
- package/dist/web/order.js +0 -0
- package/dist/web/order.js.map +1 -1
- package/dist/web/parse.js +10 -1
- package/dist/web/parse.js.map +1 -1
- package/dist/web/patch.js +0 -0
- package/dist/web/patch.js.map +1 -1
- package/dist/web/range.js +0 -0
- package/dist/web/range.js.map +0 -0
- package/dist/web/stopwatch.js +0 -0
- package/dist/web/stopwatch.js.map +1 -1
- package/dist/web/string.js +8 -5
- package/dist/web/string.js.map +1 -1
- package/dist/web/timer.js +1 -1
- package/dist/web/timer.js.map +1 -1
- package/dist/web/timespan.js +21 -8
- package/dist/web/timespan.js.map +1 -1
- package/dist/web/type-code.js +0 -0
- package/dist/web/type-code.js.map +1 -1
- package/dist/web/type.js +20 -19
- package/dist/web/type.js.map +1 -1
- package/dist/web/types.js +0 -0
- package/dist/web/types.js.map +1 -1
- package/dist/web/utilities.js +174 -39
- package/dist/web/utilities.js.map +1 -1
- package/package.json +84 -60
- package/src/array.ts +116 -84
- package/src/async-pool-executor.ts +107 -85
- package/src/browser/form-utilities.ts +32 -31
- package/src/browser/geo-location.ts +113 -113
- package/src/browser/native-bridge.ts +118 -116
- package/src/cancellable-promise.ts +102 -76
- package/src/clean-url-search-params.ts +36 -11
- package/src/collections/cache-dictionary.ts +115 -115
- package/src/collections/default-map.ts +48 -48
- package/src/collections/default-weak-map.ts +51 -51
- package/src/collections/from.ts +859 -859
- package/src/collections/index.ts +3 -3
- package/src/draft.ts +55 -16
- package/src/event-bus.ts +83 -83
- package/src/functions.ts +496 -0
- package/src/index.ts +1 -0
- package/src/key-value-pair.ts +24 -24
- package/src/lazy.ts +32 -32
- package/src/math.ts +33 -30
- package/src/order.ts +5 -5
- package/src/parse.ts +251 -240
- package/src/patch.ts +79 -79
- package/src/stopwatch.ts +43 -43
- package/src/string.ts +8 -5
- package/src/timer.ts +67 -67
- package/src/timespan.ts +412 -398
- package/src/type-code.ts +15 -15
- package/src/type.ts +372 -371
- package/src/types.ts +34 -30
- 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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
if (
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
* parseBoolean(
|
|
212
|
-
* parseBoolean(
|
|
213
|
-
* parseBoolean(
|
|
214
|
-
*
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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
|
}
|