@fluixi/utils 1.0.0-alpha.100
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 +21 -0
- package/README.md +68 -0
- package/dist/index.cjs +2858 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +2838 -0
- package/dist/lib/array/advanced.cjs +2227 -0
- package/dist/lib/array/advanced.d.ts +1116 -0
- package/dist/lib/array/advanced.d.ts.map +1 -0
- package/dist/lib/array/advanced.js +2429 -0
- package/dist/lib/array/advanced.mjs +2204 -0
- package/dist/lib/array/array.cjs +57 -0
- package/dist/lib/array/array.d.ts +6 -0
- package/dist/lib/array/array.d.ts.map +1 -0
- package/dist/lib/array/array.js +31 -0
- package/dist/lib/array/array.mjs +36 -0
- package/dist/lib/array/index.cjs +2344 -0
- package/dist/lib/array/index.d.ts +4 -0
- package/dist/lib/array/index.d.ts.map +1 -0
- package/dist/lib/array/index.js +3 -0
- package/dist/lib/array/index.mjs +2322 -0
- package/dist/lib/array/utils.cjs +103 -0
- package/dist/lib/array/utils.d.ts +100 -0
- package/dist/lib/array/utils.d.ts.map +1 -0
- package/dist/lib/array/utils.js +165 -0
- package/dist/lib/array/utils.mjs +82 -0
- package/dist/lib/color/color.cjs +2190 -0
- package/dist/lib/color/color.d.ts +366 -0
- package/dist/lib/color/color.d.ts.map +1 -0
- package/dist/lib/color/color.js +722 -0
- package/dist/lib/color/color.mjs +2167 -0
- package/dist/lib/color/constants.cjs +380 -0
- package/dist/lib/color/constants.d.ts +361 -0
- package/dist/lib/color/constants.d.ts.map +1 -0
- package/dist/lib/color/constants.js +358 -0
- package/dist/lib/color/constants.mjs +359 -0
- package/dist/lib/color/conversions.cjs +629 -0
- package/dist/lib/color/conversions.d.ts +166 -0
- package/dist/lib/color/conversions.d.ts.map +1 -0
- package/dist/lib/color/conversions.js +766 -0
- package/dist/lib/color/conversions.mjs +608 -0
- package/dist/lib/color/helpers.cjs +2534 -0
- package/dist/lib/color/helpers.d.ts +75 -0
- package/dist/lib/color/helpers.d.ts.map +1 -0
- package/dist/lib/color/helpers.js +419 -0
- package/dist/lib/color/helpers.mjs +2511 -0
- package/dist/lib/color/index.cjs +2641 -0
- package/dist/lib/color/index.d.ts +12 -0
- package/dist/lib/color/index.d.ts.map +1 -0
- package/dist/lib/color/index.js +15 -0
- package/dist/lib/color/index.mjs +2619 -0
- package/dist/lib/color/types.cjs +18 -0
- package/dist/lib/color/types.d.ts +179 -0
- package/dist/lib/color/types.d.ts.map +1 -0
- package/dist/lib/color/types.js +1 -0
- package/dist/lib/color/types.mjs +0 -0
- package/dist/lib/color/utils.cjs +962 -0
- package/dist/lib/color/utils.d.ts +199 -0
- package/dist/lib/color/utils.d.ts.map +1 -0
- package/dist/lib/color/utils.js +858 -0
- package/dist/lib/color/utils.mjs +939 -0
- package/dist/lib/compare/comparators.cjs +171 -0
- package/dist/lib/compare/comparators.d.ts +65 -0
- package/dist/lib/compare/comparators.d.ts.map +1 -0
- package/dist/lib/compare/comparators.js +210 -0
- package/dist/lib/compare/comparators.mjs +150 -0
- package/dist/lib/compare/index.cjs +172 -0
- package/dist/lib/compare/index.d.ts +2 -0
- package/dist/lib/compare/index.d.ts.map +1 -0
- package/dist/lib/compare/index.js +1 -0
- package/dist/lib/compare/index.mjs +152 -0
- package/dist/lib/cookie/codec.cjs +72 -0
- package/dist/lib/cookie/codec.d.ts +22 -0
- package/dist/lib/cookie/codec.d.ts.map +1 -0
- package/dist/lib/cookie/codec.js +67 -0
- package/dist/lib/cookie/codec.mjs +51 -0
- package/dist/lib/cookie/index.cjs +75 -0
- package/dist/lib/cookie/index.d.ts +2 -0
- package/dist/lib/cookie/index.d.ts.map +1 -0
- package/dist/lib/cookie/index.js +1 -0
- package/dist/lib/cookie/index.mjs +53 -0
- package/dist/lib/crypto/hash.cjs +83 -0
- package/dist/lib/crypto/hash.d.ts +6 -0
- package/dist/lib/crypto/hash.d.ts.map +1 -0
- package/dist/lib/crypto/hash.js +53 -0
- package/dist/lib/crypto/hash.mjs +62 -0
- package/dist/lib/crypto/index.cjs +85 -0
- package/dist/lib/crypto/index.d.ts +2 -0
- package/dist/lib/crypto/index.d.ts.map +1 -0
- package/dist/lib/crypto/index.js +1 -0
- package/dist/lib/crypto/index.mjs +62 -0
- package/dist/lib/dom/events.cjs +40 -0
- package/dist/lib/dom/events.d.ts +159 -0
- package/dist/lib/dom/events.d.ts.map +1 -0
- package/dist/lib/dom/events.js +50 -0
- package/dist/lib/dom/events.mjs +19 -0
- package/dist/lib/dom/index.cjs +78 -0
- package/dist/lib/dom/index.d.ts +3 -0
- package/dist/lib/dom/index.d.ts.map +1 -0
- package/dist/lib/dom/index.js +2 -0
- package/dist/lib/dom/index.mjs +56 -0
- package/dist/lib/dom/utils.cjs +57 -0
- package/dist/lib/dom/utils.d.ts +6 -0
- package/dist/lib/dom/utils.d.ts.map +1 -0
- package/dist/lib/dom/utils.js +37 -0
- package/dist/lib/dom/utils.mjs +36 -0
- package/dist/lib/functions/functions.cjs +366 -0
- package/dist/lib/functions/functions.d.ts +329 -0
- package/dist/lib/functions/functions.d.ts.map +1 -0
- package/dist/lib/functions/functions.js +585 -0
- package/dist/lib/functions/functions.mjs +345 -0
- package/dist/lib/functions/index.cjs +369 -0
- package/dist/lib/functions/index.d.ts +3 -0
- package/dist/lib/functions/index.d.ts.map +1 -0
- package/dist/lib/functions/index.js +1 -0
- package/dist/lib/functions/index.mjs +347 -0
- package/dist/lib/functions/types.cjs +18 -0
- package/dist/lib/functions/types.d.ts +2 -0
- package/dist/lib/functions/types.d.ts.map +1 -0
- package/dist/lib/functions/types.js +1 -0
- package/dist/lib/functions/types.mjs +0 -0
- package/dist/lib/helpers.cjs +78 -0
- package/dist/lib/helpers.d.ts +13 -0
- package/dist/lib/helpers.d.ts.map +1 -0
- package/dist/lib/helpers.js +66 -0
- package/dist/lib/helpers.mjs +59 -0
- package/dist/lib/index.cjs +2857 -0
- package/dist/lib/index.d.ts +6 -0
- package/dist/lib/index.d.ts.map +1 -0
- package/dist/lib/index.js +8 -0
- package/dist/lib/index.mjs +2836 -0
- package/dist/lib/maths/constants.cjs +70 -0
- package/dist/lib/maths/constants.d.ts +84 -0
- package/dist/lib/maths/constants.d.ts.map +1 -0
- package/dist/lib/maths/constants.js +79 -0
- package/dist/lib/maths/constants.mjs +50 -0
- package/dist/lib/maths/fn/advanced.cjs +231 -0
- package/dist/lib/maths/fn/advanced.d.ts +149 -0
- package/dist/lib/maths/fn/advanced.d.ts.map +1 -0
- package/dist/lib/maths/fn/advanced.js +264 -0
- package/dist/lib/maths/fn/advanced.mjs +208 -0
- package/dist/lib/maths/fn/basic.cjs +212 -0
- package/dist/lib/maths/fn/basic.d.ts +182 -0
- package/dist/lib/maths/fn/basic.d.ts.map +1 -0
- package/dist/lib/maths/fn/basic.js +343 -0
- package/dist/lib/maths/fn/basic.mjs +191 -0
- package/dist/lib/maths/fn/complex.cjs +268 -0
- package/dist/lib/maths/fn/complex.d.ts +153 -0
- package/dist/lib/maths/fn/complex.d.ts.map +1 -0
- package/dist/lib/maths/fn/complex.js +383 -0
- package/dist/lib/maths/fn/complex.mjs +247 -0
- package/dist/lib/maths/fn/index.cjs +877 -0
- package/dist/lib/maths/fn/index.d.ts +10 -0
- package/dist/lib/maths/fn/index.d.ts.map +1 -0
- package/dist/lib/maths/fn/index.js +9 -0
- package/dist/lib/maths/fn/index.mjs +855 -0
- package/dist/lib/maths/fn/trigo.cjs +179 -0
- package/dist/lib/maths/fn/trigo.d.ts +147 -0
- package/dist/lib/maths/fn/trigo.d.ts.map +1 -0
- package/dist/lib/maths/fn/trigo.js +227 -0
- package/dist/lib/maths/fn/trigo.mjs +156 -0
- package/dist/lib/maths/fn/vector.cjs +728 -0
- package/dist/lib/maths/fn/vector.d.ts +512 -0
- package/dist/lib/maths/fn/vector.d.ts.map +1 -0
- package/dist/lib/maths/fn/vector.js +1136 -0
- package/dist/lib/maths/fn/vector.mjs +707 -0
- package/dist/lib/maths/formatter.cjs +61 -0
- package/dist/lib/maths/formatter.d.ts +35 -0
- package/dist/lib/maths/formatter.d.ts.map +1 -0
- package/dist/lib/maths/formatter.js +56 -0
- package/dist/lib/maths/formatter.mjs +40 -0
- package/dist/lib/maths/guards.cjs +164 -0
- package/dist/lib/maths/guards.d.ts +166 -0
- package/dist/lib/maths/guards.d.ts.map +1 -0
- package/dist/lib/maths/guards.js +315 -0
- package/dist/lib/maths/guards.mjs +144 -0
- package/dist/lib/maths/index.cjs +1152 -0
- package/dist/lib/maths/index.d.ts +24 -0
- package/dist/lib/maths/index.d.ts.map +1 -0
- package/dist/lib/maths/index.js +29 -0
- package/dist/lib/maths/index.mjs +1130 -0
- package/dist/lib/maths/parser.cjs +75 -0
- package/dist/lib/maths/parser.d.ts +20 -0
- package/dist/lib/maths/parser.d.ts.map +1 -0
- package/dist/lib/maths/parser.js +78 -0
- package/dist/lib/maths/parser.mjs +54 -0
- package/dist/lib/maths/random.cjs +49 -0
- package/dist/lib/maths/random.d.ts +27 -0
- package/dist/lib/maths/random.d.ts.map +1 -0
- package/dist/lib/maths/random.js +49 -0
- package/dist/lib/maths/random.mjs +28 -0
- package/dist/lib/maths/types/advanced.cjs +18 -0
- package/dist/lib/maths/types/advanced.d.ts +357 -0
- package/dist/lib/maths/types/advanced.d.ts.map +1 -0
- package/dist/lib/maths/types/advanced.js +5 -0
- package/dist/lib/maths/types/advanced.mjs +0 -0
- package/dist/lib/maths/types/basic.cjs +18 -0
- package/dist/lib/maths/types/basic.d.ts +95 -0
- package/dist/lib/maths/types/basic.d.ts.map +1 -0
- package/dist/lib/maths/types/basic.js +5 -0
- package/dist/lib/maths/types/basic.mjs +0 -0
- package/dist/lib/maths/types/index.cjs +19 -0
- package/dist/lib/maths/types/index.d.ts +4 -0
- package/dist/lib/maths/types/index.d.ts.map +1 -0
- package/dist/lib/maths/types/index.js +3 -0
- package/dist/lib/maths/types/index.mjs +1 -0
- package/dist/lib/maths/types/negative.cjs +18 -0
- package/dist/lib/maths/types/negative.d.ts +51 -0
- package/dist/lib/maths/types/negative.d.ts.map +1 -0
- package/dist/lib/maths/types/negative.js +5 -0
- package/dist/lib/maths/types/negative.mjs +0 -0
- package/dist/lib/maths/types/operations.cjs +18 -0
- package/dist/lib/maths/types/operations.d.ts +188 -0
- package/dist/lib/maths/types/operations.d.ts.map +1 -0
- package/dist/lib/maths/types/operations.js +6 -0
- package/dist/lib/maths/types/operations.mjs +0 -0
- package/dist/lib/object/clone.cjs +88 -0
- package/dist/lib/object/clone.d.ts +46 -0
- package/dist/lib/object/clone.d.ts.map +1 -0
- package/dist/lib/object/clone.js +104 -0
- package/dist/lib/object/clone.mjs +65 -0
- package/dist/lib/object/diff.cjs +573 -0
- package/dist/lib/object/diff.d.ts +203 -0
- package/dist/lib/object/diff.d.ts.map +1 -0
- package/dist/lib/object/diff.js +805 -0
- package/dist/lib/object/diff.mjs +550 -0
- package/dist/lib/object/getter-setter.cjs +2351 -0
- package/dist/lib/object/getter-setter.d.ts +361 -0
- package/dist/lib/object/getter-setter.d.ts.map +1 -0
- package/dist/lib/object/getter-setter.js +1771 -0
- package/dist/lib/object/getter-setter.mjs +2330 -0
- package/dist/lib/object/index.cjs +3956 -0
- package/dist/lib/object/index.d.ts +13 -0
- package/dist/lib/object/index.d.ts.map +1 -0
- package/dist/lib/object/index.js +12 -0
- package/dist/lib/object/index.mjs +3936 -0
- package/dist/lib/object/merge.cjs +1029 -0
- package/dist/lib/object/merge.d.ts +93 -0
- package/dist/lib/object/merge.d.ts.map +1 -0
- package/dist/lib/object/merge.js +474 -0
- package/dist/lib/object/merge.mjs +1006 -0
- package/dist/lib/object/object.cjs +280 -0
- package/dist/lib/object/object.d.ts +128 -0
- package/dist/lib/object/object.d.ts.map +1 -0
- package/dist/lib/object/object.js +432 -0
- package/dist/lib/object/object.mjs +257 -0
- package/dist/lib/object/omit.cjs +2022 -0
- package/dist/lib/object/omit.d.ts +351 -0
- package/dist/lib/object/omit.d.ts.map +1 -0
- package/dist/lib/object/omit.js +656 -0
- package/dist/lib/object/omit.mjs +1999 -0
- package/dist/lib/object/reconcile.cjs +126 -0
- package/dist/lib/object/reconcile.d.ts +7 -0
- package/dist/lib/object/reconcile.d.ts.map +1 -0
- package/dist/lib/object/reconcile.js +122 -0
- package/dist/lib/object/reconcile.mjs +105 -0
- package/dist/lib/object/selector.cjs +303 -0
- package/dist/lib/object/selector.d.ts +50 -0
- package/dist/lib/object/selector.d.ts.map +1 -0
- package/dist/lib/object/selector.js +371 -0
- package/dist/lib/object/selector.mjs +282 -0
- package/dist/lib/object/sort.cjs +209 -0
- package/dist/lib/object/sort.d.ts +149 -0
- package/dist/lib/object/sort.d.ts.map +1 -0
- package/dist/lib/object/sort.js +298 -0
- package/dist/lib/object/sort.mjs +186 -0
- package/dist/lib/object/transform.cjs +2211 -0
- package/dist/lib/object/transform.d.ts +620 -0
- package/dist/lib/object/transform.d.ts.map +1 -0
- package/dist/lib/object/transform.js +1058 -0
- package/dist/lib/object/transform.mjs +2190 -0
- package/dist/lib/object/types.cjs +18 -0
- package/dist/lib/object/types.d.ts +250 -0
- package/dist/lib/object/types.d.ts.map +1 -0
- package/dist/lib/object/types.js +1 -0
- package/dist/lib/object/types.mjs +0 -0
- package/dist/lib/object/utils.cjs +326 -0
- package/dist/lib/object/utils.d.ts +165 -0
- package/dist/lib/object/utils.d.ts.map +1 -0
- package/dist/lib/object/utils.js +444 -0
- package/dist/lib/object/utils.mjs +305 -0
- package/dist/lib/primitive/boolean/boolean.cjs +407 -0
- package/dist/lib/primitive/boolean/boolean.d.ts +276 -0
- package/dist/lib/primitive/boolean/boolean.d.ts.map +1 -0
- package/dist/lib/primitive/boolean/boolean.js +615 -0
- package/dist/lib/primitive/boolean/boolean.mjs +386 -0
- package/dist/lib/primitive/boolean/index.cjs +382 -0
- package/dist/lib/primitive/boolean/index.d.ts +3 -0
- package/dist/lib/primitive/boolean/index.d.ts.map +1 -0
- package/dist/lib/primitive/boolean/index.js +2 -0
- package/dist/lib/primitive/boolean/index.mjs +359 -0
- package/dist/lib/primitive/boolean/types.cjs +18 -0
- package/dist/lib/primitive/boolean/types.d.ts +14 -0
- package/dist/lib/primitive/boolean/types.d.ts.map +1 -0
- package/dist/lib/primitive/boolean/types.js +1 -0
- package/dist/lib/primitive/boolean/types.mjs +0 -0
- package/dist/lib/primitive/date/date.cjs +1126 -0
- package/dist/lib/primitive/date/date.d.ts +641 -0
- package/dist/lib/primitive/date/date.d.ts.map +1 -0
- package/dist/lib/primitive/date/date.js +1998 -0
- package/dist/lib/primitive/date/date.mjs +1105 -0
- package/dist/lib/primitive/date/index.cjs +1128 -0
- package/dist/lib/primitive/date/index.d.ts +3 -0
- package/dist/lib/primitive/date/index.d.ts.map +1 -0
- package/dist/lib/primitive/date/index.js +2 -0
- package/dist/lib/primitive/date/index.mjs +1105 -0
- package/dist/lib/primitive/date/types.cjs +18 -0
- package/dist/lib/primitive/date/types.d.ts +205 -0
- package/dist/lib/primitive/date/types.d.ts.map +1 -0
- package/dist/lib/primitive/date/types.js +7 -0
- package/dist/lib/primitive/date/types.mjs +0 -0
- package/dist/lib/primitive/index.cjs +2338 -0
- package/dist/lib/primitive/index.d.ts +5 -0
- package/dist/lib/primitive/index.d.ts.map +1 -0
- package/dist/lib/primitive/index.js +4 -0
- package/dist/lib/primitive/index.mjs +2316 -0
- package/dist/lib/primitive/string/index.cjs +875 -0
- package/dist/lib/primitive/string/index.d.ts +3 -0
- package/dist/lib/primitive/string/index.d.ts.map +1 -0
- package/dist/lib/primitive/string/index.js +2 -0
- package/dist/lib/primitive/string/index.mjs +852 -0
- package/dist/lib/primitive/string/parser-2.cjs +543 -0
- package/dist/lib/primitive/string/parser-2.d.ts +17 -0
- package/dist/lib/primitive/string/parser-2.d.ts.map +1 -0
- package/dist/lib/primitive/string/parser-2.js +656 -0
- package/dist/lib/primitive/string/parser-2.mjs +522 -0
- package/dist/lib/primitive/string/parser.cjs +236 -0
- package/dist/lib/primitive/string/parser.d.ts +9 -0
- package/dist/lib/primitive/string/parser.d.ts.map +1 -0
- package/dist/lib/primitive/string/parser.js +775 -0
- package/dist/lib/primitive/string/parser.mjs +215 -0
- package/dist/lib/primitive/string/string.cjs +926 -0
- package/dist/lib/primitive/string/string.d.ts +535 -0
- package/dist/lib/primitive/string/string.d.ts.map +1 -0
- package/dist/lib/primitive/string/string.js +1301 -0
- package/dist/lib/primitive/string/string.mjs +903 -0
- package/dist/lib/primitive/string/types.cjs +18 -0
- package/dist/lib/primitive/string/types.d.ts +318 -0
- package/dist/lib/primitive/string/types.d.ts.map +1 -0
- package/dist/lib/primitive/string/types.js +5 -0
- package/dist/lib/primitive/string/types.mjs +0 -0
- package/dist/lib/primitive/types.cjs +18 -0
- package/dist/lib/primitive/types.d.ts +27 -0
- package/dist/lib/primitive/types.d.ts.map +1 -0
- package/dist/lib/primitive/types.js +1 -0
- package/dist/lib/primitive/types.mjs +0 -0
- package/dist/lib/url/index.cjs +327 -0
- package/dist/lib/url/index.d.ts +3 -0
- package/dist/lib/url/index.d.ts.map +1 -0
- package/dist/lib/url/index.js +2 -0
- package/dist/lib/url/index.mjs +304 -0
- package/dist/lib/url/parser.cjs +325 -0
- package/dist/lib/url/parser.d.ts +216 -0
- package/dist/lib/url/parser.d.ts.map +1 -0
- package/dist/lib/url/parser.js +488 -0
- package/dist/lib/url/parser.mjs +304 -0
- package/dist/lib/url/types.cjs +18 -0
- package/dist/lib/url/types.d.ts +18 -0
- package/dist/lib/url/types.d.ts.map +1 -0
- package/dist/lib/url/types.js +1 -0
- package/dist/lib/url/types.mjs +0 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -0
- package/package.json +127 -0
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Advanced mathematical operations.
|
|
3
|
+
* @module AdvancedMathFunctions
|
|
4
|
+
*/
|
|
5
|
+
import { gcd, round } from './basic.js';
|
|
6
|
+
/**
|
|
7
|
+
* Calculates the natural logarithm of a number.
|
|
8
|
+
* @param value - The number
|
|
9
|
+
* @returns The natural logarithm
|
|
10
|
+
* @throws {Error} If value is not positive
|
|
11
|
+
*/
|
|
12
|
+
export function ln(value) {
|
|
13
|
+
if (value <= 0) {
|
|
14
|
+
throw new Error('Natural logarithm is only defined for positive numbers');
|
|
15
|
+
}
|
|
16
|
+
return Math.log(value);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Calculates the base-10 logarithm of a number.
|
|
20
|
+
* @param value - The number
|
|
21
|
+
* @returns The base-10 logarithm
|
|
22
|
+
* @throws {Error} If value is not positive
|
|
23
|
+
*/
|
|
24
|
+
export function log10(value) {
|
|
25
|
+
if (value <= 0) {
|
|
26
|
+
throw new Error('Logarithm is only defined for positive numbers');
|
|
27
|
+
}
|
|
28
|
+
return Math.log10(value);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Calculates the base-2 logarithm of a number.
|
|
32
|
+
* @param value - The number
|
|
33
|
+
* @returns The base-2 logarithm
|
|
34
|
+
* @throws {Error} If value is not positive
|
|
35
|
+
*/
|
|
36
|
+
export function log2(value) {
|
|
37
|
+
if (value <= 0) {
|
|
38
|
+
throw new Error('Logarithm is only defined for positive numbers');
|
|
39
|
+
}
|
|
40
|
+
return Math.log2(value);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Calculates the logarithm of a number with a custom base.
|
|
44
|
+
* @param value - The number
|
|
45
|
+
* @param base - The logarithm base
|
|
46
|
+
* @returns The logarithm with specified base
|
|
47
|
+
* @throws {Error} If value or base is not positive
|
|
48
|
+
*/
|
|
49
|
+
export function log(value, base) {
|
|
50
|
+
if (value <= 0 || base <= 0) {
|
|
51
|
+
throw new Error('Logarithm is only defined for positive numbers and bases');
|
|
52
|
+
}
|
|
53
|
+
return Math.log(value) / Math.log(base);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Calculates the exponential function e^x.
|
|
57
|
+
* @param x - The exponent
|
|
58
|
+
* @returns e raised to the power x
|
|
59
|
+
*/
|
|
60
|
+
export function exp(x) {
|
|
61
|
+
return Math.exp(x);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Calculates the cube root of a number.
|
|
65
|
+
* @param value - The number
|
|
66
|
+
* @returns The cube root
|
|
67
|
+
*/
|
|
68
|
+
export function cbrt(value) {
|
|
69
|
+
return Math.cbrt(value);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Calculates the nth root of a number.
|
|
73
|
+
* @param value - The number
|
|
74
|
+
* @param n - The root degree
|
|
75
|
+
* @returns The nth root
|
|
76
|
+
* @throws {Error} If n is 0 or value is negative for even n
|
|
77
|
+
*/
|
|
78
|
+
export function nthRoot(value, n) {
|
|
79
|
+
if (n === 0) {
|
|
80
|
+
throw new Error('Cannot calculate 0th root');
|
|
81
|
+
}
|
|
82
|
+
if (value < 0 && n % 2 === 0) {
|
|
83
|
+
throw new Error('Cannot calculate even root of negative number');
|
|
84
|
+
}
|
|
85
|
+
const exponent = 1 / n;
|
|
86
|
+
return Math.sign(value) * Math.pow(Math.abs(value), exponent);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Solves a quadratic equation ax^2 + bx + c = 0.
|
|
90
|
+
* @param a - Coefficient of x^2
|
|
91
|
+
* @param b - Coefficient of x
|
|
92
|
+
* @param c - Constant term
|
|
93
|
+
* @returns Array of real roots (0, 1, or 2 roots)
|
|
94
|
+
*/
|
|
95
|
+
export function solveQuadratic(a, b, c) {
|
|
96
|
+
if (a === 0) {
|
|
97
|
+
// Linear equation
|
|
98
|
+
if (b === 0)
|
|
99
|
+
return [];
|
|
100
|
+
return [-c / b];
|
|
101
|
+
}
|
|
102
|
+
const discriminant = b * b - 4 * a * c;
|
|
103
|
+
if (discriminant < 0) {
|
|
104
|
+
return [];
|
|
105
|
+
}
|
|
106
|
+
else if (discriminant === 0) {
|
|
107
|
+
return [-b / (2 * a)];
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
const sqrtDiscriminant = Math.sqrt(discriminant);
|
|
111
|
+
return [
|
|
112
|
+
(-b - sqrtDiscriminant) / (2 * a),
|
|
113
|
+
(-b + sqrtDiscriminant) / (2 * a)
|
|
114
|
+
];
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Evaluates a polynomial at a given x value.
|
|
119
|
+
* @param coefficients - Polynomial coefficients from highest to lowest degree
|
|
120
|
+
* @param x - The value to evaluate at
|
|
121
|
+
* @returns The polynomial value at x
|
|
122
|
+
*/
|
|
123
|
+
export function evaluatePolynomial(coefficients, x) {
|
|
124
|
+
let result = 0;
|
|
125
|
+
for (let i = 0; i < coefficients.length; i++) {
|
|
126
|
+
result = result * x + coefficients[i];
|
|
127
|
+
}
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Simplifies a fraction to its lowest terms.
|
|
132
|
+
* @param fraction - The fraction to simplify
|
|
133
|
+
* @returns The simplified fraction
|
|
134
|
+
*/
|
|
135
|
+
export function simplifyFraction(fraction) {
|
|
136
|
+
const divisor = gcd(fraction.numerator, fraction.denominator);
|
|
137
|
+
return {
|
|
138
|
+
numerator: fraction.numerator / divisor,
|
|
139
|
+
denominator: fraction.denominator / divisor
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Adds two fractions.
|
|
144
|
+
* @param a - First fraction
|
|
145
|
+
* @param b - Second fraction
|
|
146
|
+
* @returns The sum as a simplified fraction
|
|
147
|
+
*/
|
|
148
|
+
export function addFractions(a, b) {
|
|
149
|
+
const numerator = a.numerator * b.denominator + b.numerator * a.denominator;
|
|
150
|
+
const denominator = a.denominator * b.denominator;
|
|
151
|
+
return simplifyFraction({ numerator, denominator });
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Multiplies two fractions.
|
|
155
|
+
* @param a - First fraction
|
|
156
|
+
* @param b - Second fraction
|
|
157
|
+
* @returns The product as a simplified fraction
|
|
158
|
+
*/
|
|
159
|
+
export function multiplyFractions(a, b) {
|
|
160
|
+
const numerator = a.numerator * b.numerator;
|
|
161
|
+
const denominator = a.denominator * b.denominator;
|
|
162
|
+
return simplifyFraction({ numerator, denominator });
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Calculates the percentage of a number.
|
|
166
|
+
* @param value - The number
|
|
167
|
+
* @param percentage - The percentage (e.g., 25 for 25%)
|
|
168
|
+
* @param precision - The number of decimal places
|
|
169
|
+
* @returns The percentage of the number
|
|
170
|
+
*/
|
|
171
|
+
export const percentage = (value, total, precision = 2) => {
|
|
172
|
+
if (total === 0)
|
|
173
|
+
return 0;
|
|
174
|
+
const percent = (value / total) * 100;
|
|
175
|
+
return round(percent, precision);
|
|
176
|
+
};
|
|
177
|
+
/**
|
|
178
|
+
* Converts a number to a specified base (2-36)
|
|
179
|
+
*
|
|
180
|
+
* @param number - The number
|
|
181
|
+
*/
|
|
182
|
+
export const toBase = (number, base, minLength = 1) => {
|
|
183
|
+
if (base < 2 || base > 36) {
|
|
184
|
+
throw new Error('Base must be between 2 and 36');
|
|
185
|
+
}
|
|
186
|
+
const result = number.toString(base);
|
|
187
|
+
return result.padStart(minLength, '0');
|
|
188
|
+
};
|
|
189
|
+
/**
|
|
190
|
+
* Converts a number to ordinal string (1st, 2nd, 3rd, etc.)
|
|
191
|
+
*
|
|
192
|
+
* @param n - The number
|
|
193
|
+
*/
|
|
194
|
+
export const toOrdinal = (n) => {
|
|
195
|
+
const suffix = ['th', 'st', 'nd', 'rd'];
|
|
196
|
+
const v = n % 100;
|
|
197
|
+
return n + (suffix[(v - 20) % 10] || suffix[v] || suffix[0]);
|
|
198
|
+
};
|
|
199
|
+
/**
|
|
200
|
+
* Generates a sequence of numbers
|
|
201
|
+
*
|
|
202
|
+
* @param start - The starting number
|
|
203
|
+
* @param end - The ending number
|
|
204
|
+
* @param step - The step size
|
|
205
|
+
*
|
|
206
|
+
* @returns An array of numbers
|
|
207
|
+
*/
|
|
208
|
+
export const range = (start, end, step = 1) => {
|
|
209
|
+
const result = [];
|
|
210
|
+
if (step === 0)
|
|
211
|
+
return result;
|
|
212
|
+
if (step > 0) {
|
|
213
|
+
for (let i = start; i < end; i += step) {
|
|
214
|
+
result.push(i);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
for (let i = start; i > end; i += step) {
|
|
219
|
+
result.push(i);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return result;
|
|
223
|
+
};
|
|
224
|
+
/**
|
|
225
|
+
* Limits the number of decimal places without rounding
|
|
226
|
+
*
|
|
227
|
+
* @param value - The number
|
|
228
|
+
* @param decimalPlaces - The number of decimal places
|
|
229
|
+
*
|
|
230
|
+
* @returns The truncated number
|
|
231
|
+
*/
|
|
232
|
+
export const truncateDecimals = (value, decimalPlaces) => {
|
|
233
|
+
if (!Number.isFinite(value))
|
|
234
|
+
return value;
|
|
235
|
+
const factor = 10 ** decimalPlaces;
|
|
236
|
+
return Math.trunc(value * factor) / factor;
|
|
237
|
+
};
|
|
238
|
+
/**
|
|
239
|
+
* Normalizes a value to a 0-1 range based on min/max
|
|
240
|
+
*
|
|
241
|
+
* @param value - The number
|
|
242
|
+
* @param min - The minimum value
|
|
243
|
+
* @param max - The maximum value
|
|
244
|
+
*
|
|
245
|
+
* @returns The normalized value
|
|
246
|
+
*/
|
|
247
|
+
export const normalize = (value, min, max) => {
|
|
248
|
+
if (min === max)
|
|
249
|
+
return 0;
|
|
250
|
+
return (value - min) / (max - min);
|
|
251
|
+
};
|
|
252
|
+
/**
|
|
253
|
+
* Wraps a value within a range (like modulo for floats)
|
|
254
|
+
*
|
|
255
|
+
* @param value - The number
|
|
256
|
+
* @param min - The minimum value
|
|
257
|
+
* @param max - The maximum value
|
|
258
|
+
*
|
|
259
|
+
* @returns The wrapped value
|
|
260
|
+
*/
|
|
261
|
+
export const wrap = (value, min, max) => {
|
|
262
|
+
const range = max - min;
|
|
263
|
+
return ((((value - min) % range) + range) % range) + min;
|
|
264
|
+
};
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// src/lib/maths/fn/basic.ts
|
|
2
|
+
var round = (value, precision = 0, mode = "round") => {
|
|
3
|
+
if (mode === "david") return roundUsingDavidGay(value, precision, mode);
|
|
4
|
+
if (!Number.isFinite(value)) return value;
|
|
5
|
+
if (precision === 0) return Math[mode](value);
|
|
6
|
+
if (precision < 0) {
|
|
7
|
+
const factor2 = 10 ** -precision;
|
|
8
|
+
return Math[mode](value / factor2) * factor2;
|
|
9
|
+
}
|
|
10
|
+
const factor = 10 ** precision;
|
|
11
|
+
const epsilon = Math.pow(2, -52);
|
|
12
|
+
const adjusted = value * factor;
|
|
13
|
+
const correction = Math.sign(adjusted) * epsilon * Math.abs(adjusted);
|
|
14
|
+
let rounded;
|
|
15
|
+
switch (mode) {
|
|
16
|
+
case "round":
|
|
17
|
+
rounded = Math.round(adjusted + correction);
|
|
18
|
+
break;
|
|
19
|
+
case "ceil":
|
|
20
|
+
rounded = Math.ceil(adjusted - epsilon);
|
|
21
|
+
break;
|
|
22
|
+
case "floor":
|
|
23
|
+
rounded = Math.floor(adjusted + epsilon);
|
|
24
|
+
break;
|
|
25
|
+
case "trunc":
|
|
26
|
+
rounded = adjusted < 0 ? Math.ceil(adjusted) : Math.floor(adjusted);
|
|
27
|
+
break;
|
|
28
|
+
default:
|
|
29
|
+
rounded = Math.round(adjusted + correction);
|
|
30
|
+
}
|
|
31
|
+
return rounded / factor;
|
|
32
|
+
};
|
|
33
|
+
var roundUsingDavidGay = (_value, precision = 0, mode = "round") => {
|
|
34
|
+
let value = Math.abs(_value);
|
|
35
|
+
const multiplicator = _value < 0 ? -1 : 1;
|
|
36
|
+
if (precision === 0) {
|
|
37
|
+
return Math.round(value) * multiplicator;
|
|
38
|
+
}
|
|
39
|
+
if (!Number.isFinite(value)) {
|
|
40
|
+
return value * multiplicator;
|
|
41
|
+
}
|
|
42
|
+
const factor = 10 ** precision;
|
|
43
|
+
let shifted = value * factor;
|
|
44
|
+
const epsilon = Math.pow(2, -53);
|
|
45
|
+
const correction = Math.sign(shifted) * epsilon * Math.abs(shifted);
|
|
46
|
+
shifted += correction;
|
|
47
|
+
const rounded = Math.round(shifted);
|
|
48
|
+
return rounded / factor * multiplicator;
|
|
49
|
+
};
|
|
50
|
+
function gcd(a, b) {
|
|
51
|
+
a = Math.abs(a);
|
|
52
|
+
b = Math.abs(b);
|
|
53
|
+
while (b !== 0) {
|
|
54
|
+
const temp = b;
|
|
55
|
+
b = a % b;
|
|
56
|
+
a = temp;
|
|
57
|
+
}
|
|
58
|
+
return a;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/lib/maths/fn/advanced.ts
|
|
62
|
+
function ln(value) {
|
|
63
|
+
if (value <= 0) {
|
|
64
|
+
throw new Error("Natural logarithm is only defined for positive numbers");
|
|
65
|
+
}
|
|
66
|
+
return Math.log(value);
|
|
67
|
+
}
|
|
68
|
+
function log10(value) {
|
|
69
|
+
if (value <= 0) {
|
|
70
|
+
throw new Error("Logarithm is only defined for positive numbers");
|
|
71
|
+
}
|
|
72
|
+
return Math.log10(value);
|
|
73
|
+
}
|
|
74
|
+
function log2(value) {
|
|
75
|
+
if (value <= 0) {
|
|
76
|
+
throw new Error("Logarithm is only defined for positive numbers");
|
|
77
|
+
}
|
|
78
|
+
return Math.log2(value);
|
|
79
|
+
}
|
|
80
|
+
function log(value, base) {
|
|
81
|
+
if (value <= 0 || base <= 0) {
|
|
82
|
+
throw new Error("Logarithm is only defined for positive numbers and bases");
|
|
83
|
+
}
|
|
84
|
+
return Math.log(value) / Math.log(base);
|
|
85
|
+
}
|
|
86
|
+
function exp(x) {
|
|
87
|
+
return Math.exp(x);
|
|
88
|
+
}
|
|
89
|
+
function cbrt(value) {
|
|
90
|
+
return Math.cbrt(value);
|
|
91
|
+
}
|
|
92
|
+
function nthRoot(value, n) {
|
|
93
|
+
if (n === 0) {
|
|
94
|
+
throw new Error("Cannot calculate 0th root");
|
|
95
|
+
}
|
|
96
|
+
if (value < 0 && n % 2 === 0) {
|
|
97
|
+
throw new Error("Cannot calculate even root of negative number");
|
|
98
|
+
}
|
|
99
|
+
const exponent = 1 / n;
|
|
100
|
+
return Math.sign(value) * Math.pow(Math.abs(value), exponent);
|
|
101
|
+
}
|
|
102
|
+
function solveQuadratic(a, b, c) {
|
|
103
|
+
if (a === 0) {
|
|
104
|
+
if (b === 0) return [];
|
|
105
|
+
return [-c / b];
|
|
106
|
+
}
|
|
107
|
+
const discriminant = b * b - 4 * a * c;
|
|
108
|
+
if (discriminant < 0) {
|
|
109
|
+
return [];
|
|
110
|
+
} else if (discriminant === 0) {
|
|
111
|
+
return [-b / (2 * a)];
|
|
112
|
+
} else {
|
|
113
|
+
const sqrtDiscriminant = Math.sqrt(discriminant);
|
|
114
|
+
return [
|
|
115
|
+
(-b - sqrtDiscriminant) / (2 * a),
|
|
116
|
+
(-b + sqrtDiscriminant) / (2 * a)
|
|
117
|
+
];
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function evaluatePolynomial(coefficients, x) {
|
|
121
|
+
let result = 0;
|
|
122
|
+
for (let i = 0; i < coefficients.length; i++) {
|
|
123
|
+
result = result * x + coefficients[i];
|
|
124
|
+
}
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
function simplifyFraction(fraction) {
|
|
128
|
+
const divisor = gcd(fraction.numerator, fraction.denominator);
|
|
129
|
+
return {
|
|
130
|
+
numerator: fraction.numerator / divisor,
|
|
131
|
+
denominator: fraction.denominator / divisor
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function addFractions(a, b) {
|
|
135
|
+
const numerator = a.numerator * b.denominator + b.numerator * a.denominator;
|
|
136
|
+
const denominator = a.denominator * b.denominator;
|
|
137
|
+
return simplifyFraction({ numerator, denominator });
|
|
138
|
+
}
|
|
139
|
+
function multiplyFractions(a, b) {
|
|
140
|
+
const numerator = a.numerator * b.numerator;
|
|
141
|
+
const denominator = a.denominator * b.denominator;
|
|
142
|
+
return simplifyFraction({ numerator, denominator });
|
|
143
|
+
}
|
|
144
|
+
var percentage = (value, total, precision = 2) => {
|
|
145
|
+
if (total === 0) return 0;
|
|
146
|
+
const percent = value / total * 100;
|
|
147
|
+
return round(percent, precision);
|
|
148
|
+
};
|
|
149
|
+
var toBase = (number, base, minLength = 1) => {
|
|
150
|
+
if (base < 2 || base > 36) {
|
|
151
|
+
throw new Error("Base must be between 2 and 36");
|
|
152
|
+
}
|
|
153
|
+
const result = number.toString(base);
|
|
154
|
+
return result.padStart(minLength, "0");
|
|
155
|
+
};
|
|
156
|
+
var toOrdinal = (n) => {
|
|
157
|
+
const suffix = ["th", "st", "nd", "rd"];
|
|
158
|
+
const v = n % 100;
|
|
159
|
+
return n + (suffix[(v - 20) % 10] || suffix[v] || suffix[0]);
|
|
160
|
+
};
|
|
161
|
+
var range = (start, end, step = 1) => {
|
|
162
|
+
const result = [];
|
|
163
|
+
if (step === 0) return result;
|
|
164
|
+
if (step > 0) {
|
|
165
|
+
for (let i = start; i < end; i += step) {
|
|
166
|
+
result.push(i);
|
|
167
|
+
}
|
|
168
|
+
} else {
|
|
169
|
+
for (let i = start; i > end; i += step) {
|
|
170
|
+
result.push(i);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return result;
|
|
174
|
+
};
|
|
175
|
+
var truncateDecimals = (value, decimalPlaces) => {
|
|
176
|
+
if (!Number.isFinite(value)) return value;
|
|
177
|
+
const factor = 10 ** decimalPlaces;
|
|
178
|
+
return Math.trunc(value * factor) / factor;
|
|
179
|
+
};
|
|
180
|
+
var normalize = (value, min, max) => {
|
|
181
|
+
if (min === max) return 0;
|
|
182
|
+
return (value - min) / (max - min);
|
|
183
|
+
};
|
|
184
|
+
var wrap = (value, min, max) => {
|
|
185
|
+
const range2 = max - min;
|
|
186
|
+
return ((value - min) % range2 + range2) % range2 + min;
|
|
187
|
+
};
|
|
188
|
+
export {
|
|
189
|
+
addFractions,
|
|
190
|
+
cbrt,
|
|
191
|
+
evaluatePolynomial,
|
|
192
|
+
exp,
|
|
193
|
+
ln,
|
|
194
|
+
log,
|
|
195
|
+
log10,
|
|
196
|
+
log2,
|
|
197
|
+
multiplyFractions,
|
|
198
|
+
normalize,
|
|
199
|
+
nthRoot,
|
|
200
|
+
percentage,
|
|
201
|
+
range,
|
|
202
|
+
simplifyFraction,
|
|
203
|
+
solveQuadratic,
|
|
204
|
+
toBase,
|
|
205
|
+
toOrdinal,
|
|
206
|
+
truncateDecimals,
|
|
207
|
+
wrap
|
|
208
|
+
};
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/lib/maths/fn/basic.ts
|
|
21
|
+
var basic_exports = {};
|
|
22
|
+
__export(basic_exports, {
|
|
23
|
+
abs: () => abs,
|
|
24
|
+
add: () => add,
|
|
25
|
+
average: () => average,
|
|
26
|
+
ceil: () => ceil,
|
|
27
|
+
clamp: () => clamp,
|
|
28
|
+
divide: () => divide,
|
|
29
|
+
factorial: () => factorial,
|
|
30
|
+
floor: () => floor,
|
|
31
|
+
gcd: () => gcd,
|
|
32
|
+
invLerp: () => invLerp,
|
|
33
|
+
lcm: () => lcm,
|
|
34
|
+
lerp: () => lerp,
|
|
35
|
+
mapRange: () => mapRange,
|
|
36
|
+
max: () => max,
|
|
37
|
+
median: () => median,
|
|
38
|
+
min: () => min,
|
|
39
|
+
modulo: () => modulo,
|
|
40
|
+
power: () => power,
|
|
41
|
+
product: () => product,
|
|
42
|
+
round: () => round,
|
|
43
|
+
sign: () => sign,
|
|
44
|
+
sqrt: () => sqrt,
|
|
45
|
+
subtract: () => subtract,
|
|
46
|
+
sum: () => sum,
|
|
47
|
+
trunc: () => trunc
|
|
48
|
+
});
|
|
49
|
+
module.exports = __toCommonJS(basic_exports);
|
|
50
|
+
function add(a, b) {
|
|
51
|
+
return a + b;
|
|
52
|
+
}
|
|
53
|
+
function subtract(a, b) {
|
|
54
|
+
return a - b;
|
|
55
|
+
}
|
|
56
|
+
var divide = (dividend, divisor, fallback) => {
|
|
57
|
+
if (divisor === 0) {
|
|
58
|
+
return fallback !== void 0 ? fallback : NaN;
|
|
59
|
+
}
|
|
60
|
+
return dividend / divisor;
|
|
61
|
+
};
|
|
62
|
+
function modulo(a, b) {
|
|
63
|
+
return a % b;
|
|
64
|
+
}
|
|
65
|
+
function power(base, exponent) {
|
|
66
|
+
return Math.pow(base, exponent);
|
|
67
|
+
}
|
|
68
|
+
function sqrt(value) {
|
|
69
|
+
if (value < 0) {
|
|
70
|
+
throw new Error("Cannot calculate square root of negative number");
|
|
71
|
+
}
|
|
72
|
+
return Math.sqrt(value);
|
|
73
|
+
}
|
|
74
|
+
function abs(value) {
|
|
75
|
+
return Math.abs(value);
|
|
76
|
+
}
|
|
77
|
+
var round = (value, precision = 0, mode = "round") => {
|
|
78
|
+
if (mode === "david") return roundUsingDavidGay(value, precision, mode);
|
|
79
|
+
if (!Number.isFinite(value)) return value;
|
|
80
|
+
if (precision === 0) return Math[mode](value);
|
|
81
|
+
if (precision < 0) {
|
|
82
|
+
const factor2 = 10 ** -precision;
|
|
83
|
+
return Math[mode](value / factor2) * factor2;
|
|
84
|
+
}
|
|
85
|
+
const factor = 10 ** precision;
|
|
86
|
+
const epsilon = Math.pow(2, -52);
|
|
87
|
+
const adjusted = value * factor;
|
|
88
|
+
const correction = Math.sign(adjusted) * epsilon * Math.abs(adjusted);
|
|
89
|
+
let rounded;
|
|
90
|
+
switch (mode) {
|
|
91
|
+
case "round":
|
|
92
|
+
rounded = Math.round(adjusted + correction);
|
|
93
|
+
break;
|
|
94
|
+
case "ceil":
|
|
95
|
+
rounded = Math.ceil(adjusted - epsilon);
|
|
96
|
+
break;
|
|
97
|
+
case "floor":
|
|
98
|
+
rounded = Math.floor(adjusted + epsilon);
|
|
99
|
+
break;
|
|
100
|
+
case "trunc":
|
|
101
|
+
rounded = adjusted < 0 ? Math.ceil(adjusted) : Math.floor(adjusted);
|
|
102
|
+
break;
|
|
103
|
+
default:
|
|
104
|
+
rounded = Math.round(adjusted + correction);
|
|
105
|
+
}
|
|
106
|
+
return rounded / factor;
|
|
107
|
+
};
|
|
108
|
+
var roundUsingDavidGay = (_value, precision = 0, mode = "round") => {
|
|
109
|
+
let value = Math.abs(_value);
|
|
110
|
+
const multiplicator = _value < 0 ? -1 : 1;
|
|
111
|
+
if (precision === 0) {
|
|
112
|
+
return Math.round(value) * multiplicator;
|
|
113
|
+
}
|
|
114
|
+
if (!Number.isFinite(value)) {
|
|
115
|
+
return value * multiplicator;
|
|
116
|
+
}
|
|
117
|
+
const factor = 10 ** precision;
|
|
118
|
+
let shifted = value * factor;
|
|
119
|
+
const epsilon = Math.pow(2, -53);
|
|
120
|
+
const correction = Math.sign(shifted) * epsilon * Math.abs(shifted);
|
|
121
|
+
shifted += correction;
|
|
122
|
+
const rounded = Math.round(shifted);
|
|
123
|
+
return rounded / factor * multiplicator;
|
|
124
|
+
};
|
|
125
|
+
var ceil = (value, precision = 0) => {
|
|
126
|
+
return round(value, precision, "ceil");
|
|
127
|
+
};
|
|
128
|
+
var floor = (value, precision = 0) => {
|
|
129
|
+
return round(value, precision, "floor");
|
|
130
|
+
};
|
|
131
|
+
var trunc = (value, precision = 0) => {
|
|
132
|
+
return round(value, precision, "trunc");
|
|
133
|
+
};
|
|
134
|
+
var clamp = (value, min2, max2) => {
|
|
135
|
+
if (min2 > max2) [min2, max2] = [max2, min2];
|
|
136
|
+
return value < min2 ? min2 : value > max2 ? max2 : value;
|
|
137
|
+
};
|
|
138
|
+
function sign(value) {
|
|
139
|
+
if (value > 0) return 1;
|
|
140
|
+
if (value < 0) return -1;
|
|
141
|
+
return 0;
|
|
142
|
+
}
|
|
143
|
+
function factorial(n) {
|
|
144
|
+
if (!Number.isInteger(n) || n < 0) {
|
|
145
|
+
throw new Error("Factorial is only defined for non-negative integers");
|
|
146
|
+
}
|
|
147
|
+
if (n === 0 || n === 1) return 1;
|
|
148
|
+
let result = 1;
|
|
149
|
+
for (let i = 2; i <= n; i++) {
|
|
150
|
+
result *= i;
|
|
151
|
+
}
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
function gcd(a, b) {
|
|
155
|
+
a = Math.abs(a);
|
|
156
|
+
b = Math.abs(b);
|
|
157
|
+
while (b !== 0) {
|
|
158
|
+
const temp = b;
|
|
159
|
+
b = a % b;
|
|
160
|
+
a = temp;
|
|
161
|
+
}
|
|
162
|
+
return a;
|
|
163
|
+
}
|
|
164
|
+
function lcm(a, b) {
|
|
165
|
+
return Math.abs(a * b) / gcd(a, b);
|
|
166
|
+
}
|
|
167
|
+
function lerp(start, end, t) {
|
|
168
|
+
return start + (end - start) * clamp(t, 0, 1);
|
|
169
|
+
}
|
|
170
|
+
var invLerp = (start, end, value) => {
|
|
171
|
+
return clamp((value - start) / (end - start), 0, 1);
|
|
172
|
+
};
|
|
173
|
+
var mapRange = (value, inMin, inMax, outMin, outMax) => {
|
|
174
|
+
const t = invLerp(inMin, inMax, value);
|
|
175
|
+
return lerp(outMin, outMax, t);
|
|
176
|
+
};
|
|
177
|
+
function min(...values) {
|
|
178
|
+
return Math.min(...values);
|
|
179
|
+
}
|
|
180
|
+
function max(...values) {
|
|
181
|
+
return Math.max(...values);
|
|
182
|
+
}
|
|
183
|
+
var average = (...numbers) => {
|
|
184
|
+
if (!Array.isArray(numbers) || numbers.length === 0) return 0;
|
|
185
|
+
let total = 0;
|
|
186
|
+
let count = 0;
|
|
187
|
+
for (const num of numbers) {
|
|
188
|
+
if (Number.isFinite(num)) {
|
|
189
|
+
total += num;
|
|
190
|
+
count++;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return count === 0 ? 0 : total / count;
|
|
194
|
+
};
|
|
195
|
+
var sum = (...numbers) => {
|
|
196
|
+
if (!Array.isArray(numbers)) return 0;
|
|
197
|
+
return numbers.reduce(
|
|
198
|
+
(acc, val) => acc + (Number.isFinite(val) ? val : 0),
|
|
199
|
+
0
|
|
200
|
+
);
|
|
201
|
+
};
|
|
202
|
+
var median = (...numbers) => {
|
|
203
|
+
if (!Array.isArray(numbers) || numbers.length === 0) return 0;
|
|
204
|
+
const validNumbers = numbers.filter(Number.isFinite);
|
|
205
|
+
if (validNumbers.length === 0) return 0;
|
|
206
|
+
validNumbers.sort((a, b) => a - b);
|
|
207
|
+
const mid = Math.floor(validNumbers.length / 2);
|
|
208
|
+
return validNumbers.length % 2 === 0 ? (validNumbers[mid - 1] + validNumbers[mid]) / 2 : validNumbers[mid];
|
|
209
|
+
};
|
|
210
|
+
function product(...values) {
|
|
211
|
+
return values.reduce((prod, val) => prod * val, 1);
|
|
212
|
+
}
|