@bitbybit-dev/base 1.2.0 → 1.3.0

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 (55) hide show
  1. package/lib/api/GlobalCDNProvider.js +1 -1
  2. package/lib/api/inputs/base-inputs.d.ts +13 -12
  3. package/lib/api/inputs/color-inputs.d.ts +97 -48
  4. package/lib/api/inputs/color-inputs.js +95 -46
  5. package/lib/api/inputs/dates-inputs.d.ts +67 -26
  6. package/lib/api/inputs/dates-inputs.js +57 -16
  7. package/lib/api/inputs/io-inputs.d.ts +13 -15
  8. package/lib/api/inputs/io-inputs.js +7 -8
  9. package/lib/api/inputs/line-inputs.d.ts +88 -33
  10. package/lib/api/inputs/line-inputs.js +61 -11
  11. package/lib/api/inputs/lists-inputs.d.ts +207 -96
  12. package/lib/api/inputs/lists-inputs.js +153 -45
  13. package/lib/api/inputs/logic-inputs.d.ts +52 -22
  14. package/lib/api/inputs/logic-inputs.js +41 -12
  15. package/lib/api/inputs/math-inputs.d.ts +113 -52
  16. package/lib/api/inputs/math-inputs.js +112 -51
  17. package/lib/api/inputs/mesh-inputs.d.ts +30 -11
  18. package/lib/api/inputs/mesh-inputs.js +21 -3
  19. package/lib/api/inputs/point-inputs.d.ts +240 -107
  20. package/lib/api/inputs/point-inputs.js +199 -73
  21. package/lib/api/inputs/polyline-inputs.d.ts +68 -28
  22. package/lib/api/inputs/polyline-inputs.js +48 -13
  23. package/lib/api/inputs/text-inputs.d.ts +117 -46
  24. package/lib/api/inputs/text-inputs.js +111 -41
  25. package/lib/api/inputs/transforms-inputs.d.ts +65 -23
  26. package/lib/api/inputs/transforms-inputs.js +64 -22
  27. package/lib/api/inputs/vector-inputs.d.ts +154 -76
  28. package/lib/api/inputs/vector-inputs.js +132 -57
  29. package/lib/api/models/point/hex-grid-data.d.ts +1 -1
  30. package/lib/api/models/point/hex-grid-data.js +1 -1
  31. package/lib/api/services/color.d.ts +149 -70
  32. package/lib/api/services/color.js +149 -70
  33. package/lib/api/services/dates.d.ts +313 -150
  34. package/lib/api/services/dates.js +313 -150
  35. package/lib/api/services/line.d.ts +138 -68
  36. package/lib/api/services/line.js +138 -68
  37. package/lib/api/services/lists.d.ts +357 -177
  38. package/lib/api/services/lists.js +357 -177
  39. package/lib/api/services/logic.d.ts +98 -45
  40. package/lib/api/services/logic.js +98 -45
  41. package/lib/api/services/math.d.ts +363 -211
  42. package/lib/api/services/math.js +363 -211
  43. package/lib/api/services/mesh.d.ts +73 -31
  44. package/lib/api/services/mesh.js +73 -31
  45. package/lib/api/services/point.d.ts +367 -147
  46. package/lib/api/services/point.js +367 -147
  47. package/lib/api/services/polyline.d.ts +146 -71
  48. package/lib/api/services/polyline.js +146 -71
  49. package/lib/api/services/text.d.ts +320 -165
  50. package/lib/api/services/text.js +320 -165
  51. package/lib/api/services/transforms.d.ts +145 -58
  52. package/lib/api/services/transforms.js +145 -58
  53. package/lib/api/services/vector.d.ts +381 -156
  54. package/lib/api/services/vector.js +381 -156
  55. package/package.json +1 -1
@@ -1,13 +1,18 @@
1
1
  import * as Inputs from "../inputs";
2
2
  /**
3
- * Contains various math methods.
3
+ * Arithmetic, rounding, ranges, random numbers and the trigonometric functions on plain numbers.
4
+ * The trigonometric functions take and give angles in radians; `degToRad` and `radToDeg` convert at
5
+ * the boundary, because almost every angle a user types is in degrees. The interpolation helpers
6
+ * (`lerp`, `remap`, `ease`, `smoothstep`, `pingPong`) are the building blocks of animation and
7
+ * parametric variation.
4
8
  */
