@helpers4/number 2.0.0-alpha.1 → 2.0.0-alpha.11
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/LICENSE.md +150 -650
- package/README.md +5 -2
- package/lib/index.d.ts +68 -3
- package/lib/index.js +85 -4
- package/lib/index.js.map +1 -0
- package/meta/api.json +195 -0
- package/meta/examples.json +60 -0
- package/meta/licenses.json +4 -0
- package/meta/native-alternatives.json +37 -0
- package/package.json +17 -6
package/README.md
CHANGED
|
@@ -9,12 +9,13 @@ A set of helpers for working with URLs.
|
|
|
9
9
|
|
|
10
10
|
## Documentation
|
|
11
11
|
|
|
12
|
-
[https://helpers4.
|
|
12
|
+
[https://helpers4.dev/typescript/categories/number/](https://helpers4.dev/typescript/categories/number/)
|
|
13
13
|
|
|
14
14
|
<!-- AUTOMATIC-METHODS -->
|
|
15
|
+
- clamp
|
|
15
16
|
- random
|
|
16
17
|
- roundTo
|
|
17
|
-
-
|
|
18
|
+
- sum
|
|
18
19
|
<!-- /AUTOMATIC-METHODS -->
|
|
19
20
|
|
|
20
21
|
## See
|
|
@@ -23,6 +24,7 @@ A set of helpers for working with URLs.
|
|
|
23
24
|
- [array](../array)
|
|
24
25
|
- [date](../date)
|
|
25
26
|
- [function](../function)
|
|
27
|
+
- [math](../math)
|
|
26
28
|
- [object](../object)
|
|
27
29
|
- [observable](../observable)
|
|
28
30
|
- [promise](../promise)
|
|
@@ -40,6 +42,7 @@ A set of helpers for working with URLs.
|
|
|
40
42
|
| array | [@helpers4/array](https://www.npmjs.com/package/@helpers4/array) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/array) | Array manipulation utilities |
|
|
41
43
|
| date | [@helpers4/date](https://www.npmjs.com/package/@helpers4/date) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/date) | date utilities |
|
|
42
44
|
| function | [@helpers4/function](https://www.npmjs.com/package/@helpers4/function) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/function) | Function utilities and type guards |
|
|
45
|
+
| math | [@helpers4/math](https://www.npmjs.com/package/@helpers4/math) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/math) | math utilities |
|
|
43
46
|
| number | [@helpers4/number](https://www.npmjs.com/package/@helpers4/number) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/number) | number utilities |
|
|
44
47
|
| object | [@helpers4/object](https://www.npmjs.com/package/@helpers4/object) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/object) | Object manipulation utilities |
|
|
45
48
|
| observable | [@helpers4/observable](https://www.npmjs.com/package/@helpers4/observable) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/observable) | Observable utilities and combinators |
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,68 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of helpers4.
|
|
3
|
+
* Copyright (C) 2025 baxyz
|
|
4
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Clamps a number between min and max values
|
|
8
|
+
* @param value - The value to clamp
|
|
9
|
+
* @param min - Minimum value
|
|
10
|
+
* @param max - Maximum value
|
|
11
|
+
* @returns Clamped value
|
|
12
|
+
* @since 1.9.0
|
|
13
|
+
*/
|
|
14
|
+
declare function clamp(value: number, min: number, max: number): number;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* This file is part of helpers4.
|
|
18
|
+
* Copyright (C) 2025 baxyz
|
|
19
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Generates a random number between min and max (inclusive)
|
|
23
|
+
* @param min - Minimum value
|
|
24
|
+
* @param max - Maximum value
|
|
25
|
+
* @returns Random number between min and max
|
|
26
|
+
* @since 1.9.0
|
|
27
|
+
*/
|
|
28
|
+
declare function randomBetween(min: number, max: number): number;
|
|
29
|
+
/**
|
|
30
|
+
* Generates a random integer between min and max (inclusive)
|
|
31
|
+
* @param min - Minimum value
|
|
32
|
+
* @param max - Maximum value
|
|
33
|
+
* @returns Random integer between min and max
|
|
34
|
+
* @since 1.9.0
|
|
35
|
+
*/
|
|
36
|
+
declare function randomIntBetween(min: number, max: number): number;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* This file is part of helpers4.
|
|
40
|
+
* Copyright (C) 2025 baxyz
|
|
41
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
42
|
+
*/
|
|
43
|
+
/**
|
|
44
|
+
* Rounds a number to specified decimal places
|
|
45
|
+
* @param value - The number to round
|
|
46
|
+
* @param decimals - Number of decimal places
|
|
47
|
+
* @returns Rounded number
|
|
48
|
+
* @since 1.9.0
|
|
49
|
+
*/
|
|
50
|
+
declare function roundTo(value: number, decimals: number): number;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* This file is part of helpers4.
|
|
54
|
+
* Copyright (C) 2025 baxyz
|
|
55
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
56
|
+
*/
|
|
57
|
+
/**
|
|
58
|
+
* Calculates the sum of an array of numbers.
|
|
59
|
+
* @param array - The array of numbers to sum
|
|
60
|
+
* @returns The sum of all values
|
|
61
|
+
* @example
|
|
62
|
+
* sum([1, 2, 3, 4])
|
|
63
|
+
* // => 10
|
|
64
|
+
* @since 2.0.0
|
|
65
|
+
*/
|
|
66
|
+
declare function sum(array: readonly number[]): number;
|
|
67
|
+
|
|
68
|
+
export { clamp, randomBetween, randomIntBetween, roundTo, sum };
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,85 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
//#region helpers/number/clamp.ts
|
|
2
|
+
/**
|
|
3
|
+
* This file is part of helpers4.
|
|
4
|
+
* Copyright (C) 2025 baxyz
|
|
5
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Clamps a number between min and max values
|
|
9
|
+
* @param value - The value to clamp
|
|
10
|
+
* @param min - Minimum value
|
|
11
|
+
* @param max - Maximum value
|
|
12
|
+
* @returns Clamped value
|
|
13
|
+
* @since 1.9.0
|
|
14
|
+
*/
|
|
15
|
+
function clamp(value, min, max) {
|
|
16
|
+
return Math.min(Math.max(value, min), max);
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region helpers/number/random.ts
|
|
20
|
+
/**
|
|
21
|
+
* This file is part of helpers4.
|
|
22
|
+
* Copyright (C) 2025 baxyz
|
|
23
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
24
|
+
*/
|
|
25
|
+
/**
|
|
26
|
+
* Generates a random number between min and max (inclusive)
|
|
27
|
+
* @param min - Minimum value
|
|
28
|
+
* @param max - Maximum value
|
|
29
|
+
* @returns Random number between min and max
|
|
30
|
+
* @since 1.9.0
|
|
31
|
+
*/
|
|
32
|
+
function randomBetween(min, max) {
|
|
33
|
+
return Math.random() * (max - min) + min;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Generates a random integer between min and max (inclusive)
|
|
37
|
+
* @param min - Minimum value
|
|
38
|
+
* @param max - Maximum value
|
|
39
|
+
* @returns Random integer between min and max
|
|
40
|
+
* @since 1.9.0
|
|
41
|
+
*/
|
|
42
|
+
function randomIntBetween(min, max) {
|
|
43
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region helpers/number/roundTo.ts
|
|
47
|
+
/**
|
|
48
|
+
* This file is part of helpers4.
|
|
49
|
+
* Copyright (C) 2025 baxyz
|
|
50
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
51
|
+
*/
|
|
52
|
+
/**
|
|
53
|
+
* Rounds a number to specified decimal places
|
|
54
|
+
* @param value - The number to round
|
|
55
|
+
* @param decimals - Number of decimal places
|
|
56
|
+
* @returns Rounded number
|
|
57
|
+
* @since 1.9.0
|
|
58
|
+
*/
|
|
59
|
+
function roundTo(value, decimals) {
|
|
60
|
+
const multiplier = Math.pow(10, decimals);
|
|
61
|
+
return Math.round(value * multiplier) / multiplier;
|
|
62
|
+
}
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region helpers/number/sum.ts
|
|
65
|
+
/**
|
|
66
|
+
* This file is part of helpers4.
|
|
67
|
+
* Copyright (C) 2025 baxyz
|
|
68
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
69
|
+
*/
|
|
70
|
+
/**
|
|
71
|
+
* Calculates the sum of an array of numbers.
|
|
72
|
+
* @param array - The array of numbers to sum
|
|
73
|
+
* @returns The sum of all values
|
|
74
|
+
* @example
|
|
75
|
+
* sum([1, 2, 3, 4])
|
|
76
|
+
* // => 10
|
|
77
|
+
* @since 2.0.0
|
|
78
|
+
*/
|
|
79
|
+
function sum(array) {
|
|
80
|
+
return array.reduce((acc, n) => acc + n, 0);
|
|
81
|
+
}
|
|
82
|
+
//#endregion
|
|
83
|
+
export { clamp, randomBetween, randomIntBetween, roundTo, sum };
|
|
84
|
+
|
|
85
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../helpers/number/clamp.ts","../../../helpers/number/random.ts","../../../helpers/number/roundTo.ts","../../../helpers/number/sum.ts"],"sourcesContent":["/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Clamps a number between min and max values\n * @param value - The value to clamp\n * @param min - Minimum value\n * @param max - Maximum value\n * @returns Clamped value\n * @since 1.9.0\n */\nexport function clamp(value: number, min: number, max: number): number {\n return Math.min(Math.max(value, min), max);\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Generates a random number between min and max (inclusive)\n * @param min - Minimum value\n * @param max - Maximum value\n * @returns Random number between min and max\n * @since 1.9.0\n */\nexport function randomBetween(min: number, max: number): number {\n return Math.random() * (max - min) + min;\n}\n\n/**\n * Generates a random integer between min and max (inclusive)\n * @param min - Minimum value\n * @param max - Maximum value\n * @returns Random integer between min and max\n * @since 1.9.0\n */\nexport function randomIntBetween(min: number, max: number): number {\n return Math.floor(Math.random() * (max - min + 1)) + min;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Rounds a number to specified decimal places\n * @param value - The number to round\n * @param decimals - Number of decimal places\n * @returns Rounded number\n * @since 1.9.0\n */\nexport function roundTo(value: number, decimals: number): number {\n const multiplier = Math.pow(10, decimals);\n return Math.round(value * multiplier) / multiplier;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Calculates the sum of an array of numbers.\n * @param array - The array of numbers to sum\n * @returns The sum of all values\n * @example\n * sum([1, 2, 3, 4])\n * // => 10\n * @since 2.0.0\n */\nexport function sum(array: readonly number[]): number {\n return array.reduce((acc, n) => acc + n, 0);\n}\n"],"mappings":";;;;;;;;;;;;;;AAcA,SAAgB,MAAM,OAAe,KAAa,KAAqB;AACrE,QAAO,KAAK,IAAI,KAAK,IAAI,OAAO,IAAI,EAAE,IAAI;;;;;;;;;;;;;;;;ACF5C,SAAgB,cAAc,KAAa,KAAqB;AAC9D,QAAO,KAAK,QAAQ,IAAI,MAAM,OAAO;;;;;;;;;AAUvC,SAAgB,iBAAiB,KAAa,KAAqB;AACjE,QAAO,KAAK,MAAM,KAAK,QAAQ,IAAI,MAAM,MAAM,GAAG,GAAG;;;;;;;;;;;;;;;;ACZvD,SAAgB,QAAQ,OAAe,UAA0B;CAC/D,MAAM,aAAa,KAAK,IAAI,IAAI,SAAS;AACzC,QAAO,KAAK,MAAM,QAAQ,WAAW,GAAG;;;;;;;;;;;;;;;;;;ACA1C,SAAgB,IAAI,OAAkC;AACpD,QAAO,MAAM,QAAQ,KAAK,MAAM,MAAM,GAAG,EAAE"}
|
package/meta/api.json
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
{
|
|
2
|
+
"category": "number",
|
|
3
|
+
"version": "2.0.0-alpha.11",
|
|
4
|
+
"functions": [
|
|
5
|
+
{
|
|
6
|
+
"name": "clamp",
|
|
7
|
+
"kind": "function",
|
|
8
|
+
"description": "Clamps a number between min and max values",
|
|
9
|
+
"since": "1.9.0",
|
|
10
|
+
"signatures": [
|
|
11
|
+
{
|
|
12
|
+
"signature": "clamp(value: number, min: number, max: number): number",
|
|
13
|
+
"description": "Clamps a number between min and max values",
|
|
14
|
+
"params": [
|
|
15
|
+
{
|
|
16
|
+
"name": "value",
|
|
17
|
+
"type": "number",
|
|
18
|
+
"description": "The value to clamp"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"name": "min",
|
|
22
|
+
"type": "number",
|
|
23
|
+
"description": "Minimum value"
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"name": "max",
|
|
27
|
+
"type": "number",
|
|
28
|
+
"description": "Maximum value"
|
|
29
|
+
}
|
|
30
|
+
],
|
|
31
|
+
"returns": {
|
|
32
|
+
"type": "number",
|
|
33
|
+
"description": "Clamped value"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
],
|
|
37
|
+
"examples": [
|
|
38
|
+
{
|
|
39
|
+
"title": "Clamp a value within range",
|
|
40
|
+
"description": "Restricts a number to be within a min/max range.",
|
|
41
|
+
"code": "clamp(15, 0, 10) // => 10\nclamp(-5, 0, 10) // => 0\nclamp(5, 0, 10) // => 5"
|
|
42
|
+
}
|
|
43
|
+
],
|
|
44
|
+
"sourceFile": "clamp.ts"
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
"name": "randomBetween",
|
|
48
|
+
"kind": "function",
|
|
49
|
+
"description": "Generates a random number between min and max (inclusive)",
|
|
50
|
+
"since": "1.9.0",
|
|
51
|
+
"signatures": [
|
|
52
|
+
{
|
|
53
|
+
"signature": "randomBetween(min: number, max: number): number",
|
|
54
|
+
"description": "Generates a random number between min and max (inclusive)",
|
|
55
|
+
"params": [
|
|
56
|
+
{
|
|
57
|
+
"name": "min",
|
|
58
|
+
"type": "number",
|
|
59
|
+
"description": "Minimum value"
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
"name": "max",
|
|
63
|
+
"type": "number",
|
|
64
|
+
"description": "Maximum value"
|
|
65
|
+
}
|
|
66
|
+
],
|
|
67
|
+
"returns": {
|
|
68
|
+
"type": "number",
|
|
69
|
+
"description": "Random number between min and max"
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
],
|
|
73
|
+
"examples": [
|
|
74
|
+
{
|
|
75
|
+
"title": "Generate a random float in range",
|
|
76
|
+
"description": "Returns a random number between min and max (inclusive).",
|
|
77
|
+
"code": "randomBetween(1, 10)\n// => e.g. 5.327..."
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
"title": "Generate a random integer in range",
|
|
81
|
+
"description": "Returns a random integer between min and max (inclusive).",
|
|
82
|
+
"code": "randomIntBetween(1, 6)\n// => e.g. 4"
|
|
83
|
+
}
|
|
84
|
+
],
|
|
85
|
+
"sourceFile": "random.ts"
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
"name": "randomIntBetween",
|
|
89
|
+
"kind": "function",
|
|
90
|
+
"description": "Generates a random integer between min and max (inclusive)",
|
|
91
|
+
"since": "1.9.0",
|
|
92
|
+
"signatures": [
|
|
93
|
+
{
|
|
94
|
+
"signature": "randomIntBetween(min: number, max: number): number",
|
|
95
|
+
"description": "Generates a random integer between min and max (inclusive)",
|
|
96
|
+
"params": [
|
|
97
|
+
{
|
|
98
|
+
"name": "min",
|
|
99
|
+
"type": "number",
|
|
100
|
+
"description": "Minimum value"
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
"name": "max",
|
|
104
|
+
"type": "number",
|
|
105
|
+
"description": "Maximum value"
|
|
106
|
+
}
|
|
107
|
+
],
|
|
108
|
+
"returns": {
|
|
109
|
+
"type": "number",
|
|
110
|
+
"description": "Random integer between min and max"
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
],
|
|
114
|
+
"examples": [],
|
|
115
|
+
"sourceFile": "random.ts"
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
"name": "roundTo",
|
|
119
|
+
"kind": "function",
|
|
120
|
+
"description": "Rounds a number to specified decimal places",
|
|
121
|
+
"since": "1.9.0",
|
|
122
|
+
"signatures": [
|
|
123
|
+
{
|
|
124
|
+
"signature": "roundTo(value: number, decimals: number): number",
|
|
125
|
+
"description": "Rounds a number to specified decimal places",
|
|
126
|
+
"params": [
|
|
127
|
+
{
|
|
128
|
+
"name": "value",
|
|
129
|
+
"type": "number",
|
|
130
|
+
"description": "The number to round"
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
"name": "decimals",
|
|
134
|
+
"type": "number",
|
|
135
|
+
"description": "Number of decimal places"
|
|
136
|
+
}
|
|
137
|
+
],
|
|
138
|
+
"returns": {
|
|
139
|
+
"type": "number",
|
|
140
|
+
"description": "Rounded number"
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
],
|
|
144
|
+
"examples": [
|
|
145
|
+
{
|
|
146
|
+
"title": "Round to 2 decimal places",
|
|
147
|
+
"description": "Rounds a floating-point number to the specified number of decimals.",
|
|
148
|
+
"code": "roundTo(3.14159, 2)\n// => 3.14"
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
"title": "Round to 0 decimal places",
|
|
152
|
+
"description": "Effectively rounds to the nearest integer.",
|
|
153
|
+
"code": "roundTo(3.7, 0)\n// => 4"
|
|
154
|
+
}
|
|
155
|
+
],
|
|
156
|
+
"sourceFile": "roundTo.ts"
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
"name": "sum",
|
|
160
|
+
"kind": "function",
|
|
161
|
+
"description": "Calculates the sum of an array of numbers.",
|
|
162
|
+
"since": "2.0.0",
|
|
163
|
+
"signatures": [
|
|
164
|
+
{
|
|
165
|
+
"signature": "sum(array: readonly number[]): number",
|
|
166
|
+
"description": "Calculates the sum of an array of numbers.",
|
|
167
|
+
"params": [
|
|
168
|
+
{
|
|
169
|
+
"name": "array",
|
|
170
|
+
"type": "readonly number[]",
|
|
171
|
+
"description": "The array of numbers to sum"
|
|
172
|
+
}
|
|
173
|
+
],
|
|
174
|
+
"returns": {
|
|
175
|
+
"type": "number",
|
|
176
|
+
"description": "The sum of all values"
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
],
|
|
180
|
+
"examples": [
|
|
181
|
+
{
|
|
182
|
+
"title": "Sum numbers",
|
|
183
|
+
"description": "Calculates the sum of an array of numbers.",
|
|
184
|
+
"code": "sum([1, 2, 3, 4])\n// => 10"
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
"title": "Sum with negative numbers",
|
|
188
|
+
"description": "Handles negative numbers correctly.",
|
|
189
|
+
"code": "sum([10, -3, 5, -2])\n// => 10"
|
|
190
|
+
}
|
|
191
|
+
],
|
|
192
|
+
"sourceFile": "sum.ts"
|
|
193
|
+
}
|
|
194
|
+
]
|
|
195
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"category": "number",
|
|
3
|
+
"functions": [
|
|
4
|
+
{
|
|
5
|
+
"name": "clamp",
|
|
6
|
+
"examples": [
|
|
7
|
+
{
|
|
8
|
+
"title": "Clamp a value within range",
|
|
9
|
+
"description": "Restricts a number to be within a min/max range.",
|
|
10
|
+
"code": "clamp(15, 0, 10) // => 10\nclamp(-5, 0, 10) // => 0\nclamp(5, 0, 10) // => 5"
|
|
11
|
+
}
|
|
12
|
+
]
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"name": "randomBetween",
|
|
16
|
+
"examples": [
|
|
17
|
+
{
|
|
18
|
+
"title": "Generate a random float in range",
|
|
19
|
+
"description": "Returns a random number between min and max (inclusive).",
|
|
20
|
+
"code": "randomBetween(1, 10)\n// => e.g. 5.327..."
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"title": "Generate a random integer in range",
|
|
24
|
+
"description": "Returns a random integer between min and max (inclusive).",
|
|
25
|
+
"code": "randomIntBetween(1, 6)\n// => e.g. 4"
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"name": "roundTo",
|
|
31
|
+
"examples": [
|
|
32
|
+
{
|
|
33
|
+
"title": "Round to 2 decimal places",
|
|
34
|
+
"description": "Rounds a floating-point number to the specified number of decimals.",
|
|
35
|
+
"code": "roundTo(3.14159, 2)\n// => 3.14"
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
"title": "Round to 0 decimal places",
|
|
39
|
+
"description": "Effectively rounds to the nearest integer.",
|
|
40
|
+
"code": "roundTo(3.7, 0)\n// => 4"
|
|
41
|
+
}
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"name": "sum",
|
|
46
|
+
"examples": [
|
|
47
|
+
{
|
|
48
|
+
"title": "Sum numbers",
|
|
49
|
+
"description": "Calculates the sum of an array of numbers.",
|
|
50
|
+
"code": "sum([1, 2, 3, 4])\n// => 10"
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
"title": "Sum with negative numbers",
|
|
54
|
+
"description": "Handles negative numbers correctly.",
|
|
55
|
+
"code": "sum([10, -3, 5, -2])\n// => 10"
|
|
56
|
+
}
|
|
57
|
+
]
|
|
58
|
+
}
|
|
59
|
+
]
|
|
60
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"category": "number",
|
|
3
|
+
"functions": [
|
|
4
|
+
{
|
|
5
|
+
"name": "min / max",
|
|
6
|
+
"libraries": [
|
|
7
|
+
"lodash",
|
|
8
|
+
"radashi",
|
|
9
|
+
"ramda"
|
|
10
|
+
],
|
|
11
|
+
"native": "Math.min(...arr) / Math.max(...arr)",
|
|
12
|
+
"since": "ES1",
|
|
13
|
+
"example": "Math.min(...numbers)\nMath.max(...numbers)",
|
|
14
|
+
"notes": "For large arrays, prefer reduce to avoid stack overflow"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"name": "ceil / floor",
|
|
18
|
+
"libraries": [
|
|
19
|
+
"lodash"
|
|
20
|
+
],
|
|
21
|
+
"native": "Math.ceil() / Math.floor()",
|
|
22
|
+
"since": "ES1",
|
|
23
|
+
"example": "Math.ceil(4.2) // 5\nMath.floor(4.8) // 4",
|
|
24
|
+
"notes": "No precision param natively; use roundTo from helpers4 for decimal precision"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"name": "toInt / toFloat",
|
|
28
|
+
"libraries": [
|
|
29
|
+
"radashi"
|
|
30
|
+
],
|
|
31
|
+
"native": "parseInt(str, 10) / parseFloat(str)",
|
|
32
|
+
"since": "ES1",
|
|
33
|
+
"example": "parseInt('42px', 10) // 42\nparseFloat('3.14') // 3.14",
|
|
34
|
+
"notes": "Radashi adds default-value fallback on NaN"
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
}
|
package/package.json
CHANGED
|
@@ -1,35 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@helpers4/number",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.11",
|
|
4
4
|
"description": "A set of helpers in TS/JS, compatible with tree-shaking, for number.",
|
|
5
5
|
"author": "baxyz <baxy@etik.com>",
|
|
6
|
-
"license": "
|
|
6
|
+
"license": "LGPL-3.0",
|
|
7
|
+
"homepage": "https://helpers4.dev/typescript/categories/number/",
|
|
7
8
|
"repository": {
|
|
8
9
|
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/helpers4/
|
|
10
|
+
"url": "git+https://github.com/helpers4/typescript.git"
|
|
10
11
|
},
|
|
11
12
|
"type": "module",
|
|
13
|
+
"sideEffects": false,
|
|
12
14
|
"main": "lib/index.js",
|
|
13
15
|
"types": "lib/index.d.ts",
|
|
14
16
|
"exports": {
|
|
15
17
|
".": {
|
|
16
18
|
"types": "./lib/index.d.ts",
|
|
17
19
|
"import": "./lib/index.js"
|
|
18
|
-
}
|
|
20
|
+
},
|
|
21
|
+
"./meta/api.json": "./meta/api.json",
|
|
22
|
+
"./meta/examples.json": "./meta/examples.json",
|
|
23
|
+
"./meta/licenses.json": "./meta/licenses.json",
|
|
24
|
+
"./package.json": "./package.json"
|
|
19
25
|
},
|
|
20
26
|
"keywords": [
|
|
21
27
|
"helpers",
|
|
22
28
|
"number",
|
|
29
|
+
"clamp",
|
|
23
30
|
"random",
|
|
24
31
|
"roundTo",
|
|
25
|
-
"
|
|
32
|
+
"sum"
|
|
26
33
|
],
|
|
27
34
|
"files": [
|
|
28
35
|
"lib/index.js",
|
|
29
36
|
"lib/index.d.ts",
|
|
30
37
|
"lib/index.js.map",
|
|
38
|
+
"meta/",
|
|
31
39
|
"LICENSE.md",
|
|
32
40
|
"package.json",
|
|
33
41
|
"README.md"
|
|
34
|
-
]
|
|
42
|
+
],
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=18.0.0"
|
|
45
|
+
}
|
|
35
46
|
}
|