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/src/number/num.mts
CHANGED
|
@@ -41,10 +41,7 @@ export namespace Num {
|
|
|
41
41
|
* @returns The numeric representation of `n`.
|
|
42
42
|
* @example
|
|
43
43
|
* ```typescript
|
|
44
|
-
* Num.from('123'); // 123
|
|
45
44
|
* Num.from('123.45'); // 123.45
|
|
46
|
-
* Num.from(true); // 1
|
|
47
|
-
* Num.from(false); // 0
|
|
48
45
|
* Num.from('hello'); // NaN
|
|
49
46
|
* ```
|
|
50
47
|
*/
|
|
@@ -230,15 +227,6 @@ export namespace Num {
|
|
|
230
227
|
*
|
|
231
228
|
* @example
|
|
232
229
|
* ```typescript
|
|
233
|
-
* // Array index validation
|
|
234
|
-
* const isValidIndex = Num.isUintInRange(0, 10);
|
|
235
|
-
* const index: number = getUserInput();
|
|
236
|
-
*
|
|
237
|
-
* if (isValidIndex(index)) {
|
|
238
|
-
* // index is typed as 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
|
|
239
|
-
* const value = array[index]; // Safe array access
|
|
240
|
-
* }
|
|
241
|
-
*
|
|
242
230
|
* // Custom range validation
|
|
243
231
|
* const isValidPercentage = Num.isUintInRange(0, 101);
|
|
244
232
|
* if (isValidPercentage(value)) {
|
|
@@ -269,20 +257,12 @@ export namespace Num {
|
|
|
269
257
|
*
|
|
270
258
|
* @example
|
|
271
259
|
* ```typescript
|
|
272
|
-
* // Score validation (0-100)
|
|
273
260
|
* const isValidScore = Num.isUintInRangeInclusive(0, 100);
|
|
274
261
|
* const score: number = getTestScore();
|
|
275
|
-
*
|
|
276
262
|
* if (isValidScore(score)) {
|
|
277
263
|
* // score is typed as 0 | 1 | 2 | ... | 100
|
|
278
264
|
* const grade = calculateGrade(score);
|
|
279
265
|
* }
|
|
280
|
-
*
|
|
281
|
-
* // Day of month validation
|
|
282
|
-
* const isValidDay = Num.isUintInRangeInclusive(1, 31);
|
|
283
|
-
* if (isValidDay(day)) {
|
|
284
|
-
* // day is typed as 1 | 2 | ... | 31
|
|
285
|
-
* }
|
|
286
266
|
* ```
|
|
287
267
|
*/
|
|
288
268
|
export const isUintInRangeInclusive =
|
|
@@ -298,37 +278,14 @@ export namespace Num {
|
|
|
298
278
|
* - **Curried usage**: Pass bounds to get a reusable clamping function
|
|
299
279
|
*
|
|
300
280
|
* @example
|
|
301
|
-
* Direct usage:
|
|
302
281
|
* ```typescript
|
|
282
|
+
* // Direct usage
|
|
303
283
|
* Num.clamp(15, 0, 10); // 10 (clamped to upper bound)
|
|
304
|
-
* Num.clamp(-5, 0, 10); // 0 (clamped to lower bound)
|
|
305
284
|
* Num.clamp(5, 0, 10); // 5 (within bounds)
|
|
306
|
-
* Num.clamp(NaN, 0, 10); // 0 (invalid values default to lower bound)
|
|
307
|
-
* ```
|
|
308
285
|
*
|
|
309
|
-
*
|
|
310
|
-
* Curried usage for reusable functions:
|
|
311
|
-
* ```typescript
|
|
286
|
+
* // Curried usage
|
|
312
287
|
* const clampToPercent = Num.clamp(0, 100);
|
|
313
288
|
* clampToPercent(150); // 100
|
|
314
|
-
* clampToPercent(-10); // 0
|
|
315
|
-
* clampToPercent(75); // 75
|
|
316
|
-
*
|
|
317
|
-
* // Perfect for pipe composition
|
|
318
|
-
* const result = pipe(userInput)
|
|
319
|
-
* .map(Number)
|
|
320
|
-
* .map(clampToPercent).value;
|
|
321
|
-
* ```
|
|
322
|
-
*
|
|
323
|
-
* @example
|
|
324
|
-
* Working with arrays and functional programming:
|
|
325
|
-
* ```typescript
|
|
326
|
-
* const clampTo0_1 = Num.clamp(0, 1);
|
|
327
|
-
* const normalizedValues = values.map(clampTo0_1);
|
|
328
|
-
*
|
|
329
|
-
* // Temperature clamping
|
|
330
|
-
* const clampTemperature = Num.clamp(-40, 50);
|
|
331
|
-
* const safeTemperatures = readings.map(clampTemperature);
|
|
332
289
|
* ```
|
|
333
290
|
*/
|
|
334
291
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
@@ -373,11 +330,7 @@ export namespace Num {
|
|
|
373
330
|
*
|
|
374
331
|
* @example
|
|
375
332
|
* ```typescript
|
|
376
|
-
*
|
|
377
|
-
* const result1 = Num.div(10, 2); // 5
|
|
378
|
-
* const result2 = Num.div(7, 3); // 2.3333...
|
|
379
|
-
*
|
|
380
|
-
* // Compile-time error prevention
|
|
333
|
+
* const result = Num.div(10, 2); // 5
|
|
381
334
|
* // Num.div(10, 0); // ❌ TypeScript error: Type '0' is not assignable
|
|
382
335
|
*
|
|
383
336
|
* // With type guards
|
|
@@ -411,9 +364,6 @@ export namespace Num {
|
|
|
411
364
|
* ```typescript
|
|
412
365
|
* Num.divInt(10, 3); // 3
|
|
413
366
|
* Num.divInt(10, -3); // -4 (floor division)
|
|
414
|
-
* Num.divInt(-10, 3); // -4
|
|
415
|
-
* Num.divInt(10.7, 3.2); // 3 (floors both inputs first)
|
|
416
|
-
* Num.divInt(10, 0); // NaN
|
|
417
367
|
* ```
|
|
418
368
|
*/
|
|
419
369
|
export const divInt = (
|
|
@@ -434,10 +384,7 @@ export namespace Num {
|
|
|
434
384
|
* @example
|
|
435
385
|
* ```typescript
|
|
436
386
|
* Num.roundAt(3.14159, 2); // 3.14
|
|
437
|
-
* Num.roundAt(3.14159, 4); // 3.1416
|
|
438
387
|
* Num.roundAt(10.5, 0); // 11
|
|
439
|
-
* Num.roundAt(-10.5, 0); // -10
|
|
440
|
-
* Num.roundAt(0.005, 2); // 0.01
|
|
441
388
|
* ```
|
|
442
389
|
*/
|
|
443
390
|
export const roundAt = (
|
|
@@ -463,9 +410,6 @@ export namespace Num {
|
|
|
463
410
|
* ```typescript
|
|
464
411
|
* Num.roundToInt(3.2); // 3
|
|
465
412
|
* Num.roundToInt(3.5); // 4
|
|
466
|
-
* Num.roundToInt(3.8); // 4
|
|
467
|
-
* Num.roundToInt(-3.2); // -3
|
|
468
|
-
* Num.roundToInt(-3.8); // -3
|
|
469
413
|
* ```
|
|
470
414
|
*/
|
|
471
415
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
@@ -483,19 +427,9 @@ export namespace Num {
|
|
|
483
427
|
*
|
|
484
428
|
* @example
|
|
485
429
|
* ```typescript
|
|
486
|
-
* // Create specialized rounding functions
|
|
487
430
|
* const roundTo2 = Num.round(2);
|
|
488
|
-
* const roundTo4 = Num.round(4);
|
|
489
|
-
*
|
|
490
431
|
* roundTo2(3.14159); // 3.14
|
|
491
432
|
* roundTo2(2.71828); // 2.72
|
|
492
|
-
* roundTo2(10); // 10
|
|
493
|
-
*
|
|
494
|
-
* roundTo4(3.14159); // 3.1416
|
|
495
|
-
*
|
|
496
|
-
* // Use with array operations
|
|
497
|
-
* const values = [1.234, 5.678, 9.012];
|
|
498
|
-
* const rounded = values.map(roundTo2); // [1.23, 5.68, 9.01]
|
|
499
433
|
* ```
|
|
500
434
|
*/
|
|
501
435
|
export const round = (
|
|
@@ -519,20 +453,8 @@ export namespace Num {
|
|
|
519
453
|
*
|
|
520
454
|
* @example
|
|
521
455
|
* ```typescript
|
|
522
|
-
* Num.mapNaN2Undefined(42);
|
|
523
|
-
* Num.mapNaN2Undefined(
|
|
524
|
-
* Num.mapNaN2Undefined(NaN); // undefined
|
|
525
|
-
* Num.mapNaN2Undefined(Math.sqrt(-1)); // undefined
|
|
526
|
-
*
|
|
527
|
-
* // Useful in chains
|
|
528
|
-
* const result = Num.mapNaN2Undefined(parseFloat(userInput)) ?? 0;
|
|
529
|
-
*
|
|
530
|
-
* // Type narrowing
|
|
531
|
-
* const value = Math.sqrt(x);
|
|
532
|
-
* const safe = Num.mapNaN2Undefined(value);
|
|
533
|
-
* if (safe !== undefined) {
|
|
534
|
-
* // safe is typed without NaN
|
|
535
|
-
* }
|
|
456
|
+
* Num.mapNaN2Undefined(42); // 42
|
|
457
|
+
* Num.mapNaN2Undefined(NaN); // undefined
|
|
536
458
|
* ```
|
|
537
459
|
*/
|
|
538
460
|
export const mapNaN2Undefined = <N extends number>(
|
|
@@ -558,15 +480,6 @@ export namespace Num {
|
|
|
558
480
|
* ```typescript
|
|
559
481
|
* const zero = 0 as 0;
|
|
560
482
|
* const one = Num.increment(zero); // type is 1, value is 1
|
|
561
|
-
*
|
|
562
|
-
* const five = 5 as 5;
|
|
563
|
-
* const six = Num.increment(five); // type is 6, value is 6
|
|
564
|
-
*
|
|
565
|
-
* // Type-safe counter
|
|
566
|
-
* type Counter<N extends SmallUint> = {
|
|
567
|
-
* value: N;
|
|
568
|
-
* next(): Counter<Increment<N>>;
|
|
569
|
-
* };
|
|
570
483
|
* ```
|
|
571
484
|
*/
|
|
572
485
|
export const increment = <N extends SmallUint>(n: N): Increment<N> =>
|
|
@@ -588,16 +501,6 @@ export namespace Num {
|
|
|
588
501
|
* ```typescript
|
|
589
502
|
* const three = 3 as 3;
|
|
590
503
|
* const two = Num.decrement(three); // type is 2, value is 2
|
|
591
|
-
*
|
|
592
|
-
* const one = 1 as 1;
|
|
593
|
-
* const zero = Num.decrement(one); // type is 0, value is 0
|
|
594
|
-
*
|
|
595
|
-
* // Type-safe countdown
|
|
596
|
-
* function countdown<N extends PositiveSmallInt>(
|
|
597
|
-
* n: N
|
|
598
|
-
* ): N extends 1 ? 0 : Decrement<N> {
|
|
599
|
-
* return Num.decrement(n);
|
|
600
|
-
* }
|
|
601
504
|
* ```
|
|
602
505
|
*/
|
|
603
506
|
export const decrement = <N extends PositiveSmallInt>(n: N): Decrement<N> =>
|