@helpers4/number 2.0.2 → 2.0.4

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/meta/api.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "category": "number",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "functions": [
5
5
  {
6
6
  "name": "clamp",
@@ -46,12 +46,12 @@
46
46
  {
47
47
  "name": "correctFloat",
48
48
  "kind": "function",
49
- "description": "Corrects floating-point arithmetic errors by rounding to a given number\nof significant digits. Useful after calculations that accumulate binary\nfloating-point drift (e.g. `0.1 + 0.2 === 0.30000000000000004`).\n\nThe default precision of 14 significant digits eliminates typical\nrounding noise for values in the range used by most applications.\nNote: for values whose integer part already consumes 14 or more digits\n(i.e. |value| ≥ 1e13), toPrecision(14) has no room left for decimal\ndigits and will silently truncate them. Increase `precision` if you\nneed to correct drift in very large numbers.",
49
+ "description": "Corrects floating-point arithmetic errors by rounding to a given number\nof significant digits. Useful after calculations that accumulate binary\nfloating-point drift (e.g. `0.1 + 0.2 === 0.30000000000000004`).\n\nThe default precision of 14 significant digits eliminates typical\nrounding noise for values in the range used by most applications.\nNote: for values whose integer part already consumes 14 or more digits\n(i.e. |value| ≥ 1e13), toPrecision(14) has no room left for decimal\ndigits and will silently truncate them. Increase `precision` if you\nneed to correct drift in very large numbers.\n\nNote: IEEE-754 doubles carry at most ~17 significant decimal digits.\nPrecision values above 17 pad with digits that reflect the underlying\nbinary representation rather than correcting drift.",
50
50
  "since": "2.0.2",
51
51
  "signatures": [
52
52
  {
53
53
  "signature": "correctFloat(value: number, precision: number): number",
54
- "description": "Corrects floating-point arithmetic errors by rounding to a given number\nof significant digits. Useful after calculations that accumulate binary\nfloating-point drift (e.g. `0.1 + 0.2 === 0.30000000000000004`).\n\nThe default precision of 14 significant digits eliminates typical\nrounding noise for values in the range used by most applications.\nNote: for values whose integer part already consumes 14 or more digits\n(i.e. |value| ≥ 1e13), toPrecision(14) has no room left for decimal\ndigits and will silently truncate them. Increase `precision` if you\nneed to correct drift in very large numbers.",
54
+ "description": "Corrects floating-point arithmetic errors by rounding to a given number\nof significant digits. Useful after calculations that accumulate binary\nfloating-point drift (e.g. `0.1 + 0.2 === 0.30000000000000004`).\n\nThe default precision of 14 significant digits eliminates typical\nrounding noise for values in the range used by most applications.\nNote: for values whose integer part already consumes 14 or more digits\n(i.e. |value| ≥ 1e13), toPrecision(14) has no room left for decimal\ndigits and will silently truncate them. Increase `precision` if you\nneed to correct drift in very large numbers.\n\nNote: IEEE-754 doubles carry at most ~17 significant decimal digits.\nPrecision values above 17 pad with digits that reflect the underlying\nbinary representation rather than correcting drift.",
55
55
  "params": [
56
56
  {
57
57
  "name": "value",
@@ -61,7 +61,7 @@
61
61
  {
62
62
  "name": "precision",
63
63
  "type": "number",
64
- "description": "Integer number of significant digits (default: 14)",
64
+ "description": "Integer number of significant digits between 1 and 100\n (default: 14). Values above 17 are valid but expose binary noise beyond\n IEEE-754's meaningful range.",
65
65
  "defaultValue": "14"
66
66
  }
67
67
  ],
@@ -85,6 +85,43 @@
85
85
  ],
86
86
  "sourceFile": "correctFloat.ts"
87
87
  },
88
+ {
89
+ "name": "extractNumber",
90
+ "kind": "function",
91
+ "description": "Extracts the first number embedded anywhere in a string, or passes through a `number`.\n\nUnlike a plain `parseFloat`/`parseInt`, the number does not need to be at the start of\nthe string: digits are searched for anywhere, so leading/trailing text (units, labels, ...)\nis ignored. A `-` before the digits and a scientific-notation suffix (`e`/`E`) are\ndisambiguated with ExtractNumberOptions.sign and ExtractNumberOptions.exponent.\n\nReturns `undefined` if no number can be found.",
92
+ "since": "2.0.4",
93
+ "signatures": [
94
+ {
95
+ "signature": "extractNumber(value: unknown, options: ExtractNumberOptions): number | undefined",
96
+ "description": "Extracts the first number embedded anywhere in a string, or passes through a `number`.\n\nUnlike a plain `parseFloat`/`parseInt`, the number does not need to be at the start of\nthe string: digits are searched for anywhere, so leading/trailing text (units, labels, ...)\nis ignored. A `-` before the digits and a scientific-notation suffix (`e`/`E`) are\ndisambiguated with ExtractNumberOptions.sign and ExtractNumberOptions.exponent.\n\nReturns `undefined` if no number can be found.",
97
+ "params": [
98
+ {
99
+ "name": "value",
100
+ "type": "unknown",
101
+ "description": "The value to extract a number from"
102
+ },
103
+ {
104
+ "name": "options",
105
+ "type": "ExtractNumberOptions",
106
+ "description": "Options controlling sign and exponent disambiguation",
107
+ "defaultValue": "{}"
108
+ }
109
+ ],
110
+ "returns": {
111
+ "type": "number | undefined",
112
+ "description": "The extracted number, or `undefined` if none was found"
113
+ }
114
+ }
115
+ ],
116
+ "examples": [
117
+ {
118
+ "title": "extractNumber",
119
+ "description": "",
120
+ "code": "```ts\nextractNumber('16.5px') // => 16.5\nextractNumber('.5rem') // => 0.5 (leading-dot decimal)\nextractNumber('-.5') // => -0.5 (leading-dot with sign)\nextractNumber('Wafer 10') // => 10\nextractNumber('xxx-111') // => 111 ('-' glued to text → separator)\nextractNumber('xxx -111') // => -111 ('-' preceded by a space → sign)\nextractNumber('x-.5') // => 0.5 ('-' glued to 'x' → separator; leading-dot decimal follows)\nextractNumber('-111') // => -111 ('-' at the start of the string → sign)\nextractNumber('1e5 mol') // => 100000\nextractNumber('1e5kg') // => 1 ('e5' glued to text → mantissa only)\nextractNumber('no number') // => undefined\nextractNumber(42) // => 42\n```"
121
+ }
122
+ ],
123
+ "sourceFile": "extractNumber.ts"
124
+ },
88
125
  {
89
126
  "name": "formatCompact",
90
127
  "kind": "function",
@@ -208,14 +245,141 @@
208
245
  "code": "inRange(5, 1, 10, { inclusive: 'none' }) // => true\ninRange(1, 1, 10, { inclusive: 'none' }) // => false\ninRange(10, 1, 10, { inclusive: 'none' }) // => false"
209
246
  }
210
247
  ],
211
- "sourceFile": "inRange.ts",
212
- "relatedTypes": [
248
+ "sourceFile": "inRange.ts"
249
+ },
250
+ {
251
+ "name": "isEven",
252
+ "kind": "function",
253
+ "description": "Checks if a value is an even integer.\n\nReturns `false` for non-numbers, non-integers, `NaN`, `Infinity`, and odd integers.",
254
+ "since": "2.0.3",
255
+ "signatures": [
256
+ {
257
+ "signature": "isEven(value: unknown): value is number",
258
+ "description": "Checks if a value is an even integer.\n\nReturns `false` for non-numbers, non-integers, `NaN`, `Infinity`, and odd integers.",
259
+ "params": [
260
+ {
261
+ "name": "value",
262
+ "type": "unknown",
263
+ "description": "The value to check"
264
+ }
265
+ ],
266
+ "returns": {
267
+ "type": "value is number",
268
+ "description": "`true` if value is an integer divisible by 2"
269
+ }
270
+ }
271
+ ],
272
+ "examples": [
273
+ {
274
+ "title": "Check if a number is even",
275
+ "description": "Returns true for integers divisible by 2, false otherwise.",
276
+ "code": "isEven(4) // => true\nisEven(0) // => true\nisEven(3) // => false\nisEven(1.5) // => false (not an integer)"
277
+ },
278
+ {
279
+ "title": "Filter even numbers from an array",
280
+ "description": "Use as a predicate in .filter() to extract even integers.",
281
+ "code": "const nums = [1, 2, 3, 4, 5, 6];\nnums.filter(isEven)\n// => [2, 4, 6]"
282
+ }
283
+ ],
284
+ "sourceFile": "isEven.ts"
285
+ },
286
+ {
287
+ "name": "isNegative",
288
+ "kind": "function",
289
+ "description": "Checks if a value is a number less than 0.\n\nReturns `false` for `NaN`, `0`, positive numbers, and non-number types.",
290
+ "since": "2.0.0",
291
+ "signatures": [
292
+ {
293
+ "signature": "isNegative(value: unknown): value is number",
294
+ "description": "Checks if a value is a number less than 0.\n\nReturns `false` for `NaN`, `0`, positive numbers, and non-number types.",
295
+ "params": [
296
+ {
297
+ "name": "value",
298
+ "type": "unknown",
299
+ "description": "The value to check"
300
+ }
301
+ ],
302
+ "returns": {
303
+ "type": "value is number",
304
+ "description": "True if value is a negative number"
305
+ }
306
+ }
307
+ ],
308
+ "examples": [
309
+ {
310
+ "title": "isNegative",
311
+ "description": "",
312
+ "code": "```ts\nisNegative(-1) // => true\nisNegative(-0.5) // => true\nisNegative(-Infinity) // => true\nisNegative(0) // => false\nisNegative(1) // => false\nisNegative(NaN) // => false\n```"
313
+ }
314
+ ],
315
+ "sourceFile": "isNegative.ts"
316
+ },
317
+ {
318
+ "name": "isOdd",
319
+ "kind": "function",
320
+ "description": "Checks if a value is an odd integer.\n\nReturns `false` for non-numbers, non-integers, `NaN`, `Infinity`, and even integers.",
321
+ "since": "2.0.3",
322
+ "signatures": [
323
+ {
324
+ "signature": "isOdd(value: unknown): value is number",
325
+ "description": "Checks if a value is an odd integer.\n\nReturns `false` for non-numbers, non-integers, `NaN`, `Infinity`, and even integers.",
326
+ "params": [
327
+ {
328
+ "name": "value",
329
+ "type": "unknown",
330
+ "description": "The value to check"
331
+ }
332
+ ],
333
+ "returns": {
334
+ "type": "value is number",
335
+ "description": "`true` if value is an integer not divisible by 2"
336
+ }
337
+ }
338
+ ],
339
+ "examples": [
340
+ {
341
+ "title": "Check if a number is odd",
342
+ "description": "Returns true for integers not divisible by 2, false otherwise.",
343
+ "code": "isOdd(3) // => true\nisOdd(1) // => true\nisOdd(2) // => false\nisOdd(0) // => false\nisOdd(1.5) // => false (not an integer)"
344
+ },
213
345
  {
214
- "name": "InRangeOptions",
215
- "description": "Options for inRange.",
216
- "typeDefinition": "interface InRangeOptions {\n inclusive?: \"min\" | \"max\" | \"both\" | \"none\";\n}"
346
+ "title": "Filter odd numbers from an array",
347
+ "description": "Use as a predicate in .filter() to extract odd integers.",
348
+ "code": "const nums = [1, 2, 3, 4, 5, 6];\nnums.filter(isOdd)\n// => [1, 3, 5]"
217
349
  }
218
- ]
350
+ ],
351
+ "sourceFile": "isOdd.ts"
352
+ },
353
+ {
354
+ "name": "isPositive",
355
+ "kind": "function",
356
+ "description": "Checks if a value is a number greater than 0.\n\nReturns `false` for `NaN`, `0`, negative numbers, and non-number types.",
357
+ "since": "2.0.0",
358
+ "signatures": [
359
+ {
360
+ "signature": "isPositive(value: unknown): value is number",
361
+ "description": "Checks if a value is a number greater than 0.\n\nReturns `false` for `NaN`, `0`, negative numbers, and non-number types.",
362
+ "params": [
363
+ {
364
+ "name": "value",
365
+ "type": "unknown",
366
+ "description": "The value to check"
367
+ }
368
+ ],
369
+ "returns": {
370
+ "type": "value is number",
371
+ "description": "True if value is a positive number"
372
+ }
373
+ }
374
+ ],
375
+ "examples": [
376
+ {
377
+ "title": "isPositive",
378
+ "description": "",
379
+ "code": "```ts\nisPositive(42) // => true\nisPositive(0.1) // => true\nisPositive(Infinity) // => true\nisPositive(0) // => false\nisPositive(-1) // => false\nisPositive(NaN) // => false\n```"
380
+ }
381
+ ],
382
+ "sourceFile": "isPositive.ts"
219
383
  },
220
384
  {
221
385
  "name": "lerp",
@@ -263,37 +427,6 @@
263
427
  ],
264
428
  "sourceFile": "lerp.ts"
265
429
  },
266
- {
267
- "name": "mean",
268
- "kind": "function",
269
- "description": "Calculates the arithmetic mean (average) of an array of numbers.\nReturns `NaN` for an empty array.\n\nPairs with sum for aggregate operations.",
270
- "since": "2.0.0",
271
- "signatures": [
272
- {
273
- "signature": "mean(array: readonly number[]): number",
274
- "description": "Calculates the arithmetic mean (average) of an array of numbers.\nReturns `NaN` for an empty array.\n\nPairs with sum for aggregate operations.",
275
- "params": [
276
- {
277
- "name": "array",
278
- "type": "readonly number[]",
279
- "description": "The array of numbers to average"
280
- }
281
- ],
282
- "returns": {
283
- "type": "number",
284
- "description": "The arithmetic mean, or `NaN` if the array is empty"
285
- }
286
- }
287
- ],
288
- "examples": [
289
- {
290
- "title": "Average a list of numbers",
291
- "description": "Returns the arithmetic mean of the array; NaN for empty arrays.",
292
- "code": "mean([1, 2, 3, 4]) // => 2.5\nmean([10, 20, 30]) // => 20\nmean([]) // => NaN"
293
- }
294
- ],
295
- "sourceFile": "mean.ts"
296
- },
297
430
  {
298
431
  "name": "randomBetween",
299
432
  "kind": "function",
@@ -405,42 +538,6 @@
405
538
  }
406
539
  ],
