@sachitv/safe-math-ts 0.1.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.
@@ -0,0 +1,350 @@
1
+ declare const quantityBrand: unique symbol;
2
+ declare const unitTagBrand: unique symbol;
3
+ declare const unitExprBrand: unique symbol;
4
+ type Count = readonly unknown[];
5
+ type UnitExponent<Numerator extends Count = Count, Denominator extends Count = Count> = {
6
+ readonly num: readonly [...Numerator];
7
+ readonly den: readonly [...Denominator];
8
+ };
9
+ type UnitMap = {
10
+ readonly [Token: string]: UnitExponent;
11
+ };
12
+ type EmptyUnitMap = Record<never, UnitExponent>;
13
+ /**
14
+ * Internal normalized unit representation used only at the type level.
15
+ *
16
+ * Examples:
17
+ * - `m/s^2` => `{ factors: { m: { num: [..1], den: [] }, s: { num: [], den: [..2] } } }`
18
+ * - `none` => `{ factors: {} }`
19
+ */
20
+ type UnitParts<Factors extends UnitMap = EmptyUnitMap> = {
21
+ readonly factors: Factors;
22
+ readonly [unitExprBrand]: Extract<keyof Factors, string>;
23
+ };
24
+ /**
25
+ * Fallback shape when a unit cannot be fully resolved at compile time.
26
+ *
27
+ * If unit string parsing fails silently (e.g. malformed tokens), the result
28
+ * is `UnknownUnit`, which is structurally identical for all failed parses and
29
+ * loses type distinctiveness. Avoid passing non-literal or malformed strings
30
+ * to `unit()`.
31
+ */
32
+ type UnknownUnit = UnitParts<UnitMap>;
33
+ /** Normalized compile-time unit representation. */
34
+ export type UnitExpr = UnknownUnit;
35
+ /** Canonical dimensionless unit representation. */
36
+ export type Dimensionless = UnitFromString<'none'>;
37
+ /** Prevents undesired generic widening in function signatures. */
38
+ export type NoInfer<ValueType> = [
39
+ ValueType
40
+ ][ValueType extends unknown ? 0 : never];
41
+ type ZeroExponent = UnitExponent<[], []>;
42
+ type IsUnknownMap<Factors extends UnitMap> = string extends keyof Factors ? true : false;
43
+ type CancelCounts<Numerator extends Count, Denominator extends Count> = Numerator extends readonly [unknown, ...infer NumTail extends unknown[]] ? Denominator extends readonly [unknown, ...infer DenTail extends unknown[]] ? CancelCounts<NumTail, DenTail> : [Numerator, Denominator] : [Numerator, Denominator];
44
+ type ExponentFor<Factors extends UnitMap, Token extends string> = Token extends keyof Factors ? Factors[Token] : ZeroExponent;
45
+ type IsZeroExponent<Exponent extends UnitExponent> = Exponent['num'] extends readonly [] ? Exponent['den'] extends readonly [] ? true : false : false;
46
+ type NormalizeMap<Factors extends UnitMap> = {
47
+ readonly [Token in keyof Factors as Token extends string ? IsZeroExponent<Factors[Token]> extends true ? never : Token : never]: Factors[Token];
48
+ };
49
+ /** Normalizes a full `UnitExpr` object. */
50
+ type NormalizeUnit<Unit extends UnitExpr> = IsUnknownMap<Unit['factors']> extends true ? UnknownUnit : UnitParts<NormalizeMap<Unit['factors']>>;
51
+ type IsNaturalNumberText<Value extends string> = Value extends '' ? false : Value extends `-${string}` ? false : Value extends `${string}.${string}` ? false : Value extends `${string}e${string}` | `${string}E${string}` ? false : true;
52
+ /**
53
+ * Parses a natural-number literal string (`'2'`) into its number literal (`2`).
54
+ *
55
+ * Returns `null` for negative/decimal/exponential forms so callers can
56
+ * safely fall back to opaque token handling for invalid exponents.
57
+ */
58
+ type ParseNat<Value extends string> = IsNaturalNumberText<Value> extends true ? Value extends `${infer Parsed extends number}` ? `${Parsed}` extends Value ? Parsed : null : null : null;
59
+ /** Builds `Count` tuple entries (`s^2` -> [unknown, unknown]). */
60
+ type RepeatCount<Count extends number, Output extends unknown[] = []> = Output['length'] extends Count ? Output : RepeatCount<Count, [...Output, unknown]>;
61
+ /**
62
+ * Parses one factor token:
63
+ * - `none` => dimensionless
64
+ * - `m` => m^1
65
+ * - `s^2` => s^2
66
+ */
67
+ type ParseFactor<Factor extends string> = string extends Factor ? UnknownUnit : Factor extends '' ? UnknownUnit : Factor extends 'none' ? UnitParts<EmptyUnitMap> : Factor extends `${infer Base}^${infer ExponentText}` ? Base extends '' ? UnknownUnit : ParseNat<ExponentText> extends infer Exponent extends number ? UnitParts<{
68
+ readonly [Token in Base]: UnitExponent<RepeatCount<Exponent>, []>;
69
+ }> : UnitParts<{
70
+ readonly [Token in Factor]: UnitExponent<[unknown], []>;
71
+ }> : UnitParts<{
72
+ readonly [Token in Factor]: UnitExponent<[unknown], []>;
73
+ }>;
74
+ /**
75
+ * Splits an expression into `[term, operator, rest]`.
76
+ *
77
+ * Example:
78
+ * - `m/s^2` -> ['m', '/', 's^2']
79
+ */
80
+ type TakeTerm<Source extends string, Current extends string = ''> = string extends Source ? [Source, '', ''] : Source extends `${infer Character}${infer Rest}` ? Character extends '*' | '/' ? [Current, Character, Rest] : TakeTerm<Rest, `${Current}${Character}`> : [Current, '', ''];
81
+ /** Type-level unit multiplication helper. */
82
+ export type MulUnit<LeftUnit extends UnitExpr, RightUnit extends UnitExpr> = IsUnknownMap<LeftUnit['factors']> extends true ? UnknownUnit : IsUnknownMap<RightUnit['factors']> extends true ? UnknownUnit : UnitParts<NormalizeMap<{
83
+ readonly [Token in keyof LeftUnit['factors'] | keyof RightUnit['factors'] as Token extends string ? Token : never]: CancelCounts<[
84
+ ...ExponentFor<LeftUnit['factors'], Token & string>['num'],
85
+ ...ExponentFor<RightUnit['factors'], Token & string>['num']
86
+ ], [
87
+ ...ExponentFor<LeftUnit['factors'], Token & string>['den'],
88
+ ...ExponentFor<RightUnit['factors'], Token & string>['den']
89
+ ]> extends [
90
+ infer Numerator extends Count,
91
+ infer Denominator extends Count
92
+ ] ? UnitExponent<Numerator, Denominator> : never;
93
+ }>>;
94
+ /** Type-level unit division helper. */
95
+ export type DivUnit<LeftUnit extends UnitExpr, RightUnit extends UnitExpr> = IsUnknownMap<LeftUnit['factors']> extends true ? UnknownUnit : IsUnknownMap<RightUnit['factors']> extends true ? UnknownUnit : UnitParts<NormalizeMap<{
96
+ readonly [Token in keyof LeftUnit['factors'] | keyof RightUnit['factors'] as Token extends string ? Token : never]: CancelCounts<[
97
+ ...ExponentFor<LeftUnit['factors'], Token & string>['num'],
98
+ ...ExponentFor<RightUnit['factors'], Token & string>['den']
99
+ ], [
100
+ ...ExponentFor<LeftUnit['factors'], Token & string>['den'],
101
+ ...ExponentFor<RightUnit['factors'], Token & string>['num']
102
+ ]> extends [
103
+ infer Numerator extends Count,
104
+ infer Denominator extends Count
105
+ ] ? UnitExponent<Numerator, Denominator> : never;
106
+ }>>;
107
+ type HalfCount<Source extends Count, Output extends unknown[] = []> = Source extends readonly [unknown, unknown, ...infer Tail extends unknown[]] ? HalfCount<Tail, [...Output, unknown]> : Source extends readonly [] ? Output : never;
108
+ type HalfFactor<Factor extends UnitExponent> = UnitExponent<HalfCount<Factor['num']>, HalfCount<Factor['den']>>;
109
+ type HasInvalidHalf<Factors extends UnitMap> = true extends {
110
+ [Token in keyof Factors]: HalfCount<Factors[Token]['num']> extends never ? true : HalfCount<Factors[Token]['den']> extends never ? true : false;
111
+ }[keyof Factors] ? true : false;
112
+ /** Type-level square-root helper for normalized unit expressions. */
113
+ export type SqrtUnit<Unit extends UnitExpr> = NormalizeUnit<Unit> extends infer Normalized extends UnitExpr ? IsUnknownMap<Normalized['factors']> extends true ? never : HasInvalidHalf<Normalized['factors']> extends true ? never : UnitParts<NormalizeMap<{
114
+ readonly [Token in keyof Normalized['factors']]: HalfFactor<Normalized['factors'][Token]>;
115
+ }>> : never;
116
+ /**
117
+ * Recursive parser:
118
+ * 1. Parse the next factor
119
+ * 2. Apply pending operator to accumulated unit
120
+ * 3. Continue until no operator remains
121
+ */
122
+ type ParseUnitTail<Accumulated extends UnitExpr, Operator extends string, Rest extends string> = Operator extends '' ? NormalizeUnit<Accumulated> : TakeTerm<Rest> extends [
123
+ infer NextFactor extends string,
124
+ infer NextOperator extends string,
125
+ infer NextRest extends string
126
+ ] ? ParseFactor<NextFactor> extends infer NextUnit extends UnitExpr ? ParseUnitTail<Operator extends '*' ? MulUnit<Accumulated, NextUnit> : Operator extends '/' ? DivUnit<Accumulated, NextUnit> : Accumulated, NextOperator, NextRest> : never : never;
127
+ /** Parses the first factor, then delegates to `ParseUnitTail`. */
128
+ type ParseUnitExpr<Expr extends string> = TakeTerm<Expr> extends [
129
+ infer FirstFactor extends string,
130
+ infer FirstOperator extends string,
131
+ infer Rest extends string
132
+ ] ? ParseFactor<FirstFactor> extends infer FirstUnit extends UnitExpr ? ParseUnitTail<FirstUnit, FirstOperator, Rest> : never : never;
133
+ /** Converts a unit expression string into a normalized compile-time unit type. */
134
+ export type UnitFromString<Expr extends string> = ParseUnitExpr<Expr>;
135
+ /** Compile-time token for explicitly declaring units. */
136
+ export type UnitTag<Unit extends UnitExpr> = string & {
137
+ readonly [unitTagBrand]: Unit;
138
+ };
139
+ /**
140
+ * Unit-branded scalar quantity.
141
+ *
142
+ * Runtime representation is a plain number.
143
+ */
144
+ export type Quantity<Unit extends UnitExpr> = number & {
145
+ readonly [quantityBrand]: Unit;
146
+ };
147
+ /**
148
+ * Creates a compile-time unit token.
149
+ *
150
+ * @param name Unit expression text (for example `m/s^2` or `none`).
151
+ * @returns Branded token used to construct unit-safe quantities.
152
+ */
153
+ export declare const unit: <Expr extends string>(name: string extends Expr ? never : Expr) => UnitTag<UnitFromString<Expr>>;
154
+ /** Shared compile-time token for the `none` (dimensionless) unit. */
155
+ export declare const dimensionlessUnit: UnitTag<Dimensionless>;
156
+ /**
157
+ * Creates a quantity from an explicit unit token and numeric value.
158
+ *
159
+ * @param unitTag Compile-time unit token.
160
+ * @param value Raw numeric value.
161
+ * @returns Branded unit-safe quantity.
162
+ */
163
+ export declare const quantity: <Unit extends UnitExpr>(unitTag: UnitTag<Unit>, value: number) => Quantity<Unit>;
164
+ /**
165
+ * Creates a dimensionless quantity.
166
+ *
167
+ * @param value Raw numeric value.
168
+ * @returns Quantity branded as `none`.
169
+ */
170
+ export declare const dimensionless: (value: number) => Quantity<Dimensionless>;
171
+ /**
172
+ * Unwraps a quantity to its raw numeric value.
173
+ *
174
+ * @param value Branded quantity.
175
+ * @returns Underlying runtime number.
176
+ */
177
+ export declare const valueOf: <Unit extends UnitExpr>(value: Quantity<Unit>) => number;
178
+ /**
179
+ * Adds two same-unit quantities.
180
+ *
181
+ * @param left Left operand.
182
+ * @param right Right operand with the same unit as `left`.
183
+ * @returns Sum in the same unit.
184
+ */
185
+ export declare const add: <Unit extends UnitExpr>(left: Quantity<Unit>, right: Quantity<NoInfer<Unit>>) => Quantity<Unit>;
186
+ /**
187
+ * Subtracts two same-unit quantities.
188
+ *
189
+ * @param left Left operand.
190
+ * @param right Right operand with the same unit as `left`.
191
+ * @returns Difference in the same unit.
192
+ */
193
+ export declare const sub: <Unit extends UnitExpr>(left: Quantity<Unit>, right: Quantity<NoInfer<Unit>>) => Quantity<Unit>;
194
+ /**
195
+ * Negates a quantity.
196
+ *
197
+ * @param value Input quantity.
198
+ * @returns Quantity with sign flipped.
199
+ */
200
+ export declare const neg: <Unit extends UnitExpr>(value: Quantity<Unit>) => Quantity<Unit>;
201
+ /**
202
+ * Computes absolute value of a quantity.
203
+ *
204
+ * @param value Input quantity.
205
+ * @returns Non-negative quantity in the same unit.
206
+ */
207
+ export declare const abs: <Unit extends UnitExpr>(value: Quantity<Unit>) => Quantity<Unit>;
208
+ /**
209
+ * Returns the smaller of two same-unit quantities.
210
+ *
211
+ * @param left Left operand.
212
+ * @param right Right operand with the same unit as `left`.
213
+ * @returns Smaller quantity.
214
+ */
215
+ export declare const min: <Unit extends UnitExpr>(left: Quantity<Unit>, right: Quantity<NoInfer<Unit>>) => Quantity<Unit>;
216
+ /**
217
+ * Returns the larger of two same-unit quantities.
218
+ *
219
+ * @param left Left operand.
220
+ * @param right Right operand with the same unit as `left`.
221
+ * @returns Larger quantity.
222
+ */
223
+ export declare const max: <Unit extends UnitExpr>(left: Quantity<Unit>, right: Quantity<NoInfer<Unit>>) => Quantity<Unit>;
224
+ /**
225
+ * Clamps a quantity to the inclusive range [`minValue`, `maxValue`].
226
+ *
227
+ * Unsafe variant: performs no bound-order validation.
228
+ *
229
+ * @param value Value to clamp.
230
+ * @param minValue Inclusive lower bound.
231
+ * @param maxValue Inclusive upper bound.
232
+ * @returns Clamped quantity.
233
+ */
234
+ export declare const clampUnsafe: <Unit extends UnitExpr>(value: Quantity<Unit>, minValue: Quantity<NoInfer<Unit>>, maxValue: Quantity<NoInfer<Unit>>) => Quantity<Unit>;
235
+ /**
236
+ * Clamps a quantity to the inclusive range [`minValue`, `maxValue`].
237
+ *
238
+ * Throws when `minValue > maxValue`.
239
+ *
240
+ * @param value Value to clamp.
241
+ * @param minValue Inclusive lower bound.
242
+ * @param maxValue Inclusive upper bound.
243
+ * @returns Clamped quantity.
244
+ * @throws {Error} When `minValue > maxValue`.
245
+ */
246
+ export declare const clamp: <Unit extends UnitExpr>(value: Quantity<Unit>, minValue: Quantity<NoInfer<Unit>>, maxValue: Quantity<NoInfer<Unit>>) => Quantity<Unit>;
247
+ /**
248
+ * Multiplies a quantity by a unitless scalar.
249
+ *
250
+ * @param value Input quantity.
251
+ * @param scalar Unitless scale factor.
252
+ * @returns Scaled quantity in the same unit.
253
+ */
254
+ export declare const scale: <Unit extends UnitExpr>(value: Quantity<Unit>, scalar: number) => Quantity<Unit>;
255
+ /**
256
+ * Multiplies two quantities and composes normalized unit expressions.
257
+ *
258
+ * @param left Left quantity.
259
+ * @param right Right quantity.
260
+ * @returns Product with type-level multiplied unit.
261
+ */
262
+ export declare const mul: <LeftUnit extends UnitExpr, RightUnit extends UnitExpr>(left: Quantity<LeftUnit>, right: Quantity<RightUnit>) => Quantity<MulUnit<LeftUnit, RightUnit>>;
263
+ /**
264
+ * Divides two quantities and composes normalized unit expressions.
265
+ *
266
+ * @param left Numerator quantity.
267
+ * @param right Denominator quantity.
268
+ * @returns Quotient with type-level divided unit.
269
+ */
270
+ export declare const div: <LeftUnit extends UnitExpr, RightUnit extends UnitExpr>(left: Quantity<LeftUnit>, right: Quantity<RightUnit>) => Quantity<DivUnit<LeftUnit, RightUnit>>;
271
+ /**
272
+ * Computes square root of a quantity.
273
+ *
274
+ * @param value Quantity whose unit can be square-rooted at compile time.
275
+ * @returns Square root with inferred root unit.
276
+ */
277
+ export declare function sqrt<Unit extends UnitExpr>(value: [SqrtUnit<Unit>] extends [never] ? never : Quantity<Unit>): Quantity<SqrtUnit<Unit>>;
278
+ /**
279
+ * Strict equality (`===`) for same-unit quantities.
280
+ *
281
+ * This uses exact comparison and does **not** account for floating-point
282
+ * rounding error. After arithmetic (e.g. `add`, `sub`, `mul`) the result
283
+ * may differ from the mathematically expected value by a small epsilon.
284
+ * Use `approxEq` when comparing quantities that have been through arithmetic.
285
+ *
286
+ * @param left Left operand.
287
+ * @param right Right operand with the same unit as `left`.
288
+ * @returns `true` only when values are exactly equal.
289
+ */
290
+ export declare const eq: <Unit extends UnitExpr>(left: Quantity<Unit>, right: Quantity<NoInfer<Unit>>) => boolean;
291
+ /**
292
+ * Approximate equality for same-unit quantities.
293
+ *
294
+ * Returns `true` when the absolute difference is within `tolerance`
295
+ * (default `1e-10`).
296
+ *
297
+ * @param left Left operand.
298
+ * @param right Right operand with the same unit as `left`.
299
+ * @param tolerance Maximum absolute difference allowed.
300
+ * @returns `true` when values are approximately equal.
301
+ */
302
+ export declare const approxEq: <Unit extends UnitExpr>(left: Quantity<Unit>, right: Quantity<NoInfer<Unit>>, tolerance?: number) => boolean;
303
+ /**
304
+ * Less-than comparison for same-unit quantities.
305
+ *
306
+ * @param left Left operand.
307
+ * @param right Right operand with the same unit as `left`.
308
+ * @returns `true` when `left < right`.
309
+ */
310
+ export declare const lt: <Unit extends UnitExpr>(left: Quantity<Unit>, right: Quantity<NoInfer<Unit>>) => boolean;
311
+ /**
312
+ * Less-than-or-equal comparison for same-unit quantities.
313
+ *
314
+ * @param left Left operand.
315
+ * @param right Right operand with the same unit as `left`.
316
+ * @returns `true` when `left <= right`.
317
+ */
318
+ export declare const lte: <Unit extends UnitExpr>(left: Quantity<Unit>, right: Quantity<NoInfer<Unit>>) => boolean;
319
+ /**
320
+ * Greater-than comparison for same-unit quantities.
321
+ *
322
+ * @param left Left operand.
323
+ * @param right Right operand with the same unit as `left`.
324
+ * @returns `true` when `left > right`.
325
+ */
326
+ export declare const gt: <Unit extends UnitExpr>(left: Quantity<Unit>, right: Quantity<NoInfer<Unit>>) => boolean;
327
+ /**
328
+ * Greater-than-or-equal comparison for same-unit quantities.
329
+ *
330
+ * @param left Left operand.
331
+ * @param right Right operand with the same unit as `left`.
332
+ * @returns `true` when `left >= right`.
333
+ */
334
+ export declare const gte: <Unit extends UnitExpr>(left: Quantity<Unit>, right: Quantity<NoInfer<Unit>>) => boolean;
335
+ /**
336
+ * Sums a list of same-unit quantities.
337
+ *
338
+ * @param values Input quantities with identical units.
339
+ * @returns Total sum in the same unit.
340
+ */
341
+ export declare const sum: <Unit extends UnitExpr>(values: readonly Quantity<Unit>[]) => Quantity<Unit>;
342
+ /**
343
+ * Computes average of a non-empty list of same-unit quantities.
344
+ *
345
+ * @param values Non-empty input list.
346
+ * @returns Arithmetic mean in the same unit.
347
+ */
348
+ export declare const average: <Unit extends UnitExpr>(values: readonly [Quantity<Unit>, ...Quantity<Unit>[]]) => Quantity<Unit>;
349
+ export {};
350
+ //# sourceMappingURL=units.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"units.d.ts","sourceRoot":"","sources":["../../src/src/units.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,aAAa,EAAE,OAAO,MAAM,CAAC;AAC3C,OAAO,CAAC,MAAM,YAAY,EAAE,OAAO,MAAM,CAAC;AAC1C,OAAO,CAAC,MAAM,aAAa,EAAE,OAAO,MAAM,CAAC;AAE3C,KAAK,KAAK,GAAG,SAAS,OAAO,EAAE,CAAC;AAEhC,KAAK,YAAY,CACf,SAAS,SAAS,KAAK,GAAG,KAAK,EAC/B,WAAW,SAAS,KAAK,GAAG,KAAK,IAC/B;IACF,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC,GAAG,SAAS,CAAC,CAAC;IACtC,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC,GAAG,WAAW,C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,CAAC,IAAI,CAAC,EACpB,OAAO,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,KAC7B,OAAwB,CAAC;AAE5B;;;;;;GAMG;AACH,eAAO,MAAM,EAAE,GAAI,IAAI,SAAS,QAAQ,EACtC,MAAM,QAAQ,CAAC,IAAI,CAAC,EACpB,OAAO,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,KAC7B,OAAuB,CAAC;AAE3B;;;;;;GAMG;AACH,eAAO,MAAM,GAAG,GAAI,IAAI,SAAS,QAAQ,EACvC,MAAM,QAAQ,CAAC,IAAI,CAAC,EACpB,OAAO,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,KAC7B,OAAwB,CAAC;AAE5B;;;;;GAKG;AACH,eAAO,MAAM,GAAG,GAAI,IAAI,SAAS,QAAQ,EACvC,QAAQ,SAAS,QAAQ,CAAC,IAAI,CAAC,EAAE,KAChC,QAAQ,CAAC,IAAI,CAOf,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,OAAO,GAAI,IAAI,SAAS,QAAQ,EAC3C,QAAQ,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,GAAG,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,KACrD,QAAQ,CAAC,IAAI,CAAkD,CAAC"}
@@ -0,0 +1,219 @@
1
+ /**
2
+ * Casts a raw number into a branded quantity.
3
+ *
4
+ * @param value Raw numeric scalar.
5
+ * @returns Branded quantity of the requested unit type.
6
+ */
7
+ const asQuantity = (value) => value;
8
+ /**
9
+ * Creates a compile-time unit token.
10
+ *
11
+ * @param name Unit expression text (for example `m/s^2` or `none`).
12
+ * @returns Branded token used to construct unit-safe quantities.
13
+ */
14
+ export const unit = (name) => name;
15
+ /** Shared compile-time token for the `none` (dimensionless) unit. */
16
+ export const dimensionlessUnit = unit('none');
17
+ /**
18
+ * Creates a quantity from an explicit unit token and numeric value.
19
+ *
20
+ * @param unitTag Compile-time unit token.
21
+ * @param value Raw numeric value.
22
+ * @returns Branded unit-safe quantity.
23
+ */
24
+ export const quantity = (unitTag, value) => {
25
+ void unitTag;
26
+ return asQuantity(value);
27
+ };
28
+ /**
29
+ * Creates a dimensionless quantity.
30
+ *
31
+ * @param value Raw numeric value.
32
+ * @returns Quantity branded as `none`.
33
+ */
34
+ export const dimensionless = (value) => asQuantity(value);
35
+ /**
36
+ * Unwraps a quantity to its raw numeric value.
37
+ *
38
+ * @param value Branded quantity.
39
+ * @returns Underlying runtime number.
40
+ */
41
+ export const valueOf = (value) => value;
42
+ /**
43
+ * Adds two same-unit quantities.
44
+ *
45
+ * @param left Left operand.
46
+ * @param right Right operand with the same unit as `left`.
47
+ * @returns Sum in the same unit.
48
+ */
49
+ export const add = (left, right) => asQuantity(left + right);
50
+ /**
51
+ * Subtracts two same-unit quantities.
52
+ *
53
+ * @param left Left operand.
54
+ * @param right Right operand with the same unit as `left`.
55
+ * @returns Difference in the same unit.
56
+ */
57
+ export const sub = (left, right) => asQuantity(left - right);
58
+ /**
59
+ * Negates a quantity.
60
+ *
61
+ * @param value Input quantity.
62
+ * @returns Quantity with sign flipped.
63
+ */
64
+ export const neg = (value) => asQuantity(-value);
65
+ /**
66
+ * Computes absolute value of a quantity.
67
+ *
68
+ * @param value Input quantity.
69
+ * @returns Non-negative quantity in the same unit.
70
+ */
71
+ export const abs = (value) => asQuantity(Math.abs(value));
72
+ /**
73
+ * Returns the smaller of two same-unit quantities.
74
+ *
75
+ * @param left Left operand.
76
+ * @param right Right operand with the same unit as `left`.
77
+ * @returns Smaller quantity.
78
+ */
79
+ export const min = (left, right) => asQuantity(Math.min(left, right));
80
+ /**
81
+ * Returns the larger of two same-unit quantities.
82
+ *
83
+ * @param left Left operand.
84
+ * @param right Right operand with the same unit as `left`.
85
+ * @returns Larger quantity.
86
+ */
87
+ export const max = (left, right) => asQuantity(Math.max(left, right));
88
+ /**
89
+ * Clamps a quantity to the inclusive range [`minValue`, `maxValue`].
90
+ *
91
+ * Unsafe variant: performs no bound-order validation.
92
+ *
93
+ * @param value Value to clamp.
94
+ * @param minValue Inclusive lower bound.
95
+ * @param maxValue Inclusive upper bound.
96
+ * @returns Clamped quantity.
97
+ */
98
+ export const clampUnsafe = (value, minValue, maxValue) => max(minValue, min(value, maxValue));
99
+ /**
100
+ * Clamps a quantity to the inclusive range [`minValue`, `maxValue`].
101
+ *
102
+ * Throws when `minValue > maxValue`.
103
+ *
104
+ * @param value Value to clamp.
105
+ * @param minValue Inclusive lower bound.
106
+ * @param maxValue Inclusive upper bound.
107
+ * @returns Clamped quantity.
108
+ * @throws {Error} When `minValue > maxValue`.
109
+ */
110
+ export const clamp = (value, minValue, maxValue) => {
111
+ if (minValue > maxValue) {
112
+ throw new Error('minValue must be <= maxValue');
113
+ }
114
+ return clampUnsafe(value, minValue, maxValue);
115
+ };
116
+ /**
117
+ * Multiplies a quantity by a unitless scalar.
118
+ *
119
+ * @param value Input quantity.
120
+ * @param scalar Unitless scale factor.
121
+ * @returns Scaled quantity in the same unit.
122
+ */
123
+ export const scale = (value, scalar) => asQuantity(value * scalar);
124
+ /**
125
+ * Multiplies two quantities and composes normalized unit expressions.
126
+ *
127
+ * @param left Left quantity.
128
+ * @param right Right quantity.
129
+ * @returns Product with type-level multiplied unit.
130
+ */
131
+ export const mul = (left, right) => asQuantity(left * right);
132
+ /**
133
+ * Divides two quantities and composes normalized unit expressions.
134
+ *
135
+ * @param left Numerator quantity.
136
+ * @param right Denominator quantity.
137
+ * @returns Quotient with type-level divided unit.
138
+ */
139
+ export const div = (left, right) => asQuantity(left / right);
140
+ export function sqrt(value) {
141
+ return asQuantity(Math.sqrt(value));
142
+ }
143
+ /**
144
+ * Strict equality (`===`) for same-unit quantities.
145
+ *
146
+ * This uses exact comparison and does **not** account for floating-point
147
+ * rounding error. After arithmetic (e.g. `add`, `sub`, `mul`) the result
148
+ * may differ from the mathematically expected value by a small epsilon.
149
+ * Use `approxEq` when comparing quantities that have been through arithmetic.
150
+ *
151
+ * @param left Left operand.
152
+ * @param right Right operand with the same unit as `left`.
153
+ * @returns `true` only when values are exactly equal.
154
+ */
155
+ export const eq = (left, right) => left === right;
156
+ /**
157
+ * Approximate equality for same-unit quantities.
158
+ *
159
+ * Returns `true` when the absolute difference is within `tolerance`
160
+ * (default `1e-10`).
161
+ *
162
+ * @param left Left operand.
163
+ * @param right Right operand with the same unit as `left`.
164
+ * @param tolerance Maximum absolute difference allowed.
165
+ * @returns `true` when values are approximately equal.
166
+ */
167
+ export const approxEq = (left, right, tolerance = 1e-10) => Math.abs(left - right) <= tolerance;
168
+ /**
169
+ * Less-than comparison for same-unit quantities.
170
+ *
171
+ * @param left Left operand.
172
+ * @param right Right operand with the same unit as `left`.
173
+ * @returns `true` when `left < right`.
174
+ */
175
+ export const lt = (left, right) => left < right;
176
+ /**
177
+ * Less-than-or-equal comparison for same-unit quantities.
178
+ *
179
+ * @param left Left operand.
180
+ * @param right Right operand with the same unit as `left`.
181
+ * @returns `true` when `left <= right`.
182
+ */
183
+ export const lte = (left, right) => left <= right;
184
+ /**
185
+ * Greater-than comparison for same-unit quantities.
186
+ *
187
+ * @param left Left operand.
188
+ * @param right Right operand with the same unit as `left`.
189
+ * @returns `true` when `left > right`.
190
+ */
191
+ export const gt = (left, right) => left > right;
192
+ /**
193
+ * Greater-than-or-equal comparison for same-unit quantities.
194
+ *
195
+ * @param left Left operand.
196
+ * @param right Right operand with the same unit as `left`.
197
+ * @returns `true` when `left >= right`.
198
+ */
199
+ export const gte = (left, right) => left >= right;
200
+ /**
201
+ * Sums a list of same-unit quantities.
202
+ *
203
+ * @param values Input quantities with identical units.
204
+ * @returns Total sum in the same unit.
205
+ */
206
+ export const sum = (values) => {
207
+ let total = 0;
208
+ for (const value of values) {
209
+ total += value;
210
+ }
211
+ return asQuantity(total);
212
+ };
213
+ /**
214
+ * Computes average of a non-empty list of same-unit quantities.
215
+ *
216
+ * @param values Non-empty input list.
217
+ * @returns Arithmetic mean in the same unit.
218
+ */
219
+ export const average = (values) => asQuantity(sum(values) / values.length);
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@sachitv/safe-math-ts",
3
+ "version": "0.1.0",
4
+ "description": "A zero-dependency Deno/TypeScript 3D math library with strict compile-time safety.",
5
+ "homepage": "https://github.com/sachitv/safe-math-ts",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/sachitv/safe-math-ts.git"
9
+ },
10
+ "license": "MIT",
11
+ "bugs": {
12
+ "url": "https://github.com/sachitv/safe-math-ts/issues"
13
+ },
14
+ "module": "./esm/mod.js",
15
+ "exports": {
16
+ ".": {
17
+ "import": "./esm/mod.js",
18
+ "types": "./esm/mod.d.ts"
19
+ }
20
+ },
21
+ "scripts": {},
22
+ "engines": {
23
+ "node": ">=22"
24
+ },
25
+ "sideEffects": false,
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "type": "module",
30
+ "devDependencies": {
31
+ "@types/node": "^20.9.0"
32
+ },
33
+ "_generatedBy": "dnt@dev"
34
+ }