curvy 1.0.7 → 1.0.9

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.
Files changed (109) hide show
  1. package/dist/curve/cubic2d.d.mts +80 -2
  2. package/dist/curve/cubic2d.d.ts +80 -2
  3. package/dist/curve/cubic2d.mjs +9 -26
  4. package/dist/curve/cubic2d.mjs.map +1 -0
  5. package/dist/curve/linear2d.d.mts +68 -2
  6. package/dist/curve/linear2d.d.ts +68 -2
  7. package/dist/curve/linear2d.mjs +11 -14
  8. package/dist/curve/linear2d.mjs.map +1 -0
  9. package/dist/curve/quadratic2d.d.mts +86 -2
  10. package/dist/curve/quadratic2d.d.ts +86 -2
  11. package/dist/curve/quadratic2d.mjs +10 -15
  12. package/dist/curve/quadratic2d.mjs.map +1 -0
  13. package/dist/interval.d.mts +195 -2
  14. package/dist/interval.d.ts +195 -2
  15. package/dist/interval.mjs +9 -12
  16. package/dist/interval.mjs.map +1 -0
  17. package/dist/matrix/matrix2x2.d.mts +273 -2
  18. package/dist/matrix/matrix2x2.d.ts +273 -2
  19. package/dist/matrix/matrix2x2.mjs +5 -4
  20. package/dist/matrix/matrix2x2.mjs.map +1 -0
  21. package/dist/matrix/matrix3x3.d.mts +308 -1
  22. package/dist/matrix/matrix3x3.d.ts +308 -1
  23. package/dist/matrix/matrix3x3.mjs +5 -4
  24. package/dist/matrix/matrix3x3.mjs.map +1 -0
  25. package/dist/matrix/matrix4x4.d.mts +310 -1
  26. package/dist/matrix/matrix4x4.d.ts +310 -1
  27. package/dist/matrix/matrix4x4.mjs +24 -61
  28. package/dist/matrix/matrix4x4.mjs.map +1 -0
  29. package/dist/path/cubic2d.d.mts +68 -2
  30. package/dist/path/cubic2d.d.ts +68 -2
  31. package/dist/path/cubic2d.mjs +10 -12
  32. package/dist/path/cubic2d.mjs.map +1 -0
  33. package/dist/path/linear2d.d.mts +68 -2
  34. package/dist/path/linear2d.d.ts +68 -2
  35. package/dist/path/linear2d.mjs +7 -9
  36. package/dist/path/linear2d.mjs.map +1 -0
  37. package/dist/path/quadratic2d.d.mts +67 -2
  38. package/dist/path/quadratic2d.d.ts +67 -2
  39. package/dist/path/quadratic2d.mjs +9 -13
  40. package/dist/path/quadratic2d.mjs.map +1 -0
  41. package/dist/pipe.mjs +1 -0
  42. package/dist/pipe.mjs.map +1 -0
  43. package/dist/polynomial/cubic.d.mts +1 -1
  44. package/dist/polynomial/cubic.d.ts +1 -1
  45. package/dist/polynomial/cubic.mjs +13 -28
  46. package/dist/polynomial/cubic.mjs.map +1 -0
  47. package/dist/polynomial/linear.d.mts +1 -1
  48. package/dist/polynomial/linear.d.ts +1 -1
  49. package/dist/polynomial/linear.mjs +2 -1
  50. package/dist/polynomial/linear.mjs.map +1 -0
  51. package/dist/polynomial/quadratic.d.mts +2 -2
  52. package/dist/polynomial/quadratic.d.ts +2 -2
  53. package/dist/polynomial/quadratic.mjs +5 -2
  54. package/dist/polynomial/quadratic.mjs.map +1 -0
  55. package/dist/shared/{curvy.Cps2R_RU.mjs → curvy.BkkW8Tx9.mjs} +11 -29
  56. package/dist/shared/curvy.BkkW8Tx9.mjs.map +1 -0
  57. package/dist/shared/{curvy.BzVmUIKm.mjs → curvy.BxdGW9he.mjs} +17 -32
  58. package/dist/shared/curvy.BxdGW9he.mjs.map +1 -0
  59. package/dist/shared/{curvy.C2XGH0R0.mjs → curvy.C_sCeg9J.mjs} +15 -24
  60. package/dist/shared/curvy.C_sCeg9J.mjs.map +1 -0
  61. package/dist/shared/curvy.Cg8lZVUz.d.ts +688 -0
  62. package/dist/shared/{curvy.CE4X6_Is.mjs → curvy.DCt1gGII.mjs} +11 -45
  63. package/dist/shared/curvy.DCt1gGII.mjs.map +1 -0
  64. package/dist/shared/{curvy.BZ_mTkJe.mjs → curvy.DJ4xO0al.mjs} +9 -6
  65. package/dist/shared/curvy.DJ4xO0al.mjs.map +1 -0
  66. package/dist/shared/curvy.DmVFTi0N.mjs +1 -0
  67. package/dist/shared/curvy.DmVFTi0N.mjs.map +1 -0
  68. package/dist/shared/curvy.FDaj5rlj.d.mts +688 -0
  69. package/dist/shared/{curvy.NLeFCr-9.mjs → curvy.lktriTpx.mjs} +19 -42
  70. package/dist/shared/curvy.lktriTpx.mjs.map +1 -0
  71. package/dist/shared/{curvy.Ck0of1PI.mjs → curvy.mLFprrGm.mjs} +26 -22
  72. package/dist/shared/curvy.mLFprrGm.mjs.map +1 -0
  73. package/dist/shared/{curvy.BDhiZ7He.mjs → curvy.r6f1j287.mjs} +8 -12
  74. package/dist/shared/curvy.r6f1j287.mjs.map +1 -0
  75. package/dist/splines/basis2d.d.mts +94 -2
  76. package/dist/splines/basis2d.d.ts +94 -2
  77. package/dist/splines/basis2d.mjs +13 -12
  78. package/dist/splines/basis2d.mjs.map +1 -0
  79. package/dist/splines/bezier2d.d.mts +136 -2
  80. package/dist/splines/bezier2d.d.ts +136 -2
  81. package/dist/splines/bezier2d.mjs +10 -9
  82. package/dist/splines/bezier2d.mjs.map +1 -0
  83. package/dist/splines/cardinal2d.d.mts +131 -2
  84. package/dist/splines/cardinal2d.d.ts +131 -2
  85. package/dist/splines/cardinal2d.mjs +15 -24
  86. package/dist/splines/cardinal2d.mjs.map +1 -0
  87. package/dist/splines/hermite2d.d.mts +94 -3
  88. package/dist/splines/hermite2d.d.ts +94 -3
  89. package/dist/splines/hermite2d.mjs +22 -36
  90. package/dist/splines/hermite2d.mjs.map +1 -0
  91. package/dist/utils.d.mts +179 -1
  92. package/dist/utils.d.ts +179 -1
  93. package/dist/utils.mjs +16 -7
  94. package/dist/utils.mjs.map +1 -0
  95. package/dist/vector/vector2.d.mts +259 -1
  96. package/dist/vector/vector2.d.ts +259 -1
  97. package/dist/vector/vector2.mjs +4 -2
  98. package/dist/vector/vector2.mjs.map +1 -0
  99. package/dist/vector/vector3.d.mts +345 -1
  100. package/dist/vector/vector3.d.ts +345 -1
  101. package/dist/vector/vector3.mjs +9 -2
  102. package/dist/vector/vector3.mjs.map +1 -0
  103. package/dist/vector/vector4.d.mts +163 -1
  104. package/dist/vector/vector4.d.ts +163 -1
  105. package/dist/vector/vector4.mjs +4 -2
  106. package/dist/vector/vector4.mjs.map +1 -0
  107. package/package.json +4 -4
  108. package/dist/shared/curvy.BsHoYJNv.d.ts +0 -155
  109. package/dist/shared/curvy.NsmY7zcr.d.mts +0 -155
