ts-data-forge 1.5.0 → 1.5.1
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/array/array-utils.d.mts +4 -72
- package/dist/array/array-utils.d.mts.map +1 -1
- package/dist/array/array-utils.mjs +4 -72
- package/dist/array/array-utils.mjs.map +1 -1
- package/dist/functional/optional.d.mts +5 -146
- package/dist/functional/optional.d.mts.map +1 -1
- package/dist/functional/optional.mjs +5 -146
- package/dist/functional/optional.mjs.map +1 -1
- package/dist/functional/result.d.mts +0 -164
- package/dist/functional/result.d.mts.map +1 -1
- package/dist/functional/result.mjs +0 -164
- package/dist/functional/result.mjs.map +1 -1
- package/dist/guard/has-key.d.mts +1 -1
- package/dist/guard/has-key.mjs +1 -1
- package/dist/json/json.d.mts +14 -438
- package/dist/json/json.d.mts.map +1 -1
- package/dist/json/json.mjs +14 -438
- package/dist/json/json.mjs.map +1 -1
- package/dist/number/num.d.mts +5 -102
- package/dist/number/num.d.mts.map +1 -1
- package/dist/number/num.mjs +5 -102
- package/dist/number/num.mjs.map +1 -1
- package/package.json +1 -1
- package/src/array/array-utils.mts +4 -72
- package/src/functional/optional.mts +5 -146
- package/src/functional/result.mts +0 -164
- package/src/guard/has-key.mts +1 -1
- package/src/json/json.mts +14 -438
- package/src/number/num.mts +5 -102
package/dist/number/num.d.mts
CHANGED
|
@@ -39,10 +39,7 @@ export declare namespace Num {
|
|
|
39
39
|
* @returns The numeric representation of `n`.
|
|
40
40
|
* @example
|
|
41
41
|
* ```typescript
|
|
42
|
-
* Num.from('123'); // 123
|
|
43
42
|
* Num.from('123.45'); // 123.45
|
|
44
|
-
* Num.from(true); // 1
|
|
45
|
-
* Num.from(false); // 0
|
|
46
43
|
* Num.from('hello'); // NaN
|
|
47
44
|
* ```
|
|
48
45
|
*/
|
|
@@ -204,15 +201,6 @@ export declare namespace Num {
|
|
|
204
201
|
*
|
|
205
202
|
* @example
|
|
206
203
|
* ```typescript
|
|
207
|
-
* // Array index validation
|
|
208
|
-
* const isValidIndex = Num.isUintInRange(0, 10);
|
|
209
|
-
* const index: number = getUserInput();
|
|
210
|
-
*
|
|
211
|
-
* if (isValidIndex(index)) {
|
|
212
|
-
* // index is typed as 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
|
|
213
|
-
* const value = array[index]; // Safe array access
|
|
214
|
-
* }
|
|
215
|
-
*
|
|
216
204
|
* // Custom range validation
|
|
217
205
|
* const isValidPercentage = Num.isUintInRange(0, 101);
|
|
218
206
|
* if (isValidPercentage(value)) {
|
|
@@ -239,20 +227,12 @@ export declare namespace Num {
|
|
|
239
227
|
*
|
|
240
228
|
* @example
|
|
241
229
|
* ```typescript
|
|
242
|
-
* // Score validation (0-100)
|
|
243
230
|
* const isValidScore = Num.isUintInRangeInclusive(0, 100);
|
|
244
231
|
* const score: number = getTestScore();
|
|
245
|
-
*
|
|
246
232
|
* if (isValidScore(score)) {
|
|
247
233
|
* // score is typed as 0 | 1 | 2 | ... | 100
|
|
248
234
|
* const grade = calculateGrade(score);
|
|
249
235
|
* }
|
|
250
|
-
*
|
|
251
|
-
* // Day of month validation
|
|
252
|
-
* const isValidDay = Num.isUintInRangeInclusive(1, 31);
|
|
253
|
-
* if (isValidDay(day)) {
|
|
254
|
-
* // day is typed as 1 | 2 | ... | 31
|
|
255
|
-
* }
|
|
256
236
|
* ```
|
|
257
237
|
*/
|
|
258
238
|
export const isUintInRangeInclusive: <L extends SmallUint, U extends SmallUint>(lowerBound: L, upperBound: U) => (x: number) => x is RelaxedExclude<LEQ[U], LT[Min<L>]>;
|
|
@@ -264,37 +244,14 @@ export declare namespace Num {
|
|
|
264
244
|
* - **Curried usage**: Pass bounds to get a reusable clamping function
|
|
265
245
|
*
|
|
266
246
|
* @example
|
|
267
|
-
* Direct usage:
|
|
268
247
|
* ```typescript
|
|
248
|
+
* // Direct usage
|
|
269
249
|
* Num.clamp(15, 0, 10); // 10 (clamped to upper bound)
|
|
270
|
-
* Num.clamp(-5, 0, 10); // 0 (clamped to lower bound)
|
|
271
250
|
* Num.clamp(5, 0, 10); // 5 (within bounds)
|
|
272
|
-
* Num.clamp(NaN, 0, 10); // 0 (invalid values default to lower bound)
|
|
273
|
-
* ```
|
|
274
251
|
*
|
|
275
|
-
*
|
|
276
|
-
* Curried usage for reusable functions:
|
|
277
|
-
* ```typescript
|
|
252
|
+
* // Curried usage
|
|
278
253
|
* const clampToPercent = Num.clamp(0, 100);
|
|
279
254
|
* clampToPercent(150); // 100
|
|
280
|
-
* clampToPercent(-10); // 0
|
|
281
|
-
* clampToPercent(75); // 75
|
|
282
|
-
*
|
|
283
|
-
* // Perfect for pipe composition
|
|
284
|
-
* const result = pipe(userInput)
|
|
285
|
-
* .map(Number)
|
|
286
|
-
* .map(clampToPercent).value;
|
|
287
|
-
* ```
|
|
288
|
-
*
|
|
289
|
-
* @example
|
|
290
|
-
* Working with arrays and functional programming:
|
|
291
|
-
* ```typescript
|
|
292
|
-
* const clampTo0_1 = Num.clamp(0, 1);
|
|
293
|
-
* const normalizedValues = values.map(clampTo0_1);
|
|
294
|
-
*
|
|
295
|
-
* // Temperature clamping
|
|
296
|
-
* const clampTemperature = Num.clamp(-40, 50);
|
|
297
|
-
* const safeTemperatures = readings.map(clampTemperature);
|
|
298
255
|
* ```
|
|
299
256
|
*/
|
|
300
257
|
export const clamp: ClampFnOverload;
|
|
@@ -315,11 +272,7 @@ export declare namespace Num {
|
|
|
315
272
|
*
|
|
316
273
|
* @example
|
|
317
274
|
* ```typescript
|
|
318
|
-
*
|
|
319
|
-
* const result1 = Num.div(10, 2); // 5
|
|
320
|
-
* const result2 = Num.div(7, 3); // 2.3333...
|
|
321
|
-
*
|
|
322
|
-
* // Compile-time error prevention
|
|
275
|
+
* const result = Num.div(10, 2); // 5
|
|
323
276
|
* // Num.div(10, 0); // ❌ TypeScript error: Type '0' is not assignable
|
|
324
277
|
*
|
|
325
278
|
* // With type guards
|
|
@@ -351,9 +304,6 @@ export declare namespace Num {
|
|
|
351
304
|
* ```typescript
|
|
352
305
|
* Num.divInt(10, 3); // 3
|
|
353
306
|
* Num.divInt(10, -3); // -4 (floor division)
|
|
354
|
-
* Num.divInt(-10, 3); // -4
|
|
355
|
-
* Num.divInt(10.7, 3.2); // 3 (floors both inputs first)
|
|
356
|
-
* Num.divInt(10, 0); // NaN
|
|
357
307
|
* ```
|
|
358
308
|
*/
|
|
359
309
|
export const divInt: (a: number, b: NonZeroNumber | SmallInt<"!=0">) => number;
|
|
@@ -370,10 +320,7 @@ export declare namespace Num {
|
|
|
370
320
|
* @example
|
|
371
321
|
* ```typescript
|
|
372
322
|
* Num.roundAt(3.14159, 2); // 3.14
|
|
373
|
-
* Num.roundAt(3.14159, 4); // 3.1416
|
|
374
323
|
* Num.roundAt(10.5, 0); // 11
|
|
375
|
-
* Num.roundAt(-10.5, 0); // -10
|
|
376
|
-
* Num.roundAt(0.005, 2); // 0.01
|
|
377
324
|
* ```
|
|
378
325
|
*/
|
|
379
326
|
export const roundAt: (num: number, precision: PositiveSafeIntWithSmallInt) => number;
|
|
@@ -391,9 +338,6 @@ export declare namespace Num {
|
|
|
391
338
|
* ```typescript
|
|
392
339
|
* Num.roundToInt(3.2); // 3
|
|
393
340
|
* Num.roundToInt(3.5); // 4
|
|
394
|
-
* Num.roundToInt(3.8); // 4
|
|
395
|
-
* Num.roundToInt(-3.2); // -3
|
|
396
|
-
* Num.roundToInt(-3.8); // -3
|
|
397
341
|
* ```
|
|
398
342
|
*/
|
|
399
343
|
export const roundToInt: (num: number) => Int;
|
|
@@ -409,19 +353,9 @@ export declare namespace Num {
|
|
|
409
353
|
*
|
|
410
354
|
* @example
|
|
411
355
|
* ```typescript
|
|
412
|
-
* // Create specialized rounding functions
|
|
413
356
|
* const roundTo2 = Num.round(2);
|
|
414
|
-
* const roundTo4 = Num.round(4);
|
|
415
|
-
*
|
|
416
357
|
* roundTo2(3.14159); // 3.14
|
|
417
358
|
* roundTo2(2.71828); // 2.72
|
|
418
|
-
* roundTo2(10); // 10
|
|
419
|
-
*
|
|
420
|
-
* roundTo4(3.14159); // 3.1416
|
|
421
|
-
*
|
|
422
|
-
* // Use with array operations
|
|
423
|
-
* const values = [1.234, 5.678, 9.012];
|
|
424
|
-
* const rounded = values.map(roundTo2); // [1.23, 5.68, 9.01]
|
|
425
359
|
* ```
|
|
426
360
|
*/
|
|
427
361
|
export const round: (digit: PositiveSafeIntWithSmallInt) => ((num: number) => number);
|
|
@@ -438,20 +372,8 @@ export declare namespace Num {
|
|
|
438
372
|
*
|
|
439
373
|
* @example
|
|
440
374
|
* ```typescript
|
|
441
|
-
* Num.mapNaN2Undefined(42);
|
|
442
|
-
* Num.mapNaN2Undefined(
|
|
443
|
-
* Num.mapNaN2Undefined(NaN); // undefined
|
|
444
|
-
* Num.mapNaN2Undefined(Math.sqrt(-1)); // undefined
|
|
445
|
-
*
|
|
446
|
-
* // Useful in chains
|
|
447
|
-
* const result = Num.mapNaN2Undefined(parseFloat(userInput)) ?? 0;
|
|
448
|
-
*
|
|
449
|
-
* // Type narrowing
|
|
450
|
-
* const value = Math.sqrt(x);
|
|
451
|
-
* const safe = Num.mapNaN2Undefined(value);
|
|
452
|
-
* if (safe !== undefined) {
|
|
453
|
-
* // safe is typed without NaN
|
|
454
|
-
* }
|
|
375
|
+
* Num.mapNaN2Undefined(42); // 42
|
|
376
|
+
* Num.mapNaN2Undefined(NaN); // undefined
|
|
455
377
|
* ```
|
|
456
378
|
*/
|
|
457
379
|
export const mapNaN2Undefined: <N extends number>(num: N) => RelaxedExclude<N, NaNType> | undefined;
|
|
@@ -470,15 +392,6 @@ export declare namespace Num {
|
|
|
470
392
|
* ```typescript
|
|
471
393
|
* const zero = 0 as 0;
|
|
472
394
|
* const one = Num.increment(zero); // type is 1, value is 1
|
|
473
|
-
*
|
|
474
|
-
* const five = 5 as 5;
|
|
475
|
-
* const six = Num.increment(five); // type is 6, value is 6
|
|
476
|
-
*
|
|
477
|
-
* // Type-safe counter
|
|
478
|
-
* type Counter<N extends SmallUint> = {
|
|
479
|
-
* value: N;
|
|
480
|
-
* next(): Counter<Increment<N>>;
|
|
481
|
-
* };
|
|
482
395
|
* ```
|
|
483
396
|
*/
|
|
484
397
|
export const increment: <N extends SmallUint>(n: N) => Increment<N>;
|
|
@@ -497,16 +410,6 @@ export declare namespace Num {
|
|
|
497
410
|
* ```typescript
|
|
498
411
|
* const three = 3 as 3;
|
|
499
412
|
* const two = Num.decrement(three); // type is 2, value is 2
|
|
500
|
-
*
|
|
501
|
-
* const one = 1 as 1;
|
|
502
|
-
* const zero = Num.decrement(one); // type is 0, value is 0
|
|
503
|
-
*
|
|
504
|
-
* // Type-safe countdown
|
|
505
|
-
* function countdown<N extends PositiveSmallInt>(
|
|
506
|
-
* n: N
|
|
507
|
-
* ): N extends 1 ? 0 : Decrement<N> {
|
|
508
|
-
* return Num.decrement(n);
|
|
509
|
-
* }
|
|
510
413
|
* ```
|
|
511
414
|
*/
|
|
512
415
|
export const decrement: <N extends PositiveSmallInt>(n: N) => Decrement<N>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"num.d.mts","sourceRoot":"","sources":["../../src/number/num.mts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,yBAAiB,GAAG,CAAC;IACnB
|
|
1
|
+
{"version":3,"file":"num.d.mts","sourceRoot":"","sources":["../../src/number/num.mts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,yBAAiB,GAAG,CAAC;IACnB;;;;;;;;;OASG;IACH,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,EAAE,OAAO,KAAK,MAAe,CAAC;IAEnD;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,MAAM,CAAC,MAAM,SAAS,GAAI,CAAC,SAAS,MAAM,EACxC,KAAK,CAAC,KACL,GAAG,IAAI,aAAa,GAAG,cAAc,C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GAAG,EAAE,MAAM,KAAK,MAAM,CAI1B,CAAC;IAEF;;;;;;;;;;;;;;;;OAgBG;IACH,MAAM,CAAC,MAAM,gBAAgB,GAAI,CAAC,SAAS,MAAM,EAC/C,KAAK,CAAC,KACL,cAAc,CAAC,CAAC,EAAE,OAAO,CAAC,GAAG,SAIS,CAAC;IAE1C;;;;;;;;;;;;;;;;OAgBG;IACH,MAAM,CAAC,MAAM,SAAS,GAAI,CAAC,SAAS,SAAS,EAAE,GAAG,CAAC,KAAG,SAAS,CAAC,CAAC,CAExC,CAAC;IAE1B;;;;;;;;;;;;;;;;OAgBG;IACH,MAAM,CAAC,MAAM,SAAS,GAAI,CAAC,SAAS,gBAAgB,EAAE,GAAG,CAAC,KAAG,SAAS,CAAC,CAAC,CAE/C,CAAC;;CAC3B"}
|
package/dist/number/num.mjs
CHANGED
|
@@ -40,10 +40,7 @@ var Num;
|
|
|
40
40
|
* @returns The numeric representation of `n`.
|
|
41
41
|
* @example
|
|
42
42
|
* ```typescript
|
|
43
|
-
* Num.from('123'); // 123
|
|
44
43
|
* Num.from('123.45'); // 123.45
|
|
45
|
-
* Num.from(true); // 1
|
|
46
|
-
* Num.from(false); // 0
|
|
47
44
|
* Num.from('hello'); // NaN
|
|
48
45
|
* ```
|
|
49
46
|
*/
|
|
@@ -177,15 +174,6 @@ var Num;
|
|
|
177
174
|
*
|
|
178
175
|
* @example
|
|
179
176
|
* ```typescript
|
|
180
|
-
* // Array index validation
|
|
181
|
-
* const isValidIndex = Num.isUintInRange(0, 10);
|
|
182
|
-
* const index: number = getUserInput();
|
|
183
|
-
*
|
|
184
|
-
* if (isValidIndex(index)) {
|
|
185
|
-
* // index is typed as 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
|
|
186
|
-
* const value = array[index]; // Safe array access
|
|
187
|
-
* }
|
|
188
|
-
*
|
|
189
177
|
* // Custom range validation
|
|
190
178
|
* const isValidPercentage = Num.isUintInRange(0, 101);
|
|
191
179
|
* if (isValidPercentage(value)) {
|
|
@@ -212,20 +200,12 @@ var Num;
|
|
|
212
200
|
*
|
|
213
201
|
* @example
|
|
214
202
|
* ```typescript
|
|
215
|
-
* // Score validation (0-100)
|
|
216
203
|
* const isValidScore = Num.isUintInRangeInclusive(0, 100);
|
|
217
204
|
* const score: number = getTestScore();
|
|
218
|
-
*
|
|
219
205
|
* if (isValidScore(score)) {
|
|
220
206
|
* // score is typed as 0 | 1 | 2 | ... | 100
|
|
221
207
|
* const grade = calculateGrade(score);
|
|
222
208
|
* }
|
|
223
|
-
*
|
|
224
|
-
* // Day of month validation
|
|
225
|
-
* const isValidDay = Num.isUintInRangeInclusive(1, 31);
|
|
226
|
-
* if (isValidDay(day)) {
|
|
227
|
-
* // day is typed as 1 | 2 | ... | 31
|
|
228
|
-
* }
|
|
229
209
|
* ```
|
|
230
210
|
*/
|
|
231
211
|
Num.isUintInRangeInclusive = (lowerBound, upperBound) => (x) => Number.isSafeInteger(x) && lowerBound <= x && x <= upperBound;
|
|
@@ -237,37 +217,14 @@ var Num;
|
|
|
237
217
|
* - **Curried usage**: Pass bounds to get a reusable clamping function
|
|
238
218
|
*
|
|
239
219
|
* @example
|
|
240
|
-
* Direct usage:
|
|
241
220
|
* ```typescript
|
|
221
|
+
* // Direct usage
|
|
242
222
|
* Num.clamp(15, 0, 10); // 10 (clamped to upper bound)
|
|
243
|
-
* Num.clamp(-5, 0, 10); // 0 (clamped to lower bound)
|
|
244
223
|
* Num.clamp(5, 0, 10); // 5 (within bounds)
|
|
245
|
-
* Num.clamp(NaN, 0, 10); // 0 (invalid values default to lower bound)
|
|
246
|
-
* ```
|
|
247
224
|
*
|
|
248
|
-
*
|
|
249
|
-
* Curried usage for reusable functions:
|
|
250
|
-
* ```typescript
|
|
225
|
+
* // Curried usage
|
|
251
226
|
* const clampToPercent = Num.clamp(0, 100);
|
|
252
227
|
* clampToPercent(150); // 100
|
|
253
|
-
* clampToPercent(-10); // 0
|
|
254
|
-
* clampToPercent(75); // 75
|
|
255
|
-
*
|
|
256
|
-
* // Perfect for pipe composition
|
|
257
|
-
* const result = pipe(userInput)
|
|
258
|
-
* .map(Number)
|
|
259
|
-
* .map(clampToPercent).value;
|
|
260
|
-
* ```
|
|
261
|
-
*
|
|
262
|
-
* @example
|
|
263
|
-
* Working with arrays and functional programming:
|
|
264
|
-
* ```typescript
|
|
265
|
-
* const clampTo0_1 = Num.clamp(0, 1);
|
|
266
|
-
* const normalizedValues = values.map(clampTo0_1);
|
|
267
|
-
*
|
|
268
|
-
* // Temperature clamping
|
|
269
|
-
* const clampTemperature = Num.clamp(-40, 50);
|
|
270
|
-
* const safeTemperatures = readings.map(clampTemperature);
|
|
271
228
|
* ```
|
|
272
229
|
*/
|
|
273
230
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
@@ -298,11 +255,7 @@ var Num;
|
|
|
298
255
|
*
|
|
299
256
|
* @example
|
|
300
257
|
* ```typescript
|
|
301
|
-
*
|
|
302
|
-
* const result1 = Num.div(10, 2); // 5
|
|
303
|
-
* const result2 = Num.div(7, 3); // 2.3333...
|
|
304
|
-
*
|
|
305
|
-
* // Compile-time error prevention
|
|
258
|
+
* const result = Num.div(10, 2); // 5
|
|
306
259
|
* // Num.div(10, 0); // ❌ TypeScript error: Type '0' is not assignable
|
|
307
260
|
*
|
|
308
261
|
* // With type guards
|
|
@@ -334,9 +287,6 @@ var Num;
|
|
|
334
287
|
* ```typescript
|
|
335
288
|
* Num.divInt(10, 3); // 3
|
|
336
289
|
* Num.divInt(10, -3); // -4 (floor division)
|
|
337
|
-
* Num.divInt(-10, 3); // -4
|
|
338
|
-
* Num.divInt(10.7, 3.2); // 3 (floors both inputs first)
|
|
339
|
-
* Num.divInt(10, 0); // NaN
|
|
340
290
|
* ```
|
|
341
291
|
*/
|
|
342
292
|
Num.divInt = (a, b) => Math.floor(Math.floor(a) / Math.floor(b));
|
|
@@ -353,10 +303,7 @@ var Num;
|
|
|
353
303
|
* @example
|
|
354
304
|
* ```typescript
|
|
355
305
|
* Num.roundAt(3.14159, 2); // 3.14
|
|
356
|
-
* Num.roundAt(3.14159, 4); // 3.1416
|
|
357
306
|
* Num.roundAt(10.5, 0); // 11
|
|
358
|
-
* Num.roundAt(-10.5, 0); // -10
|
|
359
|
-
* Num.roundAt(0.005, 2); // 0.01
|
|
360
307
|
* ```
|
|
361
308
|
*/
|
|
362
309
|
Num.roundAt = (num, precision) => {
|
|
@@ -377,9 +324,6 @@ var Num;
|
|
|
377
324
|
* ```typescript
|
|
378
325
|
* Num.roundToInt(3.2); // 3
|
|
379
326
|
* Num.roundToInt(3.5); // 4
|
|
380
|
-
* Num.roundToInt(3.8); // 4
|
|
381
|
-
* Num.roundToInt(-3.2); // -3
|
|
382
|
-
* Num.roundToInt(-3.8); // -3
|
|
383
327
|
* ```
|
|
384
328
|
*/
|
|
385
329
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
@@ -396,19 +340,9 @@ var Num;
|
|
|
396
340
|
*
|
|
397
341
|
* @example
|
|
398
342
|
* ```typescript
|
|
399
|
-
* // Create specialized rounding functions
|
|
400
343
|
* const roundTo2 = Num.round(2);
|
|
401
|
-
* const roundTo4 = Num.round(4);
|
|
402
|
-
*
|
|
403
344
|
* roundTo2(3.14159); // 3.14
|
|
404
345
|
* roundTo2(2.71828); // 2.72
|
|
405
|
-
* roundTo2(10); // 10
|
|
406
|
-
*
|
|
407
|
-
* roundTo4(3.14159); // 3.1416
|
|
408
|
-
*
|
|
409
|
-
* // Use with array operations
|
|
410
|
-
* const values = [1.234, 5.678, 9.012];
|
|
411
|
-
* const rounded = values.map(roundTo2); // [1.23, 5.68, 9.01]
|
|
412
346
|
* ```
|
|
413
347
|
*/
|
|
414
348
|
Num.round = (digit) => {
|
|
@@ -428,20 +362,8 @@ var Num;
|
|
|
428
362
|
*
|
|
429
363
|
* @example
|
|
430
364
|
* ```typescript
|
|
431
|
-
* Num.mapNaN2Undefined(42);
|
|
432
|
-
* Num.mapNaN2Undefined(
|
|
433
|
-
* Num.mapNaN2Undefined(NaN); // undefined
|
|
434
|
-
* Num.mapNaN2Undefined(Math.sqrt(-1)); // undefined
|
|
435
|
-
*
|
|
436
|
-
* // Useful in chains
|
|
437
|
-
* const result = Num.mapNaN2Undefined(parseFloat(userInput)) ?? 0;
|
|
438
|
-
*
|
|
439
|
-
* // Type narrowing
|
|
440
|
-
* const value = Math.sqrt(x);
|
|
441
|
-
* const safe = Num.mapNaN2Undefined(value);
|
|
442
|
-
* if (safe !== undefined) {
|
|
443
|
-
* // safe is typed without NaN
|
|
444
|
-
* }
|
|
365
|
+
* Num.mapNaN2Undefined(42); // 42
|
|
366
|
+
* Num.mapNaN2Undefined(NaN); // undefined
|
|
445
367
|
* ```
|
|
446
368
|
*/
|
|
447
369
|
Num.mapNaN2Undefined = (num) => Number.isNaN(num)
|
|
@@ -463,15 +385,6 @@ var Num;
|
|
|
463
385
|
* ```typescript
|
|
464
386
|
* const zero = 0 as 0;
|
|
465
387
|
* const one = Num.increment(zero); // type is 1, value is 1
|
|
466
|
-
*
|
|
467
|
-
* const five = 5 as 5;
|
|
468
|
-
* const six = Num.increment(five); // type is 6, value is 6
|
|
469
|
-
*
|
|
470
|
-
* // Type-safe counter
|
|
471
|
-
* type Counter<N extends SmallUint> = {
|
|
472
|
-
* value: N;
|
|
473
|
-
* next(): Counter<Increment<N>>;
|
|
474
|
-
* };
|
|
475
388
|
* ```
|
|
476
389
|
*/
|
|
477
390
|
Num.increment = (n) =>
|
|
@@ -492,16 +405,6 @@ var Num;
|
|
|
492
405
|
* ```typescript
|
|
493
406
|
* const three = 3 as 3;
|
|
494
407
|
* const two = Num.decrement(three); // type is 2, value is 2
|
|
495
|
-
*
|
|
496
|
-
* const one = 1 as 1;
|
|
497
|
-
* const zero = Num.decrement(one); // type is 0, value is 0
|
|
498
|
-
*
|
|
499
|
-
* // Type-safe countdown
|
|
500
|
-
* function countdown<N extends PositiveSmallInt>(
|
|
501
|
-
* n: N
|
|
502
|
-
* ): N extends 1 ? 0 : Decrement<N> {
|
|
503
|
-
* return Num.decrement(n);
|
|
504
|
-
* }
|
|
505
408
|
* ```
|
|
506
409
|
*/
|
|
507
410
|
Num.decrement = (n) =>
|
package/dist/number/num.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"num.mjs","sources":["../../src/number/num.mts"],"sourcesContent":[null],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCG;IACc;AAAjB,CAAA,UAAiB,GAAG,EAAA;AAClB
|
|
1
|
+
{"version":3,"file":"num.mjs","sources":["../../src/number/num.mts"],"sourcesContent":[null],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCG;IACc;AAAjB,CAAA,UAAiB,GAAG,EAAA;AAClB;;;;;;;;;AASG;IACU,GAAA,CAAA,IAAI,GAA2B,MAAM;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;IACU,GAAA,CAAA,SAAS,GAAG,CACvB,GAAM,KAC0C,GAAG,KAAK,CAAC;AAI3D;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;IACU,GAAA,CAAA,aAAa,GAAG,CAC3B,GAAM,KAEN,GAAG,IAAI,CAAC;AAEV;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;IACU,GAAA,CAAA,UAAU,GAAG,CACxB,GAAM,KAEN,GAAG,GAAG,CAAC;AAET;;;;;;;;;;;;;AAaG;IACU,GAAA,CAAA,SAAS,GACpB,CAAC,UAAkB,EAAE,UAAkB,KACvC,CAAC,CAAS,KACR,UAAU,IAAI,CAAC,IAAI,CAAC,GAAG,UAAU;AAErC;;;;;;;;;;;;;AAaG;IACU,GAAA,CAAA,kBAAkB,GAC7B,CAAC,UAAkB,EAAE,UAAkB,KACvC,CAAC,CAAS,KACR,UAAU,IAAI,CAAC,IAAI,CAAC,IAAI,UAAU;AAgCtC;;;;;;;;;;;;;;;;;;;;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|
* ```typescript
|
|
2115
|
-
* // Basic element finding
|
|
2116
2115
|
* const numbers = [1, 2, 3, 4, 5];
|
|
2117
2116
|
* const firstEven = Arr.find(numbers, x => x % 2 === 0);
|
|
2118
2117
|
* if (Optional.isSome(firstEven)) {
|
|
2119
|
-
* console.log(firstEven.value); // 2
|
|
2118
|
+
* console.log(firstEven.value); // 2
|
|
2120
2119
|
* }
|
|
2121
2120
|
*
|
|
2122
|
-
*
|
|
2123
|
-
* const findLargeAtEnd = Arr.find(numbers, (value, index) => value > 3 && index > 2);
|
|
2124
|
-
* // Optional.Some(4) - first number > 3 after index 2
|
|
2125
|
-
*
|
|
2126
|
-
* // Finding objects by property
|
|
2127
|
-
* const users = [
|
|
2128
|
-
* { id: 1, name: 'Alice', age: 25 },
|
|
2129
|
-
* { id: 2, name: 'Bob', age: 30 },
|
|
2130
|
-
* { id: 3, name: 'Charlie', age: 35 }
|
|
2131
|
-
* ];
|
|
2132
|
-
*
|
|
2121
|
+
* const users = [{ id: 1, name: 'Alice', age: 25 }, { id: 2, name: 'Bob', age: 30 }];
|
|
2133
2122
|
* const adult = Arr.find(users, user => user.age >= 30);
|
|
2134
2123
|
* // Optional.Some({ id: 2, name: 'Bob', age: 30 })
|
|
2135
|
-
*
|
|
2136
|
-
* const nonExistent = Arr.find(users, user => user.age > 100);
|
|
2137
|
-
* // Optional.None
|
|
2138
|
-
*
|
|
2139
|
-
* // Empty array handling
|
|
2140
|
-
* const emptyResult = Arr.find([], x => x > 0); // Optional.None
|
|
2141
|
-
*
|
|
2142
|
-
* // Curried usage for functional composition
|
|
2143
|
-
* const findNegative = Arr.find((x: number) => x < 0);
|
|
2144
|
-
* const findLongString = Arr.find((s: string) => s.length > 5);
|
|
2145
|
-
*
|
|
2146
|
-
* const datasets = [
|
|
2147
|
-
* [1, 2, -3, 4],
|
|
2148
|
-
* [5, 6, 7, 8],
|
|
2149
|
-
* [-1, 0, 1]
|
|
2150
|
-
* ];
|
|
2151
|
-
*
|
|
2152
|
-
* const negativeNumbers = datasets.map(findNegative);
|
|
2153
|
-
* // [Optional.Some(-3), Optional.None, Optional.Some(-1)]
|
|
2154
|
-
*
|
|
2155
|
-
* // Pipe composition
|
|
2156
|
-
* const result = pipe(['short', 'medium', 'very long string'])
|
|
2157
|
-
* .map(findLongString)
|
|
2158
|
-
* .map(opt => Optional.unwrapOr(opt, 'default'))
|
|
2159
|
-
* .value; // 'very long string'
|
|
2160
|
-
*
|
|
2161
|
-
* // Complex predicate with all parameters
|
|
2162
|
-
* const findSpecial = (arr: readonly number[]) =>
|
|
2163
|
-
* Arr.find(arr, (value, index, array) => {
|
|
2164
|
-
* return value > 10 && index > 0 && index < array.length - 1;
|
|
2165
|
-
* });
|
|
2166
|
-
*
|
|
2167
|
-
* const hasMiddleSpecial = findSpecial([5, 15, 20, 3]);
|
|
2168
|
-
* // Optional.Some(15) - first value > 10 that's not at start or end
|
|
2169
|
-
*
|
|
2170
|
-
* // Safe unwrapping patterns
|
|
2171
|
-
* const maybeFound = Arr.find(numbers, x => x > 10);
|
|
2172
|
-
* const foundOrDefault = Optional.unwrapOr(maybeFound, 0); // 0 (not found)
|
|
2173
|
-
* const foundOrThrow = Optional.isSome(maybeFound)
|
|
2174
|
-
* ? maybeFound.value
|
|
2175
|
-
* : (() => { throw new Error('Not found'); })();
|
|
2176
|
-
*
|
|
2177
|
-
* // Type inference examples
|
|
2178
|
-
* expectType<typeof firstEven, Optional<number>>('=');
|
|
2179
|
-
* expectType<typeof adult, Optional<{id: number, name: string, age: number}>>('=');
|
|
2180
|
-
* expectType<typeof findNegative, (array: readonly number[]) => Optional<number>>('=');
|
|
2181
2124
|
* ```
|
|
2182
2125
|
*
|
|
2183
2126
|
* @see {@link findIndex} for finding the index instead of the element
|
|
@@ -2564,13 +2507,8 @@ export namespace Arr {
|
|
|
2564
2507
|
* @returns The single value that results from the reduction.
|
|
2565
2508
|
* @example
|
|
2566
2509
|
* ```ts
|
|
2567
|
-
* // Regular usage
|
|
2568
2510
|
* Arr.foldl([1, 2, 3], (sum, n) => sum + n, 0); // 6
|
|
2569
|
-
*
|
|
2570
|
-
* // Curried usage for pipe composition
|
|
2571
|
-
* const sumWithZero = Arr.foldl((sum: number, n: number) => sum + n, 0);
|
|
2572
|
-
* const result = pipe([1, 2, 3, 4]).map(sumWithZero).value;
|
|
2573
|
-
* console.log(result); // 10
|
|
2511
|
+
* Arr.foldl(['a', 'b', 'c'], (acc, str) => acc + str.toUpperCase(), ''); // 'ABC'
|
|
2574
2512
|
* ```
|
|
2575
2513
|
*/
|
|
2576
2514
|
export const foldl: FoldlFnOverload = <E, P>(
|
|
@@ -3245,13 +3183,8 @@ export namespace Arr {
|
|
|
3245
3183
|
* @returns An array of arrays, where each inner array has up to `chunkSize` elements.
|
|
3246
3184
|
* @example
|
|
3247
3185
|
* ```ts
|
|
3248
|
-
* // Regular usage
|
|
3249
3186
|
* Arr.partition([1, 2, 3, 4, 5, 6], 2); // [[1, 2], [3, 4], [5, 6]]
|
|
3250
|
-
*
|
|
3251
|
-
* // Curried usage for pipe composition
|
|
3252
|
-
* const chunkBy3 = Arr.partition(3);
|
|
3253
|
-
* const result = pipe([1, 2, 3, 4, 5, 6, 7]).map(chunkBy3).value;
|
|
3254
|
-
* console.log(result); // [[1, 2, 3], [4, 5, 6], [7]]
|
|
3187
|
+
* Arr.partition([1, 2, 3, 4, 5, 6, 7], 3); // [[1, 2, 3], [4, 5, 6], [7]]
|
|
3255
3188
|
* ```
|
|
3256
3189
|
*/
|
|
3257
3190
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
@@ -3762,7 +3695,6 @@ export namespace Arr {
|
|
|
3762
3695
|
* @example
|
|
3763
3696
|
* ```ts
|
|
3764
3697
|
* Arr.uniq([1, 2, 2, 3, 1, 4]); // [1, 2, 3, 4]
|
|
3765
|
-
* Arr.uniq(['a', 'b', 'a']); // ['a', 'b']
|
|
3766
3698
|
* ```
|
|
3767
3699
|
*/
|
|
3768
3700
|
export const uniq = <P extends Primitive>(
|