407
540
  "sourceFile": "roundTo.ts"
408
- },
409
- {
410
- "name": "sum",
411
- "kind": "function",
412
- "description": "Calculates the sum of an array of numbers.",
413
- "since": "2.0.0",
414
- "signatures": [
415
- {
416
- "signature": "sum(array: readonly number[]): number",
417
- "description": "Calculates the sum of an array of numbers.",
418
- "params": [
419
- {
420
- "name": "array",
421
- "type": "readonly number[]",
422
- "description": "The array of numbers to sum"
423
- }
424
- ],
425
- "returns": {
426
- "type": "number",
427
- "description": "The sum of all values"
428
- }
429
- }
430
- ],
431
- "examples": [
432
- {
433
- "title": "Sum numbers",
434
- "description": "Calculates the sum of an array of numbers.",
435
- "code": "sum([1, 2, 3, 4])\n// => 10"
436
- },
437
- {
438
- "title": "Sum with negative numbers",
439
- "description": "Handles negative numbers correctly.",
440
- "code": "sum([10, -3, 5, -2])\n// => 10"
441
- }
442
- ],
443
- "sourceFile": "sum.ts"
444
541
  }
445
542
  ]
446
543
  }
@@ -26,6 +26,16 @@
26
26
  }
27
27
  ]