package/dist/utils.d.mts CHANGED
@@ -1,27 +1,205 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
1
4
  declare const PRECISION = 8;
5
+ /**
6
+ * Rounding function that rounds a number to a specified precision.
7
+ *
8
+ * @param value - The number to round.
9
+ * @param precision - The number of decimal places to round to (default is 8).
10
+ * @returns The rounded number.
11
+ * @example
12
+ * ```ts
13
+ * import { round } from 'curvy/utils'
14
+ *
15
+ * const roundedValue = round(1.23456789, 4) // 1.2346
16
+ * const roundedValue2 = round(1.23456789) // 1.23456789
17
+ * ```
18
+ * @since 1.0.0
19
+ */
2
20
  declare function round(value: number, precision?: number): number;
21
+ /**
22
+ * Rounding function that rounds a number down to a specified precision.
23
+ *
24
+ * @param value - The number to round down.
25
+ * @param precision - The number of decimal places to round down to (default is 8).
26
+ * @returns The rounded down number.
27
+ * @example
28
+ * ```ts
29
+ * import { roundDown } from 'curvy/utils'
30
+ *
31
+ * const roundedDownValue = roundDown(1.23456789, 4) // 1.2345
32
+ * const roundedDownValue2 = roundDown(1.23456789) // 1.23456789
33
+ * ```
34
+ * @since 1.0.0
35
+ */
3
36
  declare function roundDown(value: number, precision?: number): number;