5
9
  export class MathBitByBit {
6
10
  /**
7
- * Creates and returns a number value (pass-through for number input).
8
- * Example: Input 42 → 42, Input 3.14 → 3.14
9
- * @param inputs a number to be created
10
- * @returns number
11
+ * Passes a number through unchanged, so a value can be given a name and reused.
12
+ *
13
+ * Example: 42 -> 42
14
+ * @param inputs - The number
15
+ * @returns The same number
11
16
  * @group create
12
17
  * @shortname number
13
18
  * @drawable false
@@ -16,13 +21,21 @@ export class MathBitByBit {
16
21
  return inputs.number;
17
22
  }
18
23
  /**
19
- * Performs basic arithmetic operations on two numbers (add, subtract, multiply, divide, power, modulus).
20
- * Example: 5 + 3 → 8, 10 % 3 → 1, 2 ^ 3 → 8
21
- * @param inputs two numbers and operator
22
- * @returns Result of math operation action
24
+ * Applies one arithmetic operation to two numbers: add, subtract, multiply, divide, power or
25
+ * modulus.
26
+ *
27
+ * The operation reads `first` then `second`: subtract gives first minus second, power gives
28
+ * first to the power of second.
29
+ * Example: 5 add 3 -> 8, 10 modulus 3 -> 1, 2 power 3 -> 8
30
+ * @param inputs - The two numbers and the operation
31
+ * @returns The result of the operation
23
32
  * @group operations
24
33
  * @shortname two numbers
25
34
  * @drawable false
35
+ * @example
36
+ * ```typescript
37
+ * const result = bitbybit.math.twoNrOperation({ first: 2, second: 3, operation: Bit.Inputs.Math.mathTwoNrOperatorEnum.power });
38
+ * ```
26
39
  */
27
40
  twoNrOperation(inputs) {
28
41
  let result;
@@ -51,50 +64,76 @@ export class MathBitByBit {
51
64
  return result;
52
65
  }
53
66
  /**
54
- * Calculates the remainder after division (modulus operation).
55
- * Example: 10 % 3 → 1, 17 % 5 → 2
56
- * @param inputs two numbers and operator
57
- * @returns Result of modulus operation
67
+ * Finds the remainder after dividing one number by another.
68
+ *
69
+ * The sign follows the first number, as it does in JavaScript.
70
+ * Example: 10 modulus 3 -> 1, 17 modulus 5 -> 2
71
+ * @param inputs - The number to divide and the number to divide by
72
+ * @returns The remainder
58
73
  * @group operations
59
74
  * @shortname modulus
60
75
  * @drawable false
76
+ * @example
77
+ * ```typescript
78
+ * const remainder = bitbybit.math.modulus({ number: 17, modulus: 5 });
79
+ * ```
61
80
  */
62
81
  modulus(inputs) {
63
82
  return this.twoNrOperation({ first: inputs.number, second: inputs.modulus, operation: Inputs.Math.mathTwoNrOperatorEnum.modulus });
64
83
  }
65
84
  /**
66
- * Rounds a number to specified decimal places.
67
- * Example: 1.32156 with 3 decimals returns 1.322
68
- * @param inputs a number and decimal places
69
- * @returns Result of rounding
85
+ * Rounds a number to a given number of decimal places.
86
+ *
87
+ * Example: 1.32156 to 3 places -> 1.322
88
+ * @param inputs - The number and how many decimal places to keep
89
+ * @returns The rounded number
70
90
  * @group operations
71
91
  * @shortname round to decimals
72
92
  * @drawable false
93
+ * @example
94
+ * ```typescript
95
+ * const rounded = bitbybit.math.roundToDecimals({ number: 1.32156, decimalPlaces: 3 });
96
+ * ```
73
97
  */
74
98
  roundToDecimals(inputs) {
75
99
  return Math.round(inputs.number * Math.pow(10, inputs.decimalPlaces)) / Math.pow(10, inputs.decimalPlaces);
76
100
  }
77
101
  /**
78
- * Rounds a number to specified decimal places and removes trailing zeros.
79
- * Example: 1.32156 with 3 decimals returns 1.322, but 1.320000001 returns 1.32, and 1.000 returns 1
80
- * @param inputs a number and decimal places
81
- * @returns Result of rounding as a number without trailing zeros
102
+ * Rounds a number to a given number of decimal places and drops the zeros at the end.
103
+ *
104
+ * As a number the result cannot carry trailing zeros anyway; the difference from
105
+ * `roundToDecimals` is that floating-point noise such as 1.320000001 is cleaned to 1.32.
106
+ * Example: 1.32156 to 3 places -> 1.322, 1.320000001 -> 1.32, 1.000 -> 1
107
+ * @param inputs - The number and how many decimal places to keep
108
+ * @returns The rounded number
82
109
  * @group operations
83
110
  * @shortname round trim zeros
84
111
  * @drawable false
112
+ * @example
113
+ * ```typescript
114
+ * const clean = bitbybit.math.roundAndRemoveTrailingZeros({ number: 1.320000001, decimalPlaces: 3 });
115
+ * ```
85
116
  */
86
117
  roundAndRemoveTrailingZeros(inputs) {
87
118
  const rounded = Math.round(inputs.number * Math.pow(10, inputs.decimalPlaces)) / Math.pow(10, inputs.decimalPlaces);
88
119
  return parseFloat(rounded.toFixed(inputs.decimalPlaces));
89
120
  }
90
121
  /**
91
- * Performs mathematical operations on a single number (absolute, negate, sqrt, trig functions, logarithms, etc.).
92
- * Example: sqrt(5) → 2.236, abs(-3) → 3, sin(π/2) → 1
93
- * @param inputs one number and operator action
94
- * @returns Result of math operation
122
+ * Applies one operation to a single number: absolute, negate, square root, rounding,
123
+ * logarithms, the trigonometric functions and their inverses, exponential, or a conversion
124
+ * between radians and degrees.
125
+ *
126
+ * The trigonometric functions work in radians.
127
+ * Example: sqrt of 5 -> 2.236, absolute of -3 -> 3
128
+ * @param inputs - The number and the operation
129
+ * @returns The result of the operation
95
130
  * @group operations
96
131
  * @shortname one number
97
132
  * @drawable false
133
+ * @example
134
+ * ```typescript
135
+ * const root = bitbybit.math.oneNrOperation({ number: 5, operation: Bit.Inputs.Math.mathOneNrOperatorEnum.sqrt });
136
+ * ```
98
137
  */
99
138
  oneNrOperation(inputs) {
100
139
  let result;
@@ -162,49 +201,66 @@ export class MathBitByBit {
162
201
  return result;
163
202
  }
164
203
  /**
165
- * Maps a number from one range to another range proportionally.
166
- * Example: 5 from [0,10] to [0,100] → 50, 0.5 from [0,1] to [-10,10] → 0
167
- * @param inputs one number and operator action
168
- * @returns Result of mapping
169
- * @group operations
170
- * @shortname remap
171
- * @drawable false
172
- */
204
+ * Maps a number from one range onto another, keeping its relative position.
205
+ *
206
+ * A number outside the source range maps proportionally beyond the target range.
207
+ * Example: 5 from [0,10] to [0,100] -> 50, 0.5 from [0,1] to [-10,10] -> 0
208
+ * @param inputs - The number, the range it is in and the range to map it to
209
+ * @returns The number at the same relative position in the target range
210
+ * @group operations
211
+ * @shortname remap
212
+ * @drawable false
213
+ * @example
214
+ * ```typescript
215
+ * const percent = bitbybit.math.remap({ number: 5, fromLow: 0, fromHigh: 10, toLow: 0, toHigh: 100 });
216
+ * ```
217
+ */
173
218
  remap(inputs) {
174
219
  return (inputs.number - inputs.fromLow) * (inputs.toHigh - inputs.toLow) / (inputs.fromHigh - inputs.fromLow) + inputs.toLow;
175
220
  }
176
221
  /**
177
- * Generates a random decimal number between 0 (inclusive) and 1 (exclusive).
178
- * Example: Outputs like 0.342, 0.891, or any value in [0, 1)
179
- * @returns A random number between 0 and 1
180
- * @group generate
181
- * @shortname random 0 - 1
182
- * @drawable false
183
- */
222
+ * Gives a random number from 0 up to, but not including, 1.
223
+ *
224
+ * Example: 0.342, 0.891 or any other value in that range
225
+ * @returns A random number between 0 and 1
226
+ * @group generate
227
+ * @shortname random 0 - 1
228
+ * @drawable false
229
+ */
184
230
  random() {
185
231
  return Math.random();
186
232
  }
187
233
  /**
188
- * Generates a random number within a specified range (low to high).
189
- * Example: Range [0, 10] → outputs like 3.7, 8.2, or any value between 0 and 10
190
- * @param inputs low and high numbers
191
- * @returns A random number
192
- * @group generate
193
- * @shortname random number
194
- * @drawable false
195
- */
234
+ * Gives a random number between `low` and `high`.
235
+ *
236
+ * Example: low 0, high 10 -> 3.7, 8.2 or any other value between them
237
+ * @param inputs - The low and high ends of the range
238
+ * @returns A random number in the range
239
+ * @group generate
240
+ * @shortname random number
241
+ * @drawable false
242
+ * @example
243
+ * ```typescript
244
+ * const value = bitbybit.math.randomNumber({ low: 0, high: 10 });
245
+ * ```
246
+ */
196
247
  randomNumber(inputs) {
197
248
  return Math.random() * (inputs.high - inputs.low) + inputs.low;
198
249
  }
199
250
  /**
200
- * Generates multiple random numbers within a specified range.
201
- * Example: Range [0, 10] with 3 items → [2.5, 7.1, 4.8]
202
- * @param inputs low and high numbers
203
- * @returns A list of random numbers
204
- * @group generate
205
- * @shortname random numbers
206
- * @drawable false
207
- */
251
+ * Gives a list of random numbers between `low` and `high`.
252
+ *
253
+ * Example: low 0, high 10, count 3 -> [2.5, 7.1, 4.8]
254
+ * @param inputs - The low and high ends of the range and how many numbers to make
255
+ * @returns The random numbers
256
+ * @group generate
257
+ * @shortname random numbers
258
+ * @drawable false
259
+ * @example
260
+ * ```typescript
261
+ * const values = bitbybit.math.randomNumbers({ low: 0, high: 10, count: 3 });
262
+ * ```
263
+ */
208
264
  randomNumbers(inputs) {
209
265
  const result = [];
210
266
  for (let i = 0; i < inputs.count; i++) {
@@ -213,93 +269,127 @@ export class MathBitByBit {
213
269
  return result;
214
270
  }
215
271
  /**
216
- * Returns the mathematical constant π (pi) ≈ 3.14159.
217
- * Example: Outputs 3.141592653589793
218
- * @returns A number PI
219
- * @group generate
220
- * @shortname π
221
- * @drawable false
222
- */
272
+ * Gives the constant pi, the ratio of a circle's circumference to its diameter.
273
+ *
274
+ * Example: 3.141592653589793
275
+ * @returns The number pi
276
+ * @group generate
277
+ * @shortname π
278
+ * @drawable false
279
+ */
223
280
  pi() {
224
281
  return Math.PI;
225
282
  }
226
283
  /**
227
- * Formats a number as a string with a fixed number of decimal places (always shows trailing zeros).
228
- * Example: 3.14159 with 2 decimals → '3.14', 5 with 3 decimals → '5.000'
229
- * @param inputs a number to be rounded to decimal places
230
- * @returns number
284
+ * Formats a number as text with a fixed number of decimal places, keeping trailing zeros.
285
+ *
286
+ * Example: 3.14159 with 2 places -> '3.14', 5 with 3 places -> '5.000'
287
+ * @param inputs - The number and how many decimal places to show
288
+ * @returns The formatted text
231
289
  * @group operations
232
290
  * @shortname to fixed
233
291
  * @drawable false
292
+ * @example
293
+ * ```typescript
294
+ * const label = bitbybit.math.toFixed({ number: 3.14159, decimalPlaces: 2 });
295
+ * ```
234
296
  */
235
297
  toFixed(inputs) {
236
298
  return inputs.number.toFixed(inputs.decimalPlaces);
237
299
  }
238
300
  /**
239
- * Adds two numbers together.
240
- * Example: 5 + 3 → 8, -2 + 7 → 5
241
- * @param inputs two numbers
242
- * @returns number
301
+ * Adds two numbers.
302
+ *
303
+ * Example: 5 and 3 -> 8, -2 and 7 -> 5
304
+ * @param inputs - The two numbers
305
+ * @returns Their sum
243
306
  * @group basics
244
307
  * @shortname add
245
308
  * @drawable false
309
+ * @example
310
+ * ```typescript
311
+ * const sum = bitbybit.math.add({ first: 5, second: 3 });
312
+ * ```
246
313
  */
247
314
  add(inputs) {
248
315
  return inputs.first + inputs.second;
249
316
  }
250
317
  /**
251
318
  * Subtracts the second number from the first.
252
- * Example: 10 - 3 → 7, 5 - 8 → -3
253
- * @param inputs two numbers
254
- * @returns number
319
+ *
320
+ * Example: 10 and 3 -> 7, 5 and 8 -> -3
321
+ * @param inputs - The number to subtract from and the number to subtract
322
+ * @returns Their difference
255
323
  * @group basics
256
324
  * @shortname subtract
257
325
  * @drawable false
326
+ * @example
327
+ * ```typescript
328
+ * const difference = bitbybit.math.subtract({ first: 10, second: 3 });
329
+ * ```
258
330
  */
259
331
  subtract(inputs) {
260
332
  return inputs.first - inputs.second;
261
333
  }
262
334
  /**
263
- * Multiplies two numbers together.
264
- * Example: 5 × 3 → 15, -2 × 4 → -8
265
- * @param inputs two numbers
266
- * @returns number
335
+ * Multiplies two numbers.
336
+ *
337
+ * Example: 5 and 3 -> 15, -2 and 4 -> -8
338
+ * @param inputs - The two numbers
339
+ * @returns Their product
267
340
  * @group basics
268
341
  * @shortname multiply
269
342
  * @drawable false
343
+ * @example
344
+ * ```typescript
345
+ * const product = bitbybit.math.multiply({ first: 5, second: 3 });
346
+ * ```
270
347
  */
271
348
  multiply(inputs) {
272
349
  return inputs.first * inputs.second;
273
350
  }
274
351
  /**
275
352
  * Divides the first number by the second.
276
- * Example: 10 ÷ 2 → 5, 7 ÷ 2 → 3.5
277
- * @param inputs two numbers
278
- * @returns number
353
+ *
354
+ * Dividing by 0 gives Infinity, as in JavaScript.
355
+ * Example: 10 and 2 -> 5, 7 and 2 -> 3.5
356
+ * @param inputs - The number to divide and the number to divide by
357
+ * @returns Their quotient
279
358
  * @group basics
280
359
  * @shortname divide
281
360
  * @drawable false
361
+ * @example
362
+ * ```typescript
363
+ * const quotient = bitbybit.math.divide({ first: 7, second: 2 });
364
+ * ```
282
365
  */
283
366
  divide(inputs) {
284
367
  return inputs.first / inputs.second;
285
368
  }
286
369
  /**
287
- * Raises the first number to the power of the second (exponentiation).
288
- * Example: 2³ → 8, 5² → 25, 10⁻¹ → 0.1
289
- * @param inputs two numbers
290
- * @returns number
370
+ * Raises the first number to the power of the second.
371
+ *
372
+ * Example: 2 to the 3 -> 8, 5 to the 2 -> 25, 10 to the -1 -> 0.1
373
+ * @param inputs - The base and the exponent
374
+ * @returns The power
291
375
  * @group basics
292
376
  * @shortname power
293
377
  * @drawable false
378
+ * @example
379
+ * ```typescript
380
+ * const cube = bitbybit.math.power({ first: 2, second: 3 });
381
+ * ```
294
382
  */
295
383
  power(inputs) {
296
384
  return Math.pow(inputs.first, inputs.second);
297
385
  }
298
386
  /**
299
- * Calculates the square root of a number.
300
- * Example: √9 → 3, √2 → 1.414, √16 → 4
301
- * @param inputs a number
302
- * @returns number
387
+ * Finds the square root of a number.
388
+ *
389
+ * A negative number gives NaN.
390
+ * Example: 9 -> 3, 2 -> 1.414
391
+ * @param inputs - The number
392
+ * @returns The square root
303
393
  * @group basics
304
394
  * @shortname sqrt
305
395
  * @drawable false
@@ -308,10 +398,11 @@ export class MathBitByBit {
308
398
  return Math.sqrt(inputs.number);
309
399
  }
310
400
  /**
311
- * Returns the absolute value (removes negative sign, always positive or zero).
312
- * Example: |-5| → 5, |3| → 3, |0| → 0
313
- * @param inputs a number
314
- * @returns number
401
+ * Drops the sign of a number, so the result is never negative.
402
+ *
403
+ * Example: -5 -> 5, 3 -> 3, 0 -> 0
404
+ * @param inputs - The number
405
+ * @returns The absolute value
315
406
  * @group basics
316
407
  * @shortname abs
317
408
  * @drawable false
@@ -320,10 +411,11 @@ export class MathBitByBit {
320
411
  return Math.abs(inputs.number);
321
412
  }
322
413
  /**
323
- * Rounds a number to the nearest integer.
324
- * Example: 3.7 → 4, 2.3 → 2, 5.5 → 6
325
- * @param inputs a number
326
- * @returns number
414
+ * Rounds a number to the nearest whole number; halves round up.
415
+ *
416
+ * Example: 3.7 -> 4, 2.3 -> 2, 5.5 -> 6
417
+ * @param inputs - The number
418
+ * @returns The nearest whole number
327
419
  * @group basics
328
420
  * @shortname round
329
421
  * @drawable false
@@ -332,10 +424,11 @@ export class MathBitByBit {
332
424
  return Math.round(inputs.number);
333
425
  }
334
426
  /**
335
- * Rounds a number down to the nearest integer (toward negative infinity).
336
- * Example: 3.7 → 3, -2.3 → -3, 5 → 5
337
- * @param inputs a number
338
- * @returns number
427
+ * Rounds a number down to the whole number below it.
428
+ *
429
+ * Example: 3.7 -> 3, -2.3 -> -3, 5 -> 5
430
+ * @param inputs - The number
431
+ * @returns The whole number below
339
432
  * @group basics
340
433
  * @shortname floor
341
434
  * @drawable false
@@ -344,10 +437,11 @@ export class MathBitByBit {
344
437
  return Math.floor(inputs.number);
345
438
  }
346
439
  /**
347
- * Rounds a number up to the nearest integer (toward positive infinity).
348
- * Example: 3.2 → 4, -2.8 → -2, 5 → 5
349
- * @param inputs a number
350
- * @returns number
440
+ * Rounds a number up to the whole number above it.
441
+ *
442
+ * Example: 3.2 -> 4, -2.8 -> -2, 5 -> 5
443
+ * @param inputs - The number
444
+ * @returns The whole number above
351
445
  * @group basics
352
446
  * @shortname ceil
353
447
  * @drawable false
@@ -356,10 +450,11 @@ export class MathBitByBit {
356
450
  return Math.ceil(inputs.number);
357
451
  }
358
452
  /**
359
- * Negates a number (flips its sign: positive becomes negative, negative becomes positive).
360
- * Example: 5 → -5, -3 → 3, 0 → 0
361
- * @param inputs a number
362
- * @returns number
453
+ * Flips the sign of a number.
454
+ *
455
+ * Example: 5 -> -5, -3 -> 3, 0 -> 0
456
+ * @param inputs - The number
457
+ * @returns The number with the opposite sign
363
458
  * @group basics
364
459
  * @shortname negate
365
460
  * @drawable false
@@ -368,10 +463,11 @@ export class MathBitByBit {
368
463
  return -inputs.number;
369
464
  }
370
465
  /**
371
- * Calculates the natural logarithm (base e) of a number.
372
- * Example: ln(2.718) → ~1, ln(7.389) → ~2, ln(1) → 0
373
- * @param inputs a number
374
- * @returns number
466
+ * Finds the natural logarithm of a number: the power e must be raised to for that number.
467
+ *
468
+ * Example: 2.718 -> about 1, 1 -> 0
469
+ * @param inputs - The number, greater than 0
470
+ * @returns The natural logarithm
375
471
  * @group basics
376
472
  * @shortname ln
377
473
  * @drawable false
@@ -380,10 +476,11 @@ export class MathBitByBit {
380
476
  return Math.log(inputs.number);
381
477
  }
382
478
  /**
383
- * Calculates the base 10 logarithm of a number.
384
- * Example: log₁₀(100) → 2, log₁₀(1000) → 3, log₁₀(10) → 1
385
- * @param inputs a number
386
- * @returns number
479
+ * Finds the base-10 logarithm of a number: the power 10 must be raised to for that number.
480
+ *
481
+ * Example: 100 -> 2, 1000 -> 3, 10 -> 1
482
+ * @param inputs - The number, greater than 0
483
+ * @returns The base-10 logarithm
387
484
  * @group basics
388
485
  * @shortname log10
389
486
  * @drawable false
@@ -392,10 +489,11 @@ export class MathBitByBit {
392
489
  return Math.log10(inputs.number);
393
490
  }
394
491
  /**
395
- * Raises 10 to the power of the input number.
396
- * Example: 10² → 100, 10³ → 1000, 10⁻¹ → 0.1
397
- * @param inputs a number
398
- * @returns number
492
+ * Raises 10 to the power of a number.
493
+ *
494
+ * Example: 2 -> 100, 3 -> 1000, -1 -> 0.1
495
+ * @param inputs - The exponent
496
+ * @returns 10 to that power
399
497
  * @group basics
400
498
  * @shortname ten pow
401
499
  * @drawable false
@@ -404,10 +502,11 @@ export class MathBitByBit {
404
502
  return Math.pow(10, inputs.number);
405
503
  }
406
504
  /**
407
- * Calculates the sine of an angle in radians.
408
- * Example: sin(0) → 0, sin(π/2) → 1, sin(π) → ~0
409
- * @param inputs a number
410
- * @returns number
505
+ * Finds the sine of an angle given in radians.
506
+ *
507
+ * Example: 0 -> 0, pi/2 -> 1
508
+ * @param inputs - The angle in radians
509
+ * @returns The sine, between -1 and 1
411
510
  * @group basics
412
511
  * @shortname sin
413
512
  * @drawable false
@@ -416,10 +515,11 @@ export class MathBitByBit {
416
515
  return Math.sin(inputs.number);
417
516
  }
418
517
  /**
419
- * Calculates the cosine of an angle in radians.
420
- * Example: cos(0) → 1, cos(π/2) → ~0, cos(π) → -1
421
- * @param inputs a number
422
- * @returns number
518
+ * Finds the cosine of an angle given in radians.
519
+ *
520
+ * Example: 0 -> 1, pi -> -1
521
+ * @param inputs - The angle in radians
522
+ * @returns The cosine, between -1 and 1
423
523
  * @group basics
424
524
  * @shortname cos
425
525
  * @drawable false
@@ -428,10 +528,11 @@ export class MathBitByBit {
428
528
  return Math.cos(inputs.number);
429
529
  }
430
530
  /**
431
- * Calculates the tangent of an angle in radians.
432
- * Example: tan(0) → 0, tan(π/4) → ~1, tan(π/2) → infinity
433
- * @param inputs a number
434
- * @returns number
531
+ * Finds the tangent of an angle given in radians.
532
+ *
533
+ * Example: 0 -> 0, pi/4 -> about 1
534
+ * @param inputs - The angle in radians
535
+ * @returns The tangent
435
536
  * @group basics
436
537
  * @shortname tan
437
538
  * @drawable false
@@ -440,10 +541,11 @@ export class MathBitByBit {
440
541
  return Math.tan(inputs.number);
441
542
  }
442
543
  /**
443
- * Calculates the arcsine (inverse sine) in radians, returns angle whose sine is the input.
444
- * Example: asin(0) → 0, asin(1) → π/2 (~1.57), asin(0.5) → π/6 (~0.524)
445
- * @param inputs a number
446
- * @returns number
544
+ * Finds the angle, in radians, whose sine is the given number.
545
+ *
546
+ * Example: 0 -> 0, 1 -> pi/2 (about 1.57)
547
+ * @param inputs - A number between -1 and 1
548
+ * @returns The angle in radians
447
549
  * @group basics
448
550
  * @shortname asin
449
551
  * @drawable false
@@ -452,10 +554,11 @@ export class MathBitByBit {
452
554
  return Math.asin(inputs.number);
453
555
  }
454
556
  /**
455
- * Calculates the arccosine (inverse cosine) in radians, returns angle whose cosine is the input.
456
- * Example: acos(1) → 0, acos(0) → π/2 (~1.57), acos(-1) → π (~3.14)
457
- * @param inputs a number
458
- * @returns number
557
+ * Finds the angle, in radians, whose cosine is the given number.
558
+ *
559
+ * Example: 1 -> 0, -1 -> pi (about 3.14)
560
+ * @param inputs - A number between -1 and 1
561
+ * @returns The angle in radians
459
562
  * @group basics
460
563
  * @shortname acos
461
564
  * @drawable false
@@ -464,10 +567,11 @@ export class MathBitByBit {
464
567
  return Math.acos(inputs.number);
465
568
  }
466
569
  /**
467
- * Calculates the arctangent (inverse tangent) in radians, returns angle whose tangent is the input.
468
- * Example: atan(0) → 0, atan(1) → π/4 (~0.785), atan(-1) → -π/4
469
- * @param inputs a number
470
- * @returns number
570
+ * Finds the angle, in radians, whose tangent is the given number.
571
+ *
572
+ * Example: 0 -> 0, 1 -> pi/4 (about 0.785)
573
+ * @param inputs - The number
574
+ * @returns The angle in radians, between -pi/2 and pi/2
471
575
  * @group basics
472
576
  * @shortname atan
473
577
  * @drawable false
@@ -476,10 +580,11 @@ export class MathBitByBit {
476
580
  return Math.atan(inputs.number);
477
581
  }
478
582
  /**
479
- * Calculates e raised to the power of the input (exponential function).
480
- * Example: e⁰ → 1, e¹ → ~2.718, e² → ~7.389
481
- * @param inputs a number
482
- * @returns number
583
+ * Raises e, the base of the natural logarithm, to the power of a number.
584
+ *
585
+ * Example: 0 -> 1, 1 -> about 2.718, 2 -> about 7.389
586
+ * @param inputs - The exponent
587
+ * @returns e to that power
483
588
  * @group basics
484
589
  * @shortname exp
485
590
  * @drawable false
@@ -489,9 +594,10 @@ export class MathBitByBit {
489
594
  }
490
595
  /**
491
596
  * Converts an angle from degrees to radians.
492
- * Example: 180° → π (~3.14159), 90° → π/2 (~1.5708), 360° → 2π
493
- * @param inputs a number in degrees
494
- * @returns number
597
+ *
598
+ * Example: 180 -> pi (about 3.14159), 90 -> pi/2
599
+ * @param inputs - The angle in degrees
600
+ * @returns The angle in radians
495
601
  * @group basics
496
602
  * @shortname deg to rad
497
603
  * @drawable false
@@ -501,9 +607,10 @@ export class MathBitByBit {
501
607
  }
502
608
  /**
503
609
  * Converts an angle from radians to degrees.
504
- * Example: π → 180°, π/2 → 90°, 2π → 360°
505
- * @param inputs a number in radians
506
- * @returns number
610
+ *
611
+ * Example: pi -> 180, pi/2 -> 90
612
+ * @param inputs - The angle in radians
613
+ * @returns The angle in degrees
507
614
  * @group basics
508
615
  * @shortname rad to deg
509
616
  * @drawable false
@@ -512,14 +619,21 @@ export class MathBitByBit {
512
619
  return inputs.number * 180 / Math.PI;
513
620
  }
514
621
  /**
515
- * Applies an easing function to interpolate smoothly between min and max values.
516
- * Example: x=0.5 from [0,100] with easeInQuad → applies quadratic acceleration curve
517
- * Useful for smooth animations with various acceleration/deceleration curves.
518
- * @param inputs a number, min and max values, and ease type
519
- * @returns number
622
+ * Maps a value from 0 to 1 onto the range `min` to `max` along an easing curve, so the result
623
+ * speeds up or slows down instead of changing evenly.
624
+ *
625
+ * An `easeIn` curve starts slowly, an `easeOut` curve ends slowly, an `easeInOut` curve does
626
+ * both.
627
+ * Example: 0.5 from [0,100] with easeInQuad -> 25
628
+ * @param inputs - The value between 0 and 1, the target range and the easing curve
629
+ * @returns The eased value in the target range
520
630
  * @group operations
521
631
  * @shortname ease
522
632
  * @drawable false
633
+ * @example
634
+ * ```typescript
635
+ * const eased = bitbybit.math.ease({ x: 0.5, min: 0, max: 100, ease: Bit.Inputs.Math.easeEnum.easeInQuad });
636
+ * ```
523
637
  */
524
638
  ease(inputs) {
525
639
  const x = inputs.x;
@@ -530,39 +644,55 @@ export class MathBitByBit {
530
644
  return res;
531
645
  }
532
646
  /**
533
- * Constrains a value between a minimum and maximum value.
534
- * Example: clamp(5, 0, 3) returns 3, clamp(-1, 0, 3) returns 0, clamp(1.5, 0, 3) returns 1.5
535
- * @param inputs a number, min and max values
536
- * @returns number clamped between min and max
647
+ * Keeps a number within a range: below `min` becomes `min`, above `max` becomes `max`.
648
+ *
649
+ * Example: 5 in [0,3] -> 3, -1 in [0,3] -> 0, 1.5 in [0,3] -> 1.5
650
+ * @param inputs - The number and the range to keep it in
651
+ * @returns The number, limited to the range
537
652
  * @group operations
538
653
  * @shortname clamp
539
654
  * @drawable false
655
+ * @example
656
+ * ```typescript
657
+ * const limited = bitbybit.math.clamp({ number: 5, min: 0, max: 3 });
658
+ * ```
540
659
  */
541
660
  clamp(inputs) {
542
661
  return Math.max(inputs.min, Math.min(inputs.max, inputs.number));
543
662
  }
544
663
  /**
545
- * Linear interpolation between two values using parameter t (0 to 1).
546
- * Example: From 0 to 100 at t=0.5 → 50, From 10 to 20 at t=0.25 → 12.5
547
- * When t=0 returns start, when t=1 returns end. Useful for smooth transitions.
548
- * @param inputs start value, end value, and interpolation parameter t
549
- * @returns interpolated value
664
+ * Blends from a start value to an end value by a fraction `t`: 0 gives the start, 1 the end,
665
+ * 0.5 the midpoint.
666
+ *
667
+ * A `t` outside 0 to 1 extrapolates past the ends.
668
+ * Example: 0 to 100 at 0.5 -> 50, 10 to 20 at 0.25 -> 12.5
669
+ * @param inputs - The start value, the end value and the fraction
670
+ * @returns The blended value
550
671
  * @group operations
551
672
  * @shortname lerp
552
673
  * @drawable false
674
+ * @example
675
+ * ```typescript
676
+ * const mid = bitbybit.math.lerp({ start: 10, end: 20, t: 0.25 });
677
+ * ```
553
678
  */
554
679
  lerp(inputs) {
555
680
  return inputs.start + (inputs.end - inputs.start) * inputs.t;
556
681
  }
557
682
  /**
558
- * Calculates the interpolation parameter t for a value between start and end (reverse of lerp).
559
- * Example: Value 5 in range [0,10] → t=0.5, Value 2.5 in range [0,10] → t=0.25
560
- * Returns what t value would produce the given value in a lerp. Useful for finding relative position.
561
- * @param inputs start value, end value, and the value to find t for
562
- * @returns interpolation parameter (typically 0-1)
683
+ * Finds where a value sits between a start and an end, as a fraction: the `t` that `lerp` would
684
+ * need to produce it.
685
+ *
686
+ * Example: 5 in [0,10] -> 0.5, 2.5 in [0,10] -> 0.25
687
+ * @param inputs - The start value, the end value and the value to locate
688
+ * @returns The fraction from start to end
563
689
  * @group operations
564
690
  * @shortname inverse lerp
565
691
  * @drawable false
692
+ * @example
693
+ * ```typescript
694
+ * const fraction = bitbybit.math.inverseLerp({ start: 0, end: 10, value: 2.5 });
695
+ * ```
566
696
  */
567
697
  inverseLerp(inputs) {
568
698
  if (inputs.start === inputs.end) {
@@ -571,11 +701,12 @@ export class MathBitByBit {
571
701
  return (inputs.value - inputs.start) / (inputs.end - inputs.start);
572
702
  }
573
703
  /**
574
- * Hermite interpolation with smooth acceleration and deceleration (smoother than linear lerp).
575
- * Example: x=0 → 0, x=0.5 → 0.5, x=1 → 1 (but with smooth S-curve in between)
576
- * Input is automatically clamped to [0,1]. Output eases in and out smoothly. Great for animations.
577
- * @param inputs a number between 0 and 1
578
- * @returns smoothly interpolated value
704
+ * Turns a value from 0 to 1 into a smooth S-curve that starts and ends gently; the value is
705
+ * clamped to that range first.
706
+ *
707
+ * Example: 0 -> 0, 0.5 -> 0.5, 0.25 -> 0.156
708
+ * @param inputs - The value between 0 and 1
709
+ * @returns The smoothed value between 0 and 1
579
710
  * @group operations
580
711
  * @shortname smoothstep
581
712
  * @drawable false
@@ -585,11 +716,11 @@ export class MathBitByBit {
585
716
  return t * t * (3 - 2 * t);
586
717
  }
587
718
  /**
588
- * Returns the sign of a number: -1 for negative, 0 for zero, 1 for positive.
589
- * Example: -5 → -1, 0 → 0, 3.14 → 1
590
- * Useful for determining direction or polarity.
591
- * @param inputs a number
592
- * @returns -1, 0, or 1
719
+ * Tells the sign of a number: -1 when negative, 0 when zero, 1 when positive.
720
+ *
721
+ * Example: -5 -> -1, 0 -> 0, 3.14 -> 1
722
+ * @param inputs - The number
723
+ * @returns -1, 0 or 1
593
724
  * @group operations
594
725
  * @shortname sign
595
726
  * @drawable false
@@ -598,11 +729,12 @@ export class MathBitByBit {
598
729
  return Math.sign(inputs.number);
599
730
  }
600
731
  /**
601
- * Returns the fractional part of a number (removes integer part, keeps decimals).
602
- * Example: 3.14 → 0.14, 5.9 → 0.9, -2.3 → 0.7
603
- * Useful for wrapping values and creating repeating patterns.
604
- * @param inputs a number
605
- * @returns fractional part (always positive)
732
+ * Keeps the part of a number after the decimal point, measured up from the whole number below
733
+ * it, so the result is always from 0 up to 1.
734
+ *
735
+ * Example: 3.14 -> 0.14, -2.3 -> 0.7
736
+ * @param inputs - The number
737
+ * @returns The fractional part, from 0 up to 1
606
738
  * @group operations
607
739
  * @shortname fract
608
740
  * @drawable false
@@ -611,14 +743,20 @@ export class MathBitByBit {
611
743
  return inputs.number - Math.floor(inputs.number);
612
744
  }
613
745
  /**
614
- * Wraps a number within a specified range (creates repeating cycle).
615
- * Example: 1.5 in range [0,1) → 0.5, -0.3 in range [0,1) → 0.7, 370° in range [0,360) → 10°
616
- * Useful for angles, UVs, or any repeating domain. Like modulo but handles negatives properly.
617
- * @param inputs a number, min and max values
618
- * @returns wrapped value within range
746
+ * Wraps a number into a range so it cycles round: past `max` it comes back in at `min`, and
747
+ * below `min` it comes back in at `max`.
748
+ *
749
+ * Useful for angles and repeating patterns; unlike a plain modulus it handles negative numbers.
750
+ * Example: 1.5 in [0,1) -> 0.5, -0.3 in [0,1) -> 0.7, 370 in [0,360) -> 10
751
+ * @param inputs - The number and the range to wrap it into
752
+ * @returns The wrapped number, from min up to max
619
753
  * @group operations
620
754
  * @shortname wrap
621
755
  * @drawable false
756
+ * @example
757
+ * ```typescript
758
+ * const angle = bitbybit.math.wrap({ number: 370, min: 0, max: 360 });
759
+ * ```
622
760
  */
623
761
  wrap(inputs) {
624
762
  const range = inputs.max - inputs.min;
@@ -629,29 +767,37 @@ export class MathBitByBit {
629
767
  return normalized < 0 ? normalized + inputs.max : normalized + inputs.min;
630
768
  }
631
769
  /**
632
- * Creates a ping-pong (back-and-forth) effect that bounces a value between 0 and length.
633
- * The value goes from 0→length, then back length→0, repeating this cycle.
634
- * Example: With length=1: t=0→0, t=0.5→0.5, t=1→1 (peak), t=1.5→0.5, t=2→0, t=2.5→0.5 (repeats)
635
- * Useful for creating bouncing animations like a ball or oscillating motion.
636
- * @param inputs time value t and length
637
- * @returns value bouncing between 0 and length
770
+ * Bounces a value back and forth between 0 and `length` as `t` grows: up to `length`, back down
771
+ * to 0, and again.
772
+ *
773
+ * Example: length 1 at t 0.5 -> 0.5, t 1 -> 1, t 1.5 -> 0.5, t 2 -> 0
774
+ * @param inputs - The running value and the length to bounce within
775
+ * @returns The bounced value between 0 and length
638
776
  * @group operations
639
777
  * @shortname ping pong
640
778
  * @drawable false
779
+ * @example
780
+ * ```typescript
781
+ * const bounce = bitbybit.math.pingPong({ t: 1.5, length: 1 });
782
+ * ```
641
783
  */
642
784
  pingPong(inputs) {
643
785
  const t = Math.abs(inputs.t) % (inputs.length * 2);
644
786
  return t > inputs.length ? inputs.length * 2 - t : t;
645
787
  }
646
788
  /**
647
- * Moves a value toward a target by a maximum delta amount (never overshooting).
648
- * Example: From 0 toward 10 by max 3 → 3, From 8 toward 10 by max 3 → 10 (reached)
649
- * Useful for smooth movement with maximum speed limits.
650
- * @param inputs current value, target value, and maximum delta
651
- * @returns new value moved toward target
789
+ * Moves a value toward a target by at most `maxDelta`, without overshooting it.
790
+ *
791
+ * Example: 0 toward 10 by 3 -> 3, 8 toward 10 by 3 -> 10
792
+ * @param inputs - The current value, the target and the largest step allowed
793
+ * @returns The value after one step
652
794
  * @group operations
653
795
  * @shortname move towards
654
796
  * @drawable false
797
+ * @example
798
+ * ```typescript
799
+ * const next = bitbybit.math.moveTowards({ current: 8, target: 10, maxDelta: 3 });
800
+ * ```
655
801
  */
656
802
  moveTowards(inputs) {
657
803
  const delta = inputs.target - inputs.current;
@@ -661,15 +807,21 @@ export class MathBitByBit {
661
807
  return inputs.current + Math.sign(delta) * inputs.maxDelta;
662
808
  }
663
809
  /**
664
- * Safely evaluates a simple arithmetic expression containing only
665
- * numbers, +, -, *, /, parentheses, and whitespace.
666
- * Uses the shunting-yard algorithm — no eval/Function.
667
- * Example: '(3+2)*4' → 20, '10/3' → 3.3333...
668
- * @param inputs arithmetic expression string
669
- * @returns evaluated result
810
+ * Works out a simple arithmetic expression written as text: numbers, +, -, the multiplication
811
+ * sign, /, parentheses and spaces.
812
+ *
813
+ * The expression is parsed and computed by the library itself, never handed to the JavaScript
814
+ * engine to run, so it is safe with text a user typed.
815
+ * Example: '(3+2) times 4' written with the sign -> 20, '10/3' -> 3.3333
816
+ * @param inputs - The expression text
817
+ * @returns The computed value
670
818
  * @group operations
671
819
  * @shortname eval arithmetic
672
820
  * @drawable false
821
+ * @example
822
+ * ```typescript
823
+ * const value = bitbybit.math.evalArithmetic({ expression: "(3 + 2) * 4" });
824
+ * ```
673
825
  */
674
826
  evalArithmetic(inputs) {
675
827
  var _a;