full-utils 2.0.4 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.cjs +1 -2
- package/dist/browser.cjs.map +1 -1
- package/dist/browser.d.cts +1 -1
- package/dist/browser.d.ts +1 -1
- package/dist/browser.mjs +1 -2
- package/dist/browser.mjs.map +1 -1
- package/dist/index.cjs +1 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +171 -1414
- package/dist/index.d.ts +171 -1414
- package/dist/index.mjs +1 -2
- package/dist/index.mjs.map +1 -1
- package/dist/node.cjs +1 -2
- package/dist/node.cjs.map +1 -1
- package/dist/node.d.cts +1 -1
- package/dist/node.d.ts +1 -1
- package/dist/node.mjs +1 -2
- package/dist/node.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -27,9 +27,9 @@
|
|
|
27
27
|
* - **Macro / call marker**: If a token starts with `$`, contains `(`, and ends
|
|
28
28
|
* with `)`, it is returned unchanged (e.g., `"$env(PATH)"`).
|
|
29
29
|
* - **Literals**: `null` → `null`, `undefined` → `undefined`.
|
|
30
|
-
* - **Booleans**: Using {@link isStrBool} + {@link
|
|
30
|
+
* - **Booleans**: Using {@link isStrBool} + {@link boolNormalize} (e.g., `"true"`,
|
|
31
31
|
* `"False"`, `"yes"`, `"0"` depending on your implementation).
|
|
32
|
-
* - **Numbers**: Using {@link
|
|
32
|
+
* - **Numbers**: Using {@link numNormalize}. If the result is finite, it is returned.
|
|
33
33
|
* Explicit `"Infinity"` and `"-Infinity"` are also supported.
|
|
34
34
|
* - **Quoted strings**: `'text'` or `"text"` → inner text with escapes processed
|
|
35
35
|
* (`\\` → `\`, `\'` → `'`, `\"` → `"`).
|
|
@@ -54,44 +54,44 @@
|
|
|
54
54
|
* do so in a sandbox with explicit allow-lists.
|
|
55
55
|
*
|
|
56
56
|
* ### Limitations
|
|
57
|
-
* - Numerical parsing relies on {@link
|
|
57
|
+
* - Numerical parsing relies on {@link numNormalize}. Extremely large or high-precision
|
|
58
58
|
* decimals may still be subject to JavaScript `number` precision limits unless your
|
|
59
|
-
* `
|
|
59
|
+
* `numNormalize` converts to a safer representation.
|
|
60
60
|
* - Only basic backslash escapes are handled in quoted strings (no `\uXXXX` decoding here).
|
|
61
61
|
* - Whitespace outside quotes is trimmed from each token; internal whitespace is preserved.
|
|
62
62
|
*
|
|
63
63
|
* @example
|
|
64
64
|
* // Basic values
|
|
65
|
-
*
|
|
65
|
+
* arrFuncArgs('1, true, "hello"'); // => [1, true, "hello"]
|
|
66
66
|
*
|
|
67
67
|
* @example
|
|
68
68
|
* // Bracket-wrapped list
|
|
69
|
-
*
|
|
69
|
+
* arrFuncArgs('[1, 2, 3]'); // => [1, 2, 3]
|
|
70
70
|
*
|
|
71
71
|
* @example
|
|
72
72
|
* // Nested structures and quoting
|
|
73
|
-
*
|
|
73
|
+
* arrFuncArgs('{"a":1,"b":[2,3]}, "te,xt", (x,y)'); // => [ {a:1,b:[2,3]}, "te,xt", "(x,y)" ]
|
|
74
74
|
*
|
|
75
75
|
* @example
|
|
76
76
|
* // Booleans, null/undefined, and Infinity
|
|
77
|
-
*
|
|
77
|
+
* arrFuncArgs('yes, NO, null, undefined, Infinity, -Infinity');
|
|
78
78
|
* // => [true, false, null, undefined, Infinity, -Infinity]
|
|
79
79
|
*
|
|
80
80
|
* @example
|
|
81
81
|
* // Macro-like token (returned as-is)
|
|
82
|
-
*
|
|
82
|
+
* arrFuncArgs('$env(PATH)'); // => ["$env(PATH)"]
|
|
83
83
|
*
|
|
84
84
|
* @example
|
|
85
85
|
* // Escapes inside quotes
|
|
86
|
-
*
|
|
86
|
+
* arrFuncArgs('"He said: \\"Hi\\"", \'It\\\'s ok\', "\\\\path"');
|
|
87
87
|
* // => ['He said: "Hi"', "It's ok", "\\path"]
|
|
88
88
|
*
|
|
89
89
|
* @example
|
|
90
90
|
* // Empty and whitespace inputs
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
91
|
+
* arrFuncArgs(' '); // => []
|
|
92
|
+
* arrFuncArgs('[]'); // => []
|
|
93
|
+
* arrFuncArgs('[ ]'); // => []
|
|
94
|
+
* arrFuncArgs(' [ a , b ] '); // => ["a", "b"]
|
|
95
95
|
*
|
|
96
96
|
* @param value - Raw string containing comma-separated arguments.
|
|
97
97
|
* If `value` is **not** a string, the function returns `[value]` unchanged.
|
|
@@ -99,14 +99,14 @@
|
|
|
99
99
|
* @returns An array of coerced values (`unknown[]`). Each item is one parsed token.
|
|
100
100
|
*
|
|
101
101
|
* @see isStrBool
|
|
102
|
-
* @see
|
|
103
|
-
* @see
|
|
102
|
+
* @see boolNormalize
|
|
103
|
+
* @see numNormalize
|
|
104
104
|
* @see jsonDecode
|
|
105
105
|
*
|
|
106
106
|
* @public
|
|
107
107
|
* @since 2.0.0
|
|
108
108
|
*/
|
|
109
|
-
declare function
|
|
109
|
+
declare function arrFuncArgs(value: string): unknown[];
|
|
110
110
|
|
|
111
111
|
/**
|
|
112
112
|
* Splits an input array into smaller subarrays (portions) of a given fixed size.
|
|
@@ -145,31 +145,31 @@ declare function formatStrToFuncArgsArr(value: string): unknown[];
|
|
|
145
145
|
*
|
|
146
146
|
* @example
|
|
147
147
|
* // Split an array into groups of 3
|
|
148
|
-
*
|
|
148
|
+
* arrSplitPortions([1, 2, 3, 4, 5, 6, 7], 3);
|
|
149
149
|
* // => [[1, 2, 3], [4, 5, 6], [7]]
|
|
150
150
|
*
|
|
151
151
|
* @example
|
|
152
152
|
* // Portion length larger than array
|
|
153
|
-
*
|
|
153
|
+
* arrSplitPortions(['a', 'b'], 5);
|
|
154
154
|
* // => [['a', 'b']]
|
|
155
155
|
*
|
|
156
156
|
* @example
|
|
157
157
|
* // Non-integer or invalid sizes
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
158
|
+
* arrSplitPortions([1, 2, 3], 0); // => []
|
|
159
|
+
* arrSplitPortions([1, 2, 3], -2); // => []
|
|
160
|
+
* arrSplitPortions([1, 2, 3], NaN); // => []
|
|
161
161
|
*
|
|
162
162
|
* @example
|
|
163
163
|
* // Works with readonly arrays
|
|
164
164
|
* const input = [10, 20, 30, 40] as const;
|
|
165
|
-
* const result =
|
|
165
|
+
* const result = arrSplitPortions(input, 2);
|
|
166
166
|
* // result: [[10, 20], [30, 40]]
|
|
167
167
|
*
|
|
168
168
|
* @category Array
|
|
169
169
|
* @public
|
|
170
170
|
* @since 2.0.0
|
|
171
171
|
*/
|
|
172
|
-
declare function
|
|
172
|
+
declare function arrSplitPortions<T>(arr: readonly T[], portionLength: number): T[][];
|
|
173
173
|
|
|
174
174
|
/**
|
|
175
175
|
* Converts any given value into a normalized boolean (`true` / `false`).
|
|
@@ -228,31 +228,31 @@ declare function splitArrToPortions<T>(arr: readonly T[], portionLength: number)
|
|
|
228
228
|
*
|
|
229
229
|
* @example
|
|
230
230
|
* // Native booleans
|
|
231
|
-
*
|
|
232
|
-
*
|
|
231
|
+
* boolNormalize(true); // => true
|
|
232
|
+
* boolNormalize(false); // => false
|
|
233
233
|
*
|
|
234
234
|
* @example
|
|
235
235
|
* // Numeric values
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
*
|
|
236
|
+
* boolNormalize(1); // => true
|
|
237
|
+
* boolNormalize(0); // => false
|
|
238
|
+
* boolNormalize(-5); // => false
|
|
239
239
|
*
|
|
240
240
|
* @example
|
|
241
241
|
* // String values
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
242
|
+
* boolNormalize('yes'); // => true
|
|
243
|
+
* boolNormalize('No'); // => false
|
|
244
|
+
* boolNormalize('TRUE'); // => true
|
|
245
|
+
* boolNormalize('false'); // => false
|
|
246
|
+
* boolNormalize('on'); // => true
|
|
247
|
+
* boolNormalize('off'); // => false
|
|
248
248
|
*
|
|
249
249
|
* @example
|
|
250
250
|
* // Mixed and invalid inputs
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
251
|
+
* boolNormalize(null); // => false
|
|
252
|
+
* boolNormalize(undefined); // => false
|
|
253
|
+
* boolNormalize([]); // => false
|
|
254
|
+
* boolNormalize({}); // => false
|
|
255
|
+
* boolNormalize('maybe'); // => false
|
|
256
256
|
*
|
|
257
257
|
* @see isBool
|
|
258
258
|
* @see isNumP
|
|
@@ -263,7 +263,7 @@ declare function splitArrToPortions<T>(arr: readonly T[], portionLength: number)
|
|
|
263
263
|
* @public
|
|
264
264
|
* @since 2.0.0
|
|
265
265
|
*/
|
|
266
|
-
declare function
|
|
266
|
+
declare function boolNormalize(value: unknown): boolean;
|
|
267
267
|
|
|
268
268
|
/**
|
|
269
269
|
* Waits asynchronously for the specified amount of time.
|
|
@@ -353,12 +353,12 @@ declare function wait(timeout?: number): Promise<void>;
|
|
|
353
353
|
*
|
|
354
354
|
* @remarks
|
|
355
355
|
* This interface is used by time utility functions such as
|
|
356
|
-
* {@link
|
|
356
|
+
* {@link dateSecParts} and {@link datePartsSec} to represent durations
|
|
357
357
|
* in a normalized structure that is easy to read, format, or serialize.
|
|
358
358
|
*
|
|
359
359
|
* Each field is an **integer number** representing the whole count of that unit.
|
|
360
360
|
* None of the fields are fractional, and they are expected to follow
|
|
361
|
-
* these conventional bounds when generated by {@link
|
|
361
|
+
* these conventional bounds when generated by {@link dateSecParts}:
|
|
362
362
|
*
|
|
363
363
|
* ```
|
|
364
364
|
* days ≥ 0
|
|
@@ -376,8 +376,8 @@ declare function wait(timeout?: number): Promise<void>;
|
|
|
376
376
|
* const t: TimeParts = { days: 1, hours: 2, minutes: 30, seconds: 45 };
|
|
377
377
|
*
|
|
378
378
|
* // Example 2: Used with utility converters
|
|
379
|
-
* const total =
|
|
380
|
-
* const back =
|
|
379
|
+
* const total = datePartsSec(t); // -> 95445
|
|
380
|
+
* const back = dateSecParts(total); // -> same structure again
|
|
381
381
|
* ```
|
|
382
382
|
*
|
|
383
383
|
* @example
|
|
@@ -393,8 +393,8 @@ declare function wait(timeout?: number): Promise<void>;
|
|
|
393
393
|
* @property minutes - Whole number of minutes (0–59 typical).
|
|
394
394
|
* @property seconds - Whole number of seconds (0–59 typical).
|
|
395
395
|
*
|
|
396
|
-
* @see {@link
|
|
397
|
-
* @see {@link
|
|
396
|
+
* @see {@link dateSecParts} — converts total seconds into this structure.
|
|
397
|
+
* @see {@link datePartsSec} — converts this structure back into total seconds.
|
|
398
398
|
*
|
|
399
399
|
* @public
|
|
400
400
|
* @category Date & Time
|
|
@@ -441,22 +441,22 @@ interface TimeParts {
|
|
|
441
441
|
* ```ts
|
|
442
442
|
* // Example 1: Floor to the nearest 15-minute mark
|
|
443
443
|
* const d = new Date('2025-10-18T10:43:27');
|
|
444
|
-
*
|
|
444
|
+
* dateFloorMin(15, d);
|
|
445
445
|
* // -> 2025-10-18T10:30:00.000Z
|
|
446
446
|
* ```
|
|
447
447
|
*
|
|
448
448
|
* @example
|
|
449
449
|
* ```ts
|
|
450
450
|
* // Example 2: Using default date (current time)
|
|
451
|
-
* const nowFloored =
|
|
451
|
+
* const nowFloored = dateFloorMin(10);
|
|
452
452
|
* // -> e.g. 2025-10-18T09:20:00.000Z
|
|
453
453
|
* ```
|
|
454
454
|
*
|
|
455
455
|
* @example
|
|
456
456
|
* ```ts
|
|
457
457
|
* // Example 3: Clamp behavior
|
|
458
|
-
*
|
|
459
|
-
*
|
|
458
|
+
* dateFloorMin(-5, new Date()); // treated as 1 minute
|
|
459
|
+
* dateFloorMin(999, new Date()); // treated as 60 minutes
|
|
460
460
|
* ```
|
|
461
461
|
*
|
|
462
462
|
* @throws Never throws — invalid step values are automatically normalized.
|
|
@@ -468,7 +468,7 @@ interface TimeParts {
|
|
|
468
468
|
* @category Date & Time
|
|
469
469
|
* @since 2.0.0
|
|
470
470
|
*/
|
|
471
|
-
declare function
|
|
471
|
+
declare function dateFloorMin(everyMinutes?: number, date?: Date): Date;
|
|
472
472
|
|
|
473
473
|
/**
|
|
474
474
|
* Formats a {@link Date} object into a human-readable timestamp string
|
|
@@ -502,14 +502,14 @@ declare function floorDateToMinutes(everyMinutes?: number, date?: Date): Date;
|
|
|
502
502
|
* ```ts
|
|
503
503
|
* // Example 1: Specific date
|
|
504
504
|
* const d = new Date('2025-10-18T10:43:27Z');
|
|
505
|
-
*
|
|
505
|
+
* dateStr(d);
|
|
506
506
|
* // -> "2025-10-18 10:43:27"
|
|
507
507
|
* ```
|
|
508
508
|
*
|
|
509
509
|
* @example
|
|
510
510
|
* ```ts
|
|
511
511
|
* // Example 2: Default (current) date
|
|
512
|
-
*
|
|
512
|
+
* dateStr();
|
|
513
513
|
* // -> e.g. "2025-10-18 12:07:55"
|
|
514
514
|
* ```
|
|
515
515
|
*
|
|
@@ -517,7 +517,7 @@ declare function floorDateToMinutes(everyMinutes?: number, date?: Date): Date;
|
|
|
517
517
|
* ```ts
|
|
518
518
|
* // Example 3: Padding behavior
|
|
519
519
|
* const d = new Date('2025-03-07T09:04:02');
|
|
520
|
-
*
|
|
520
|
+
* dateStr(d);
|
|
521
521
|
* // -> "2025-03-07 09:04:02"
|
|
522
522
|
* ```
|
|
523
523
|
*
|
|
@@ -530,7 +530,7 @@ declare function floorDateToMinutes(everyMinutes?: number, date?: Date): Date;
|
|
|
530
530
|
* @category Date & Time
|
|
531
531
|
* @since 2.0.0
|
|
532
532
|
*/
|
|
533
|
-
declare function
|
|
533
|
+
declare function dateStr(date?: Date): string;
|
|
534
534
|
|
|
535
535
|
/**
|
|
536
536
|
* Converts a partial {@link TimeParts} structure (days, hours, minutes, seconds)
|
|
@@ -564,38 +564,33 @@ declare function formatDateToString(date?: Date): string;
|
|
|
564
564
|
* @example
|
|
565
565
|
* ```ts
|
|
566
566
|
* // Example 1: Simple conversion
|
|
567
|
-
*
|
|
567
|
+
* datePartsSec({ hours: 1, minutes: 30 }); // -> 5400
|
|
568
568
|
*
|
|
569
569
|
* // Example 2: Full time span
|
|
570
|
-
*
|
|
570
|
+
* datePartsSec({ days: 2, hours: 3, minutes: 5, seconds: 10 });
|
|
571
571
|
* // -> 183910
|
|
572
572
|
*
|
|
573
573
|
* // Example 3: Partial / missing fields
|
|
574
|
-
*
|
|
575
|
-
*
|
|
574
|
+
* datePartsSec({}); // -> 0
|
|
575
|
+
* datePartsSec({ minutes: 5 }); // -> 300
|
|
576
576
|
* ```
|
|
577
577
|
*
|
|
578
578
|
* @throws Never throws.
|
|
579
579
|
*
|
|
580
|
-
* @see {@link
|
|
580
|
+
* @see {@link dateSecParts} — the inverse operation that expands seconds back into components.
|
|
581
581
|
*
|
|
582
582
|
* @public
|
|
583
583
|
* @category Date & Time
|
|
584
584
|
* @since 2.0.0
|
|
585
585
|
*/
|
|
586
|
-
declare function
|
|
587
|
-
days?: number;
|
|
588
|
-
hours?: number;
|
|
589
|
-
minutes?: number;
|
|
590
|
-
seconds?: number;
|
|
591
|
-
}): number;
|
|
586
|
+
declare function datePartsSec(parts: TimeParts): number;
|
|
592
587
|
|
|
593
588
|
/**
|
|
594
589
|
* Decomposes a total number of seconds into discrete time components:
|
|
595
590
|
* **days**, **hours**, **minutes**, and **seconds**.
|
|
596
591
|
*
|
|
597
592
|
* @remarks
|
|
598
|
-
* This is the inverse operation of {@link
|
|
593
|
+
* This is the inverse operation of {@link datePartsSec}.
|
|
599
594
|
* It converts a flat duration (in seconds) into a more human-readable structure
|
|
600
595
|
* suitable for display, logging, or formatting.
|
|
601
596
|
*
|
|
@@ -627,130 +622,35 @@ declare function partsToSeconds(parts: {
|
|
|
627
622
|
* @example
|
|
628
623
|
* ```ts
|
|
629
624
|
* // Example 1: Basic conversion
|
|
630
|
-
*
|
|
625
|
+
* dateSecParts(3661);
|
|
631
626
|
* // -> { days: 0, hours: 1, minutes: 1, seconds: 1 }
|
|
632
627
|
* ```
|
|
633
628
|
*
|
|
634
629
|
* @example
|
|
635
630
|
* ```ts
|
|
636
631
|
* // Example 2: Multi-day value
|
|
637
|
-
*
|
|
632
|
+
* dateSecParts(90061);
|
|
638
633
|
* // -> { days: 1, hours: 1, minutes: 1, seconds: 1 }
|
|
639
634
|
* ```
|
|
640
635
|
*
|
|
641
636
|
* @example
|
|
642
637
|
* ```ts
|
|
643
638
|
* // Example 3: Invalid input
|
|
644
|
-
*
|
|
645
|
-
*
|
|
639
|
+
* dateSecParts(-10); // throws Error("Invalid total seconds")
|
|
640
|
+
* dateSecParts(NaN); // throws Error("Invalid total seconds")
|
|
646
641
|
* ```
|
|
647
642
|
*
|
|
648
643
|
* @throws {Error}
|
|
649
644
|
* Thrown when `total` is not a finite, non-negative number.
|
|
650
645
|
*
|
|
651
|
-
* @see {@link
|
|
646
|
+
* @see {@link datePartsSec} — the complementary function that aggregates components into seconds.
|
|
652
647
|
* @see {@link TimeParts} — the return type describing the breakdown of time.
|
|
653
648
|
*
|
|
654
649
|
* @public
|
|
655
650
|
* @category Date & Time
|
|
656
651
|
* @since 2.0.0
|
|
657
652
|
*/
|
|
658
|
-
declare function
|
|
659
|
-
|
|
660
|
-
/**
|
|
661
|
-
* Options that control how IPv4 ranges are iterated and materialized.
|
|
662
|
-
*
|
|
663
|
-
* @remarks
|
|
664
|
-
* These options are primarily consumed by {@link rangeIPv4} and {@link rangeIPv4ToArr}.
|
|
665
|
-
* They let you include/exclude the network and broadcast addresses when the input
|
|
666
|
-
* is a CIDR block, and limit the maximum number of items when materializing to an array.
|
|
667
|
-
*
|
|
668
|
-
* @public
|
|
669
|
-
* @category IPv4
|
|
670
|
-
* @since 2.0.0
|
|
671
|
-
*/
|
|
672
|
-
interface RangeIPv4Options {
|
|
673
|
-
/**
|
|
674
|
-
* Hard cap on the number of elements to materialize into a returned array.
|
|
675
|
-
*
|
|
676
|
-
* @remarks
|
|
677
|
-
* This option is **only** consulted by {@link rangeIPv4ToArr}. It prevents
|
|
678
|
-
* accidentally allocating huge arrays when the supplied range is very large
|
|
679
|
-
* (e.g. `0.0.0.0/0` contains 4,294,967,296 addresses).
|
|
680
|
-
*
|
|
681
|
-
* If the computed range size exceeds this limit, an error will be thrown.
|
|
682
|
-
*
|
|
683
|
-
* @defaultValue `1_000_000`
|
|
684
|
-
* @example
|
|
685
|
-
* ```ts
|
|
686
|
-
* // Will throw because /16 has 65,536 addresses (> 10_000)
|
|
687
|
-
* rangeIPv4ToArr('10.0.0.0/16', undefined, { limit: 10_000 })
|
|
688
|
-
* ```
|
|
689
|
-
*/
|
|
690
|
-
limit?: number;
|
|
691
|
-
/**
|
|
692
|
-
* Whether to include the *network* address when iterating a CIDR range.
|
|
693
|
-
*
|
|
694
|
-
* @remarks
|
|
695
|
-
* This flag is only applied when the input is a CIDR (e.g. `"192.168.0.0/24"`).
|
|
696
|
-
* For non-CIDR, ad-hoc ranges, network/broadcast semantics are not inferred.
|
|
697
|
-
*
|
|
698
|
-
* For `/31` and `/32` specifically, there is no distinct network/broadcast
|
|
699
|
-
* address to exclude, so this flag has no effect.
|
|
700
|
-
*
|
|
701
|
-
* @defaultValue `true`
|
|
702
|
-
* @example
|
|
703
|
-
* ```ts
|
|
704
|
-
* // Exclude 192.168.1.0 from a /24 network
|
|
705
|
-
* [...rangeIPv4('192.168.1.0/24', undefined, { includeNetwork: false })];
|
|
706
|
-
* ```
|
|
707
|
-
*/
|
|
708
|
-
includeNetwork?: boolean;
|
|
709
|
-
/**
|
|
710
|
-
* Whether to include the *broadcast* address when iterating a CIDR range.
|
|
711
|
-
*
|
|
712
|
-
* @remarks
|
|
713
|
-
* This flag is only applied when the input is a CIDR (e.g. `"192.168.0.0/24"`).
|
|
714
|
-
* For `/31` and `/32`, there is no broadcast in the traditional sense,
|
|
715
|
-
* so this flag has no effect.
|
|
716
|
-
*
|
|
717
|
-
* @defaultValue `true`
|
|
718
|
-
* @example
|
|
719
|
-
* ```ts
|
|
720
|
-
* // Exclude 192.168.1.255 from a /24 network
|
|
721
|
-
* [...rangeIPv4('192.168.1.0/24', undefined, { includeBroadcast: false })];
|
|
722
|
-
* ```
|
|
723
|
-
*/
|
|
724
|
-
includeBroadcast?: boolean;
|
|
725
|
-
}
|
|
726
|
-
|
|
727
|
-
/**
|
|
728
|
-
* Converts a CIDR block into its inclusive start/end IPv4 addresses.
|
|
729
|
-
*
|
|
730
|
-
* @remarks
|
|
731
|
-
* The function validates the CIDR notation and returns a tuple of dotted-quad
|
|
732
|
-
* IPv4 strings representing the first and last addresses in the block.
|
|
733
|
-
* For `/0` the range spans the entire IPv4 address space. For `/32` the start
|
|
734
|
-
* and end are the same single address.
|
|
735
|
-
*
|
|
736
|
-
* @param cidr - CIDR notation string, e.g. `"192.168.1.0/24"` or `"10.0.0.1/32"`.
|
|
737
|
-
* @returns A tuple `[start, end]` in dotted-quad form, or `null` if input is invalid.
|
|
738
|
-
*
|
|
739
|
-
* @example
|
|
740
|
-
* ```ts
|
|
741
|
-
* cidrToRange('192.168.1.0/24'); // ['192.168.1.0','192.168.1.255']
|
|
742
|
-
* cidrToRange('10.0.0.1/32'); // ['10.0.0.1','10.0.0.1']
|
|
743
|
-
* cidrToRange('bad'); // null
|
|
744
|
-
* ```
|
|
745
|
-
*
|
|
746
|
-
* @throws Never throws; returns `null` on invalid input.
|
|
747
|
-
* @see {@link parseIPv4} to parse an IPv4 string to a 32-bit number.
|
|
748
|
-
* @see {@link toIPv4} to convert a number back to dotted-quad.
|
|
749
|
-
* @public
|
|
750
|
-
* @category IPv4
|
|
751
|
-
* @since 2.0.0
|
|
752
|
-
*/
|
|
753
|
-
declare function cidrToRange(cidr: string): [string, string] | null;
|
|
653
|
+
declare function dateSecParts(total: number): TimeParts;
|
|
754
654
|
|
|
755
655
|
/**
|
|
756
656
|
* Converts a dotted-decimal IPv4 address string (e.g. `"192.168.0.1"`)
|
|
@@ -783,20 +683,20 @@ declare function cidrToRange(cidr: string): [string, string] | null;
|
|
|
783
683
|
* @example
|
|
784
684
|
* ```ts
|
|
785
685
|
* // Example 1: Simple conversion
|
|
786
|
-
*
|
|
686
|
+
* ipStrNum("192.168.0.1");
|
|
787
687
|
* // -> 3232235521
|
|
788
688
|
*
|
|
789
689
|
* // Example 2: Edge values
|
|
790
|
-
*
|
|
791
|
-
*
|
|
690
|
+
* ipStrNum("0.0.0.0"); // -> 0
|
|
691
|
+
* ipStrNum("255.255.255.255"); // -> 4294967295
|
|
792
692
|
* ```
|
|
793
693
|
*
|
|
794
694
|
* @example
|
|
795
695
|
* ```ts
|
|
796
696
|
* // Example 3: Invalid input
|
|
797
|
-
*
|
|
798
|
-
*
|
|
799
|
-
*
|
|
697
|
+
* ipStrNum("192.168.1"); // throws Error("Invalid IPv4 address")
|
|
698
|
+
* ipStrNum("256.0.0.1"); // throws Error("Invalid IPv4 address")
|
|
699
|
+
* ipStrNum("abc.def.ghi.jkl"); // throws Error("Invalid IPv4 address")
|
|
800
700
|
* ```
|
|
801
701
|
*
|
|
802
702
|
* @throws {Error}
|
|
@@ -811,14 +711,14 @@ declare function cidrToRange(cidr: string): [string, string] | null;
|
|
|
811
711
|
* @category Network & IP
|
|
812
712
|
* @since 2.0.0
|
|
813
713
|
*/
|
|
814
|
-
declare function
|
|
714
|
+
declare function ipStrNum(ip: string): number;
|
|
815
715
|
|
|
816
716
|
/**
|
|
817
717
|
* Converts a 32-bit unsigned integer (numeric IPv4 representation)
|
|
818
718
|
* back into its dotted-decimal string form (e.g. `"192.168.0.1"`).
|
|
819
719
|
*
|
|
820
720
|
* @remarks
|
|
821
|
-
* This is the inverse of {@link
|
|
721
|
+
* This is the inverse of {@link ipStrNum}.
|
|
822
722
|
* It interprets the input number as a **big-endian (network-byte-order)**
|
|
823
723
|
* IPv4 value, extracting each of the four octets and joining them into
|
|
824
724
|
* the standard dotted-quad notation.
|
|
@@ -832,7 +732,7 @@ declare function ipAddrToNum(ip: string): number;
|
|
|
832
732
|
* - Internally uses {@link DataView} to ensure consistent big-endian behavior
|
|
833
733
|
* across all platforms.
|
|
834
734
|
* - The output format is always normalized (no leading zeros, no spaces).
|
|
835
|
-
* - For the forward direction (string → number), see {@link
|
|
735
|
+
* - For the forward direction (string → number), see {@link ipStrNum}.
|
|
836
736
|
*
|
|
837
737
|
* @param num - The 32-bit unsigned integer representing an IPv4 address.
|
|
838
738
|
*
|
|
@@ -842,195 +742,32 @@ declare function ipAddrToNum(ip: string): number;
|
|
|
842
742
|
* @example
|
|
843
743
|
* ```ts
|
|
844
744
|
* // Example 1: Basic conversion
|
|
845
|
-
*
|
|
745
|
+
* ipNumStr(3232235521);
|
|
846
746
|
* // -> "192.168.0.1"
|
|
847
747
|
*
|
|
848
748
|
* // Example 2: Edge values
|
|
849
|
-
*
|
|
850
|
-
*
|
|
749
|
+
* ipNumStr(0); // -> "0.0.0.0"
|
|
750
|
+
* ipNumStr(4294967295); // -> "255.255.255.255"
|
|
851
751
|
* ```
|
|
852
752
|
*
|
|
853
753
|
* @example
|
|
854
754
|
* ```ts
|
|
855
755
|
* // Example 3: Invalid inputs
|
|
856
|
-
*
|
|
857
|
-
*
|
|
858
|
-
*
|
|
756
|
+
* ipNumStr(NaN); // -> ""
|
|
757
|
+
* ipNumStr(Infinity); // -> ""
|
|
758
|
+
* ipNumStr(-5); // -> ""
|
|
859
759
|
* ```
|
|
860
760
|
*
|
|
861
761
|
* @throws Never throws; invalid inputs simply return an empty string.
|
|
862
762
|
*
|
|
863
|
-
* @see {@link
|
|
763
|
+
* @see {@link ipStrNum} — converts dotted IPv4 strings to numeric form.
|
|
864
764
|
* @see {@link parseIPv4} — alternative parser using bitwise arithmetic.
|
|
865
765
|
*
|
|
866
766
|
* @public
|
|
867
767
|
* @category Network & IP
|
|
868
768
|
* @since 2.0.0
|
|
869
769
|
*/
|
|
870
|
-
declare function
|
|
871
|
-
|
|
872
|
-
/**
|
|
873
|
-
* Parses a dotted-quad IPv4 string (e.g. `"192.168.0.1"`) into a 32-bit unsigned integer.
|
|
874
|
-
*
|
|
875
|
-
* @remarks
|
|
876
|
-
* The returned number is in the range `0..0xFFFFFFFF` and represents the IPv4 address
|
|
877
|
-
* in big-endian order (i.e. the usual network order).
|
|
878
|
-
*
|
|
879
|
-
* The function is strict about format:
|
|
880
|
-
* - Exactly 4 decimal octets separated by dots.
|
|
881
|
-
* - Each octet must be `0..255`.
|
|
882
|
-
* - Only digits are allowed in each octet.
|
|
883
|
-
*
|
|
884
|
-
* Leading zeros in octets are permitted (e.g. `"001.002.003.004"`), but you may
|
|
885
|
-
* choose to forbid them in a custom variant to avoid legacy octal confusions.
|
|
886
|
-
*
|
|
887
|
-
* @param ip - Dotted-quad IPv4 string to parse.
|
|
888
|
-
* @returns The IPv4 as an unsigned 32-bit number, or `null` if invalid.
|
|
889
|
-
*
|
|
890
|
-
* @example
|
|
891
|
-
* ```ts
|
|
892
|
-
* parseIPv4('127.0.0.1'); // 2130706433
|
|
893
|
-
* parseIPv4('256.0.0.1'); // null
|
|
894
|
-
* parseIPv4('1.2.3'); // null
|
|
895
|
-
* ```
|
|
896
|
-
*
|
|
897
|
-
* @public
|
|
898
|
-
* @category IPv4
|
|
899
|
-
* @since 2.0.0
|
|
900
|
-
*/
|
|
901
|
-
declare function parseIPv4(ip: string): number | null;
|
|
902
|
-
|
|
903
|
-
declare function rangeIPv4(from: string, to?: string): Generator<string>;
|
|
904
|
-
/**
|
|
905
|
-
* Creates a lazy generator over an inclusive IPv4 range.
|
|
906
|
-
*
|
|
907
|
-
* @remarks
|
|
908
|
-
* The function supports two input modes:
|
|
909
|
-
*
|
|
910
|
-
* 1. **CIDR mode** — when `from` contains a slash and `to` is `undefined`:
|
|
911
|
-
* ```ts
|
|
912
|
-
* for (const ip of rangeIPv4('192.168.1.0/30')) { ... }
|
|
913
|
-
* ```
|
|
914
|
-
* In this mode, {@link RangeIPv4Options.includeNetwork} and
|
|
915
|
-
* {@link RangeIPv4Options.includeBroadcast} are honored for masks `<= /30`.
|
|
916
|
-
* For `/31` and `/32` there is no traditional network/broadcast to exclude.
|
|
917
|
-
*
|
|
918
|
-
* 2. **Ad-hoc range mode** — when `from` is an IPv4 and `to` is either an IPv4
|
|
919
|
-
* or empty/omitted:
|
|
920
|
-
* - If `to` is provided, the range is `[min(from,to) .. max(from,to)]`.
|
|
921
|
-
* - If `to` is omitted or blank, the range spans to the end of the `/24`
|
|
922
|
-
* block: `A.B.C.D .. A.B.C.255`.
|
|
923
|
-
*
|
|
924
|
-
* The iteration is inclusive of both endpoints and is safe around the upper
|
|
925
|
-
* bound: if the current value is `0xFFFFFFFF`, the generator yields it once and terminates.
|
|
926
|
-
*
|
|
927
|
-
* This is a lazy generator: it does **not** allocate the entire range up-front,
|
|
928
|
-
* making it suitable for very large ranges (iterate/stream/process on the fly).
|
|
929
|
-
* If you need a materialized array, consider {@link rangeIPv4ToArr} but mind its `limit`.
|
|
930
|
-
*
|
|
931
|
-
* @overload
|
|
932
|
-
* @param from - A CIDR string (e.g. `"10.0.0.0/8"`). If this overload is used, `to` must be `undefined`.
|
|
933
|
-
*
|
|
934
|
-
* @overload
|
|
935
|
-
* @param from - Starting IPv4 address in dotted-quad form.
|
|
936
|
-
* @param to - Optional ending IPv4 address in dotted-quad form. If omitted or empty, the range ends at `A.B.C.255`.
|
|
937
|
-
* @param opts - Iteration options.
|
|
938
|
-
* @returns A generator of dotted-quad IPv4 strings.
|
|
939
|
-
*
|
|
940
|
-
* @param from - See overloads.
|
|
941
|
-
* @param to - See overloads.
|
|
942
|
-
* @param opts - See overloads.
|
|
943
|
-
*
|
|
944
|
-
* @example
|
|
945
|
-
* ```ts
|
|
946
|
-
* // 1) CIDR iteration (include all)
|
|
947
|
-
* [...rangeIPv4('192.168.1.0/30')];
|
|
948
|
-
* // -> ['192.168.1.0','192.168.1.1','192.168.1.2','192.168.1.3']
|
|
949
|
-
*
|
|
950
|
-
* // 2) CIDR iteration without network/broadcast
|
|
951
|
-
* [...rangeIPv4('192.168.1.0/30', undefined, { includeNetwork: false, includeBroadcast: false })];
|
|
952
|
-
* // -> ['192.168.1.1','192.168.1.2']
|
|
953
|
-
*
|
|
954
|
-
* // 3) Ad-hoc range (explicit end)
|
|
955
|
-
* [...rangeIPv4('10.0.0.1', '10.0.0.4')];
|
|
956
|
-
* // -> ['10.0.0.1','10.0.0.2','10.0.0.3','10.0.0.4']
|
|
957
|
-
*
|
|
958
|
-
* // 4) Single address ⇒ to end of /24
|
|
959
|
-
* [...rangeIPv4('10.1.2.3')].at(-1); // '10.1.2.255'
|
|
960
|
-
* ```
|
|
961
|
-
*
|
|
962
|
-
* @throws {Error}
|
|
963
|
-
* - If `from` is not a valid IPv4 (in non-CIDR mode).
|
|
964
|
-
* - If `to` is supplied and is not a valid IPv4 (in non-CIDR mode).
|
|
965
|
-
* - If `from` is not a valid CIDR in CIDR mode.
|
|
966
|
-
*
|
|
967
|
-
* @see {@link cidrToRange} to convert a CIDR to `[ start, end ]`.
|
|
968
|
-
* @see {@link rangeIPv4ToArr} to materialize a range into an array with a safe limit.
|
|
969
|
-
* @public
|
|
970
|
-
* @category IPv4
|
|
971
|
-
* @since 2.0.0
|
|
972
|
-
*/
|
|
973
|
-
declare function rangeIPv4(from: string, to: string | undefined, opts?: RangeIPv4Options): Generator<string>;
|
|
974
|
-
|
|
975
|
-
/**
|
|
976
|
-
* Materializes an IPv4 range into an array of dotted-quad strings.
|
|
977
|
-
*
|
|
978
|
-
* @remarks
|
|
979
|
-
* This is a convenience wrapper around the lazy {@link rangeIPv4} generator that
|
|
980
|
-
* collects the yielded addresses into a new array. To protect against excessive
|
|
981
|
-
* memory usage, a hard {@link RangeIPv4Options.limit | limit} is enforced
|
|
982
|
-
* (default `1_000_000`). If the range exceeds the limit, an error is thrown and
|
|
983
|
-
* **no** partial array is returned.
|
|
984
|
-
*
|
|
985
|
-
* Prefer using the generator for very large ranges, streaming the results into
|
|
986
|
-
* your processing pipeline (e.g. writing to a file or socket).
|
|
987
|
-
*
|
|
988
|
-
* @param from - See {@link rangeIPv4} for accepted forms (CIDR or IPv4).
|
|
989
|
-
* @param to - Optional end address (non-CIDR mode). Ignored for CIDR input.
|
|
990
|
-
* @param opts - Options including the array size `limit`. See {@link RangeIPv4Options}.
|
|
991
|
-
* @returns A new array with all IPv4 addresses in the range (inclusive).
|
|
992
|
-
*
|
|
993
|
-
* @example
|
|
994
|
-
* ```ts
|
|
995
|
-
* // Small range OK
|
|
996
|
-
* rangeIPv4ToArray('192.168.1.10', '192.168.1.13');
|
|
997
|
-
* // -> ['192.168.1.10','192.168.1.11','192.168.1.12','192.168.1.13']
|
|
998
|
-
*
|
|
999
|
-
* // Will throw if exceeds the limit
|
|
1000
|
-
* rangeIPv4ToArray('10.0.0.0/16', undefined, { limit: 10_000 }); // Error
|
|
1001
|
-
* ```
|
|
1002
|
-
*
|
|
1003
|
-
* @throws {Error} If the realized array would exceed `opts.limit`.
|
|
1004
|
-
* @see {@link rangeIPv4} for lazy iteration without materialization.
|
|
1005
|
-
* @public
|
|
1006
|
-
* @category IPv4
|
|
1007
|
-
* @since 2.0.0
|
|
1008
|
-
*/
|
|
1009
|
-
declare function rangeIPv4ToArr(from: string, to?: string, opts?: RangeIPv4Options): string[];
|
|
1010
|
-
|
|
1011
|
-
/**
|
|
1012
|
-
* Converts an unsigned 32-bit integer into a dotted-quad IPv4 string.
|
|
1013
|
-
*
|
|
1014
|
-
* @remarks
|
|
1015
|
-
* This is the inverse of {@link parseIPv4}. The function validates that the input
|
|
1016
|
-
* is a finite integer within `0..0xFFFFFFFF` and then formats it as `A.B.C.D`.
|
|
1017
|
-
*
|
|
1018
|
-
* @param n - Unsigned 32-bit IPv4 value (`0..0xFFFFFFFF`).
|
|
1019
|
-
* @returns A dotted-quad string such as `"192.168.0.1"`.
|
|
1020
|
-
*
|
|
1021
|
-
* @example
|
|
1022
|
-
* ```ts
|
|
1023
|
-
* toIPv4(0); // "0.0.0.0"
|
|
1024
|
-
* toIPv4(0xFFFFFFFF); // "255.255.255.255"
|
|
1025
|
-
* ```
|
|
1026
|
-
*
|
|
1027
|
-
* @throws {Error} If `n` is not an integer in `0..0xFFFFFFFF`.
|
|
1028
|
-
* @see {@link parseIPv4}
|
|
1029
|
-
* @public
|
|
1030
|
-
* @category IPv4
|
|
1031
|
-
* @since 2.0.0
|
|
1032
|
-
*/
|
|
1033
|
-
declare function toIPv4(n: number): string;
|
|
770
|
+
declare function ipNumStr(num: number): string;
|
|
1034
771
|
|
|
1035
772
|
/**
|
|
1036
773
|
* Configuration options that define password validation rules.
|
|
@@ -2828,6 +2565,8 @@ declare function isStrAscDesc(value: unknown): value is 'asc' | 'desc';
|
|
|
2828
2565
|
*/
|
|
2829
2566
|
declare function isVar(value: unknown): value is string;
|
|
2830
2567
|
|
|
2568
|
+
declare function isClass(value: unknown): boolean;
|
|
2569
|
+
|
|
2831
2570
|
/**
|
|
2832
2571
|
* Structural JSON-like type used throughout decoding.
|
|
2833
2572
|
*
|
|
@@ -2850,7 +2589,7 @@ type JSONLike = null | boolean | number | string | JSONLike[] | {
|
|
|
2850
2589
|
* @remarks
|
|
2851
2590
|
* The parsing order is:
|
|
2852
2591
|
*
|
|
2853
|
-
* 1. Try JSON via {@link
|
|
2592
|
+
* 1. Try JSON via {@link jsonParse}. If it works, return the parsed value.
|
|
2854
2593
|
* 2. If not JSON, check if the string is quoted with `'`, `"` or `` ` ``.
|
|
2855
2594
|
* If quoted, return the **unquoted** inner text.
|
|
2856
2595
|
* 3. If not quoted:
|
|
@@ -2867,15 +2606,15 @@ type JSONLike = null | boolean | number | string | JSONLike[] | {
|
|
|
2867
2606
|
*
|
|
2868
2607
|
* @example
|
|
2869
2608
|
* ```ts
|
|
2870
|
-
*
|
|
2871
|
-
*
|
|
2872
|
-
*
|
|
2873
|
-
*
|
|
2609
|
+
* jsonStrLike('{"a":1}', false); // -> { a: 1 }
|
|
2610
|
+
* jsonStrLike('"hello"', false); // -> "hello"
|
|
2611
|
+
* jsonStrLike('hello', false); // -> null
|
|
2612
|
+
* jsonStrLike('hello', true); // -> "hello"
|
|
2874
2613
|
* ```
|
|
2875
2614
|
*
|
|
2876
2615
|
* @internal
|
|
2877
2616
|
*/
|
|
2878
|
-
declare function
|
|
2617
|
+
declare function jsonStrLike(s: string, allowString: boolean): JSONLike | null;
|
|
2879
2618
|
|
|
2880
2619
|
/**
|
|
2881
2620
|
* Attempts to parse a string using native {@link JSON.parse}.
|
|
@@ -2890,13 +2629,13 @@ declare function parseStringLike(s: string, allowString: boolean): JSONLike | nu
|
|
|
2890
2629
|
*
|
|
2891
2630
|
* @example
|
|
2892
2631
|
* ```ts
|
|
2893
|
-
*
|
|
2894
|
-
*
|
|
2632
|
+
* jsonParse('{"a":1}'); // { ok: true, value: { a: 1 } }
|
|
2633
|
+
* jsonParse('not json'); // { ok: false }
|
|
2895
2634
|
* ```
|
|
2896
2635
|
*
|
|
2897
2636
|
* @internal
|
|
2898
2637
|
*/
|
|
2899
|
-
declare function
|
|
2638
|
+
declare function jsonParse(str: string): {
|
|
2900
2639
|
ok: true;
|
|
2901
2640
|
value: JSONLike;
|
|
2902
2641
|
} | {
|
|
@@ -2912,10 +2651,10 @@ declare function tryParseJSON(str: string): {
|
|
|
2912
2651
|
*
|
|
2913
2652
|
* - **Primitive passthrough**: `null`, numbers, and booleans are returned as-is.
|
|
2914
2653
|
* - **Arrays/Objects (filled)**: for each string element/property, we attempt to decode
|
|
2915
|
-
* it via {@link
|
|
2654
|
+
* it via {@link jsonStrLike} (JSON → unquoted string → raw string if `allowString`).
|
|
2916
2655
|
* Non-string items are passed through unchanged (cast to `JSONLike`).
|
|
2917
2656
|
* - **Arrays/Objects (empty)**: returned as-is (they’re valid JSON).
|
|
2918
|
-
* - **Standalone strings**: decoded via {@link
|
|
2657
|
+
* - **Standalone strings**: decoded via {@link jsonStrLike}.
|
|
2919
2658
|
* - **Other values**: return `null`.
|
|
2920
2659
|
*
|
|
2921
2660
|
* The function is **non-throwing**; any JSON parse errors are swallowed internally
|
|
@@ -3024,381 +2763,6 @@ declare function jsonDecode<T = JSONLike>(value: unknown, allowString?: boolean)
|
|
|
3024
2763
|
*/
|
|
3025
2764
|
declare function jsonEncode(value: unknown): string;
|
|
3026
2765
|
|
|
3027
|
-
/**
|
|
3028
|
-
* Represents a **fixed-precision decimal number** using exact integer arithmetic.
|
|
3029
|
-
*
|
|
3030
|
-
* @remarks
|
|
3031
|
-
* JavaScript’s native `number` type uses 64-bit IEEE-754 floating-point representation,
|
|
3032
|
-
* which introduces rounding errors for many decimal values
|
|
3033
|
-
* (for example, `0.1 + 0.2 !== 0.3`).
|
|
3034
|
-
*
|
|
3035
|
-
* `FixedDecimal` provides a lossless, predictable alternative by separating
|
|
3036
|
-
* the number into three explicit components:
|
|
3037
|
-
*
|
|
3038
|
-
* - **`sign`** → numeric sign (`1` for positive values, `-1` for negative ones)
|
|
3039
|
-
* - **`digitsInteger`** → all digits of the number stored as a `BigInt`
|
|
3040
|
-
* (the decimal point is removed)
|
|
3041
|
-
* - **`scale`** → number of digits that were originally after the decimal point
|
|
3042
|
-
*
|
|
3043
|
-
* Together, these three parts allow precise decimal math, rounding,
|
|
3044
|
-
* and string/number conversions without losing accuracy.
|
|
3045
|
-
*
|
|
3046
|
-
* ---
|
|
3047
|
-
* **Conceptual example**
|
|
3048
|
-
*
|
|
3049
|
-
* The decimal value:
|
|
3050
|
-
* ```
|
|
3051
|
-
* -123.456
|
|
3052
|
-
* ```
|
|
3053
|
-
* would be represented as:
|
|
3054
|
-
* ```ts
|
|
3055
|
-
* {
|
|
3056
|
-
* sign: -1,
|
|
3057
|
-
* digitsInteger: 123456n,
|
|
3058
|
-
* scale: 3
|
|
3059
|
-
* }
|
|
3060
|
-
* ```
|
|
3061
|
-
*
|
|
3062
|
-
* which mathematically equals:
|
|
3063
|
-
* ```
|
|
3064
|
-
* sign × (digitsInteger / 10^scale)
|
|
3065
|
-
* = -1 × (123456 / 10^3)
|
|
3066
|
-
* = -123.456
|
|
3067
|
-
* ```
|
|
3068
|
-
*
|
|
3069
|
-
* ---
|
|
3070
|
-
* **Usage**
|
|
3071
|
-
*
|
|
3072
|
-
* Instances of `FixedDecimal` are typically produced and consumed by utility functions:
|
|
3073
|
-
* - {@link parseToFixedDecimal} — converts arbitrary input (`string`/`number`/`bigint`) to `FixedDecimal`
|
|
3074
|
-
* - {@link roundFixedDecimal} — rounds to a target number of fractional digits
|
|
3075
|
-
* - {@link fixedDecimalToStr} — converts back to a human-readable string
|
|
3076
|
-
* - {@link fixedDecimalToNum} — converts to a JavaScript `number` (with possible precision loss)
|
|
3077
|
-
*
|
|
3078
|
-
* ---
|
|
3079
|
-
* @property sign - Numeric sign of the value.
|
|
3080
|
-
* `1` for positive and zero values, `-1` for negative ones.
|
|
3081
|
-
*
|
|
3082
|
-
* @property digitsInteger - A `BigInt` holding all digits of the number
|
|
3083
|
-
* without any decimal separator.
|
|
3084
|
-
* Example: `"123.45"` → `digitsInteger = 12345n`.
|
|
3085
|
-
*
|
|
3086
|
-
* @property scale - Number of digits that appear after the decimal point
|
|
3087
|
-
* in the original value.
|
|
3088
|
-
* Example: `"123.45"` → `scale = 2`.
|
|
3089
|
-
*
|
|
3090
|
-
* ---
|
|
3091
|
-
* @example
|
|
3092
|
-
* // Example: 0.034 represented as FixedDecimal
|
|
3093
|
-
* const fd: FixedDecimal = {
|
|
3094
|
-
* sign: 1,
|
|
3095
|
-
* digitsInteger: 34n,
|
|
3096
|
-
* scale: 3
|
|
3097
|
-
* };
|
|
3098
|
-
* // mathematically: 1 × (34 / 10³) = 0.034
|
|
3099
|
-
*
|
|
3100
|
-
* @example
|
|
3101
|
-
* // Example: -987000.00 → same magnitude, different scale
|
|
3102
|
-
* const fd2: FixedDecimal = {
|
|
3103
|
-
* sign: -1,
|
|
3104
|
-
* digitsInteger: 98700000n,
|
|
3105
|
-
* scale: 2
|
|
3106
|
-
* };
|
|
3107
|
-
* // -987000.00
|
|
3108
|
-
*
|
|
3109
|
-
* @example
|
|
3110
|
-
* // Zero value representation
|
|
3111
|
-
* const zero: FixedDecimal = {
|
|
3112
|
-
* sign: 1,
|
|
3113
|
-
* digitsInteger: 0n,
|
|
3114
|
-
* scale: 0
|
|
3115
|
-
* };
|
|
3116
|
-
*
|
|
3117
|
-
* @see parseToFixedDecimal
|
|
3118
|
-
* @see roundFixedDecimal
|
|
3119
|
-
* @see fixedDecimalToStr
|
|
3120
|
-
* @see fixedDecimalToNum
|
|
3121
|
-
*
|
|
3122
|
-
* @category Types
|
|
3123
|
-
* @since 2.0.0
|
|
3124
|
-
* @public
|
|
3125
|
-
*/
|
|
3126
|
-
interface FixedDecimal {
|
|
3127
|
-
/**
|
|
3128
|
-
* The numeric sign of the value:
|
|
3129
|
-
* `1` for positive or zero, `-1` for negative.
|
|
3130
|
-
*/
|
|
3131
|
-
sign: 1 | -1;
|
|
3132
|
-
/**
|
|
3133
|
-
* All digits of the number stored as a `BigInt`,
|
|
3134
|
-
* with the decimal point removed.
|
|
3135
|
-
*/
|
|
3136
|
-
digitsInteger: bigint;
|
|
3137
|
-
/**
|
|
3138
|
-
* The number of digits that originally appeared
|
|
3139
|
-
* after the decimal point.
|
|
3140
|
-
*/
|
|
3141
|
-
scale: number;
|
|
3142
|
-
}
|
|
3143
|
-
|
|
3144
|
-
/**
|
|
3145
|
-
* Adjusts the **scale** (i.e., number of fractional digits) of a {@link FixedDecimal}
|
|
3146
|
-
* by a given offset without altering its numeric meaning.
|
|
3147
|
-
*
|
|
3148
|
-
* @remarks
|
|
3149
|
-
* A `FixedDecimal` represents a decimal number as:
|
|
3150
|
-
* ```ts
|
|
3151
|
-
* { sign: 1 | -1, digitsInteger: bigint, scale: number }
|
|
3152
|
-
* ```
|
|
3153
|
-
* where `digitsInteger × 10^(-scale)` yields the real value.
|
|
3154
|
-
*
|
|
3155
|
-
* This helper changes the `scale` while keeping the numerical value consistent.
|
|
3156
|
-
* When `scaleDelta > 0`, the function *increases* the scale (adds more fractional
|
|
3157
|
-
* digits of precision) **without changing** `digitsInteger`.
|
|
3158
|
-
* When `scaleDelta < 0`, it *reduces* the scale by multiplying `digitsInteger`
|
|
3159
|
-
* to preserve the same actual numeric value.
|
|
3160
|
-
*
|
|
3161
|
-
* O(1) time and space, except for the BigInt multiplication when `scaleDelta < 0`.
|
|
3162
|
-
*
|
|
3163
|
-
* @param value - The {@link FixedDecimal} to adjust.
|
|
3164
|
-
* Should contain:
|
|
3165
|
-
* - `sign`: either `1` (positive) or `-1` (negative),
|
|
3166
|
-
* - `digitsInteger`: the integer form of all digits without a decimal point,
|
|
3167
|
-
* - `scale`: the number of fractional digits currently represented.
|
|
3168
|
-
*
|
|
3169
|
-
* @param scaleDelta - The number of fractional places to add or remove.
|
|
3170
|
-
* Positive values mean **increase precision** (more digits after the decimal point);
|
|
3171
|
-
* negative values mean **reduce precision**, scaling the integer part accordingly.
|
|
3172
|
-
*
|
|
3173
|
-
* @returns A new {@link FixedDecimal} with the adjusted `scale` and appropriately
|
|
3174
|
-
* modified `digitsInteger` if needed.
|
|
3175
|
-
*
|
|
3176
|
-
* @throws {RangeError} If the operation would cause an invalid scale
|
|
3177
|
-
* (e.g., non-integer `scaleDelta`), though `Math.trunc` is used to normalize inputs.
|
|
3178
|
-
*
|
|
3179
|
-
* @example
|
|
3180
|
-
* ```ts
|
|
3181
|
-
* const num: FixedDecimal = { sign: 1, digitsInteger: 12345n, scale: 2 };
|
|
3182
|
-
* // Represents 123.45
|
|
3183
|
-
*
|
|
3184
|
-
* // Increase scale by +2 → same numeric value but more fractional digits
|
|
3185
|
-
* const up = changeFixedDecimalScale(num, +2);
|
|
3186
|
-
* // up = { sign: 1, digitsInteger: 12345n, scale: 4 } → 1.2345 × 10^2 = 123.45
|
|
3187
|
-
*
|
|
3188
|
-
* // Decrease scale by -2 → multiply digitsInteger to preserve value
|
|
3189
|
-
* const down = changeFixedDecimalScale(num, -2);
|
|
3190
|
-
* // down = { sign: 1, digitsInteger: 1234500n, scale: 0 } → 1234500 × 10^0 = 123.45
|
|
3191
|
-
* ```
|
|
3192
|
-
*
|
|
3193
|
-
* @example
|
|
3194
|
-
* ```ts
|
|
3195
|
-
* // No change when delta = 0
|
|
3196
|
-
* const same = changeFixedDecimalScale(num, 0);
|
|
3197
|
-
* // same === { sign: 1, digitsInteger: 12345n, scale: 2 }
|
|
3198
|
-
* ```
|
|
3199
|
-
*
|
|
3200
|
-
* @since 2.0.0
|
|
3201
|
-
*/
|
|
3202
|
-
declare function changeFixedDecimalScale(value: FixedDecimal, scaleDelta: number): FixedDecimal;
|
|
3203
|
-
|
|
3204
|
-
/**
|
|
3205
|
-
* Converts a number expressed in normalized exponential/scientific notation
|
|
3206
|
-
* (e.g., `"1.23e+5"`, `"4e-7"`, `"0009.500e2"`) into its **sign**, **integer**,
|
|
3207
|
-
* and **fractional** parts as plain decimal digit strings — **without** any
|
|
3208
|
-
* decimal point or leading `+`/`-` characters.
|
|
3209
|
-
*
|
|
3210
|
-
* @remarks
|
|
3211
|
-
* - This function does **not** perform numeric arithmetic; instead it does a
|
|
3212
|
-
* textual normalization that preserves all significant digits exactly.
|
|
3213
|
-
* - It accepts the `sign` separately so that callers can pre-parse signs from
|
|
3214
|
-
* inputs like `"-1.23e3"` and pass `sign = -1` with `exponentialString = "1.23e3"`.
|
|
3215
|
-
* (If you already have a positive string, pass `sign = 1`.)
|
|
3216
|
-
* - The function normalizes the coefficient and exponent first, then uses a
|
|
3217
|
-
* single regex match to validate the shape:
|
|
3218
|
-
* - Coefficient: `([0-9]+)(?:\.([0-9]*))?`
|
|
3219
|
-
* - Exponent: `e([+\-]?[0-9]+)`
|
|
3220
|
-
* - Trailing zeros are added to the **integer** side for positive net exponents,
|
|
3221
|
-
* while leading zeros are added to the **fractional** side for negative net exponents.
|
|
3222
|
-
* - All returned parts (`integerPart`, `fractionalPart`) contain only ASCII digits.
|
|
3223
|
-
* The decimal point is **not** returned; you can re-insert it if needed by
|
|
3224
|
-
* placing it between `integerPart` and `fractionalPart`.
|
|
3225
|
-
*
|
|
3226
|
-
* Time O(n) and space O(n), where n is the number of digits in the coefficient,
|
|
3227
|
-
* due to string concatenation and slicing.
|
|
3228
|
-
*
|
|
3229
|
-
* @param sign - The pre-parsed sign of the number: `1` for positive, `-1` for negative.
|
|
3230
|
-
* @param exponentialString - A string in exponential notation.
|
|
3231
|
-
* Examples: `"1e3"`, `"1.23e+5"`, `"4.560e-2"`, `"0009.500e2"`.
|
|
3232
|
-
* The function is case-insensitive for the `'e'` and tolerates leading zeros in the coefficient.
|
|
3233
|
-
*
|
|
3234
|
-
* @returns An object with three properties:
|
|
3235
|
-
* - `sign`: Echo of the input sign (`1` | `-1`).
|
|
3236
|
-
* - `integerPart`: All digits to the left of the decimal point after expansion.
|
|
3237
|
-
* - `fractionalPart`: All digits to the right of the decimal point after expansion.
|
|
3238
|
-
*
|
|
3239
|
-
* @throws {Error} If `exponentialString` cannot be parsed as scientific notation.
|
|
3240
|
-
*
|
|
3241
|
-
* @example
|
|
3242
|
-
* ```ts
|
|
3243
|
-
* // 1.23 × 10^5 = 123000
|
|
3244
|
-
* convertExponentialToParts(1, "1.23e5");
|
|
3245
|
-
* // => { sign: 1, integerPart: "123000", fractionalPart: "" }
|
|
3246
|
-
* ```
|
|
3247
|
-
*
|
|
3248
|
-
* @example
|
|
3249
|
-
* ```ts
|
|
3250
|
-
* // 4.5 × 10^-3 = 0.0045
|
|
3251
|
-
* convertExponentialToParts(1, "4.5e-3");
|
|
3252
|
-
* // => { sign: 1, integerPart: "0", fractionalPart: "0045" }
|
|
3253
|
-
* ```
|
|
3254
|
-
*
|
|
3255
|
-
* @example
|
|
3256
|
-
* ```ts
|
|
3257
|
-
* // -7.00e+0 = -7
|
|
3258
|
-
* convertExponentialToParts(-1, "7.00e0");
|
|
3259
|
-
* // => { sign: -1, integerPart: "7", fractionalPart: "" }
|
|
3260
|
-
* ```
|
|
3261
|
-
*
|
|
3262
|
-
* @example
|
|
3263
|
-
* ```ts
|
|
3264
|
-
* // Leading zeros in coefficient are handled; fractional length adjusts exponent
|
|
3265
|
-
* convertExponentialToParts(1, "0009.500e2"); // 9.500 × 10^2 = 950
|
|
3266
|
-
* // => { sign: 1, integerPart: "950", fractionalPart: "" }
|
|
3267
|
-
* ```
|
|
3268
|
-
*
|
|
3269
|
-
* @example
|
|
3270
|
-
* ```ts
|
|
3271
|
-
* // You can reconstruct a normalized decimal string yourself:
|
|
3272
|
-
* const { sign, integerPart, fractionalPart } = convertExponentialToParts(-1, "1.234e-2");
|
|
3273
|
-
* const signChar = sign < 0 ? "-" : "";
|
|
3274
|
-
* const plain = fractionalPart ? `${integerPart}.${fractionalPart}` : integerPart;
|
|
3275
|
-
* // plain == "0.01234"
|
|
3276
|
-
* const result = signChar + plain; // "-0.01234"
|
|
3277
|
-
* ```
|
|
3278
|
-
*
|
|
3279
|
-
* @see isNumPZ — utility used here to detect non-negative integers (exponent ≥ 0).
|
|
3280
|
-
*
|
|
3281
|
-
* @since 2.0.0
|
|
3282
|
-
*/
|
|
3283
|
-
declare function convertExponentialToParts(sign: 1 | -1, exponentialString: string): {
|
|
3284
|
-
sign: 1 | -1;
|
|
3285
|
-
integerPart: string;
|
|
3286
|
-
fractionalPart: string;
|
|
3287
|
-
};
|
|
3288
|
-
|
|
3289
|
-
/**
|
|
3290
|
-
* Converts a {@link FixedDecimal} — an exact decimal representation —
|
|
3291
|
-
* into a native JavaScript `number` (IEEE 754 double precision).
|
|
3292
|
-
*
|
|
3293
|
-
* @remarks
|
|
3294
|
-
* This function is a **lossy** conversion when `value` contains
|
|
3295
|
-
* more precision than JavaScript’s floating-point format can represent.
|
|
3296
|
-
* It internally calls {@link fixedDecimalToStr} to produce a normalized
|
|
3297
|
-
* string such as `"-123.4567"` and then passes it to the built-in
|
|
3298
|
-
* `Number()` constructor.
|
|
3299
|
-
*
|
|
3300
|
-
* O(n) relative to the number of digits in `digitsInteger`
|
|
3301
|
-
* (due to string creation in {@link fixedDecimalToStr}).
|
|
3302
|
-
*
|
|
3303
|
-
* @param value - The {@link FixedDecimal} instance to convert.
|
|
3304
|
-
* Must contain:
|
|
3305
|
-
* - `sign`: either `1` or `-1`;
|
|
3306
|
-
* - `digitsInteger`: a `bigint` representing all digits without any decimal point;
|
|
3307
|
-
* - `scale`: how many digits belong after the decimal point.
|
|
3308
|
-
*
|
|
3309
|
-
* @returns The approximate numeric value as a JavaScript `number`.
|
|
3310
|
-
* If the string form is too large or too precise, the result may be
|
|
3311
|
-
* rounded or become `Infinity`.
|
|
3312
|
-
*
|
|
3313
|
-
* @example
|
|
3314
|
-
* ```ts
|
|
3315
|
-
* const fd: FixedDecimal = { sign: 1, digitsInteger: 12345n, scale: 2 };
|
|
3316
|
-
* // Represents exactly 123.45
|
|
3317
|
-
* const num = fixedDecimalToNum(fd); // 123.45 (Number)
|
|
3318
|
-
* ```
|
|
3319
|
-
*
|
|
3320
|
-
* @example
|
|
3321
|
-
* ```ts
|
|
3322
|
-
* // Handles negative numbers as well
|
|
3323
|
-
* const neg: FixedDecimal = { sign: -1, digitsInteger: 987n, scale: 3 };
|
|
3324
|
-
* const n = fixedDecimalToNum(neg); // -0.987
|
|
3325
|
-
* ```
|
|
3326
|
-
*
|
|
3327
|
-
* @example
|
|
3328
|
-
* ```ts
|
|
3329
|
-
* // Extreme precision may lose digits beyond ~15–17 significant figures
|
|
3330
|
-
* const big: FixedDecimal = { sign: 1, digitsInteger: 12345678901234567890n, scale: 10 };
|
|
3331
|
-
* const approx = fixedDecimalToNum(big);
|
|
3332
|
-
* console.log(approx); // ~1234567890.1234567 (rounded)
|
|
3333
|
-
* ```
|
|
3334
|
-
*
|
|
3335
|
-
* @see fixedDecimalToStr — for the exact string representation
|
|
3336
|
-
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number#precision JavaScript number precision
|
|
3337
|
-
*
|
|
3338
|
-
* @since 2.0.0
|
|
3339
|
-
*/
|
|
3340
|
-
declare function fixedDecimalToNum(value: FixedDecimal): number;
|
|
3341
|
-
|
|
3342
|
-
/**
|
|
3343
|
-
* Converts a {@link FixedDecimal} — an exact, integer-based decimal structure —
|
|
3344
|
-
* into its canonical string representation (e.g. `"-123.456"`).
|
|
3345
|
-
*
|
|
3346
|
-
* @remarks
|
|
3347
|
-
* This function is **exact and reversible**: no precision loss occurs because it
|
|
3348
|
-
* operates on the raw digit string (`digitsInteger`) and inserts the decimal point
|
|
3349
|
-
* purely by string manipulation.
|
|
3350
|
-
*
|
|
3351
|
-
* The algorithm:
|
|
3352
|
-
* 1. Converts the `digitsInteger` `BigInt` to a string of digits.
|
|
3353
|
-
* 2. If `scale` is `0`, returns the signed integer directly.
|
|
3354
|
-
* 3. If `scale` is positive:
|
|
3355
|
-
* - Pads with leading zeros when the number of digits is smaller than `scale`.
|
|
3356
|
-
* - Otherwise, splits at the appropriate boundary between integer and fractional parts.
|
|
3357
|
-
*
|
|
3358
|
-
* O(n) where `n` is the number of digits in `digitsInteger.toString()`.
|
|
3359
|
-
*
|
|
3360
|
-
* @param value - The {@link FixedDecimal} object:
|
|
3361
|
-
* - `sign`: `1` for positive or `-1` for negative numbers.
|
|
3362
|
-
* - `digitsInteger`: All digits as a `BigInt` without a decimal point.
|
|
3363
|
-
* - `scale`: The number of digits after the decimal point.
|
|
3364
|
-
*
|
|
3365
|
-
* @returns A string representation of the number, including a `-` sign if negative.
|
|
3366
|
-
* If the number is fractional, a single `.` separates integer and fractional parts.
|
|
3367
|
-
*
|
|
3368
|
-
* @example
|
|
3369
|
-
* ```ts
|
|
3370
|
-
* const fd: FixedDecimal = { sign: 1, digitsInteger: 12345n, scale: 2 };
|
|
3371
|
-
* fixedDecimalToStr(fd); // "123.45"
|
|
3372
|
-
* ```
|
|
3373
|
-
*
|
|
3374
|
-
* @example
|
|
3375
|
-
* ```ts
|
|
3376
|
-
* // Negative number
|
|
3377
|
-
* const neg: FixedDecimal = { sign: -1, digitsInteger: 987n, scale: 3 };
|
|
3378
|
-
* fixedDecimalToStr(neg); // "-0.987"
|
|
3379
|
-
* ```
|
|
3380
|
-
*
|
|
3381
|
-
* @example
|
|
3382
|
-
* ```ts
|
|
3383
|
-
* // Small fractional with fewer digits than scale
|
|
3384
|
-
* const tiny: FixedDecimal = { sign: 1, digitsInteger: 12n, scale: 5 };
|
|
3385
|
-
* fixedDecimalToStr(tiny); // "0.00012"
|
|
3386
|
-
* ```
|
|
3387
|
-
*
|
|
3388
|
-
* @example
|
|
3389
|
-
* ```ts
|
|
3390
|
-
* // Whole number (scale = 0)
|
|
3391
|
-
* const whole: FixedDecimal = { sign: 1, digitsInteger: 42n, scale: 0 };
|
|
3392
|
-
* fixedDecimalToStr(whole); // "42"
|
|
3393
|
-
* ```
|
|
3394
|
-
*
|
|
3395
|
-
* @see changeFixedDecimalScale — for adjusting the scale value safely
|
|
3396
|
-
* @see fixedDecimalToNum — for converting to a JavaScript number
|
|
3397
|
-
*
|
|
3398
|
-
* @since 2.0.0
|
|
3399
|
-
*/
|
|
3400
|
-
declare function fixedDecimalToStr(value: FixedDecimal): string;
|
|
3401
|
-
|
|
3402
2766
|
/**
|
|
3403
2767
|
* Converts an arbitrary numeric input to a JavaScript `number`, optionally rounding
|
|
3404
2768
|
* it to a fixed number of fractional digits using **half-up** rounding.
|
|
@@ -3440,23 +2804,23 @@ declare function fixedDecimalToStr(value: FixedDecimal): string;
|
|
|
3440
2804
|
*
|
|
3441
2805
|
* @example
|
|
3442
2806
|
* // No rounding (default `round = 1` → passthrough)
|
|
3443
|
-
*
|
|
3444
|
-
*
|
|
2807
|
+
* numNormalize("123.456"); // => 123.456
|
|
2808
|
+
* numNormalize(0.034); // => 0.034
|
|
3445
2809
|
*
|
|
3446
2810
|
* @example
|
|
3447
2811
|
* // Round to 2 fractional digits (half-up)
|
|
3448
|
-
*
|
|
3449
|
-
*
|
|
2812
|
+
* numNormalize("123.456", 2); // => 123.46 (because .456 → .46)
|
|
2813
|
+
* numNormalize("-1.235", 2); // => -1.24 (half-up away from zero)
|
|
3450
2814
|
*
|
|
3451
2815
|
* @example
|
|
3452
2816
|
* // Large values: parsed and rounded in fixed precision, then converted to number
|
|
3453
|
-
*
|
|
2817
|
+
* numNormalize("234893249238948.000003432", 6); // precise rounding in fixed space,
|
|
3454
2818
|
* // final result as JS number
|
|
3455
2819
|
*
|
|
3456
2820
|
* @example
|
|
3457
2821
|
* // Explicit passthrough (any `round <= 1` behaves the same)
|
|
3458
|
-
*
|
|
3459
|
-
*
|
|
2822
|
+
* numNormalize("1000.555", 1); // => 1000.555 (no rounding step invoked)
|
|
2823
|
+
* numNormalize("1000.555", 0); // => 1000.555 (still no rounding)
|
|
3460
2824
|
*
|
|
3461
2825
|
* @see parseToFixedDecimal
|
|
3462
2826
|
* @see fixedDecimalToNum
|
|
@@ -3466,278 +2830,7 @@ declare function fixedDecimalToStr(value: FixedDecimal): string;
|
|
|
3466
2830
|
* @since 2.0.0
|
|
3467
2831
|
* @public
|
|
3468
2832
|
*/
|
|
3469
|
-
declare function
|
|
3470
|
-
|
|
3471
|
-
/**
|
|
3472
|
-
* Normalizes an arbitrary numeric input into sign, integer, and fractional string parts
|
|
3473
|
-
* without losing precision, preparing it for exact fixed-decimal processing.
|
|
3474
|
-
*
|
|
3475
|
-
* @remarks
|
|
3476
|
-
* This utility accepts `bigint`, `number`, and `string` inputs and returns a tuple-like
|
|
3477
|
-
* object with three fields:
|
|
3478
|
-
*
|
|
3479
|
-
* - `sign`: `1` for non-negative values, `-1` for negative values.
|
|
3480
|
-
* - `integerPart`: the digits before the decimal point as a string (no sign, no separators).
|
|
3481
|
-
* - `fractionalPart`: the digits after the decimal point as a string (no sign, no separators).
|
|
3482
|
-
*
|
|
3483
|
-
* For JavaScript `number`s, the function first checks finiteness and then converts the
|
|
3484
|
-
* absolute value to an exponential string (via `toExponential(30)`) to avoid
|
|
3485
|
-
* floating-point artifacts when extracting digits. It delegates parsing of that exponential
|
|
3486
|
-
* form to {@link convertExponentialToParts}.
|
|
3487
|
-
*
|
|
3488
|
-
* For `string`s, the function:
|
|
3489
|
-
* - Trims whitespace and replaces a comma `,` decimal separator with a dot `.`.
|
|
3490
|
-
* - Parses and removes a leading `+`/`-` sign (if present).
|
|
3491
|
-
* - Validates the remaining characters with a permissive numeric pattern that allows:
|
|
3492
|
-
* - optional integer digits,
|
|
3493
|
-
* - an optional decimal point with digits,
|
|
3494
|
-
* - an optional scientific exponent (`e` or `E` with optional `+`/`-` and digits).
|
|
3495
|
-
* - If an exponent is present, it delegates to {@link convertExponentialToParts};
|
|
3496
|
-
* otherwise it splits on the decimal point.
|
|
3497
|
-
*
|
|
3498
|
-
* **Important:** This function does not strip leading zeros in `integerPart` or trailing
|
|
3499
|
-
* zeros in `fractionalPart`. If you need canonicalized output (e.g., remove leading/trailing
|
|
3500
|
-
* zeros), do that in a later step (e.g., when building your fixed-decimal representation).
|
|
3501
|
-
*
|
|
3502
|
-
* @param input - The numeric input to normalize. Supported types:
|
|
3503
|
-
* - `bigint` (e.g., `123n`, `-9007199254740993n`)
|
|
3504
|
-
* - `number` (finite only; `NaN`, `Infinity`, `-Infinity` will throw)
|
|
3505
|
-
* - `string` (e.g., `"42"`, `"-0.034"`, `"1.2e-5"`, with optional leading sign, optional
|
|
3506
|
-
* decimal part, optional exponent; commas are allowed as decimal separators and are
|
|
3507
|
-
* converted to dots)
|
|
3508
|
-
*
|
|
3509
|
-
* @returns An object with the normalized components:
|
|
3510
|
-
* ```ts
|
|
3511
|
-
* {
|
|
3512
|
-
* sign: 1 | -1;
|
|
3513
|
-
* integerPart: string;
|
|
3514
|
-
* fractionalPart: string;
|
|
3515
|
-
* }
|
|
3516
|
-
* ```
|
|
3517
|
-
* The `integerPart` and `fractionalPart` contain only ASCII digits (`0`–`9`) and
|
|
3518
|
-
* **never** include sign or exponent markers.
|
|
3519
|
-
*
|
|
3520
|
-
* @throws {Error}
|
|
3521
|
-
* - If `input` is a `number` but not finite (`NaN`, `Infinity`, `-Infinity`).
|
|
3522
|
-
* - If `input` is a `string` that is empty after trimming/replacement.
|
|
3523
|
-
* - If `input` is a `string` that fails the numeric validation regex.
|
|
3524
|
-
* - If `input` is of an unsupported type (not `bigint`, `number`, or `string`).
|
|
3525
|
-
* - Any errors propagated from {@link convertExponentialToParts} when parsing exponential forms.
|
|
3526
|
-
*
|
|
3527
|
-
* @example
|
|
3528
|
-
* // bigint input → fractional part is empty
|
|
3529
|
-
* normalizeToDecimalComponents(123n);
|
|
3530
|
-
* // => { sign: 1, integerPart: "123", fractionalPart: "" }
|
|
3531
|
-
*
|
|
3532
|
-
* @example
|
|
3533
|
-
* // negative bigint
|
|
3534
|
-
* normalizeToDecimalComponents(-987654321n);
|
|
3535
|
-
* // => { sign: -1, integerPart: "987654321", fractionalPart: "" }
|
|
3536
|
-
*
|
|
3537
|
-
* @example
|
|
3538
|
-
* // finite number input; uses exponential internally to avoid FP artifacts
|
|
3539
|
-
* normalizeToDecimalComponents(0.0000034);
|
|
3540
|
-
* // => { sign: 1, integerPart: "0", fractionalPart: "0000034" } // exact digits
|
|
3541
|
-
*
|
|
3542
|
-
* @example
|
|
3543
|
-
* // string with decimal comma and explicit sign
|
|
3544
|
-
* normalizeToDecimalComponents("+1,250");
|
|
3545
|
-
* // => { sign: 1, integerPart: "1", fractionalPart: "250" }
|
|
3546
|
-
*
|
|
3547
|
-
* @example
|
|
3548
|
-
* // scientific notation string
|
|
3549
|
-
* normalizeToDecimalComponents("-1.234e+3");
|
|
3550
|
-
* // => { sign: -1, integerPart: "1234", fractionalPart: "" }
|
|
3551
|
-
*
|
|
3552
|
-
* @example
|
|
3553
|
-
* // invalid string (throws)
|
|
3554
|
-
* normalizeToDecimalComponents("12.34.56"); // Error: Invalid numeric string.
|
|
3555
|
-
*
|
|
3556
|
-
* @see convertExponentialToParts
|
|
3557
|
-
* @category Parsing
|
|
3558
|
-
* @since 2.0.0
|
|
3559
|
-
* @public
|
|
3560
|
-
*/
|
|
3561
|
-
declare function normalizeToDecimalComponents(input: unknown): {
|
|
3562
|
-
sign: 1 | -1;
|
|
3563
|
-
integerPart: string;
|
|
3564
|
-
fractionalPart: string;
|
|
3565
|
-
};
|
|
3566
|
-
|
|
3567
|
-
/**
|
|
3568
|
-
* Parses any numeric input (`number`, `bigint`, or `string`) into an exact {@link FixedDecimal}
|
|
3569
|
-
* representation, preserving all digits without floating-point rounding errors.
|
|
3570
|
-
*
|
|
3571
|
-
* @remarks
|
|
3572
|
-
* This function transforms arbitrary numeric input into a **fixed-precision decimal structure**
|
|
3573
|
-
* that can safely represent extremely large or small values without losing information.
|
|
3574
|
-
* It achieves this by first decomposing the input using {@link normalizeToDecimalComponents}
|
|
3575
|
-
* and then constructing a `FixedDecimal` object consisting of:
|
|
3576
|
-
*
|
|
3577
|
-
* - `sign` → `1` for positive values, `-1` for negative values.
|
|
3578
|
-
* - `digitsInteger` → a `BigInt` that encodes *all digits* of the number as if
|
|
3579
|
-
* the decimal point were removed.
|
|
3580
|
-
* - `scale` → the count of digits that were originally after the decimal point.
|
|
3581
|
-
*
|
|
3582
|
-
* The resulting value can later be precisely converted back to a string or to a
|
|
3583
|
-
* JavaScript `number` using helpers like `fixedDecimalToStr` or `fixedDecimalToNum`.
|
|
3584
|
-
*
|
|
3585
|
-
* This process **avoids IEEE-754 floating-point artifacts**, ensuring mathematically
|
|
3586
|
-
* exact handling of decimal fractions (e.g. `"0.1" + "0.2" → 0.3` instead of `0.30000000000000004`).
|
|
3587
|
-
*
|
|
3588
|
-
* @param input - Any numeric source value. Supported types:
|
|
3589
|
-
* - `number` — finite only (`NaN`, `Infinity`, `-Infinity` will throw).
|
|
3590
|
-
* - `bigint` — directly converted without fractional digits.
|
|
3591
|
-
* - `string` — may contain optional sign, decimal point, or exponential notation (`1.23e-5`).
|
|
3592
|
-
* Commas `,` as decimal separators are also supported and converted to dots `.`.
|
|
3593
|
-
*
|
|
3594
|
-
* @returns A normalized {@link FixedDecimal} object:
|
|
3595
|
-
* ```ts
|
|
3596
|
-
* {
|
|
3597
|
-
* sign: 1 | -1; // the numeric sign
|
|
3598
|
-
* digitsInteger: bigint; // all digits as a BigInt, no decimal point
|
|
3599
|
-
* scale: number; // number of digits after the decimal
|
|
3600
|
-
* }
|
|
3601
|
-
* ```
|
|
3602
|
-
*
|
|
3603
|
-
* @throws {Error}
|
|
3604
|
-
* - If the input is not a supported type (`number`, `bigint`, or `string`).
|
|
3605
|
-
* - If the string cannot be parsed as a valid numeric representation.
|
|
3606
|
-
* - If the number is not finite.
|
|
3607
|
-
* - Any error propagated from {@link normalizeToDecimalComponents}.
|
|
3608
|
-
*
|
|
3609
|
-
* @example
|
|
3610
|
-
* // Simple integer number
|
|
3611
|
-
* parseToFixedDecimal(42);
|
|
3612
|
-
* // => { sign: 1, digitsInteger: 42n, scale: 0 }
|
|
3613
|
-
*
|
|
3614
|
-
* @example
|
|
3615
|
-
* // Decimal fraction
|
|
3616
|
-
* parseToFixedDecimal("123.456");
|
|
3617
|
-
* // => { sign: 1, digitsInteger: 123456n, scale: 3 }
|
|
3618
|
-
*
|
|
3619
|
-
* @example
|
|
3620
|
-
* // Number in exponential notation
|
|
3621
|
-
* parseToFixedDecimal("-1.23e-4");
|
|
3622
|
-
* // => { sign: -1, digitsInteger: 123n, scale: 6 } // represents -0.000123
|
|
3623
|
-
*
|
|
3624
|
-
* @example
|
|
3625
|
-
* // Leading zeros and trailing fractional zeros are trimmed internally
|
|
3626
|
-
* parseToFixedDecimal("00045.67000");
|
|
3627
|
-
* // => { sign: 1, digitsInteger: 4567n, scale: 2 } // "45.67"
|
|
3628
|
-
*
|
|
3629
|
-
* @example
|
|
3630
|
-
* // Very large integer input using bigint
|
|
3631
|
-
* parseToFixedDecimal(123456789012345678901234567890n);
|
|
3632
|
-
* // => { sign: 1, digitsInteger: 123456789012345678901234567890n, scale: 0 }
|
|
3633
|
-
*
|
|
3634
|
-
* @example
|
|
3635
|
-
* // Negative value with high precision fraction
|
|
3636
|
-
* parseToFixedDecimal("-0.00000003432");
|
|
3637
|
-
* // => { sign: -1, digitsInteger: 3432n, scale: 11 }
|
|
3638
|
-
*
|
|
3639
|
-
* @see normalizeToDecimalComponents
|
|
3640
|
-
* @see FixedDecimal
|
|
3641
|
-
* @see fixedDecimalToNum
|
|
3642
|
-
* @see fixedDecimalToStr
|
|
3643
|
-
*
|
|
3644
|
-
* @category Parsing
|
|
3645
|
-
* @since 2.0.0
|
|
3646
|
-
* @public
|
|
3647
|
-
*/
|
|
3648
|
-
declare function parseToFixedDecimal(input: unknown): FixedDecimal;
|
|
3649
|
-
|
|
3650
|
-
/**
|
|
3651
|
-
* Rounds a {@link FixedDecimal} value to the specified number of fractional digits,
|
|
3652
|
-
* using either **half-up** (standard rounding) or **truncation** (cut-off without rounding).
|
|
3653
|
-
*
|
|
3654
|
-
* @remarks
|
|
3655
|
-
* This function operates entirely in fixed-precision integer space (`BigInt` arithmetic),
|
|
3656
|
-
* so rounding is mathematically exact — it never suffers from floating-point errors.
|
|
3657
|
-
*
|
|
3658
|
-
* Internally, it determines how many fractional digits must be removed from
|
|
3659
|
-
* `source.digitsInteger` to reach the desired precision (`decimalPlaces`), divides
|
|
3660
|
-
* by `10 ** digitsToRemove`, and conditionally increments the result depending on
|
|
3661
|
-
* the remainder and rounding mode.
|
|
3662
|
-
*
|
|
3663
|
-
* It preserves the original `sign` and returns a new {@link FixedDecimal} object
|
|
3664
|
-
* with an updated `scale` (number of digits after the decimal point).
|
|
3665
|
-
*
|
|
3666
|
-
* ---
|
|
3667
|
-
* **Rounding modes:**
|
|
3668
|
-
* - `'half-up'` — standard rounding to nearest neighbor; `.5` rounds **away from zero**.
|
|
3669
|
-
* Example: `1.235 → 1.24`, `-1.235 → -1.24`.
|
|
3670
|
-
* - `'trunc'` — truncates digits beyond the target precision (rounds **toward zero**).
|
|
3671
|
-
* Example: `1.239 → 1.23`, `-1.239 → -1.23`.
|
|
3672
|
-
*
|
|
3673
|
-
* ---
|
|
3674
|
-
* @param source - The input {@link FixedDecimal} to round.
|
|
3675
|
-
* Must contain a valid combination of:
|
|
3676
|
-
* - `sign`: `1` or `-1`
|
|
3677
|
-
* - `digitsInteger`: a `BigInt` representing all digits (no decimal point)
|
|
3678
|
-
* - `scale`: number of digits after the decimal
|
|
3679
|
-
*
|
|
3680
|
-
* @param decimalPlaces - Target number of digits to keep **after the decimal point**.
|
|
3681
|
-
* Values less than 0 are treated as 0.
|
|
3682
|
-
* For example, rounding to `2` means "keep two digits after the decimal".
|
|
3683
|
-
*
|
|
3684
|
-
* @param roundMode - Rounding algorithm to use:
|
|
3685
|
-
* - `'half-up'` (default) → rounds 0.5 and higher up.
|
|
3686
|
-
* - `'trunc'` → simply removes extra digits without rounding up.
|
|
3687
|
-
*
|
|
3688
|
-
* @defaultValue `'half-up'`
|
|
3689
|
-
*
|
|
3690
|
-
* @returns A **new** {@link FixedDecimal} with the adjusted `digitsInteger`
|
|
3691
|
-
* and `scale` equal to `decimalPlaces`.
|
|
3692
|
-
* If the requested precision is greater than or equal to `source.scale`,
|
|
3693
|
-
* the source value is returned unchanged (shallow copy).
|
|
3694
|
-
*
|
|
3695
|
-
* @throws {Error}
|
|
3696
|
-
* - Never throws directly in normal use, but may propagate errors if invalid
|
|
3697
|
-
* numeric parameters are passed (e.g., non-integer `decimalPlaces`).
|
|
3698
|
-
*
|
|
3699
|
-
* ---
|
|
3700
|
-
* @example
|
|
3701
|
-
* // Half-up rounding
|
|
3702
|
-
* const value: FixedDecimal = { sign: 1, digitsInteger: 123456n, scale: 3 }; // 123.456
|
|
3703
|
-
* roundFixedDecimal(value, 2, 'half-up');
|
|
3704
|
-
* // => { sign: 1, digitsInteger: 12346n, scale: 2 } // 123.46
|
|
3705
|
-
*
|
|
3706
|
-
* @example
|
|
3707
|
-
* // Truncation (cut-off)
|
|
3708
|
-
* const value: FixedDecimal = { sign: 1, digitsInteger: 123456n, scale: 3 };
|
|
3709
|
-
* roundFixedDecimal(value, 2, 'trunc');
|
|
3710
|
-
* // => { sign: 1, digitsInteger: 12345n, scale: 2 } // 123.45
|
|
3711
|
-
*
|
|
3712
|
-
* @example
|
|
3713
|
-
* // No rounding needed (scale already smaller)
|
|
3714
|
-
* const v: FixedDecimal = { sign: -1, digitsInteger: 1234n, scale: 1 }; // -123.4
|
|
3715
|
-
* roundFixedDecimal(v, 3);
|
|
3716
|
-
* // => identical copy: { sign: -1, digitsInteger: 1234n, scale: 1 }
|
|
3717
|
-
*
|
|
3718
|
-
* @example
|
|
3719
|
-
* // Rounding very small values
|
|
3720
|
-
* const v: FixedDecimal = { sign: 1, digitsInteger: 123n, scale: 6 }; // 0.000123
|
|
3721
|
-
* roundFixedDecimal(v, 4);
|
|
3722
|
-
* // => { sign: 1, digitsInteger: 1n, scale: 4 } // 0.0001
|
|
3723
|
-
*
|
|
3724
|
-
* @example
|
|
3725
|
-
* // Rounding negative numbers (half-up → away from zero)
|
|
3726
|
-
* const v: FixedDecimal = { sign: -1, digitsInteger: 125n, scale: 2 }; // -1.25
|
|
3727
|
-
* roundFixedDecimal(v, 1, 'half-up');
|
|
3728
|
-
* // => { sign: -1, digitsInteger: 13n, scale: 1 } // -1.3
|
|
3729
|
-
*
|
|
3730
|
-
* @see FixedDecimal
|
|
3731
|
-
* @see parseToFixedDecimal
|
|
3732
|
-
* @see fixedDecimalToNum
|
|
3733
|
-
* @see fixedDecimalToStr
|
|
3734
|
-
* @see formatToNum
|
|
3735
|
-
*
|
|
3736
|
-
* @category Math
|
|
3737
|
-
* @since 2.0.0
|
|
3738
|
-
* @public
|
|
3739
|
-
*/
|
|
3740
|
-
declare function roundFixedDecimal(source: FixedDecimal, decimalPlaces: number, roundMode?: 'half-up' | 'trunc'): FixedDecimal;
|
|
2833
|
+
declare function numNormalize(value: unknown, round?: number, throwError?: boolean): number;
|
|
3741
2834
|
|
|
3742
2835
|
/**
|
|
3743
2836
|
* Converts any given string-like value into a lower-cased, trimmed string.
|
|
@@ -3753,7 +2846,7 @@ declare function roundFixedDecimal(source: FixedDecimal, decimalPlaces: number,
|
|
|
3753
2846
|
* @remarks
|
|
3754
2847
|
* ### Processing steps
|
|
3755
2848
|
* 1. **Type check** — ensures the input is a string using {@link isStr}.
|
|
3756
|
-
* 2. **Trimming** — removes leading and trailing whitespace via {@link
|
|
2849
|
+
* 2. **Trimming** — removes leading and trailing whitespace via {@link strTrim}.
|
|
3757
2850
|
* 3. **Validation** — ensures the result is non-empty with {@link isStrFilled}.
|
|
3758
2851
|
* 4. **Lowercasing** — calls `String.prototype.toLowerCase()` on the trimmed text.
|
|
3759
2852
|
* 5. If the string is empty or not valid at any step, returns `""`.
|
|
@@ -3763,7 +2856,7 @@ declare function roundFixedDecimal(source: FixedDecimal, decimalPlaces: number,
|
|
|
3763
2856
|
* - Non-string inputs (numbers, booleans, objects, arrays, `null`, `undefined`) all yield `""`.
|
|
3764
2857
|
*
|
|
3765
2858
|
* ### Use cases
|
|
3766
|
-
* - Case-insensitive string comparison (normalize both sides with `
|
|
2859
|
+
* - Case-insensitive string comparison (normalize both sides with `strLowerCase`).
|
|
3767
2860
|
* - Normalizing user input before storing or indexing.
|
|
3768
2861
|
* - Simplifying logic where optional strings may be `null` or empty.
|
|
3769
2862
|
*
|
|
@@ -3775,35 +2868,35 @@ declare function roundFixedDecimal(source: FixedDecimal, decimalPlaces: number,
|
|
|
3775
2868
|
*
|
|
3776
2869
|
* @example
|
|
3777
2870
|
* // Basic strings
|
|
3778
|
-
*
|
|
3779
|
-
*
|
|
2871
|
+
* strLowerCase('HELLO'); // => "hello"
|
|
2872
|
+
* strLowerCase(' TEST '); // => "test"
|
|
3780
2873
|
*
|
|
3781
2874
|
* @example
|
|
3782
2875
|
* // Mixed types
|
|
3783
|
-
*
|
|
3784
|
-
*
|
|
3785
|
-
*
|
|
2876
|
+
* strLowerCase(123); // => ""
|
|
2877
|
+
* strLowerCase(true); // => ""
|
|
2878
|
+
* strLowerCase(null); // => ""
|
|
3786
2879
|
*
|
|
3787
2880
|
* @example
|
|
3788
2881
|
* // Empty or whitespace inputs
|
|
3789
|
-
*
|
|
3790
|
-
*
|
|
2882
|
+
* strLowerCase(' '); // => ""
|
|
2883
|
+
* strLowerCase(''); // => ""
|
|
3791
2884
|
*
|
|
3792
2885
|
* @example
|
|
3793
2886
|
* // Already lowercase
|
|
3794
|
-
*
|
|
2887
|
+
* strLowerCase('data'); // => "data"
|
|
3795
2888
|
*
|
|
3796
2889
|
* @see isStr
|
|
3797
2890
|
* @see isStrFilled
|
|
3798
|
-
* @see
|
|
2891
|
+
* @see strTrim
|
|
3799
2892
|
*
|
|
3800
2893
|
* @category String
|
|
3801
2894
|
* @public
|
|
3802
2895
|
* @since 2.0.0
|
|
3803
2896
|
*/
|
|
3804
|
-
declare function
|
|
2897
|
+
declare function strLowerCase(value?: unknown): string;
|
|
3805
2898
|
|
|
3806
|
-
declare function
|
|
2899
|
+
declare function strNormalCase(value?: unknown): string;
|
|
3807
2900
|
|
|
3808
2901
|
/**
|
|
3809
2902
|
* Converts `undefined` or empty (whitespace-only) strings into `null`.
|
|
@@ -3819,14 +2912,14 @@ declare function formatToNormalCase(value?: unknown): string;
|
|
|
3819
2912
|
* @returns
|
|
3820
2913
|
* - `null` if:
|
|
3821
2914
|
* - The input is `undefined`, or
|
|
3822
|
-
* - The input is a string that becomes empty after trimming (using {@link
|
|
2915
|
+
* - The input is a string that becomes empty after trimming (using {@link strTrim}).
|
|
3823
2916
|
* - Otherwise returns the original `value`.
|
|
3824
2917
|
*
|
|
3825
2918
|
* @remarks
|
|
3826
2919
|
* ### Processing steps
|
|
3827
2920
|
* 1. If `value` is `undefined`, returns `null`.
|
|
3828
2921
|
* 2. If `value` is a string:
|
|
3829
|
-
* - Trims it with {@link
|
|
2922
|
+
* - Trims it with {@link strTrim}, removing whitespace and invisible characters.
|
|
3830
2923
|
* - If the trimmed result is empty (`''`), returns `null`.
|
|
3831
2924
|
* 3. Otherwise returns the original value unchanged.
|
|
3832
2925
|
*
|
|
@@ -3835,8 +2928,8 @@ declare function formatToNormalCase(value?: unknown): string;
|
|
|
3835
2928
|
* - The function is **pure** (non-mutating) and safe for use in JSON or ORM normalization.
|
|
3836
2929
|
* - Often used to prepare form data, REST payloads, or DB entities for consistent nullability.
|
|
3837
2930
|
*
|
|
3838
|
-
* ### Comparison with {@link
|
|
3839
|
-
* | Case | `
|
|
2931
|
+
* ### Comparison with {@link strUndefined}
|
|
2932
|
+
* | Case | `strNull` | `strUndefined` |
|
|
3840
2933
|
* |------|----------------|----------------------|
|
|
3841
2934
|
* | `undefined` | `null` | `undefined` |
|
|
3842
2935
|
* | `''` (empty string) | `null` | `undefined` |
|
|
@@ -3856,34 +2949,34 @@ declare function formatToNormalCase(value?: unknown): string;
|
|
|
3856
2949
|
*
|
|
3857
2950
|
* @example
|
|
3858
2951
|
* // Empty and whitespace strings
|
|
3859
|
-
*
|
|
3860
|
-
*
|
|
2952
|
+
* strNull(''); // => null
|
|
2953
|
+
* strNull(' '); // => null
|
|
3861
2954
|
*
|
|
3862
2955
|
* @example
|
|
3863
2956
|
* // Undefined also becomes null
|
|
3864
|
-
*
|
|
2957
|
+
* strNull(undefined); // => null
|
|
3865
2958
|
*
|
|
3866
2959
|
* @example
|
|
3867
2960
|
* // Non-empty strings remain as-is
|
|
3868
|
-
*
|
|
3869
|
-
*
|
|
2961
|
+
* strNull('Hello'); // => "Hello"
|
|
2962
|
+
* strNull(' Data '); // => " Data "
|
|
3870
2963
|
*
|
|
3871
2964
|
* @example
|
|
3872
2965
|
* // Non-string types are preserved
|
|
3873
|
-
*
|
|
3874
|
-
*
|
|
3875
|
-
*
|
|
3876
|
-
*
|
|
2966
|
+
* strNull(0); // => 0
|
|
2967
|
+
* strNull(false); // => false
|
|
2968
|
+
* strNull([]); // => []
|
|
2969
|
+
* strNull({}); // => {}
|
|
3877
2970
|
*
|
|
3878
2971
|
* @see isStr
|
|
3879
|
-
* @see
|
|
3880
|
-
* @see
|
|
2972
|
+
* @see strTrim
|
|
2973
|
+
* @see strUndefined
|
|
3881
2974
|
*
|
|
3882
2975
|
* @category String
|
|
3883
2976
|
* @public
|
|
3884
2977
|
* @since 2.0.0
|
|
3885
2978
|
*/
|
|
3886
|
-
declare function
|
|
2979
|
+
declare function strNull(value: unknown): {} | null;
|
|
3887
2980
|
|
|
3888
2981
|
/**
|
|
3889
2982
|
* Normalizes and validates a phone number into international format (`E.164` style).
|
|
@@ -3976,7 +3069,7 @@ declare function formatToNull(value: unknown): {} | null;
|
|
|
3976
3069
|
* formatToPhone(true); // => null
|
|
3977
3070
|
*
|
|
3978
3071
|
* @see isStr
|
|
3979
|
-
* @see
|
|
3072
|
+
* @see strTrim
|
|
3980
3073
|
*
|
|
3981
3074
|
* @category String
|
|
3982
3075
|
* @public
|
|
@@ -4041,23 +3134,23 @@ declare function formatToPhone(value?: unknown, defaultCountry?: string): string
|
|
|
4041
3134
|
*
|
|
4042
3135
|
* @example
|
|
4043
3136
|
* // Basic trimming
|
|
4044
|
-
*
|
|
3137
|
+
* strTrim(' Hello '); // => "Hello"
|
|
4045
3138
|
*
|
|
4046
3139
|
* @example
|
|
4047
3140
|
* // Removes zero-width characters
|
|
4048
|
-
*
|
|
3141
|
+
* strTrim('word\u200B'); // => "word"
|
|
4049
3142
|
*
|
|
4050
3143
|
* @example
|
|
4051
3144
|
* // Unicode normalization
|
|
4052
|
-
*
|
|
4053
|
-
*
|
|
3145
|
+
* strTrim('ABC'); // => "ABC"
|
|
3146
|
+
* strTrim('e\u0301'); // => "é"
|
|
4054
3147
|
*
|
|
4055
3148
|
* @example
|
|
4056
3149
|
* // Non-string inputs
|
|
4057
|
-
*
|
|
4058
|
-
*
|
|
4059
|
-
*
|
|
4060
|
-
*
|
|
3150
|
+
* strTrim(123); // => ""
|
|
3151
|
+
* strTrim(null); // => ""
|
|
3152
|
+
* strTrim(undefined); // => ""
|
|
3153
|
+
* strTrim({ text: 'hi' }); // => ""
|
|
4061
3154
|
*
|
|
4062
3155
|
* @see isStr
|
|
4063
3156
|
* @see String.prototype.normalize
|
|
@@ -4066,7 +3159,7 @@ declare function formatToPhone(value?: unknown, defaultCountry?: string): string
|
|
|
4066
3159
|
* @public
|
|
4067
3160
|
* @since 2.0.0
|
|
4068
3161
|
*/
|
|
4069
|
-
declare function
|
|
3162
|
+
declare function strTrim(value: unknown): string;
|
|
4070
3163
|
|
|
4071
3164
|
/**
|
|
4072
3165
|
* Converts `null` or empty (whitespace-only) strings into `undefined`.
|
|
@@ -4082,25 +3175,25 @@ declare function formatToTrim(value: unknown): string;
|
|
|
4082
3175
|
* @returns
|
|
4083
3176
|
* - `undefined` if:
|
|
4084
3177
|
* - The value is `null`, or
|
|
4085
|
-
* - The value is a string that becomes empty after trimming (via {@link
|
|
3178
|
+
* - The value is a string that becomes empty after trimming (via {@link strTrim}).
|
|
4086
3179
|
* - Otherwise returns the original value.
|
|
4087
3180
|
*
|
|
4088
3181
|
* @remarks
|
|
4089
3182
|
* ### Processing steps
|
|
4090
3183
|
* 1. If `value` is `null`, return `undefined`.
|
|
4091
3184
|
* 2. If `value` is a string:
|
|
4092
|
-
* - Trim using {@link
|
|
3185
|
+
* - Trim using {@link strTrim} to remove whitespace and invisible characters.
|
|
4093
3186
|
* - If trimmed result is empty, return `undefined`.
|
|
4094
3187
|
* 3. Otherwise, return the original value unchanged.
|
|
4095
3188
|
*
|
|
4096
3189
|
* ### Behavior notes
|
|
4097
3190
|
* - Non-string, non-null values (`0`, `false`, `{}`, `[]`, etc.) are **not modified**.
|
|
4098
3191
|
* - The function is **non-mutating** — it never changes the original reference.
|
|
4099
|
-
* - It complements {@link
|
|
3192
|
+
* - It complements {@link strNull}, depending on whether your system
|
|
4100
3193
|
* prefers `undefined` (omit field) or `null` (explicit empty value).
|
|
4101
3194
|
*
|
|
4102
|
-
* ### Comparison with {@link
|
|
4103
|
-
* | Case | `
|
|
3195
|
+
* ### Comparison with {@link strNull}
|
|
3196
|
+
* | Case | `strNull` | `strUndefined` |
|
|
4104
3197
|
* |------|----------------|----------------------|
|
|
4105
3198
|
* | `null` | `null` | `undefined` |
|
|
4106
3199
|
* | `undefined` | `null` | `undefined` |
|
|
@@ -4122,378 +3215,42 @@ declare function formatToTrim(value: unknown): string;
|
|
|
4122
3215
|
*
|
|
4123
3216
|
* @example
|
|
4124
3217
|
* // Empty and whitespace-only strings
|
|
4125
|
-
*
|
|
4126
|
-
*
|
|
3218
|
+
* strUndefined(''); // => undefined
|
|
3219
|
+
* strUndefined(' '); // => undefined
|
|
4127
3220
|
*
|
|
4128
3221
|
* @example
|
|
4129
3222
|
* // null is also normalized
|
|
4130
|
-
*
|
|
3223
|
+
* strUndefined(null); // => undefined
|
|
4131
3224
|
*
|
|
4132
3225
|
* @example
|
|
4133
3226
|
* // Non-empty strings remain unchanged
|
|
4134
|
-
*
|
|
3227
|
+
* strUndefined('Hello'); // => "Hello"
|
|
4135
3228
|
*
|
|
4136
3229
|
* @example
|
|
4137
3230
|
* // Non-string types are preserved
|
|
4138
|
-
*
|
|
4139
|
-
*
|
|
4140
|
-
*
|
|
4141
|
-
*
|
|
3231
|
+
* strUndefined(0); // => 0
|
|
3232
|
+
* strUndefined(false); // => false
|
|
3233
|
+
* strUndefined([]); // => []
|
|
3234
|
+
* strUndefined({}); // => {}
|
|
4142
3235
|
*
|
|
4143
3236
|
* @see isStr
|
|
4144
|
-
* @see
|
|
4145
|
-
* @see
|
|
3237
|
+
* @see strTrim
|
|
3238
|
+
* @see strNull
|
|
4146
3239
|
*
|
|
4147
3240
|
* @category String
|
|
4148
3241
|
* @public
|
|
4149
3242
|
* @since 2.0.0
|
|
4150
3243
|
*/
|
|
4151
|
-
declare function
|
|
4152
|
-
|
|
4153
|
-
declare function formatArrToSqlWhenCaseStr(d: Array<any>): string;
|
|
4154
|
-
|
|
4155
|
-
/**
|
|
4156
|
-
* Converts a numeric value with a given unit (bytes, KB, MB, GB, TB, PB)
|
|
4157
|
-
* into gigabytes (`GB`).
|
|
4158
|
-
*
|
|
4159
|
-
* @summary
|
|
4160
|
-
* Safely normalizes a size value of any supported unit into **gigabytes**,
|
|
4161
|
-
* using binary multiples (`1 KB = 1024 bytes`, `1 MB = 1024 KB`, etc.).
|
|
4162
|
-
*
|
|
4163
|
-
* @param value - Numeric value to convert (e.g., `512`, `"1.5"`, `"2048MB"`).
|
|
4164
|
-
* @param unit - Optional unit string indicating the scale of `value`.
|
|
4165
|
-
* Case-insensitive. Supported prefixes:
|
|
4166
|
-
* `"B"`, `"KB"`, `"MB"`, `"GB"`, `"TB"`, `"PB"`.
|
|
4167
|
-
*
|
|
4168
|
-
* @returns The value converted to gigabytes (`number`).
|
|
4169
|
-
*
|
|
4170
|
-
* @throws {Error}
|
|
4171
|
-
* Throws an error if the input value cannot be parsed as a finite number,
|
|
4172
|
-
* or if the computed result is `NaN` or `Infinity`.
|
|
4173
|
-
*
|
|
4174
|
-
* @remarks
|
|
4175
|
-
* ### Supported units
|
|
4176
|
-
* | Unit | Interpreted as | Conversion formula |
|
|
4177
|
-
* |------|----------------|--------------------|
|
|
4178
|
-
* | `b`, `B`, `bytes` | bytes | `v / GB` |
|
|
4179
|
-
* | `k`, `kb`, `kilobyte` | kilobytes | `v / MB` |
|
|
4180
|
-
* | `m`, `mb`, `megabyte` | megabytes | `v / GB` |
|
|
4181
|
-
* | `g`, `gb`, `gigabyte` | gigabytes | `v` |
|
|
4182
|
-
* | `t`, `tb`, `terabyte` | terabytes | `v * 1024` |
|
|
4183
|
-
* | `p`, `pb`, `petabyte` | petabytes | `v * (1024²)` |
|
|
4184
|
-
*
|
|
4185
|
-
* If no unit is provided, gigabytes (`GB`) are assumed by default.
|
|
4186
|
-
*
|
|
4187
|
-
* ### Normalization steps
|
|
4188
|
-
* 1. Convert `value` to a numeric value using {@link formatToNum}.
|
|
4189
|
-
* 2. Validate via {@link isNum} — must be finite.
|
|
4190
|
-
* 3. Normalize `unit`:
|
|
4191
|
-
* - Trim whitespace.
|
|
4192
|
-
* - Convert to lowercase.
|
|
4193
|
-
* - Extract first character (e.g., `'k'` for `"KB"`).
|
|
4194
|
-
* 4. Compute conversion factor depending on the first letter.
|
|
4195
|
-
* 5. Normalize `-0` to `0` before returning.
|
|
4196
|
-
*
|
|
4197
|
-
* ### Behavior notes
|
|
4198
|
-
* - Throws descriptive errors on invalid inputs.
|
|
4199
|
-
* - Supports string-based numeric inputs (`"2048"`, `"1.5"`, etc.).
|
|
4200
|
-
* - Ignores case and trailing spaces in the unit name.
|
|
4201
|
-
* - Uses binary (IEC-style) 1024-based units — not SI 1000-based.
|
|
4202
|
-
*
|
|
4203
|
-
* ### Performance
|
|
4204
|
-
* - Time complexity: **O(1)**.
|
|
4205
|
-
* - Space complexity: **O(1)**.
|
|
4206
|
-
*
|
|
4207
|
-
* ### Examples
|
|
4208
|
-
*
|
|
4209
|
-
* @example
|
|
4210
|
-
* // Basic conversions
|
|
4211
|
-
* toGB(1024, 'MB'); // => 1
|
|
4212
|
-
* toGB(1, 'GB'); // => 1
|
|
4213
|
-
* toGB(1, 'TB'); // => 1024
|
|
4214
|
-
* toGB(0.5, 'TB'); // => 512
|
|
4215
|
-
*
|
|
4216
|
-
* @example
|
|
4217
|
-
* // From bytes
|
|
4218
|
-
* toGB(1073741824, 'B'); // => 1
|
|
4219
|
-
* toGB('2147483648', 'b'); // => 2
|
|
4220
|
-
*
|
|
4221
|
-
* @example
|
|
4222
|
-
* // From petabytes
|
|
4223
|
-
* toGB(1, 'PB'); // => 1048576
|
|
4224
|
-
*
|
|
4225
|
-
* @example
|
|
4226
|
-
* // Auto-handling and normalization
|
|
4227
|
-
* toGB(' 1.5 ', ' mb '); // => 0.00146484375
|
|
4228
|
-
* toGB('2', ''); // => 2 (default is GB)
|
|
4229
|
-
*
|
|
4230
|
-
* @example
|
|
4231
|
-
* // Invalid inputs
|
|
4232
|
-
* toGB('abc', 'GB'); // throws Error("toGB: value "abc" is not numeric")
|
|
4233
|
-
* toGB(NaN, 'MB'); // throws Error("toGB: result is not finite...")
|
|
4234
|
-
*
|
|
4235
|
-
* @see isNum
|
|
4236
|
-
* @see formatToNum
|
|
4237
|
-
*
|
|
4238
|
-
* @category Conversion
|
|
4239
|
-
* @public
|
|
4240
|
-
* @since 2.0.0
|
|
4241
|
-
*/
|
|
4242
|
-
declare function toGB(value: unknown, unit?: string): number;
|
|
3244
|
+
declare function strUndefined(value: unknown): {} | undefined;
|
|
4243
3245
|
|
|
4244
|
-
|
|
4245
|
-
|
|
4246
|
-
|
|
4247
|
-
|
|
4248
|
-
|
|
4249
|
-
|
|
4250
|
-
|
|
4251
|
-
* @param value - Input numeric value or string representing a number (e.g. `"1200"`, `3.5`).
|
|
4252
|
-
* @param unit - Optional unit string representing the magnitude (`H`, `kH`, `MH`, `GH`, `TH`, `PH`, `EH`).
|
|
4253
|
-
* Case-insensitive and automatically normalized via {@link normalizeUnit}.
|
|
4254
|
-
*
|
|
4255
|
-
* @returns The normalized value expressed in **gigahashes per second (GH/s)**.
|
|
4256
|
-
* Returns `0` if the input is invalid or non-numeric.
|
|
4257
|
-
*
|
|
4258
|
-
* @remarks
|
|
4259
|
-
* ### Conversion logic
|
|
4260
|
-
* 1. **Parsing:**
|
|
4261
|
-
* Uses {@link formatToNum} to safely convert `value` into a number.
|
|
4262
|
-
*
|
|
4263
|
-
* 2. **Validation:**
|
|
4264
|
-
* If the parsed number is not finite (`NaN`, `Infinity`, etc.), returns `0`.
|
|
4265
|
-
*
|
|
4266
|
-
* 3. **Unit normalization:**
|
|
4267
|
-
* Calls {@link normalizeUnit} to clean up and simplify the unit name (e.g. `" MH/s "` → `"mh"`).
|
|
4268
|
-
* Then takes the first character (`m`, `g`, `t`, etc.) to determine scale.
|
|
4269
|
-
*
|
|
4270
|
-
* 4. **Factor lookup:**
|
|
4271
|
-
* Applies the following conversion factors relative to gigahashes:
|
|
4272
|
-
*
|
|
4273
|
-
* | Prefix | Unit | Factor (to GH) | Example conversion |
|
|
4274
|
-
* |---------|-----------|----------------|--------------------------------|
|
|
4275
|
-
* | `h` | hashes/s | `1e-9` | `1e9 H/s` = `1 GH/s` |
|
|
4276
|
-
* | `k` | kilohash | `1e-6` | `1e6 kH/s` = `1 GH/s` |
|
|
4277
|
-
* | `m` | megahash | `1e-3` | `1e3 MH/s` = `1 GH/s` |
|
|
4278
|
-
* | `g` | gigahash | `1` | identity |
|
|
4279
|
-
* | `t` | terahash | `1e3` | `1 TH/s` = `1000 GH/s` |
|
|
4280
|
-
* | `p` | petahash | `1e6` | `1 PH/s` = `1,000,000 GH/s` |
|
|
4281
|
-
* | `e` | exahash | `1e9` | `1 EH/s` = `1,000,000,000 GH/s`|
|
|
4282
|
-
*
|
|
4283
|
-
* 5. **Result validation:**
|
|
4284
|
-
* If the final result is not a finite number, throws an error.
|
|
4285
|
-
* Otherwise returns the computed gigahashes value (with `-0` normalized to `0`).
|
|
4286
|
-
*
|
|
4287
|
-
* ### Behavior notes
|
|
4288
|
-
* - Returns `0` instead of throwing when the input cannot be parsed as a number.
|
|
4289
|
-
* - Uses **decimal scaling** (`×1000`), not binary (`×1024`).
|
|
4290
|
-
* - Handles both numeric and string inputs seamlessly.
|
|
4291
|
-
* - Automatically ignores plural or rate suffixes (like `/s`, `hashes`).
|
|
4292
|
-
*
|
|
4293
|
-
* ### Performance
|
|
4294
|
-
* - Time complexity: **O(1)**.
|
|
4295
|
-
* - Space complexity: **O(1)**.
|
|
4296
|
-
*
|
|
4297
|
-
* ### Examples
|
|
4298
|
-
*
|
|
4299
|
-
* @example
|
|
4300
|
-
* // Basic conversions
|
|
4301
|
-
* toGH(1, 'GH'); // => 1
|
|
4302
|
-
* toGH(1, 'TH'); // => 1000
|
|
4303
|
-
* toGH(1, 'MH'); // => 0.001
|
|
4304
|
-
* toGH(1, 'kH'); // => 0.000001
|
|
4305
|
-
* toGH(1, 'PH'); // => 1_000_000
|
|
4306
|
-
* toGH(1, 'EH'); // => 1_000_000_000
|
|
4307
|
-
*
|
|
4308
|
-
* @example
|
|
4309
|
-
* // String inputs and normalization
|
|
4310
|
-
* toGH('1200', 'mh/s'); // => 1.2
|
|
4311
|
-
* toGH('3.5', 'TH/s'); // => 3500
|
|
4312
|
-
*
|
|
4313
|
-
* @example
|
|
4314
|
-
* // Invalid or edge inputs
|
|
4315
|
-
* toGH('', 'GH'); // => 0
|
|
4316
|
-
* toGH(null, 'GH'); // => 0
|
|
4317
|
-
* toGH(NaN, 'MH'); // => 0
|
|
4318
|
-
*
|
|
4319
|
-
* @example
|
|
4320
|
-
* // Custom normalization of unit formatting
|
|
4321
|
-
* toGH(5, ' khash / s '); // => 0.000005
|
|
4322
|
-
* toGH(2, 'T'); // => 2000
|
|
4323
|
-
*
|
|
4324
|
-
* @see formatToNum
|
|
4325
|
-
* @see isNum
|
|
4326
|
-
* @see normalizeUnit
|
|
4327
|
-
*
|
|
4328
|
-
* @category Conversion
|
|
4329
|
-
* @public
|
|
4330
|
-
* @since 2.0.0
|
|
4331
|
-
*/
|
|
4332
|
-
declare function toGH(value: any, unit?: string): number;
|
|
4333
|
-
|
|
4334
|
-
/**
|
|
4335
|
-
* Converts a rate value expressed with SI hash-rate prefixes into **hashes per second** (`H/s`).
|
|
4336
|
-
*
|
|
4337
|
-
* @summary
|
|
4338
|
-
* Normalizes a numeric value given in `kH/s`, `MH/s`, `GH/s`, `TH/s`, `PH/s`, or `EH/s`
|
|
4339
|
-
* to plain **hashes per second** using **decimal scaling** (×1000 between adjacent prefixes).
|
|
4340
|
-
* Units are case-insensitive and sanitized via {@link normalizeUnit}.
|
|
4341
|
-
*
|
|
4342
|
-
* @param value - Input value to convert (number or number-like string, e.g. `3.5`, `"1200"`).
|
|
4343
|
-
* @param unit - Optional unit string (e.g. `"kH/s"`, `"MH"`, `"ghashes"`).
|
|
4344
|
-
* If omitted or empty, the function assumes **`h`** (hashes).
|
|
4345
|
-
*
|
|
4346
|
-
* @returns The value converted to **H/s** (`number`). Returns `0` if `value` is not numeric.
|
|
4347
|
-
*
|
|
4348
|
-
* @remarks
|
|
4349
|
-
* ### Unit handling
|
|
4350
|
-
* `unit` is first cleaned by {@link normalizeUnit} (trim, lowercase, remove spaces and `"/s"`, `"hash(es)"`),
|
|
4351
|
-
* then the **first character** determines the scale:
|
|
4352
|
-
*
|
|
4353
|
-
* | Prefix | Interpreted as | Multiplier to H/s |
|
|
4354
|
-
* |:------:|-----------------|-------------------|
|
|
4355
|
-
* | `h` | hashes | `1` |
|
|
4356
|
-
* | `k` | kilohashes | `1e3` |
|
|
4357
|
-
* | `m` | megahashes | `1e6` |
|
|
4358
|
-
* | `g` | gigahashes | `1e9` |
|
|
4359
|
-
* | `t` | terahashes | `1e12` |
|
|
4360
|
-
* | `p` | petahashes | `1e15` |
|
|
4361
|
-
* | `e` | exahashes | `1e18` |
|
|
4362
|
-
*
|
|
4363
|
-
* Unknown or empty prefixes default to `h` (no scaling).
|
|
4364
|
-
*
|
|
4365
|
-
* ### Parsing & validation
|
|
4366
|
-
* - Uses {@link formatToNum} to parse `value`.
|
|
4367
|
-
* - If parsed value is not a finite number, returns `0`.
|
|
4368
|
-
* - After conversion, if the result is not finite, throws an error with details.
|
|
4369
|
-
* - Normalizes `-0` to `0` before returning.
|
|
4370
|
-
*
|
|
4371
|
-
* ### Decimal vs binary
|
|
4372
|
-
* This function uses **decimal** (SI) steps (×1000) common for hashrates.
|
|
4373
|
-
* For byte/size conversions use binary (×1024) helpers like `toGB`.
|
|
4374
|
-
*
|
|
4375
|
-
* ### Performance
|
|
4376
|
-
* - Time complexity: **O(1)**
|
|
4377
|
-
* - Space complexity: **O(1)**
|
|
4378
|
-
*
|
|
4379
|
-
* ### Examples
|
|
4380
|
-
*
|
|
4381
|
-
* @example
|
|
4382
|
-
* // Identity and basic scales
|
|
4383
|
-
* toH(1, 'H'); // => 1
|
|
4384
|
-
* toH(1, 'kH'); // => 1_000
|
|
4385
|
-
* toH(1, 'MH'); // => 1_000_000
|
|
4386
|
-
* toH(1, 'GH'); // => 1_000_000_000
|
|
4387
|
-
* toH(2.5, 'TH'); // => 2_500_000_000_000
|
|
4388
|
-
*
|
|
4389
|
-
* @example
|
|
4390
|
-
* // Large magnitudes
|
|
4391
|
-
* toH(1, 'PH'); // => 1_000_000_000_000_000
|
|
4392
|
-
* toH(0.01, 'EH'); // => 10_000_000_000_000_000
|
|
4393
|
-
*
|
|
4394
|
-
* @example
|
|
4395
|
-
* // Flexible, noisy units and strings
|
|
4396
|
-
* toH('1200', ' mh/s '); // => 1_200_000
|
|
4397
|
-
* toH(3.2, 'khashes'); // => 3_200
|
|
4398
|
-
* toH('5', ''); // => 5 (defaults to hashes)
|
|
4399
|
-
*
|
|
4400
|
-
* @example
|
|
4401
|
-
* // Invalid inputs
|
|
4402
|
-
* toH('', 'GH'); // => 0
|
|
4403
|
-
* toH(null, 'MH'); // => 0
|
|
4404
|
-
* toH(NaN, 'kH'); // => 0
|
|
4405
|
-
*
|
|
4406
|
-
* @see normalizeUnit
|
|
4407
|
-
* @see formatToNum
|
|
4408
|
-
* @see isNum
|
|
4409
|
-
*
|
|
4410
|
-
* @category Conversion
|
|
4411
|
-
* @public
|
|
4412
|
-
* @since 2.0.0
|
|
4413
|
-
*/
|
|
4414
|
-
declare function toH(value: any, unit?: string): number;
|
|
3246
|
+
type UrlObj = {
|
|
3247
|
+
protocol: string;
|
|
3248
|
+
user: string;
|
|
3249
|
+
url: string;
|
|
3250
|
+
port: number;
|
|
3251
|
+
path: string;
|
|
3252
|
+
};
|
|
4415
3253
|
|
|
4416
|
-
|
|
4417
|
-
* Extracts the **hostname** (IPv4, IPv6, or domain) from a URL-like string.
|
|
4418
|
-
*
|
|
4419
|
-
* @summary
|
|
4420
|
-
* Parses a given string as a URL or host reference and returns the **host component**
|
|
4421
|
-
* (without protocol, port, path, query, or credentials).
|
|
4422
|
-
* Handles both valid URLs (via {@link URL}) and malformed or scheme-less inputs using fallback parsing.
|
|
4423
|
-
*
|
|
4424
|
-
* @param value - Any string that may represent a URL, host, or address.
|
|
4425
|
-
*
|
|
4426
|
-
* @returns
|
|
4427
|
-
* The hostname (domain or IP address) as a plain string.
|
|
4428
|
-
* Returns an empty string (`""`) if the input is blank or cannot be parsed.
|
|
4429
|
-
*
|
|
4430
|
-
* @remarks
|
|
4431
|
-
* ### Parsing strategy
|
|
4432
|
-
* 1. **Scheme detection:**
|
|
4433
|
-
* - If the input starts with a protocol scheme (e.g., `http:`, `ftp:`), it is parsed directly.
|
|
4434
|
-
* - Otherwise, a default `http://` prefix is prepended so the built-in {@link URL} parser can process it.
|
|
4435
|
-
*
|
|
4436
|
-
* 2. **Primary parsing:**
|
|
4437
|
-
* - Uses the native {@link URL} class to extract `hostname`.
|
|
4438
|
-
* - Automatically handles IPv6 literals, punycode domains, and IDNs.
|
|
4439
|
-
*
|
|
4440
|
-
* 3. **Fallback parsing (for invalid URLs):**
|
|
4441
|
-
* - Strips credentials (`user:pass@`).
|
|
4442
|
-
* - Extracts the first segment before `/`, `?`, or `#`.
|
|
4443
|
-
* - If the result looks like `[::1]` or `[2001:db8::1]`, returns the IPv6 address inside brackets.
|
|
4444
|
-
* - Otherwise, takes everything before the first colon (`:`) as the hostname.
|
|
4445
|
-
*
|
|
4446
|
-
* ### Supported formats
|
|
4447
|
-
* - Full URLs:
|
|
4448
|
-
* `"https://user:pass@sub.example.com:8080/path"` → `"sub.example.com"`
|
|
4449
|
-
* - Host with port:
|
|
4450
|
-
* `"example.com:3000"` → `"example.com"`
|
|
4451
|
-
* - IPv4:
|
|
4452
|
-
* `"192.168.1.1:4028"` → `"192.168.1.1"`
|
|
4453
|
-
* - IPv6 (with or without brackets):
|
|
4454
|
-
* `"[2001:db8::1]:4028"` → `"2001:db8::1"`
|
|
4455
|
-
* - Without scheme:
|
|
4456
|
-
* `"example.com/test?q=1"` → `"example.com"`
|
|
4457
|
-
*
|
|
4458
|
-
* ### Behavior notes
|
|
4459
|
-
* - Returns an empty string if `value` is empty, blank, or unparsable.
|
|
4460
|
-
* - Never throws — all exceptions are caught and handled gracefully.
|
|
4461
|
-
* - Does **not** include port numbers or authentication info.
|
|
4462
|
-
* - Automatically trims whitespace before parsing.
|
|
4463
|
-
*
|
|
4464
|
-
* ### Performance
|
|
4465
|
-
* - Time complexity: **O(n)** (linear in string length).
|
|
4466
|
-
* - Space complexity: **O(n)** (creates trimmed and split substrings).
|
|
4467
|
-
*
|
|
4468
|
-
* ### Examples
|
|
4469
|
-
*
|
|
4470
|
-
* @example
|
|
4471
|
-
* // Standard URLs
|
|
4472
|
-
* extractHost('https://example.com/path'); // => "example.com"
|
|
4473
|
-
* extractHost('http://user:pass@site.org:8080'); // => "site.org"
|
|
4474
|
-
*
|
|
4475
|
-
* @example
|
|
4476
|
-
* // Without scheme
|
|
4477
|
-
* extractHost('example.com/foo/bar'); // => "example.com"
|
|
4478
|
-
* extractHost('sub.domain.net:3000'); // => "sub.domain.net"
|
|
4479
|
-
*
|
|
4480
|
-
* @example
|
|
4481
|
-
* // IP addresses
|
|
4482
|
-
* extractHost('192.168.0.10:4028'); // => "192.168.0.10"
|
|
4483
|
-
* extractHost('[2001:db8::1]:80'); // => "2001:db8::1"
|
|
4484
|
-
*
|
|
4485
|
-
* @example
|
|
4486
|
-
* // Invalid or blank input
|
|
4487
|
-
* extractHost(''); // => ""
|
|
4488
|
-
* extractHost('not a valid host'); // => "not"
|
|
4489
|
-
*
|
|
4490
|
-
* @see URL
|
|
4491
|
-
* @see isStrFilled
|
|
4492
|
-
*
|
|
4493
|
-
* @category Network
|
|
4494
|
-
* @public
|
|
4495
|
-
* @since 2.0.0
|
|
4496
|
-
*/
|
|
4497
|
-
declare function extractHost(value?: string): string;
|
|
3254
|
+
declare function urlObj(value?: string): UrlObj;
|
|
4498
3255
|
|
|
4499
|
-
export { type
|
|
3256
|
+
export { type JSONLike, type PasswordOptions, type TimeParts, arrFuncArgs, arrSplitPortions, boolNormalize, dateFloorMin, datePartsSec, dateSecParts, dateStr, formatToPhone, ipNumStr, ipStrNum, isArr, isArrFilled, isBool, isClass, isDate, isEmail, isExists, isFunc, isIpAddr, isMacAddr, isNum, isNumFloat, isNumN, isNumNZ, isNumP, isNumPZ, isObj, isObjFilled, isPassword, isPhone, isStr, isStrAscDesc, isStrBool, isStrFilled, isVar, jsonDecode, jsonEncode, jsonParse, jsonStrLike, numNormalize, strLowerCase, strNormalCase, strNull, strTrim, strUndefined, urlObj, wait };
|