37
+ /**
38
+ * Rounding function that rounds a number up to a specified precision.
39
+ *
40
+ * @param value - The number to round up.
41
+ * @param precision - The number of decimal places to round up to (default is 8).
42
+ * @returns The rounded up number.
43
+ * @example
44
+ * ```ts
45
+ * import { roundUp } from 'curvy/utils'
46
+ *
47
+ * const roundedUpValue = roundUp(1.23456789, 4) // 1.2346
48
+ * const roundedUpValue2 = roundUp(1.23456789) // 1.23456789
49
+ * ```
50
+ * @since 1.0.0
51
+ */
52
+ declare function roundUp(value: number, precision?: number): number;
53
+ /**
54
+ * Linear interpolation function.
55
+ *
56
+ * @param t - The interpolation factor (between 0 and 1).
57
+ * @param a - The start value.
58
+ * @param b - The end value.
59
+ * @returns The interpolated value.
60
+ * @example
61
+ * ```ts
62
+ * import { lerp } from 'curvy/utils'
63
+ *
64
+ * const interpolatedValue = lerp(0.5, 10, 20) // 15
65
+ * const interpolatedValue2 = lerp(0.5)(10, 20) // 15
66
+ * ```
67
+ * @since 1.0.0
68
+ */
4
69
  declare const lerp: {
5
70
  (t: number, a: number, b: number): number;
6
71
  (a: number, b: number): (t: number) => number;
7
72
  };
73
+ /**
74
+ * Normalizes a value between two bounds.
75
+ *
76
+ * @param x - The value to normalize.
77
+ * @param a - The lower bound.
78
+ * @param b - The upper bound.
79
+ * @returns The normalized value.
80
+ * @example
81
+ * ```ts
82
+ * import { normalize } from 'curvy/utils'
83
+ *
84
+ * const normalizedValue = normalize(15, 10, 20) // 0.5
85
+ * const normalizedValue2 = normalize(10, 20)(15) // 0.5
86
+ * ```
87
+ * @since 1.0.0
88
+ */
8
89
  declare const normalize: {
9
90
  (x: number, a: number, b: number): number;
10
91
  (a: number, b: number): (x: number) => number;
11
92
  };
93
+ /**
94
+ * Remaps a value from one range to another.
95
+ *
96
+ * @param x - The value to remap.
97
+ * @param x1 - The lower bound of the original range.
98
+ * @param x2 - The upper bound of the original range.
99
+ * @param y1 - The lower bound of the new range.
100
+ * @param y2 - The upper bound of the new range.
101
+ * @returns The remapped value.
102
+ * @example
103
+ * ```ts
104
+ * import { remap } from 'curvy/utils'
105
+ *
106
+ * const remappedValue = remap(15, 10, 20, 0, 100) // 50
107
+ * const remappedValue2 = remap(10, 20, 0, 100)(15) // 50
108
+ * ```
109
+ * @since 1.0.0
110
+ */
12
111
  declare const remap: {
13
112
  (x: number, x1: number, x2: number, y1: number, y2: number): number;
14
113
  (x1: number, x2: number, y1: number, y2: number): (x: number) => number;
15
114
  };
115
+ /**
116
+ * Clamps a value between two bounds.
117
+ *
118
+ * @param value - The value to clamp.
119
+ * @param min - The lower bound.
120
+ * @param max - The upper bound.
121
+ * @returns The clamped value.
122
+ * @example
123
+ * ```ts
124
+ * import { clamp } from 'curvy/utils'
125
+ *
126
+ * const clampedValue = clamp(25, 10, 20) // 20
127
+ * const clampedValue2 = clamp(10, 20)(25) // 20
128
+ * ```
129
+ * @since 1.0.0
130
+ */
16
131
  declare const clamp: {
17
132
  (value: number, min: number, max: number): number;
18
133
  (min: number, max: number): (value: number) => number;
19
134
  };
