curvy 1.0.9 → 2.0.0-alpha.2
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/characteristic/index.d.mts +88 -0
- package/dist/characteristic/index.mjs +2 -0
- package/dist/curve/index.d.mts +2 -0
- package/dist/curve/index.mjs +2 -0
- package/dist/interval/index.d.mts +3 -0
- package/dist/interval/index.mjs +3 -0
- package/dist/matrix/index.d.mts +2 -0
- package/dist/matrix/index.mjs +267 -0
- package/dist/matrix/index.mjs.map +1 -0
- package/dist/number.d.mts +280 -0
- package/dist/number.mjs +140 -0
- package/dist/number.mjs.map +1 -0
- package/dist/path/index.d.mts +261 -0
- package/dist/path/index.mjs +325 -0
- package/dist/path/index.mjs.map +1 -0
- package/dist/polynomial/index.d.mts +2 -0
- package/dist/polynomial/index.mjs +3 -0
- package/dist/shared/characteristic-B9nCh_cx.mjs +104 -0
- package/dist/shared/characteristic-B9nCh_cx.mjs.map +1 -0
- package/dist/shared/chunk-25Hzd3z8.mjs +13 -0
- package/dist/shared/cubic-BajUY0JT.d.mts +1100 -0
- package/dist/shared/cubic-CpHAx8cC.mjs +479 -0
- package/dist/shared/cubic-CpHAx8cC.mjs.map +1 -0
- package/dist/shared/interval-05CR6eBf.mjs +421 -0
- package/dist/shared/interval-05CR6eBf.mjs.map +1 -0
- package/dist/shared/interval-Dy-RMEE3.d.mts +533 -0
- package/dist/shared/interval2d-8IBZnUOq.mjs +108 -0
- package/dist/shared/interval2d-8IBZnUOq.mjs.map +1 -0
- package/dist/shared/interval2d-C2id9u31.d.mts +172 -0
- package/dist/shared/matrix4x4-Ck2WROK-.mjs +440 -0
- package/dist/shared/matrix4x4-Ck2WROK-.mjs.map +1 -0
- package/dist/shared/matrix4x4-Dm_tSiH1.d.mts +1183 -0
- package/dist/shared/quadratic-CamozcFc.mjs +330 -0
- package/dist/shared/quadratic-CamozcFc.mjs.map +1 -0
- package/dist/shared/rationalCubic2d-B6g97ryJ.d.mts +308 -0
- package/dist/shared/rationalCubic2d-BEgUGSFf.mjs +295 -0
- package/dist/shared/rationalCubic2d-BEgUGSFf.mjs.map +1 -0
- package/dist/shared/rationalCubic2d-Bys2Uory.mjs +695 -0
- package/dist/shared/rationalCubic2d-Bys2Uory.mjs.map +1 -0
- package/dist/shared/rationalCubic2d-DlQfBURs.d.mts +818 -0
- package/dist/shared/solution-CrtLfqEq.mjs +306 -0
- package/dist/shared/solution-CrtLfqEq.mjs.map +1 -0
- package/dist/shared/solution-sNQ-ZVhN.d.mts +251 -0
- package/dist/{pipe.d.mts → shared/utils-MCckigYk.d.mts} +44 -26
- package/dist/shared/vector2-CL1HDdPr.mjs +187 -0
- package/dist/shared/vector2-CL1HDdPr.mjs.map +1 -0
- package/dist/shared/vector2-DPUY3_eA.d.mts +470 -0
- package/dist/shared/vector2.internal-DU_E808S.mjs +97 -0
- package/dist/shared/vector2.internal-DU_E808S.mjs.map +1 -0
- package/dist/shared/vector3-D-tv_aKL.mjs +204 -0
- package/dist/shared/vector3-D-tv_aKL.mjs.map +1 -0
- package/dist/shared/vector3-DqeOZtOB.d.mts +703 -0
- package/dist/shared/vector4-5KbjoHVj.mjs +109 -0
- package/dist/shared/vector4-5KbjoHVj.mjs.map +1 -0
- package/dist/shared/vector4.internal-5XcaX12X.mjs +163 -0
- package/dist/shared/vector4.internal-5XcaX12X.mjs.map +1 -0
- package/dist/solution/index.d.mts +2 -0
- package/dist/solution/index.mjs +2 -0
- package/dist/splines/index.d.mts +701 -0
- package/dist/splines/index.mjs +734 -0
- package/dist/splines/index.mjs.map +1 -0
- package/dist/utils.d.mts +2 -205
- package/dist/utils.mjs +88 -52
- package/dist/utils.mjs.map +1 -1
- package/dist/vector/index.d.mts +3 -0
- package/dist/vector/index.mjs +4 -0
- package/package.json +49 -92
- package/dist/curve/cubic2d.d.mts +0 -109
- package/dist/curve/cubic2d.d.ts +0 -109
- package/dist/curve/cubic2d.mjs +0 -80
- package/dist/curve/cubic2d.mjs.map +0 -1
- package/dist/curve/linear2d.d.mts +0 -94
- package/dist/curve/linear2d.d.ts +0 -94
- package/dist/curve/linear2d.mjs +0 -46
- package/dist/curve/linear2d.mjs.map +0 -1
- package/dist/curve/quadratic2d.d.mts +0 -117
- package/dist/curve/quadratic2d.d.ts +0 -117
- package/dist/curve/quadratic2d.mjs +0 -82
- package/dist/curve/quadratic2d.mjs.map +0 -1
- package/dist/interval.d.mts +0 -251
- package/dist/interval.d.ts +0 -251
- package/dist/interval.mjs +0 -86
- package/dist/interval.mjs.map +0 -1
- package/dist/matrix/matrix2x2.d.mts +0 -320
- package/dist/matrix/matrix2x2.d.ts +0 -320
- package/dist/matrix/matrix2x2.mjs +0 -25
- package/dist/matrix/matrix2x2.mjs.map +0 -1
- package/dist/matrix/matrix3x3.d.mts +0 -370
- package/dist/matrix/matrix3x3.d.ts +0 -370
- package/dist/matrix/matrix3x3.mjs +0 -28
- package/dist/matrix/matrix3x3.mjs.map +0 -1
- package/dist/matrix/matrix4x4.d.mts +0 -381
- package/dist/matrix/matrix4x4.d.ts +0 -381
- package/dist/matrix/matrix4x4.mjs +0 -178
- package/dist/matrix/matrix4x4.mjs.map +0 -1
- package/dist/path/cubic2d.d.mts +0 -95
- package/dist/path/cubic2d.d.ts +0 -95
- package/dist/path/cubic2d.mjs +0 -60
- package/dist/path/cubic2d.mjs.map +0 -1
- package/dist/path/linear2d.d.mts +0 -95
- package/dist/path/linear2d.d.ts +0 -95
- package/dist/path/linear2d.mjs +0 -53
- package/dist/path/linear2d.mjs.map +0 -1
- package/dist/path/quadratic2d.d.mts +0 -95
- package/dist/path/quadratic2d.d.ts +0 -95
- package/dist/path/quadratic2d.mjs +0 -57
- package/dist/path/quadratic2d.mjs.map +0 -1
- package/dist/pipe.d.ts +0 -58
- package/dist/pipe.mjs +0 -92
- package/dist/pipe.mjs.map +0 -1
- package/dist/polynomial/cubic.d.mts +0 -7
- package/dist/polynomial/cubic.d.ts +0 -7
- package/dist/polynomial/cubic.mjs +0 -133
- package/dist/polynomial/cubic.mjs.map +0 -1
- package/dist/polynomial/linear.d.mts +0 -7
- package/dist/polynomial/linear.d.ts +0 -7
- package/dist/polynomial/linear.mjs +0 -24
- package/dist/polynomial/linear.mjs.map +0 -1
- package/dist/polynomial/quadratic.d.mts +0 -7
- package/dist/polynomial/quadratic.d.ts +0 -7
- package/dist/polynomial/quadratic.mjs +0 -27
- package/dist/polynomial/quadratic.mjs.map +0 -1
- package/dist/shared/curvy.Bf5TIk31.d.mts +0 -22
- package/dist/shared/curvy.Bf5TIk31.d.ts +0 -22
- package/dist/shared/curvy.BkkW8Tx9.mjs +0 -102
- package/dist/shared/curvy.BkkW8Tx9.mjs.map +0 -1
- package/dist/shared/curvy.BxdGW9he.mjs +0 -116
- package/dist/shared/curvy.BxdGW9he.mjs.map +0 -1
- package/dist/shared/curvy.C_sCeg9J.mjs +0 -66
- package/dist/shared/curvy.C_sCeg9J.mjs.map +0 -1
- package/dist/shared/curvy.Cg8lZVUz.d.ts +0 -688
- package/dist/shared/curvy.DCt1gGII.mjs +0 -147
- package/dist/shared/curvy.DCt1gGII.mjs.map +0 -1
- package/dist/shared/curvy.DJ4xO0al.mjs +0 -63
- package/dist/shared/curvy.DJ4xO0al.mjs.map +0 -1
- package/dist/shared/curvy.DmVFTi0N.mjs +0 -35
- package/dist/shared/curvy.DmVFTi0N.mjs.map +0 -1
- package/dist/shared/curvy.FDaj5rlj.d.mts +0 -688
- package/dist/shared/curvy.lktriTpx.mjs +0 -101
- package/dist/shared/curvy.lktriTpx.mjs.map +0 -1
- package/dist/shared/curvy.mLFprrGm.mjs +0 -85
- package/dist/shared/curvy.mLFprrGm.mjs.map +0 -1
- package/dist/shared/curvy.r6f1j287.mjs +0 -76
- package/dist/shared/curvy.r6f1j287.mjs.map +0 -1
- package/dist/splines/basis2d.d.mts +0 -130
- package/dist/splines/basis2d.d.ts +0 -130
- package/dist/splines/basis2d.mjs +0 -90
- package/dist/splines/basis2d.mjs.map +0 -1
- package/dist/splines/bezier2d.d.mts +0 -182
- package/dist/splines/bezier2d.d.ts +0 -182
- package/dist/splines/bezier2d.mjs +0 -23
- package/dist/splines/bezier2d.mjs.map +0 -1
- package/dist/splines/cardinal2d.d.mts +0 -178
- package/dist/splines/cardinal2d.d.ts +0 -178
- package/dist/splines/cardinal2d.mjs +0 -114
- package/dist/splines/cardinal2d.mjs.map +0 -1
- package/dist/splines/hermite2d.d.mts +0 -124
- package/dist/splines/hermite2d.d.ts +0 -124
- package/dist/splines/hermite2d.mjs +0 -63
- package/dist/splines/hermite2d.mjs.map +0 -1
- package/dist/utils.d.ts +0 -205
- package/dist/vector/vector2.d.mts +0 -324
- package/dist/vector/vector2.d.ts +0 -324
- package/dist/vector/vector2.mjs +0 -30
- package/dist/vector/vector2.mjs.map +0 -1
- package/dist/vector/vector3.d.mts +0 -417
- package/dist/vector/vector3.d.ts +0 -417
- package/dist/vector/vector3.mjs +0 -38
- package/dist/vector/vector3.mjs.map +0 -1
- package/dist/vector/vector4.d.mts +0 -205
- package/dist/vector/vector4.d.ts +0 -205
- package/dist/vector/vector4.mjs +0 -20
- package/dist/vector/vector4.mjs.map +0 -1
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
//#region src/number.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Default tolerance for approximate equality between floating-point numbers.
|
|
4
|
+
*
|
|
5
|
+
* @since 1.1.0
|
|
6
|
+
*/
|
|
7
|
+
declare const EPSILON = 1e-10;
|
|
8
|
+
/**
|
|
9
|
+
* Approximate equality between two numbers, within an absolute tolerance.
|
|
10
|
+
*
|
|
11
|
+
* @param a - The first value.
|
|
12
|
+
* @param b - The second value.
|
|
13
|
+
* @param eps - The maximum allowed absolute difference. Defaults to {@link EPSILON}.
|
|
14
|
+
* @returns `true` when `|a - b| <= eps`.
|
|
15
|
+
* @since 1.1.0
|
|
16
|
+
*/
|
|
17
|
+
declare function epsEquals(a: number, b: number, eps?: number): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Snaps near-zero values to exactly zero for stable sign tests.
|
|
20
|
+
*
|
|
21
|
+
* @param value - The value to test.
|
|
22
|
+
* @param eps - The threshold below which `value` is considered zero.
|
|
23
|
+
* @returns `0` when `|value| < eps`, otherwise `value` unchanged.
|
|
24
|
+
* @since 1.1.0
|
|
25
|
+
*/
|
|
26
|
+
declare function clampToZero(value: number, eps: number): number;
|
|
27
|
+
/**
|
|
28
|
+
* Rounding function that rounds a number to a specified precision.
|
|
29
|
+
*
|
|
30
|
+
* @param value - The number to round.
|
|
31
|
+
* @param precision - The number of decimal places to round to (default is 8).
|
|
32
|
+
* @returns The rounded number.
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { round } from 'curvy/utils'
|
|
36
|
+
*
|
|
37
|
+
* const roundedValue = round(1.23456789, 4) // 1.2346
|
|
38
|
+
* const roundedValue2 = round(1.23456789) // 1.23456789
|
|
39
|
+
* ```
|
|
40
|
+
* @since 1.0.0
|
|
41
|
+
*/
|
|
42
|
+
declare function round(value: number, precision?: number): number;
|
|
43
|
+
/**
|
|
44
|
+
* Rounding function that rounds a number down to a specified precision.
|
|
45
|
+
*
|
|
46
|
+
* @param value - The number to round down.
|
|
47
|
+
* @param precision - The number of decimal places to round down to (default is 8).
|
|
48
|
+
* @returns The rounded down number.
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { roundDown } from 'curvy/utils'
|
|
52
|
+
*
|
|
53
|
+
* const roundedDownValue = roundDown(1.23456789, 4) // 1.2345
|
|
54
|
+
* const roundedDownValue2 = roundDown(1.23456789) // 1.23456789
|
|
55
|
+
* ```
|
|
56
|
+
* @since 1.0.0
|
|
57
|
+
*/
|
|
58
|
+
declare function roundDown(value: number, precision?: number): number;
|
|
59
|
+
/**
|
|
60
|
+
* Rounding function that rounds a number up to a specified precision.
|
|
61
|
+
*
|
|
62
|
+
* @param value - The number to round up.
|
|
63
|
+
* @param precision - The number of decimal places to round up to (default is 8).
|
|
64
|
+
* @returns The rounded up number.
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* import { roundUp } from 'curvy/utils'
|
|
68
|
+
*
|
|
69
|
+
* const roundedUpValue = roundUp(1.23456789, 4) // 1.2346
|
|
70
|
+
* const roundedUpValue2 = roundUp(1.23456789) // 1.23456789
|
|
71
|
+
* ```
|
|
72
|
+
* @since 1.0.0
|
|
73
|
+
*/
|
|
74
|
+
declare function roundUp(value: number, precision?: number): number;
|
|
75
|
+
declare const lerp: {
|
|
76
|
+
/**
|
|
77
|
+
* Linearly interpolates between two values.
|
|
78
|
+
*
|
|
79
|
+
* @param t - The interpolation factor (between 0 and 1).
|
|
80
|
+
* @param a - The start value.
|
|
81
|
+
* @param b - The end value.
|
|
82
|
+
* @returns The interpolated value.
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* import { lerp } from 'curvy/utils'
|
|
86
|
+
*
|
|
87
|
+
* lerp(0.5, 10, 20) // 15
|
|
88
|
+
* ```
|
|
89
|
+
* @since 1.0.0
|
|
90
|
+
*/
|
|
91
|
+
(t: number, a: number, b: number): number;
|
|
92
|
+
/**
|
|
93
|
+
* Linearly interpolates between two values.
|
|
94
|
+
*
|
|
95
|
+
* @param a - The start value.
|
|
96
|
+
* @param b - The end value.
|
|
97
|
+
* @returns A function that takes the interpolation factor and returns the interpolated value.
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* import { lerp } from 'curvy/utils'
|
|
101
|
+
*
|
|
102
|
+
* lerp(10, 20)(0.5) // 15
|
|
103
|
+
* ```
|
|
104
|
+
* @since 1.0.0
|
|
105
|
+
*/
|
|
106
|
+
(a: number, b: number): (t: number) => number;
|
|
107
|
+
};
|
|
108
|
+
declare const normalize: {
|
|
109
|
+
/**
|
|
110
|
+
* Normalizes a value between two bounds to the unit interval.
|
|
111
|
+
*
|
|
112
|
+
* @param x - The value to normalize.
|
|
113
|
+
* @param a - The lower bound.
|
|
114
|
+
* @param b - The upper bound.
|
|
115
|
+
* @returns The normalized value.
|
|
116
|
+
* @example
|
|
117
|
+
* ```ts
|
|
118
|
+
* import { normalize } from 'curvy/utils'
|
|
119
|
+
*
|
|
120
|
+
* normalize(15, 10, 20) // 0.5
|
|
121
|
+
* ```
|
|
122
|
+
* @since 1.0.0
|
|
123
|
+
*/
|
|
124
|
+
(x: number, a: number, b: number): number;
|
|
125
|
+
/**
|
|
126
|
+
* Normalizes a value between two bounds to the unit interval.
|
|
127
|
+
*
|
|
128
|
+
* @param a - The lower bound.
|
|
129
|
+
* @param b - The upper bound.
|
|
130
|
+
* @returns A function that takes a value and returns the normalized value.
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* import { normalize } from 'curvy/utils'
|
|
134
|
+
*
|
|
135
|
+
* normalize(10, 20)(15) // 0.5
|
|
136
|
+
* ```
|
|
137
|
+
* @since 1.0.0
|
|
138
|
+
*/
|
|
139
|
+
(a: number, b: number): (x: number) => number;
|
|
140
|
+
};
|
|
141
|
+
declare const remap: {
|
|
142
|
+
/**
|
|
143
|
+
* Remaps a value from one range to another.
|
|
144
|
+
*
|
|
145
|
+
* @param x - The value to remap.
|
|
146
|
+
* @param x1 - The lower bound of the original range.
|
|
147
|
+
* @param x2 - The upper bound of the original range.
|
|
148
|
+
* @param y1 - The lower bound of the new range.
|
|
149
|
+
* @param y2 - The upper bound of the new range.
|
|
150
|
+
* @returns The remapped value.
|
|
151
|
+
* @example
|
|
152
|
+
* ```ts
|
|
153
|
+
* import { remap } from 'curvy/utils'
|
|
154
|
+
*
|
|
155
|
+
* remap(15, 10, 20, 0, 100) // 50
|
|
156
|
+
* ```
|
|
157
|
+
* @since 1.0.0
|
|
158
|
+
*/
|
|
159
|
+
(x: number, x1: number, x2: number, y1: number, y2: number): number;
|
|
160
|
+
/**
|
|
161
|
+
* Remaps a value from one range to another.
|
|
162
|
+
*
|
|
163
|
+
* @param x1 - The lower bound of the original range.
|
|
164
|
+
* @param x2 - The upper bound of the original range.
|
|
165
|
+
* @param y1 - The lower bound of the new range.
|
|
166
|
+
* @param y2 - The upper bound of the new range.
|
|
167
|
+
* @returns A function that takes a value and returns the remapped value.
|
|
168
|
+
* @example
|
|
169
|
+
* ```ts
|
|
170
|
+
* import { remap } from 'curvy/utils'
|
|
171
|
+
*
|
|
172
|
+
* remap(10, 20, 0, 100)(15) // 50
|
|
173
|
+
* ```
|
|
174
|
+
* @since 1.0.0
|
|
175
|
+
*/
|
|
176
|
+
(x1: number, x2: number, y1: number, y2: number): (x: number) => number;
|
|
177
|
+
};
|
|
178
|
+
declare const clamp: {
|
|
179
|
+
/**
|
|
180
|
+
* Clamps a value between two bounds.
|
|
181
|
+
*
|
|
182
|
+
* @param value - The value to clamp.
|
|
183
|
+
* @param min - The lower bound.
|
|
184
|
+
* @param max - The upper bound.
|
|
185
|
+
* @returns The clamped value.
|
|
186
|
+
* @example
|
|
187
|
+
* ```ts
|
|
188
|
+
* import { clamp } from 'curvy/utils'
|
|
189
|
+
*
|
|
190
|
+
* clamp(25, 10, 20) // 20
|
|
191
|
+
* ```
|
|
192
|
+
* @since 1.0.0
|
|
193
|
+
*/
|
|
194
|
+
(value: number, min: number, max: number): number;
|
|
195
|
+
/**
|
|
196
|
+
* Clamps a value between two bounds.
|
|
197
|
+
*
|
|
198
|
+
* @param min - The lower bound.
|
|
199
|
+
* @param max - The upper bound.
|
|
200
|
+
* @returns A function that takes a value and returns the clamped value.
|
|
201
|
+
* @example
|
|
202
|
+
* ```ts
|
|
203
|
+
* import { clamp } from 'curvy/utils'
|
|
204
|
+
*
|
|
205
|
+
* clamp(10, 20)(25) // 20
|
|
206
|
+
* ```
|
|
207
|
+
* @since 1.0.0
|
|
208
|
+
*/
|
|
209
|
+
(min: number, max: number): (value: number) => number;
|
|
210
|
+
};
|
|
211
|
+
declare const clip: {
|
|
212
|
+
/**
|
|
213
|
+
* Clips a value to a specified range, returning a fallback value if the value is outside the range.
|
|
214
|
+
*
|
|
215
|
+
* @param value - The value to clip.
|
|
216
|
+
* @param min - The lower bound.
|
|
217
|
+
* @param max - The upper bound.
|
|
218
|
+
* @param onClip - The fallback value to return if the value is outside the range.
|
|
219
|
+
* @returns The original value if within range, otherwise the fallback.
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts
|
|
222
|
+
* import { clip } from 'curvy/utils'
|
|
223
|
+
*
|
|
224
|
+
* clip(25, 10, 20, 15) // 15
|
|
225
|
+
* ```
|
|
226
|
+
* @since 1.0.0
|
|
227
|
+
*/
|
|
228
|
+
<T>(value: number, min: number, max: number, onClip: T): T | number;
|
|
229
|
+
/**
|
|
230
|
+
* Clips a value to a specified range, returning a fallback value if the value is outside the range.
|
|
231
|
+
*
|
|
232
|
+
* @param min - The lower bound.
|
|
233
|
+
* @param max - The upper bound.
|
|
234
|
+
* @param onClip - The fallback value to return if the value is outside the range.
|
|
235
|
+
* @returns A function that takes a value and returns it if within range, otherwise the fallback.
|
|
236
|
+
* @example
|
|
237
|
+
* ```ts
|
|
238
|
+
* import { clip } from 'curvy/utils'
|
|
239
|
+
*
|
|
240
|
+
* clip(10, 20, 15)(25) // 15
|
|
241
|
+
* ```
|
|
242
|
+
* @since 1.0.0
|
|
243
|
+
*/
|
|
244
|
+
<T>(min: number, max: number, onClip: T): (value: number) => T | number;
|
|
245
|
+
};
|
|
246
|
+
/**
|
|
247
|
+
* Returns the minimum and maximum of two numbers.
|
|
248
|
+
*
|
|
249
|
+
* @param a - The first number.
|
|
250
|
+
* @param b - The second number.
|
|
251
|
+
* @returns A tuple containing the minimum and maximum values.
|
|
252
|
+
* @example
|
|
253
|
+
* ```ts
|
|
254
|
+
* import { minMax } from 'curvy/utils'
|
|
255
|
+
*
|
|
256
|
+
* const [minValue, maxValue] = minMax(10, 20) // [10, 20]
|
|
257
|
+
* const [minValue2, maxValue2] = minMax(20, 10) // [10, 20]
|
|
258
|
+
* ```
|
|
259
|
+
* @since 1.0.0
|
|
260
|
+
*/
|
|
261
|
+
declare function minMax(a: number, b: number): [number, number];
|
|
262
|
+
/**
|
|
263
|
+
* Calculates the modulus of a number.
|
|
264
|
+
*
|
|
265
|
+
* @param n - The number to calculate the modulus of.
|
|
266
|
+
* @param m - The modulus.
|
|
267
|
+
* @returns The modulus of the number.
|
|
268
|
+
* @example
|
|
269
|
+
* ```ts
|
|
270
|
+
* import { mod } from 'curvy/utils'
|
|
271
|
+
*
|
|
272
|
+
* const modulus = mod(10, 3) // 1
|
|
273
|
+
* const modulus2 = mod(-10, 3) // 2
|
|
274
|
+
* ```
|
|
275
|
+
* @since 1.0.0
|
|
276
|
+
*/
|
|
277
|
+
declare function mod(n: number, m: number): number;
|
|
278
|
+
//#endregion
|
|
279
|
+
export { EPSILON, clamp, clampToZero, clip, epsEquals, lerp, minMax, mod, normalize, remap, round, roundDown, roundUp };
|
|
280
|
+
//# sourceMappingURL=number.d.mts.map
|
package/dist/number.mjs
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { dual } from "./utils.mjs";
|
|
2
|
+
//#region src/number.ts
|
|
3
|
+
const PRECISION = 8;
|
|
4
|
+
/**
|
|
5
|
+
* Default tolerance for approximate equality between floating-point numbers.
|
|
6
|
+
*
|
|
7
|
+
* @since 1.1.0
|
|
8
|
+
*/
|
|
9
|
+
const EPSILON = 1e-10;
|
|
10
|
+
/**
|
|
11
|
+
* Approximate equality between two numbers, within an absolute tolerance.
|
|
12
|
+
*
|
|
13
|
+
* @param a - The first value.
|
|
14
|
+
* @param b - The second value.
|
|
15
|
+
* @param eps - The maximum allowed absolute difference. Defaults to {@link EPSILON}.
|
|
16
|
+
* @returns `true` when `|a - b| <= eps`.
|
|
17
|
+
* @since 1.1.0
|
|
18
|
+
*/
|
|
19
|
+
function epsEquals(a, b, eps = EPSILON) {
|
|
20
|
+
return Math.abs(a - b) <= eps;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Snaps near-zero values to exactly zero for stable sign tests.
|
|
24
|
+
*
|
|
25
|
+
* @param value - The value to test.
|
|
26
|
+
* @param eps - The threshold below which `value` is considered zero.
|
|
27
|
+
* @returns `0` when `|value| < eps`, otherwise `value` unchanged.
|
|
28
|
+
* @since 1.1.0
|
|
29
|
+
*/
|
|
30
|
+
function clampToZero(value, eps) {
|
|
31
|
+
return Math.abs(value) < eps ? 0 : value;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Rounding function that rounds a number to a specified precision.
|
|
35
|
+
*
|
|
36
|
+
* @param value - The number to round.
|
|
37
|
+
* @param precision - The number of decimal places to round to (default is 8).
|
|
38
|
+
* @returns The rounded number.
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* import { round } from 'curvy/utils'
|
|
42
|
+
*
|
|
43
|
+
* const roundedValue = round(1.23456789, 4) // 1.2346
|
|
44
|
+
* const roundedValue2 = round(1.23456789) // 1.23456789
|
|
45
|
+
* ```
|
|
46
|
+
* @since 1.0.0
|
|
47
|
+
*/
|
|
48
|
+
function round(value, precision = PRECISION) {
|
|
49
|
+
const scale = 10 ** precision;
|
|
50
|
+
const result = Math.round(value * scale) / scale;
|
|
51
|
+
if (result === 0) return 0;
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Rounding function that rounds a number down to a specified precision.
|
|
56
|
+
*
|
|
57
|
+
* @param value - The number to round down.
|
|
58
|
+
* @param precision - The number of decimal places to round down to (default is 8).
|
|
59
|
+
* @returns The rounded down number.
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* import { roundDown } from 'curvy/utils'
|
|
63
|
+
*
|
|
64
|
+
* const roundedDownValue = roundDown(1.23456789, 4) // 1.2345
|
|
65
|
+
* const roundedDownValue2 = roundDown(1.23456789) // 1.23456789
|
|
66
|
+
* ```
|
|
67
|
+
* @since 1.0.0
|
|
68
|
+
*/
|
|
69
|
+
function roundDown(value, precision = PRECISION) {
|
|
70
|
+
const scale = 10 ** precision;
|
|
71
|
+
const result = Math.floor(value * scale) / scale;
|
|
72
|
+
if (result === 0) return 0;
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Rounding function that rounds a number up to a specified precision.
|
|
77
|
+
*
|
|
78
|
+
* @param value - The number to round up.
|
|
79
|
+
* @param precision - The number of decimal places to round up to (default is 8).
|
|
80
|
+
* @returns The rounded up number.
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* import { roundUp } from 'curvy/utils'
|
|
84
|
+
*
|
|
85
|
+
* const roundedUpValue = roundUp(1.23456789, 4) // 1.2346
|
|
86
|
+
* const roundedUpValue2 = roundUp(1.23456789) // 1.23456789
|
|
87
|
+
* ```
|
|
88
|
+
* @since 1.0.0
|
|
89
|
+
*/
|
|
90
|
+
function roundUp(value, precision = PRECISION) {
|
|
91
|
+
const scale = 10 ** precision;
|
|
92
|
+
const result = Math.ceil(value * scale) / scale;
|
|
93
|
+
if (result === 0) return 0;
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
const lerp = dual(3, (t, a, b) => b * t + a * (1 - t));
|
|
97
|
+
const normalize = dual(3, (x, a, b) => (x - a) / (b - a));
|
|
98
|
+
const remap = dual(5, (x, x1, x2, y1, y2) => y1 + (x - x1) / (x2 - x1) * (y2 - y1));
|
|
99
|
+
const clamp = dual(3, (value, min, max) => value < min ? min : value > max ? max : value);
|
|
100
|
+
const clip = dual(4, (value, min, max, onClip) => value < min ? onClip : value > max ? onClip : value);
|
|
101
|
+
/**
|
|
102
|
+
* Returns the minimum and maximum of two numbers.
|
|
103
|
+
*
|
|
104
|
+
* @param a - The first number.
|
|
105
|
+
* @param b - The second number.
|
|
106
|
+
* @returns A tuple containing the minimum and maximum values.
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* import { minMax } from 'curvy/utils'
|
|
110
|
+
*
|
|
111
|
+
* const [minValue, maxValue] = minMax(10, 20) // [10, 20]
|
|
112
|
+
* const [minValue2, maxValue2] = minMax(20, 10) // [10, 20]
|
|
113
|
+
* ```
|
|
114
|
+
* @since 1.0.0
|
|
115
|
+
*/
|
|
116
|
+
function minMax(a, b) {
|
|
117
|
+
return a < b ? [a, b] : [b, a];
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Calculates the modulus of a number.
|
|
121
|
+
*
|
|
122
|
+
* @param n - The number to calculate the modulus of.
|
|
123
|
+
* @param m - The modulus.
|
|
124
|
+
* @returns The modulus of the number.
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* import { mod } from 'curvy/utils'
|
|
128
|
+
*
|
|
129
|
+
* const modulus = mod(10, 3) // 1
|
|
130
|
+
* const modulus2 = mod(-10, 3) // 2
|
|
131
|
+
* ```
|
|
132
|
+
* @since 1.0.0
|
|
133
|
+
*/
|
|
134
|
+
function mod(n, m) {
|
|
135
|
+
return (n % m + m) % m;
|
|
136
|
+
}
|
|
137
|
+
//#endregion
|
|
138
|
+
export { EPSILON, clamp, clampToZero, clip, epsEquals, lerp, minMax, mod, normalize, remap, round, roundDown, roundUp };
|
|
139
|
+
|
|
140
|
+
//# sourceMappingURL=number.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"number.mjs","names":[],"sources":["../src/number.ts"],"sourcesContent":["import { dual } from './utils'\n\nconst PRECISION = 8\n\n/**\n * Default tolerance for approximate equality between floating-point numbers.\n *\n * @since 1.1.0\n */\nexport const EPSILON = 1e-10\n\n/**\n * Approximate equality between two numbers, within an absolute tolerance.\n *\n * @param a - The first value.\n * @param b - The second value.\n * @param eps - The maximum allowed absolute difference. Defaults to {@link EPSILON}.\n * @returns `true` when `|a - b| <= eps`.\n * @since 1.1.0\n */\nexport function epsEquals(a: number, b: number, eps: number = EPSILON): boolean {\n return Math.abs(a - b) <= eps\n}\n\n/**\n * Snaps near-zero values to exactly zero for stable sign tests.\n *\n * @param value - The value to test.\n * @param eps - The threshold below which `value` is considered zero.\n * @returns `0` when `|value| < eps`, otherwise `value` unchanged.\n * @since 1.1.0\n */\nexport function clampToZero(value: number, eps: number): number {\n return Math.abs(value) < eps ? 0 : value\n}\n\n/**\n * Rounding function that rounds a number to a specified precision.\n *\n * @param value - The number to round.\n * @param precision - The number of decimal places to round to (default is 8).\n * @returns The rounded number.\n * @example\n * ```ts\n * import { round } from 'curvy/utils'\n *\n * const roundedValue = round(1.23456789, 4) // 1.2346\n * const roundedValue2 = round(1.23456789) // 1.23456789\n * ```\n * @since 1.0.0\n */\nexport function round(value: number, precision = PRECISION): number {\n const scale = 10 ** precision\n const result = Math.round(value * scale) / scale\n if (result === 0) {\n return 0\n }\n return result\n}\n\n/**\n * Rounding function that rounds a number down to a specified precision.\n *\n * @param value - The number to round down.\n * @param precision - The number of decimal places to round down to (default is 8).\n * @returns The rounded down number.\n * @example\n * ```ts\n * import { roundDown } from 'curvy/utils'\n *\n * const roundedDownValue = roundDown(1.23456789, 4) // 1.2345\n * const roundedDownValue2 = roundDown(1.23456789) // 1.23456789\n * ```\n * @since 1.0.0\n */\nexport function roundDown(value: number, precision = PRECISION): number {\n const scale = 10 ** precision\n const result = Math.floor(value * scale) / scale\n if (result === 0) {\n return 0\n }\n return result\n}\n\n/**\n * Rounding function that rounds a number up to a specified precision.\n *\n * @param value - The number to round up.\n * @param precision - The number of decimal places to round up to (default is 8).\n * @returns The rounded up number.\n * @example\n * ```ts\n * import { roundUp } from 'curvy/utils'\n *\n * const roundedUpValue = roundUp(1.23456789, 4) // 1.2346\n * const roundedUpValue2 = roundUp(1.23456789) // 1.23456789\n * ```\n * @since 1.0.0\n */\nexport function roundUp(value: number, precision = PRECISION): number {\n const scale = 10 ** precision\n const result = Math.ceil(value * scale) / scale\n if (result === 0) {\n return 0\n }\n return result\n}\n\nexport const lerp: {\n /**\n * Linearly interpolates between two values.\n *\n * @param t - The interpolation factor (between 0 and 1).\n * @param a - The start value.\n * @param b - The end value.\n * @returns The interpolated value.\n * @example\n * ```ts\n * import { lerp } from 'curvy/utils'\n *\n * lerp(0.5, 10, 20) // 15\n * ```\n * @since 1.0.0\n */\n (t: number, a: number, b: number): number\n /**\n * Linearly interpolates between two values.\n *\n * @param a - The start value.\n * @param b - The end value.\n * @returns A function that takes the interpolation factor and returns the interpolated value.\n * @example\n * ```ts\n * import { lerp } from 'curvy/utils'\n *\n * lerp(10, 20)(0.5) // 15\n * ```\n * @since 1.0.0\n */\n (a: number, b: number): (t: number) => number\n} = dual(3, (t: number, a: number, b: number) => b * t + a * (1 - t))\n\nexport const normalize: {\n /**\n * Normalizes a value between two bounds to the unit interval.\n *\n * @param x - The value to normalize.\n * @param a - The lower bound.\n * @param b - The upper bound.\n * @returns The normalized value.\n * @example\n * ```ts\n * import { normalize } from 'curvy/utils'\n *\n * normalize(15, 10, 20) // 0.5\n * ```\n * @since 1.0.0\n */\n (x: number, a: number, b: number): number\n /**\n * Normalizes a value between two bounds to the unit interval.\n *\n * @param a - The lower bound.\n * @param b - The upper bound.\n * @returns A function that takes a value and returns the normalized value.\n * @example\n * ```ts\n * import { normalize } from 'curvy/utils'\n *\n * normalize(10, 20)(15) // 0.5\n * ```\n * @since 1.0.0\n */\n (a: number, b: number): (x: number) => number\n} = dual(3, (x: number, a: number, b: number) => (x - a) / (b - a))\n\nexport const remap: {\n /**\n * Remaps a value from one range to another.\n *\n * @param x - The value to remap.\n * @param x1 - The lower bound of the original range.\n * @param x2 - The upper bound of the original range.\n * @param y1 - The lower bound of the new range.\n * @param y2 - The upper bound of the new range.\n * @returns The remapped value.\n * @example\n * ```ts\n * import { remap } from 'curvy/utils'\n *\n * remap(15, 10, 20, 0, 100) // 50\n * ```\n * @since 1.0.0\n */\n (x: number, x1: number, x2: number, y1: number, y2: number): number\n /**\n * Remaps a value from one range to another.\n *\n * @param x1 - The lower bound of the original range.\n * @param x2 - The upper bound of the original range.\n * @param y1 - The lower bound of the new range.\n * @param y2 - The upper bound of the new range.\n * @returns A function that takes a value and returns the remapped value.\n * @example\n * ```ts\n * import { remap } from 'curvy/utils'\n *\n * remap(10, 20, 0, 100)(15) // 50\n * ```\n * @since 1.0.0\n */\n (x1: number, x2: number, y1: number, y2: number): (x: number) => number\n} = dual(\n 5,\n (x: number, x1: number, x2: number, y1: number, y2: number) =>\n y1 + ((x - x1) / (x2 - x1)) * (y2 - y1),\n)\n\nexport const clamp: {\n /**\n * Clamps a value between two bounds.\n *\n * @param value - The value to clamp.\n * @param min - The lower bound.\n * @param max - The upper bound.\n * @returns The clamped value.\n * @example\n * ```ts\n * import { clamp } from 'curvy/utils'\n *\n * clamp(25, 10, 20) // 20\n * ```\n * @since 1.0.0\n */\n (value: number, min: number, max: number): number\n /**\n * Clamps a value between two bounds.\n *\n * @param min - The lower bound.\n * @param max - The upper bound.\n * @returns A function that takes a value and returns the clamped value.\n * @example\n * ```ts\n * import { clamp } from 'curvy/utils'\n *\n * clamp(10, 20)(25) // 20\n * ```\n * @since 1.0.0\n */\n (min: number, max: number): (value: number) => number\n} = dual(3, (value: number, min: number, max: number) =>\n value < min ? min : value > max ? max : value,\n)\n\nexport const clip: {\n /**\n * Clips a value to a specified range, returning a fallback value if the value is outside the range.\n *\n * @param value - The value to clip.\n * @param min - The lower bound.\n * @param max - The upper bound.\n * @param onClip - The fallback value to return if the value is outside the range.\n * @returns The original value if within range, otherwise the fallback.\n * @example\n * ```ts\n * import { clip } from 'curvy/utils'\n *\n * clip(25, 10, 20, 15) // 15\n * ```\n * @since 1.0.0\n */\n <T>(value: number, min: number, max: number, onClip: T): T | number\n /**\n * Clips a value to a specified range, returning a fallback value if the value is outside the range.\n *\n * @param min - The lower bound.\n * @param max - The upper bound.\n * @param onClip - The fallback value to return if the value is outside the range.\n * @returns A function that takes a value and returns it if within range, otherwise the fallback.\n * @example\n * ```ts\n * import { clip } from 'curvy/utils'\n *\n * clip(10, 20, 15)(25) // 15\n * ```\n * @since 1.0.0\n */\n <T>(min: number, max: number, onClip: T): (value: number) => T | number\n} = dual(4, (value: number, min: number, max: number, onClip: unknown) =>\n value < min ? onClip : value > max ? onClip : value,\n)\n\n/**\n * Returns the minimum and maximum of two numbers.\n *\n * @param a - The first number.\n * @param b - The second number.\n * @returns A tuple containing the minimum and maximum values.\n * @example\n * ```ts\n * import { minMax } from 'curvy/utils'\n *\n * const [minValue, maxValue] = minMax(10, 20) // [10, 20]\n * const [minValue2, maxValue2] = minMax(20, 10) // [10, 20]\n * ```\n * @since 1.0.0\n */\nexport function minMax(a: number, b: number): [number, number] {\n return a < b ? [a, b] : [b, a]\n}\n\n/**\n * Calculates the modulus of a number.\n *\n * @param n - The number to calculate the modulus of.\n * @param m - The modulus.\n * @returns The modulus of the number.\n * @example\n * ```ts\n * import { mod } from 'curvy/utils'\n *\n * const modulus = mod(10, 3) // 1\n * const modulus2 = mod(-10, 3) // 2\n * ```\n * @since 1.0.0\n */\nexport function mod(n: number, m: number): number {\n return ((n % m) + m) % m\n}\n"],"mappings":";;AAEA,MAAM,YAAY;;;;;;AAOlB,MAAa,UAAU;;;;;;;;;;AAWvB,SAAgB,UAAU,GAAW,GAAW,MAAc,SAAkB;CAC9E,OAAO,KAAK,IAAI,IAAI,EAAE,IAAI;;;;;;;;;;AAW5B,SAAgB,YAAY,OAAe,KAAqB;CAC9D,OAAO,KAAK,IAAI,MAAM,GAAG,MAAM,IAAI;;;;;;;;;;;;;;;;;AAkBrC,SAAgB,MAAM,OAAe,YAAY,WAAmB;CAClE,MAAM,QAAQ,MAAM;CACpB,MAAM,SAAS,KAAK,MAAM,QAAQ,MAAM,GAAG;CAC3C,IAAI,WAAW,GACb,OAAO;CAET,OAAO;;;;;;;;;;;;;;;;;AAkBT,SAAgB,UAAU,OAAe,YAAY,WAAmB;CACtE,MAAM,QAAQ,MAAM;CACpB,MAAM,SAAS,KAAK,MAAM,QAAQ,MAAM,GAAG;CAC3C,IAAI,WAAW,GACb,OAAO;CAET,OAAO;;;;;;;;;;;;;;;;;AAkBT,SAAgB,QAAQ,OAAe,YAAY,WAAmB;CACpE,MAAM,QAAQ,MAAM;CACpB,MAAM,SAAS,KAAK,KAAK,QAAQ,MAAM,GAAG;CAC1C,IAAI,WAAW,GACb,OAAO;CAET,OAAO;;AAGT,MAAa,OAgCT,KAAK,IAAI,GAAW,GAAW,MAAc,IAAI,IAAI,KAAK,IAAI,GAAG;AAErE,MAAa,YAgCT,KAAK,IAAI,GAAW,GAAW,OAAe,IAAI,MAAM,IAAI,GAAG;AAEnE,MAAa,QAoCT,KACF,IACC,GAAW,IAAY,IAAY,IAAY,OAC9C,MAAO,IAAI,OAAO,KAAK,OAAQ,KAAK,IACvC;AAED,MAAa,QAgCT,KAAK,IAAI,OAAe,KAAa,QACvC,QAAQ,MAAM,MAAM,QAAQ,MAAM,MAAM,MACzC;AAED,MAAa,OAkCT,KAAK,IAAI,OAAe,KAAa,KAAa,WACpD,QAAQ,MAAM,SAAS,QAAQ,MAAM,SAAS,MAC/C;;;;;;;;;;;;;;;;AAiBD,SAAgB,OAAO,GAAW,GAA6B;CAC7D,OAAO,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE;;;;;;;;;;;;;;;;;AAkBhC,SAAgB,IAAI,GAAW,GAAmB;CAChD,QAAS,IAAI,IAAK,KAAK"}
|