28
28
  },
29
+ {
30
+ "name": "extractNumber",
31
+ "examples": [
32
+ {
33
+ "title": "extractNumber",
34
+ "description": "",
35
+ "code": "```ts\nextractNumber('16.5px') // => 16.5\nextractNumber('.5rem') // => 0.5 (leading-dot decimal)\nextractNumber('-.5') // => -0.5 (leading-dot with sign)\nextractNumber('Wafer 10') // => 10\nextractNumber('xxx-111') // => 111 ('-' glued to text → separator)\nextractNumber('xxx -111') // => -111 ('-' preceded by a space → sign)\nextractNumber('x-.5') // => 0.5 ('-' glued to 'x' → separator; leading-dot decimal follows)\nextractNumber('-111') // => -111 ('-' at the start of the string → sign)\nextractNumber('1e5 mol') // => 100000\nextractNumber('1e5kg') // => 1 ('e5' glued to text → mantissa only)\nextractNumber('no number') // => undefined\nextractNumber(42) // => 42\n```"
36
+ }
37
+ ]
38
+ },
29
39
  {
30
40
  "name": "formatCompact",
31
41
  "examples": [
@@ -66,6 +76,56 @@
66
76
  }
67
77
  ]
68
78
  },
79
+ {
80
+ "name": "isEven",
81
+ "examples": [
82
+ {
83
+ "title": "Check if a number is even",
84
+ "description": "Returns true for integers divisible by 2, false otherwise.",
85
+ "code": "isEven(4) // => true\nisEven(0) // => true\nisEven(3) // => false\nisEven(1.5) // => false (not an integer)"
86
+ },
87
+ {
88
+ "title": "Filter even numbers from an array",
89
+ "description": "Use as a predicate in .filter() to extract even integers.",
90
+ "code": "const nums = [1, 2, 3, 4, 5, 6];\nnums.filter(isEven)\n// => [2, 4, 6]"
91
+ }
92
+ ]
93
+ },
94
+ {
95
+ "name": "isNegative",
96
+ "examples": [
97
+ {
98
+ "title": "isNegative",
99
+ "description": "",
100
+ "code": "```ts\nisNegative(-1) // => true\nisNegative(-0.5) // => true\nisNegative(-Infinity) // => true\nisNegative(0) // => false\nisNegative(1) // => false\nisNegative(NaN) // => false\n```"
101
+ }
102
+ ]
103
+ },
104
+ {
105
+ "name": "isOdd",
106
+ "examples": [
107
+ {
108
+ "title": "Check if a number is odd",
109
+ "description": "Returns true for integers not divisible by 2, false otherwise.",
110
+ "code": "isOdd(3) // => true\nisOdd(1) // => true\nisOdd(2) // => false\nisOdd(0) // => false\nisOdd(1.5) // => false (not an integer)"
111
+ },
112
+ {
113
+ "title": "Filter odd numbers from an array",
114
+ "description": "Use as a predicate in .filter() to extract odd integers.",
115
+ "code": "const nums = [1, 2, 3, 4, 5, 6];\nnums.filter(isOdd)\n// => [1, 3, 5]"
116
+ }
117
+ ]
118
+ },
119
+ {
120
+ "name": "isPositive",
121
+ "examples": [
122
+ {
123
+ "title": "isPositive",
124
+ "description": "",
125
+ "code": "```ts\nisPositive(42) // => true\nisPositive(0.1) // => true\nisPositive(Infinity) // => true\nisPositive(0) // => false\nisPositive(-1) // => false\nisPositive(NaN) // => false\n```"
126
+ }
127
+ ]
128
+ },
69
129
  {
70
130
  "name": "lerp",
71
131
  "examples": [
@@ -81,16 +141,6 @@
81
141
  }
82
142
  ]