135
+ /**
136
+ * Clips a value to a specified range, returning a fallback value if the value is outside the range.
137
+ *
138
+ * @param value - The value to clip.
139
+ * @param min - The lower bound.
140
+ * @param max - The upper bound.
141
+ * @param onClip - The fallback value to return if the value is outside the range.
142
+ * @returns The clipped value or the fallback value.
143
+ * @example
144
+ * ```ts
145
+ * import { clip } from 'curvy/utils'
146
+ *
147
+ * const clippedValue = clip(25, 10, 20, 15) // 15
148
+ * const clippedValue2 = clip(10, 20, 15)(25) // 15
149
+ * ```
150
+ * @since 1.0.0
151
+ */
20
152
  declare const clip: {
21
153
  <T>(value: number, min: number, max: number, onClip: T): T | number;
22
154
  <T>(min: number, max: number, onClip: T): (value: number) => T | number;
23
155
  };
156
+ /**
157
+ * Returns the minimum and maximum of two numbers.
158
+ *
159
+ * @param a - The first number.
160
+ * @param b - The second number.
161
+ * @returns A tuple containing the minimum and maximum values.
162
+ * @example
163
+ * ```ts
164
+ * import { minMax } from 'curvy/utils'
165
+ *
166
+ * const [minValue, maxValue] = minMax(10, 20) // [10, 20]
167
+ * const [minValue2, maxValue2] = minMax(20, 10) // [10, 20]
168
+ * ```
169
+ * @since 1.0.0
170
+ */
24
171
  declare function minMax(a: number, b: number): [number, number];
172
+ /**
173
+ * Calculates the modulus of a number.
174
+ *
175
+ * @param n - The number to calculate the modulus of.
176
+ * @param m - The modulus.
177
+ * @returns The modulus of the number.
178
+ * @example
179
+ * ```ts
180
+ * import { mod } from 'curvy/utils'
181
+ *
182
+ * const modulus = mod(10, 3) // 1
183
+ * const modulus2 = mod(-10, 3) // 2
184
+ * ```
185
+ * @since 1.0.0
186
+ */
187
+ declare function mod(n: number, m: number): number;
188
+ /**
189
+ * Asserts that a condition is true, throwing an error with the provided message if it is not.
190
+ *
191
+ * @param condition - The condition to assert.
192
+ * @param message - The error message to throw if the condition is false.
193
+ * @throws An error with the provided message if the condition is false.
194
+ * @example
195
+ * ```ts
196
+ * import { invariant } from 'curvy/utils'
197
+ *
198
+ * invariant(1 + 1 === 2, 'Math is broken!') // No error
199
+ * invariant(1 + 1 === 3, 'Math is broken!') // Throws: Math is broken!
200
+ * ```
201
+ * @since 1.0.0
202
+ */
25
203
  declare function invariant(condition: boolean, message: string): asserts condition;
26
204
 
27
- export { PRECISION, clamp, clip, invariant, lerp, minMax, normalize, remap, round, roundDown };
205
+ export { PRECISION, clamp, clip, invariant, lerp, minMax, mod, normalize, remap, round, roundDown, roundUp };
package/dist/utils.d.ts CHANGED
@@ -1,27 +1,205 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
1
4
  declare const PRECISION = 8;
5
+ /**
6
+ * Rounding function that rounds a number to a specified precision.
7
+ *
8
+ * @param value - The number to round.
9
+ * @param precision - The number of decimal places to round to (default is 8).
10
+ * @returns The rounded number.
11
+ * @example
12
+ * ```ts
13
+ * import { round } from 'curvy/utils'
14
+ *
15
+ * const roundedValue = round(1.23456789, 4) // 1.2346
16
+ * const roundedValue2 = round(1.23456789) // 1.23456789
17
+ * ```
18
+ * @since 1.0.0
19
+ */
2
20
  declare function round(value: number, precision?: number): number;
