fashionable 0.0.0 → 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.
- package/LICENSE.md +21 -0
- package/dist/calc/calc.d.ts +425 -0
- package/dist/calc/calc.internal.d.ts +2 -0
- package/dist/calc/index.d.ts +6 -0
- package/dist/calc/index.mjs +335 -0
- package/dist/calc/index.mjs.map +1 -0
- package/dist/calc/precision.d.ts +72 -0
- package/dist/calc/precision.internal.d.ts +2 -0
- package/dist/color/color.d.ts +121 -0
- package/dist/color/color.internal.d.ts +2 -0
- package/dist/color/index.d.ts +3 -0
- package/dist/color/index.mjs +73 -0
- package/dist/color/index.mjs.map +1 -0
- package/dist/declaration/declaration.d.ts +159 -0
- package/dist/declaration/declaration.internal.d.ts +2 -0
- package/dist/declaration/index.d.ts +3 -0
- package/dist/declaration/index.mjs +74 -0
- package/dist/declaration/index.mjs.map +1 -0
- package/dist/fontFace/fontFaceRule.d.ts +257 -0
- package/dist/fontFace/fontFaceRule.internal.d.ts +4 -0
- package/dist/fontFace/index.d.ts +3 -0
- package/dist/fontFace/index.mjs +100 -0
- package/dist/fontFace/index.mjs.map +1 -0
- package/dist/internal/equal.d.ts +13 -0
- package/dist/internal/format.d.ts +6 -0
- package/dist/internal/refs.d.ts +8 -0
- package/dist/internal/render.d.ts +6 -0
- package/dist/property/index.d.ts +6 -0
- package/dist/property/index.mjs +289 -0
- package/dist/property/index.mjs.map +1 -0
- package/dist/property/propertyRule.d.ts +174 -0
- package/dist/property/propertyRule.internal.d.ts +2 -0
- package/dist/property/propertySyntax.d.ts +282 -0
- package/dist/property/propertySyntax.internal.d.ts +2 -0
- package/dist/query/index.d.ts +3 -0
- package/dist/query/index.mjs +61 -0
- package/dist/query/index.mjs.map +1 -0
- package/dist/query/mediaQuery.d.ts +134 -0
- package/dist/query/mediaQuery.internal.d.ts +2 -0
- package/dist/rule/index.d.ts +9 -0
- package/dist/rule/index.mjs +217 -0
- package/dist/rule/index.mjs.map +1 -0
- package/dist/rule/mediaRule.d.ts +115 -0
- package/dist/rule/mediaRule.internal.d.ts +2 -0
- package/dist/rule/rule.internal.d.ts +13 -0
- package/dist/rule/ruleSet.d.ts +249 -0
- package/dist/rule/ruleSet.internal.d.ts +2 -0
- package/dist/rule/styleRule.d.ts +110 -0
- package/dist/rule/styleRule.internal.d.ts +2 -0
- package/dist/selector/index.d.ts +6 -0
- package/dist/selector/index.mjs +178 -0
- package/dist/selector/index.mjs.map +1 -0
- package/dist/selector/selector.d.ts +243 -0
- package/dist/selector/selector.internal.d.ts +2 -0
- package/dist/selector/specificity.d.ts +92 -0
- package/dist/selector/specificity.internal.d.ts +2 -0
- package/dist/shared/calc.internal-C1g_NRsl.mjs +568 -0
- package/dist/shared/calc.internal-C1g_NRsl.mjs.map +1 -0
- package/dist/shared/color.internal-D7P91Rvv.mjs +100 -0
- package/dist/shared/color.internal-D7P91Rvv.mjs.map +1 -0
- package/dist/shared/declaration.internal-DvZknsDc.mjs +73 -0
- package/dist/shared/declaration.internal-DvZknsDc.mjs.map +1 -0
- package/dist/shared/equal-C96LR7p4.mjs +83 -0
- package/dist/shared/equal-C96LR7p4.mjs.map +1 -0
- package/dist/shared/fontFaceRule.internal-CgnEJbNS.mjs +173 -0
- package/dist/shared/fontFaceRule.internal-CgnEJbNS.mjs.map +1 -0
- package/dist/shared/format-B9jsmCER.mjs +53 -0
- package/dist/shared/format-B9jsmCER.mjs.map +1 -0
- package/dist/shared/mediaQuery.internal-BZF1beZO.mjs +100 -0
- package/dist/shared/mediaQuery.internal-BZF1beZO.mjs.map +1 -0
- package/dist/shared/propertyRule.internal-BY1ghubC.mjs +246 -0
- package/dist/shared/propertyRule.internal-BY1ghubC.mjs.map +1 -0
- package/dist/shared/render-BqcFH9Vr.mjs +9 -0
- package/dist/shared/render-BqcFH9Vr.mjs.map +1 -0
- package/dist/shared/ruleSet.internal-DQjyPgko.mjs +212 -0
- package/dist/shared/ruleSet.internal-DQjyPgko.mjs.map +1 -0
- package/dist/shared/selector.internal-BLQtVoIM.mjs +290 -0
- package/dist/shared/selector.internal-BLQtVoIM.mjs.map +1 -0
- package/dist/stylesheet/index.d.ts +3 -0
- package/dist/stylesheet/index.mjs +257 -0
- package/dist/stylesheet/index.mjs.map +1 -0
- package/dist/stylesheet/stylesheet.d.ts +275 -0
- package/dist/stylesheet/stylesheet.internal.d.ts +2 -0
- package/dist/utils.d.ts +176 -0
- package/dist/utils.mjs +131 -0
- package/dist/utils.mjs.map +1 -0
- package/package.json +96 -8
- package/index.js +0 -1
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tim Morris
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
import type { Pipeable } from '#util';
|
|
2
|
+
import type { CalcTypeId } from './calc.internal.ts';
|
|
3
|
+
import type { Precision } from './precision.ts';
|
|
4
|
+
declare const CalcRefs: unique symbol;
|
|
5
|
+
/**
|
|
6
|
+
* A CSS number expression: a tree of constants, unbound references, and
|
|
7
|
+
* math operations that can be solved to a number, serialized to CSS
|
|
8
|
+
* `calc()` text, or partially bound and passed along.
|
|
9
|
+
*
|
|
10
|
+
* The `Refs` parameter tracks the expression's unbound reference names at
|
|
11
|
+
* the type level. `ref('u')` is a `Calc<'u'>`; combining expressions
|
|
12
|
+
* unions their parameters; `bind` subtracts the names it binds. A fully
|
|
13
|
+
* bound (or constant) expression is a `Calc<never>`, which is what `solve`
|
|
14
|
+
* accepts without bindings.
|
|
15
|
+
*
|
|
16
|
+
* Values are immutable and structurally comparable via `equals`.
|
|
17
|
+
* Construction folds constant subtrees eagerly: `add(1, 2)` is the
|
|
18
|
+
* constant `3`, and binding every reference of a tree collapses it to a
|
|
19
|
+
* constant.
|
|
20
|
+
*
|
|
21
|
+
* An unbound reference serializes as `var(--name)` — the reference channel
|
|
22
|
+
* is the custom-property channel.
|
|
23
|
+
*
|
|
24
|
+
* Construct via `of`, `ref`, and the math combinators.
|
|
25
|
+
*
|
|
26
|
+
* @since 0.1.0
|
|
27
|
+
*/
|
|
28
|
+
export interface Calc<out Refs extends string = string> extends Pipeable {
|
|
29
|
+
readonly [CalcTypeId]: CalcTypeId;
|
|
30
|
+
readonly [CalcRefs]?: Refs;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* A value accepted where an expression is expected: an existing `Calc` or
|
|
34
|
+
* a bare number, which is coerced to an unannotated constant.
|
|
35
|
+
*
|
|
36
|
+
* @since 0.1.0
|
|
37
|
+
*/
|
|
38
|
+
export type Input<Refs extends string = string> = Calc<Refs> | number;
|
|
39
|
+
/**
|
|
40
|
+
* A bindings record: reference names to the values that replace them. A
|
|
41
|
+
* value may itself be an expression, whose own references join the result.
|
|
42
|
+
*
|
|
43
|
+
* @since 0.1.0
|
|
44
|
+
*/
|
|
45
|
+
export type Bindings<Refs extends string = string> = Record<Refs, Input>;
|
|
46
|
+
type ValueRefs<V> = V extends Calc<infer R> ? R : never;
|
|
47
|
+
type BindingRefs<T> = T extends Record<string, infer V> ? ValueRefs<V> : never;
|
|
48
|
+
/**
|
|
49
|
+
* The reference names remaining after applying the bindings `B` to an
|
|
50
|
+
* expression with references `Refs`: bound names are removed, and the
|
|
51
|
+
* references of any expression-valued bindings are added.
|
|
52
|
+
*
|
|
53
|
+
* @since 0.1.0
|
|
54
|
+
*/
|
|
55
|
+
export type ApplyBindings<Refs extends string, B> = Exclude<Refs, keyof B & string> | BindingRefs<Pick<B, Extract<keyof B, Refs>>>;
|
|
56
|
+
/**
|
|
57
|
+
* Options for `serialize`.
|
|
58
|
+
*
|
|
59
|
+
* @since 0.1.0
|
|
60
|
+
*/
|
|
61
|
+
export interface SerializeOptions<Refs extends string = string> {
|
|
62
|
+
/**
|
|
63
|
+
* Bindings applied before rendering. May be partial: references left
|
|
64
|
+
* unbound render as `var(--name)`.
|
|
65
|
+
*/
|
|
66
|
+
readonly bindings?: Partial<Bindings<Refs>>;
|
|
67
|
+
/**
|
|
68
|
+
* The precision for constants that carry no annotation of their own.
|
|
69
|
+
* Defaults to `Precision.decimals(5)`.
|
|
70
|
+
*/
|
|
71
|
+
readonly precision?: Precision;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Checks if a value is a `Calc`.
|
|
75
|
+
*
|
|
76
|
+
* True only for values built by this module's constructors, which carry
|
|
77
|
+
* the brand.
|
|
78
|
+
*
|
|
79
|
+
* @param u - The value to check.
|
|
80
|
+
* @returns `true` if the value is a `Calc`, `false` otherwise.
|
|
81
|
+
* @since 0.1.0
|
|
82
|
+
*/
|
|
83
|
+
export declare const isCalc: (u: unknown) => u is Calc<string>;
|
|
84
|
+
/**
|
|
85
|
+
* Creates a constant expression, optionally annotated with a serialization
|
|
86
|
+
* precision that overrides the context default wherever the constant lands
|
|
87
|
+
* — including in constants produced by folding it with others (the
|
|
88
|
+
* highest-fidelity operand annotation wins).
|
|
89
|
+
*
|
|
90
|
+
* Bare numbers passed to combinators become unannotated constants; `of`
|
|
91
|
+
* exists to name a value or pin its precision.
|
|
92
|
+
*
|
|
93
|
+
* @param value - The constant value. Must be finite.
|
|
94
|
+
* @param precision - Optional precision annotation.
|
|
95
|
+
* @returns A constant `Calc<never>`.
|
|
96
|
+
* @throws `Error` when `value` is not finite.
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* const k = Calc.of(0.8377580409572781, Precision.significant(10))
|
|
100
|
+
* Calc.serialize(Calc.multiply(k, Calc.ref('t'))) // 'calc(0.837758041 * var(--t))'
|
|
101
|
+
* ```
|
|
102
|
+
* @since 0.1.0
|
|
103
|
+
*/
|
|
104
|
+
export declare const of: (value: number, precision?: Precision) => Calc<never>;
|
|
105
|
+
/**
|
|
106
|
+
* Creates a reference to an unbound name. References serialize as
|
|
107
|
+
* `var(--name)` and are the channel through which expressions read CSS
|
|
108
|
+
* custom properties; `bind` replaces them with values or other
|
|
109
|
+
* expressions.
|
|
110
|
+
*
|
|
111
|
+
* Repeated calls with the same name return the same instance.
|
|
112
|
+
*
|
|
113
|
+
* @param name - The reference name, without the `--` prefix. Must be non-empty.
|
|
114
|
+
* @returns A `Calc` with `name` as its one unbound reference.
|
|
115
|
+
* @throws `Error` when `name` is empty.
|
|
116
|
+
* @example
|
|
117
|
+
* ```ts
|
|
118
|
+
* Calc.serialize(Calc.ref('width')) // 'var(--width)'
|
|
119
|
+
* ```
|
|
120
|
+
* @since 0.1.0
|
|
121
|
+
*/
|
|
122
|
+
export declare const ref: <Name extends string>(name: Name) => Calc<Name>;
|
|
123
|
+
/**
|
|
124
|
+
* Adds expressions. Constant operands fold at construction.
|
|
125
|
+
*
|
|
126
|
+
* Serializes as `a + b + ...` inside a `calc()` wrapper; negative constant
|
|
127
|
+
* terms (and products led by a negative constant) render subtractively:
|
|
128
|
+
* `a + (-2)` serializes as `a - 2`.
|
|
129
|
+
*
|
|
130
|
+
* @param a - The first operand.
|
|
131
|
+
* @param b - The second operand.
|
|
132
|
+
* @returns The sum, with the operands' references unioned.
|
|
133
|
+
* @example
|
|
134
|
+
* ```ts
|
|
135
|
+
* Calc.serialize(Calc.add(Calc.ref('x'), 10)) // 'calc(var(--x) + 10)'
|
|
136
|
+
* ```
|
|
137
|
+
* @since 0.1.0
|
|
138
|
+
*/
|
|
139
|
+
export declare const add: {
|
|
140
|
+
<A extends string = never, B extends string = never>(a: Input<A>, b: Input<B>): Calc<A | B>;
|
|
141
|
+
<A extends string = never, B extends string = never, C extends string = never>(a: Input<A>, b: Input<B>, c: Input<C>): Calc<A | B | C>;
|
|
142
|
+
<A extends string = never, B extends string = never, C extends string = never, D extends string = never>(a: Input<A>, b: Input<B>, c: Input<C>, d: Input<D>): Calc<A | B | C | D>;
|
|
143
|
+
(...args: readonly [Input, Input, ...ReadonlyArray<Input>]): Calc<string>;
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* Subtracts `right` from `left`. Constant operands fold at construction.
|
|
147
|
+
*
|
|
148
|
+
* @param left - The minuend.
|
|
149
|
+
* @param right - The subtrahend.
|
|
150
|
+
* @returns The difference, with the operands' references unioned.
|
|
151
|
+
* @since 0.1.0
|
|
152
|
+
*/
|
|
153
|
+
export declare const subtract: <A extends string = never, B extends string = never>(left: Input<A>, right: Input<B>) => Calc<A | B>;
|
|
154
|
+
/**
|
|
155
|
+
* Multiplies expressions. Constant operands fold at construction.
|
|
156
|
+
*
|
|
157
|
+
* Addition and subtraction operands are parenthesized when serialized
|
|
158
|
+
* under a product: `(a + b) * c`.
|
|
159
|
+
*
|
|
160
|
+
* @param left - The first factor.
|
|
161
|
+
* @param right - The second factor.
|
|
162
|
+
* @returns The product, with the operands' references unioned.
|
|
163
|
+
* @since 0.1.0
|
|
164
|
+
*/
|
|
165
|
+
export declare const multiply: <A extends string = never, B extends string = never>(left: Input<A>, right: Input<B>) => Calc<A | B>;
|
|
166
|
+
/**
|
|
167
|
+
* Divides `left` by `right`. Constant operands fold at construction.
|
|
168
|
+
*
|
|
169
|
+
* @param left - The dividend.
|
|
170
|
+
* @param right - The divisor.
|
|
171
|
+
* @returns The quotient, with the operands' references unioned.
|
|
172
|
+
* @since 0.1.0
|
|
173
|
+
*/
|
|
174
|
+
export declare const divide: <A extends string = never, B extends string = never>(left: Input<A>, right: Input<B>) => Calc<A | B>;
|
|
175
|
+
/**
|
|
176
|
+
* Raises `base` to `exponent`. Serializes as the CSS `pow()` function.
|
|
177
|
+
*
|
|
178
|
+
* @param base - The base.
|
|
179
|
+
* @param exponent - The exponent.
|
|
180
|
+
* @returns The power, with the operands' references unioned.
|
|
181
|
+
* @since 0.1.0
|
|
182
|
+
*/
|
|
183
|
+
export declare const pow: <A extends string = never, B extends string = never>(base: Input<A>, exponent: Input<B>) => Calc<A | B>;
|
|
184
|
+
/**
|
|
185
|
+
* Sign-preserving power: `abs(base) ^ exponent * sign(base)`. Unlike
|
|
186
|
+
* `pow`, well-defined for negative bases with fractional exponents.
|
|
187
|
+
* Serializes as `pow(abs(base), exponent) * sign(base)`.
|
|
188
|
+
*
|
|
189
|
+
* @param base - The base.
|
|
190
|
+
* @param exponent - The exponent.
|
|
191
|
+
* @returns The signed power, with the operands' references unioned.
|
|
192
|
+
* @since 0.1.0
|
|
193
|
+
*/
|
|
194
|
+
export declare const signedPow: <A extends string = never, B extends string = never>(base: Input<A>, exponent: Input<B>) => Calc<A | B>;
|
|
195
|
+
/**
|
|
196
|
+
* The minimum of the operands. Serializes as the CSS `min()` function.
|
|
197
|
+
*
|
|
198
|
+
* @param a - The first operand.
|
|
199
|
+
* @param b - The second operand.
|
|
200
|
+
* @returns The minimum, with the operands' references unioned.
|
|
201
|
+
* @since 0.1.0
|
|
202
|
+
*/
|
|
203
|
+
export declare const min: {
|
|
204
|
+
<A extends string = never, B extends string = never>(a: Input<A>, b: Input<B>): Calc<A | B>;
|
|
205
|
+
<A extends string = never, B extends string = never, C extends string = never>(a: Input<A>, b: Input<B>, c: Input<C>): Calc<A | B | C>;
|
|
206
|
+
<A extends string = never, B extends string = never, C extends string = never, D extends string = never>(a: Input<A>, b: Input<B>, c: Input<C>, d: Input<D>): Calc<A | B | C | D>;
|
|
207
|
+
(...args: readonly [Input, Input, ...ReadonlyArray<Input>]): Calc<string>;
|
|
208
|
+
};
|
|
209
|
+
/**
|
|
210
|
+
* The maximum of the operands. Serializes as the CSS `max()` function.
|
|
211
|
+
*
|
|
212
|
+
* @param a - The first operand.
|
|
213
|
+
* @param b - The second operand.
|
|
214
|
+
* @returns The maximum, with the operands' references unioned.
|
|
215
|
+
* @since 0.1.0
|
|
216
|
+
*/
|
|
217
|
+
export declare const max: {
|
|
218
|
+
<A extends string = never, B extends string = never>(a: Input<A>, b: Input<B>): Calc<A | B>;
|
|
219
|
+
<A extends string = never, B extends string = never, C extends string = never>(a: Input<A>, b: Input<B>, c: Input<C>): Calc<A | B | C>;
|
|
220
|
+
<A extends string = never, B extends string = never, C extends string = never, D extends string = never>(a: Input<A>, b: Input<B>, c: Input<C>, d: Input<D>): Calc<A | B | C | D>;
|
|
221
|
+
(...args: readonly [Input, Input, ...ReadonlyArray<Input>]): Calc<string>;
|
|
222
|
+
};
|
|
223
|
+
/**
|
|
224
|
+
* Clamps `value` between `minimum` and `maximum`. Serializes as the CSS
|
|
225
|
+
* `clamp()` function, with the CSS argument order `(min, value, max)`.
|
|
226
|
+
*
|
|
227
|
+
* @param minimum - The lower bound.
|
|
228
|
+
* @param value - The value to clamp.
|
|
229
|
+
* @param maximum - The upper bound.
|
|
230
|
+
* @returns The clamped expression, with the operands' references unioned.
|
|
231
|
+
* @example
|
|
232
|
+
* ```ts
|
|
233
|
+
* Calc.serialize(Calc.clamp(-1, Calc.ref('u'), 1)) // 'clamp(-1, var(--u), 1)'
|
|
234
|
+
* ```
|
|
235
|
+
* @since 0.1.0
|
|
236
|
+
*/
|
|
237
|
+
export declare const clamp: <A extends string = never, B extends string = never, C extends string = never>(minimum: Input<A>, value: Input<B>, maximum: Input<C>) => Calc<A | B | C>;
|
|
238
|
+
/**
|
|
239
|
+
* Linear interpolation from `a` to `b` by `t`. This is sugar, not a node:
|
|
240
|
+
* it desugars to `(1 - t) * a + t * b`, and serializes as that expanded
|
|
241
|
+
* form.
|
|
242
|
+
*
|
|
243
|
+
* @param a - The value at `t = 0`.
|
|
244
|
+
* @param b - The value at `t = 1`.
|
|
245
|
+
* @param t - The interpolation parameter.
|
|
246
|
+
* @returns The interpolated expression, with the operands' references unioned.
|
|
247
|
+
* @since 0.1.0
|
|
248
|
+
*/
|
|
249
|
+
export declare const lerp: <A extends string = never, B extends string = never, T extends string = never>(a: Input<A>, b: Input<B>, t: Input<T>) => Calc<A | B | T>;
|
|
250
|
+
/**
|
|
251
|
+
* The absolute value. Serializes as the CSS `abs()` function.
|
|
252
|
+
*
|
|
253
|
+
* @param argument - The operand.
|
|
254
|
+
* @returns The absolute value expression.
|
|
255
|
+
* @since 0.1.0
|
|
256
|
+
*/
|
|
257
|
+
export declare const abs: <A extends string = never>(argument: Input<A>) => Calc<A>;
|
|
258
|
+
/**
|
|
259
|
+
* The sign (`-1`, `0`, or `1`). Serializes as the CSS `sign()` function.
|
|
260
|
+
*
|
|
261
|
+
* @param argument - The operand.
|
|
262
|
+
* @returns The sign expression.
|
|
263
|
+
* @since 0.1.0
|
|
264
|
+
*/
|
|
265
|
+
export declare const sign: <A extends string = never>(argument: Input<A>) => Calc<A>;
|
|
266
|
+
/**
|
|
267
|
+
* The sine of an angle in radians. Serializes as the CSS `sin()`
|
|
268
|
+
* function; CSS treats a plain-number argument as radians, so no unit
|
|
269
|
+
* conversion is involved.
|
|
270
|
+
*
|
|
271
|
+
* @param argument - The angle, in radians.
|
|
272
|
+
* @returns The sine expression.
|
|
273
|
+
* @since 0.1.0
|
|
274
|
+
*/
|
|
275
|
+
export declare const sin: <A extends string = never>(argument: Input<A>) => Calc<A>;
|
|
276
|
+
/**
|
|
277
|
+
* The cosine of an angle in radians. Serializes as the CSS `cos()`
|
|
278
|
+
* function; CSS treats a plain-number argument as radians, so no unit
|
|
279
|
+
* conversion is involved.
|
|
280
|
+
*
|
|
281
|
+
* @param argument - The angle, in radians.
|
|
282
|
+
* @returns The cosine expression.
|
|
283
|
+
* @since 0.1.0
|
|
284
|
+
*/
|
|
285
|
+
export declare const cos: <A extends string = never>(argument: Input<A>) => Calc<A>;
|
|
286
|
+
/**
|
|
287
|
+
* The arccosine of a value in `[-1, 1]`, in radians. Solving evaluates
|
|
288
|
+
* `Math.acos`.
|
|
289
|
+
*
|
|
290
|
+
* Serialization note: CSS's `acos()` returns an `<angle>`, not a number.
|
|
291
|
+
* The serializer keeps such subtrees angle-typed — plain-number terms
|
|
292
|
+
* added to or subtracted from an acos-carrying term are rendered with a
|
|
293
|
+
* `rad` suffix (constants) or a `* 1rad` factor (anything else), so the
|
|
294
|
+
* emitted CSS stays valid without typed division. In v1, consume
|
|
295
|
+
* acos-carrying subtrees with `sin` or `cos`; feeding one into a plain
|
|
296
|
+
* number context serializes to angle-typed CSS.
|
|
297
|
+
*
|
|
298
|
+
* @param argument - The cosine value, in `[-1, 1]`.
|
|
299
|
+
* @returns The arccosine expression, in radians.
|
|
300
|
+
* @example
|
|
301
|
+
* ```ts
|
|
302
|
+
* Calc.serialize(Calc.cos(Calc.subtract(Calc.divide(Calc.acos(Calc.ref('u')), 3), 2.0943951)))
|
|
303
|
+
* // 'cos(acos(var(--u)) / 3 - 2.0944rad)'
|
|
304
|
+
* ```
|
|
305
|
+
* @since 0.1.0
|
|
306
|
+
*/
|
|
307
|
+
export declare const acos: <A extends string = never>(argument: Input<A>) => Calc<A>;
|
|
308
|
+
export declare const bind: {
|
|
309
|
+
/**
|
|
310
|
+
* Returns a function that binds the given names in an expression.
|
|
311
|
+
*
|
|
312
|
+
* @param bindings - Reference names to values or expressions. Names the expression does not reference are ignored, as are `undefined` values.
|
|
313
|
+
* @returns A function replacing bound references in its argument.
|
|
314
|
+
* @since 0.1.0
|
|
315
|
+
*/
|
|
316
|
+
<const B extends Bindings>(bindings: B): <Refs extends string>(expr: Calc<Refs>) => Calc<ApplyBindings<Refs, B>>;
|
|
317
|
+
/**
|
|
318
|
+
* Replaces references with values or other expressions. Binding is
|
|
319
|
+
* partial evaluation: substituted subtrees re-fold, so binding every
|
|
320
|
+
* reference collapses the tree to a constant.
|
|
321
|
+
*
|
|
322
|
+
* Names the expression does not reference are ignored (spreading a wider
|
|
323
|
+
* bindings object is fine), as are `undefined` values. Binding a
|
|
324
|
+
* reference to another expression composes trees; the bound expression's
|
|
325
|
+
* own references join the result, tracked in the return type by
|
|
326
|
+
* `ApplyBindings`.
|
|
327
|
+
*
|
|
328
|
+
* @param expr - The expression to bind.
|
|
329
|
+
* @param bindings - Reference names to values or expressions.
|
|
330
|
+
* @returns The bound expression.
|
|
331
|
+
* @example
|
|
332
|
+
* ```ts
|
|
333
|
+
* const half = Calc.bind(Calc.divide(Calc.ref('x'), 2), { x: Calc.ref('width') })
|
|
334
|
+
* Calc.serialize(half) // 'calc(var(--width) / 2)'
|
|
335
|
+
* Calc.solve(Calc.bind(half, { width: 100 })) // 50
|
|
336
|
+
* ```
|
|
337
|
+
* @since 0.1.0
|
|
338
|
+
*/
|
|
339
|
+
<Refs extends string, const B extends Bindings>(expr: Calc<Refs>, bindings: B): Calc<ApplyBindings<Refs, B>>;
|
|
340
|
+
};
|
|
341
|
+
export declare const solve: {
|
|
342
|
+
/**
|
|
343
|
+
* Evaluates a fully bound expression to a number.
|
|
344
|
+
*
|
|
345
|
+
* The parameter type `Calc<never>` makes closedness a compile-time
|
|
346
|
+
* requirement; an expression with unbound references needs the bindings
|
|
347
|
+
* overload.
|
|
348
|
+
*
|
|
349
|
+
* @param expr - The expression to evaluate. Must have no unbound references.
|
|
350
|
+
* @returns The numeric value.
|
|
351
|
+
* @throws `Error` when unbound references remain at runtime.
|
|
352
|
+
* @since 0.1.0
|
|
353
|
+
*/
|
|
354
|
+
(expr: Calc<never>): number;
|
|
355
|
+
/**
|
|
356
|
+
* Applies bindings, then evaluates to a number. The bindings must cover
|
|
357
|
+
* every unbound reference.
|
|
358
|
+
*
|
|
359
|
+
* @param expr - The expression to evaluate.
|
|
360
|
+
* @param bindings - Values for every unbound reference.
|
|
361
|
+
* @returns The numeric value.
|
|
362
|
+
* @throws `Error` when unbound references remain after binding.
|
|
363
|
+
* @example
|
|
364
|
+
* ```ts
|
|
365
|
+
* Calc.solve(Calc.lerp(Calc.ref('a'), Calc.ref('b'), 0.5), { a: 0, b: 10 }) // 5
|
|
366
|
+
* ```
|
|
367
|
+
* @since 0.1.0
|
|
368
|
+
*/
|
|
369
|
+
<Refs extends string, B extends Bindings<Refs>>(expr: Calc<Refs>, bindings: B): number;
|
|
370
|
+
};
|
|
371
|
+
/**
|
|
372
|
+
* Renders an expression as CSS text. Arithmetic gets a `calc()` wrapper;
|
|
373
|
+
* function forms (`min`, `clamp`, `sin`, ...) and leaves stand alone.
|
|
374
|
+
* References render as `var(--name)`.
|
|
375
|
+
*
|
|
376
|
+
* Bindings in `options` are applied first and may be partial — this is
|
|
377
|
+
* the serialize half of the solve/serialize duality, and unbound
|
|
378
|
+
* references are the values left for the browser. Constants render with
|
|
379
|
+
* their annotated precision, or the context `precision` option (default
|
|
380
|
+
* `Precision.decimals(5)`). Constants equal to pi render as the CSS
|
|
381
|
+
* constant `pi` where a math function surrounds them.
|
|
382
|
+
*
|
|
383
|
+
* @param expr - The expression to render.
|
|
384
|
+
* @param options - Optional bindings and precision context.
|
|
385
|
+
* @returns Deterministic CSS text.
|
|
386
|
+
* @example
|
|
387
|
+
* ```ts
|
|
388
|
+
* const fluid = Calc.add(10, Calc.ref('runtime'))
|
|
389
|
+
* Calc.serialize(fluid) // 'calc(10 + var(--runtime))'
|
|
390
|
+
* Calc.serialize(fluid, { bindings: { runtime: 4 } }) // '14'
|
|
391
|
+
* ```
|
|
392
|
+
* @since 0.1.0
|
|
393
|
+
*/
|
|
394
|
+
export declare const serialize: <Refs extends string>(expr: Calc<Refs>, options?: SerializeOptions<Refs>) => string;
|
|
395
|
+
/**
|
|
396
|
+
* The expression's unbound reference names.
|
|
397
|
+
*
|
|
398
|
+
* @param expr - The expression to inspect.
|
|
399
|
+
* @returns The set of unbound reference names.
|
|
400
|
+
* @since 0.1.0
|
|
401
|
+
*/
|
|
402
|
+
export declare const refs: <Refs extends string>(expr: Calc<Refs>) => ReadonlySet<Refs>;
|
|
403
|
+
export declare const equals: {
|
|
404
|
+
/**
|
|
405
|
+
* Returns a function that checks structural equality against `that`.
|
|
406
|
+
*
|
|
407
|
+
* @param that - The expression to compare against.
|
|
408
|
+
* @returns A function testing its argument for structural equality with `that`.
|
|
409
|
+
* @since 0.1.0
|
|
410
|
+
*/
|
|
411
|
+
(that: Calc<string>): (self: Calc<string>) => boolean;
|
|
412
|
+
/**
|
|
413
|
+
* Structural equality over expression trees. Two expressions are equal
|
|
414
|
+
* when their trees match node for node — including constant precision
|
|
415
|
+
* annotations, which affect serialization. Expression trees are ordered
|
|
416
|
+
* syntax: `add(a, b)` and `add(b, a)` are not equal.
|
|
417
|
+
*
|
|
418
|
+
* @param self - The first expression.
|
|
419
|
+
* @param that - The second expression.
|
|
420
|
+
* @returns `true` if the expressions are structurally equal.
|
|
421
|
+
* @since 0.1.0
|
|
422
|
+
*/
|
|
423
|
+
(self: Calc<string>, that: Calc<string>): boolean;
|
|
424
|
+
};
|
|
425
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import * as CalcNs from './calc.ts';
|
|
2
|
+
import * as PrecisionNs from './precision.ts';
|
|
3
|
+
export type Calc<Refs extends string = string> = CalcNs.Calc<Refs>;
|
|
4
|
+
export type Precision = PrecisionNs.Precision;
|
|
5
|
+
export { CalcNs as Calc };
|
|
6
|
+
export { PrecisionNs as Precision };
|