83
143
  },
84
- {
85
- "name": "mean",
86
- "examples": [
87
- {
88
- "title": "Average a list of numbers",
89
- "description": "Returns the arithmetic mean of the array; NaN for empty arrays.",
90
- "code": "mean([1, 2, 3, 4]) // => 2.5\nmean([10, 20, 30]) // => 20\nmean([]) // => NaN"
91
- }
92
- ]
93
- },
94
144
  {
95
145
  "name": "randomBetween",
96
146
  "examples": [
@@ -120,21 +170,6 @@
120
170
  "code": "roundTo(3.7, 0)\n// => 4"
121
171
  }
122
172
  ]
123
- },
124
- {
125
- "name": "sum",
126
- "examples": [
127
- {
128
- "title": "Sum numbers",
129
- "description": "Calculates the sum of an array of numbers.",
130
- "code": "sum([1, 2, 3, 4])\n// => 10"
131
- },
132
- {
133
- "title": "Sum with negative numbers",
134
- "description": "Handles negative numbers correctly.",
135
- "code": "sum([10, -3, 5, -2])\n// => 10"
136
- }
137
- ]
138
173
  }
139
174
  ]
140
175
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@helpers4/number",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "description": "A set of helpers in TS/JS, compatible with tree-shaking, for number.",
5
5
  "author": "baxyz <baxy@etik.com>",
6
6
  "license": "LGPL-3.0",
@@ -29,14 +29,17 @@
29
29
  "number",
30
30
  "clamp",
31
31
  "correctFloat",
32
+ "extractNumber",
32
33
  "formatCompact",
33
34
  "formatSize",
34
35
  "inRange",
36
+ "isEven",
37
+ "isNegative",
38
+ "isOdd",
39
+ "isPositive",
35
40
  "lerp",
36
- "mean",
37
41
  "random",
38
- "roundTo",
39
- "sum"
42
+ "roundTo"
40
43
  ],
41
44
  "files": [
42
45
  "lib/index.js",