21
+ /**
22
+ * Rounding function that rounds a number down to a specified precision.
23
+ *
24
+ * @param value - The number to round down.
25
+ * @param precision - The number of decimal places to round down to (default is 8).
26
+ * @returns The rounded down number.
27
+ * @example
28
+ * ```ts
29
+ * import { roundDown } from 'curvy/utils'
30
+ *
31
+ * const roundedDownValue = roundDown(1.23456789, 4) // 1.2345
32
+ * const roundedDownValue2 = roundDown(1.23456789) // 1.23456789
33
+ * ```
34
+ * @since 1.0.0
35
+ */
3
36
  declare function roundDown(value: number, precision?: number): number;
37
+ /**
38
+ * Rounding function that rounds a number up to a specified precision.
39
+ *
40
+ * @param value - The number to round up.
41
+ * @param precision - The number of decimal places to round up to (default is 8).
42
+ * @returns The rounded up number.
43
+ * @example
44
+ * ```ts
45
+ * import { roundUp } from 'curvy/utils'
46
+ *
47
+ * const roundedUpValue = roundUp(1.23456789, 4) // 1.2346
48
+ * const roundedUpValue2 = roundUp(1.23456789) // 1.23456789
49
+ * ```
50
+ * @since 1.0.0
51
+ */
52
+ declare function roundUp(value: number, precision?: number): number;
53
+ /**
54
+ * Linear interpolation function.
55
+ *
56
+ * @param t - The interpolation factor (between 0 and 1).
57
+ * @param a - The start value.
58
+ * @param b - The end value.
59
+ * @returns The interpolated value.
60
+ * @example
61
+ * ```ts
62
+ * import { lerp } from 'curvy/utils'
63
+ *
64
+ * const interpolatedValue = lerp(0.5, 10, 20) // 15
65
+ * const interpolatedValue2 = lerp(0.5)(10, 20) // 15
66
+ * ```
67
+ * @since 1.0.0
68
+ */
4
69
  declare const lerp: {
5
70
  (t: number, a: number, b: number): number;
6
71
  (a: number, b: number): (t: number) => number;
7
72
  };
73
+ /**
74
+ * Normalizes a value between two bounds.
75
+ *
76
+ * @param x - The value to normalize.
77
+ * @param a - The lower bound.
78
+ * @param b - The upper bound.
79
+ * @returns The normalized value.
80
+ * @example
81
+ * ```ts
82
+ * import { normalize } from 'curvy/utils'
83
+ *
84
+ * const normalizedValue = normalize(15, 10, 20) // 0.5
85
+ * const normalizedValue2 = normalize(10, 20)(15) // 0.5
86
+ * ```
87
+ * @since 1.0.0
88
+ */
8
89
  declare const normalize: {
9
90
  (x: number, a: number, b: number): number;
10
91
  (a: number, b: number): (x: number) => number;
11
92
  };
93
+ /**
94
+ * Remaps a value from one range to another.
95
+ *
96
+ * @param x - The value to remap.
97
+ * @param x1 - The lower bound of the original range.
98
+ * @param x2 - The upper bound of the original range.
99
+ * @param y1 - The lower bound of the new range.
100
+ * @param y2 - The upper bound of the new range.
101
+ * @returns The remapped value.
102
+ * @example
103
+ * ```ts
104
+ * import { remap } from 'curvy/utils'
105
+ *
106
+ * const remappedValue = remap(15, 10, 20, 0, 100) // 50
107
+ * const remappedValue2 = remap(10, 20, 0, 100)(15) // 50
108
+ * ```
109
+ * @since 1.0.0
110
+ */
12
111
  declare const remap: {
13
112
  (x: number, x1: number, x2: number, y1: number, y2: number): number;
14
113
  (x1: number, x2: number, y1: number, y2: number): (x: number) => number;
15
114
  };
115
+ /**
116
+ * Clamps a value between two bounds.
117
+ *
118
+ * @param value - The value to clamp.
119
+ * @param min - The lower bound.
120
+ * @param max - The upper bound.
121
+ * @returns The clamped value.
122
+ * @example
123
+ * ```ts
124
+ * import { clamp } from 'curvy/utils'
125
+ *
126
+ * const clampedValue = clamp(25, 10, 20) // 20
127
+ * const clampedValue2 = clamp(10, 20)(25) // 20
128
+ * ```
129
+ * @since 1.0.0
130
+ */
16
131
  declare const clamp: {
17
132
  (value: number, min: number, max: number): number;
18
133
  (min: number, max: number): (value: number) => number;
19
134
  };
135
+ /**
136
+ * Clips a value to a specified range, returning a fallback value if the value is outside the range.
137
+ *
138
+ * @param value - The value to clip.
139
+ * @param min - The lower bound.
140
+ * @param max - The upper bound.
141
+ * @param onClip - The fallback value to return if the value is outside the range.
142
+ * @returns The clipped value or the fallback value.
143
+ * @example
144
+ * ```ts
145
+ * import { clip } from 'curvy/utils'
146
+ *
147
+ * const clippedValue = clip(25, 10, 20, 15) // 15
148
+ * const clippedValue2 = clip(10, 20, 15)(25) // 15
149
+ * ```
150
+ * @since 1.0.0
151
+ */
20
152
  declare const clip: {
21
153
  <T>(value: number, min: number, max: number, onClip: T): T | number;
22
154
  <T>(min: number, max: number, onClip: T): (value: number) => T | number;
23
155
  };
156
+ /**
157
+ * Returns the minimum and maximum of two numbers.
158
+ *
159
+ * @param a - The first number.
160
+ * @param b - The second number.
161
+ * @returns A tuple containing the minimum and maximum values.
162
+ * @example
163
+ * ```ts
164
+ * import { minMax } from 'curvy/utils'
165
+ *
166
+ * const [minValue, maxValue] = minMax(10, 20) // [10, 20]
167
+ * const [minValue2, maxValue2] = minMax(20, 10) // [10, 20]
168
+ * ```
169
+ * @since 1.0.0
170
+ */
24
171
  declare function minMax(a: number, b: number): [number, number];
172
+ /**
173
+ * Calculates the modulus of a number.
174
+ *
175
+ * @param n - The number to calculate the modulus of.
176
+ * @param m - The modulus.
177
+ * @returns The modulus of the number.
178
+ * @example
179
+ * ```ts
180
+ * import { mod } from 'curvy/utils'
181
+ *
182
+ * const modulus = mod(10, 3) // 1
183
+ * const modulus2 = mod(-10, 3) // 2
184
+ * ```
185
+ * @since 1.0.0
186
+ */
187
+ declare function mod(n: number, m: number): number;
188
+ /**
189
+ * Asserts that a condition is true, throwing an error with the provided message if it is not.
190
+ *
191
+ * @param condition - The condition to assert.
192
+ * @param message - The error message to throw if the condition is false.
193
+ * @throws An error with the provided message if the condition is false.
194
+ * @example
195
+ * ```ts
196
+ * import { invariant } from 'curvy/utils'
197
+ *
198
+ * invariant(1 + 1 === 2, 'Math is broken!') // No error
199
+ * invariant(1 + 1 === 3, 'Math is broken!') // Throws: Math is broken!
200
+ * ```
201
+ * @since 1.0.0
202
+ */
25
203
  declare function invariant(condition: boolean, message: string): asserts condition;
26
204
 
27
- export { PRECISION, clamp, clip, invariant, lerp, minMax, normalize, remap, round, roundDown };
205
+ export { PRECISION, clamp, clip, invariant, lerp, minMax, mod, normalize, remap, round, roundDown, roundUp };
package/dist/utils.mjs CHANGED
@@ -17,18 +17,23 @@ function roundDown(value, precision = PRECISION) {
17
17
  }
18
18
  return result;
19
19
  }
20
+ function roundUp(value, precision = PRECISION) {
21
+ const scale = 10 ** precision;
22
+ const result = Math.ceil(value * scale) / scale;
23
+ if (result === 0) {
24
+ return 0;
25
+ }
26
+ return result;
27
+ }
20
28
  const lerp = dual(3, (t, a, b) => {
21
29
  return b * t + a * (1 - t);
22
30
  });
23
31
  const normalize = dual(3, (x, a, b) => {
24
32
  return (x - a) / (b - a);
25
33
  });
26
- const remap = dual(
27
- 5,
28
- (x, x1, x2, y1, y2) => {
29
- return y1 + (x - x1) / (x2 - x1) * (y2 - y1);
30
- }
31
- );
34
+ const remap = dual(5, (x, x1, x2, y1, y2) => {
35
+ return y1 + (x - x1) / (x2 - x1) * (y2 - y1);
36
+ });
32
37
  const clamp = dual(3, (value, min, max) => {
33
38
  return value < min ? min : value > max ? max : value;
34
39
  });
@@ -38,10 +43,14 @@ const clip = dual(4, (value, min, max, onClip) => {
38
43
  function minMax(a, b) {
39
44
  return a < b ? [a, b] : [b, a];
40
45
  }
46
+ function mod(n, m) {
47
+ return (n % m + m) % m;
48
+ }
41
49
  function invariant(condition, message) {
42
50
  if (!condition) {
43
51
  throw new Error(message);
44
52
  }
45
53
  }
46
54
 
47
- export { PRECISION, clamp, clip, invariant, lerp, minMax, normalize, remap, round, roundDown };
55
+ export { PRECISION, clamp, clip, invariant, lerp, minMax, mod, normalize, remap, round, roundDown, roundUp };
56
+ //# sourceMappingURL=utils.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.mjs","sources":["../src/utils.ts"],"sourcesContent":["import { dual } from './pipe'\n\n/**\n * @since 1.0.0\n */\nexport const PRECISION = 8\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\n/**\n * Linear interpolation function.\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 * const interpolatedValue = lerp(0.5, 10, 20) // 15\n * const interpolatedValue2 = lerp(0.5)(10, 20) // 15\n * ```\n * @since 1.0.0\n */\nexport const lerp: {\n (t: number, a: number, b: number): number\n (a: number, b: number): (t: number) => number\n} = dual(3, (t: number, a: number, b: number): number => {\n return b * t + a * (1 - t)\n})\n\n/**\n * Normalizes a value between two bounds.\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 * const normalizedValue = normalize(15, 10, 20) // 0.5\n * const normalizedValue2 = normalize(10, 20)(15) // 0.5\n * ```\n * @since 1.0.0\n */\nexport const normalize: {\n (x: number, a: number, b: number): number\n (a: number, b: number): (x: number) => number\n} = dual(3, (x: number, a: number, b: number): number => {\n return (x - a) / (b - a)\n})\n\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 * const remappedValue = remap(15, 10, 20, 0, 100) // 50\n * const remappedValue2 = remap(10, 20, 0, 100)(15) // 50\n * ```\n * @since 1.0.0\n */\nexport const remap: {\n (x: number, x1: number, x2: number, y1: number, y2: number): number\n (x1: number, x2: number, y1: number, y2: number): (x: number) => number\n} = dual(5, (x: number, x1: number, x2: number, y1: number, y2: number): number => {\n return y1 + ((x - x1) / (x2 - x1)) * (y2 - y1)\n})\n\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 * const clampedValue = clamp(25, 10, 20) // 20\n * const clampedValue2 = clamp(10, 20)(25) // 20\n * ```\n * @since 1.0.0\n */\nexport const clamp: {\n (value: number, min: number, max: number): number\n (min: number, max: number): (value: number) => number\n} = dual(3, (value: number, min: number, max: number) => {\n return value < min ? min : value > max ? max : value\n})\n\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 clipped value or the fallback value.\n * @example\n * ```ts\n * import { clip } from 'curvy/utils'\n *\n * const clippedValue = clip(25, 10, 20, 15) // 15\n * const clippedValue2 = clip(10, 20, 15)(25) // 15\n * ```\n * @since 1.0.0\n */\nexport const clip: {\n <T>(value: number, min: number, max: number, onClip: T): T | number\n <T>(min: number, max: number, onClip: T): (value: number) => T | number\n} = dual(4, (value: number, min: number, max: number, onClip: unknown) => {\n return 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\n/**\n * Asserts that a condition is true, throwing an error with the provided message if it is not.\n *\n * @param condition - The condition to assert.\n * @param message - The error message to throw if the condition is false.\n * @throws An error with the provided message if the condition is false.\n * @example\n * ```ts\n * import { invariant } from 'curvy/utils'\n *\n * invariant(1 + 1 === 2, 'Math is broken!') // No error\n * invariant(1 + 1 === 3, 'Math is broken!') // Throws: Math is broken!\n * ```\n * @since 1.0.0\n */\nexport function invariant(condition: boolean, message: string): asserts condition {\n if (!condition) {\n throw new Error(message)\n }\n}\n"],"names":[],"mappings":";;AAKO,MAAM,SAAY,GAAA;AAiBT,SAAA,KAAA,CAAM,KAAe,EAAA,SAAA,GAAY,SAAmB,EAAA;AAClE,EAAA,MAAM,QAAQ,EAAM,IAAA,SAAA;AACpB,EAAA,MAAM,MAAS,GAAA,IAAA,CAAK,KAAM,CAAA,KAAA,GAAQ,KAAK,CAAI,GAAA,KAAA;AAC3C,EAAA,IAAI,WAAW,CAAG,EAAA;AAChB,IAAO,OAAA,CAAA;AAAA;AAET,EAAO,OAAA,MAAA;AACT;AAiBgB,SAAA,SAAA,CAAU,KAAe,EAAA,SAAA,GAAY,SAAmB,EAAA;AACtE,EAAA,MAAM,QAAQ,EAAM,IAAA,SAAA;AACpB,EAAA,MAAM,MAAS,GAAA,IAAA,CAAK,KAAM,CAAA,KAAA,GAAQ,KAAK,CAAI,GAAA,KAAA;AAC3C,EAAA,IAAI,WAAW,CAAG,EAAA;AAChB,IAAO,OAAA,CAAA;AAAA;AAET,EAAO,OAAA,MAAA;AACT;AAiBgB,SAAA,OAAA,CAAQ,KAAe,EAAA,SAAA,GAAY,SAAmB,EAAA;AACpE,EAAA,MAAM,QAAQ,EAAM,IAAA,SAAA;AACpB,EAAA,MAAM,MAAS,GAAA,IAAA,CAAK,IAAK,CAAA,KAAA,GAAQ,KAAK,CAAI,GAAA,KAAA;AAC1C,EAAA,IAAI,WAAW,CAAG,EAAA;AAChB,IAAO,OAAA,CAAA;AAAA;AAET,EAAO,OAAA,MAAA;AACT;AAkBO,MAAM,OAGT,IAAK,CAAA,CAAA,EAAG,CAAC,CAAA,EAAW,GAAW,CAAsB,KAAA;AACvD,EAAO,OAAA,CAAA,GAAI,CAAI,GAAA,CAAA,IAAK,CAAI,GAAA,CAAA,CAAA;AAC1B,CAAC;AAkBM,MAAM,YAGT,IAAK,CAAA,CAAA,EAAG,CAAC,CAAA,EAAW,GAAW,CAAsB,KAAA;AACvD,EAAQ,OAAA,CAAA,CAAA,GAAI,MAAM,CAAI,GAAA,CAAA,CAAA;AACxB,CAAC;AAoBY,MAAA,KAAA,GAGT,KAAK,CAAG,EAAA,CAAC,GAAW,EAAY,EAAA,EAAA,EAAY,IAAY,EAAuB,KAAA;AACjF,EAAA,OAAO,EAAO,GAAA,CAAA,CAAA,GAAI,EAAO,KAAA,EAAA,GAAK,OAAQ,EAAK,GAAA,EAAA,CAAA;AAC7C,CAAC;AAkBM,MAAM,QAGT,IAAK,CAAA,CAAA,EAAG,CAAC,KAAA,EAAe,KAAa,GAAgB,KAAA;AACvD,EAAA,OAAO,KAAQ,GAAA,GAAA,GAAM,GAAM,GAAA,KAAA,GAAQ,MAAM,GAAM,GAAA,KAAA;AACjD,CAAC;AAmBM,MAAM,OAGT,IAAK,CAAA,CAAA,EAAG,CAAC,KAAe,EAAA,GAAA,EAAa,KAAa,MAAoB,KAAA;AACxE,EAAA,OAAO,KAAQ,GAAA,GAAA,GAAM,MAAS,GAAA,KAAA,GAAQ,MAAM,MAAS,GAAA,KAAA;AACvD,CAAC;AAiBe,SAAA,MAAA,CAAO,GAAW,CAA6B,EAAA;AAC7D,EAAO,OAAA,CAAA,GAAI,IAAI,CAAC,CAAA,EAAG,CAAC,CAAI,GAAA,CAAC,GAAG,CAAC,CAAA;AAC/B;AAiBgB,SAAA,GAAA,CAAI,GAAW,CAAmB,EAAA;AAChD,EAAS,OAAA,CAAA,CAAA,GAAI,IAAK,CAAK,IAAA,CAAA;AACzB;AAiBgB,SAAA,SAAA,CAAU,WAAoB,OAAoC,EAAA;AAChF,EAAA,IAAI,CAAC,SAAW,EAAA;AACd,IAAM,MAAA,IAAI,MAAM,OAAO,CAAA;AAAA;AAE3B;;;;"}