@mkbabb/value.js 0.5.0 → 0.10.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 (43) hide show
  1. package/README.md +38 -56
  2. package/dist/easing.d.ts +49 -0
  3. package/dist/index.d.ts +19 -8
  4. package/dist/math.d.ts +1 -1
  5. package/dist/parsing/animation-shorthand.d.ts +19 -0
  6. package/dist/parsing/color.d.ts +10 -3
  7. package/dist/parsing/extract.d.ts +48 -0
  8. package/dist/parsing/index.d.ts +5 -20
  9. package/dist/parsing/serialize.d.ts +24 -0
  10. package/dist/parsing/stylesheet.d.ts +44 -0
  11. package/dist/parsing/units.d.ts +24 -6
  12. package/dist/parsing/utils.d.ts +5 -0
  13. package/dist/postcss-CRluLK2m.js +6400 -0
  14. package/dist/quantize/cluster.d.ts +45 -0
  15. package/dist/quantize/index.d.ts +14 -0
  16. package/dist/quantize/types.d.ts +39 -0
  17. package/dist/standalone-Ck3UyY5I.js +3458 -0
  18. package/dist/units/color/constants.d.ts +21 -0
  19. package/dist/units/color/contrast.d.ts +35 -0
  20. package/dist/units/color/conversions/cylindrical.d.ts +13 -0
  21. package/dist/units/color/conversions/direct.d.ts +41 -0
  22. package/dist/units/color/conversions/hex.d.ts +3 -0
  23. package/dist/units/color/conversions/index.d.ts +17 -0
  24. package/dist/units/color/conversions/kelvin.d.ts +5 -0
  25. package/dist/units/color/conversions/lab.d.ts +8 -0
  26. package/dist/units/color/conversions/oklab.d.ts +11 -0
  27. package/dist/units/color/conversions/transfer.d.ts +22 -0
  28. package/dist/units/color/conversions/xyz-extended.d.ts +13 -0
  29. package/dist/units/color/dispatch.d.ts +26 -0
  30. package/dist/units/color/gamut.d.ts +6 -0
  31. package/dist/units/color/index.d.ts +220 -116
  32. package/dist/units/color/mix.d.ts +16 -0
  33. package/dist/units/color/normalize.d.ts +3 -3
  34. package/dist/units/index.d.ts +39 -1
  35. package/dist/units/interpolate.d.ts +47 -0
  36. package/dist/units/normalize.d.ts +47 -7
  37. package/dist/units/utils.d.ts +1 -1
  38. package/dist/utils.d.ts +3 -2
  39. package/dist/value.js +4229 -5229
  40. package/package.json +62 -30
  41. package/scripts/migrate-keyframes-js-lerp.mjs +257 -0
  42. package/dist/units/color/utils.d.ts +0 -77
  43. package/dist/value.cjs +0 -20
@@ -0,0 +1,16 @@
1
+ import { Color } from '.';
2
+ import { ColorSpace } from './constants';
3
+ import { HueInterpolationMethod } from './dispatch';
4
+ /**
5
+ * Mix N colors with optional per-color weights.
6
+ *
7
+ * @param colors Array of Color objects (must be ≥ 1). All should be
8
+ * normalized (components in [0, 1]).
9
+ * @param weights Optional per-color weights (same length as `colors`).
10
+ * Defaults to equal weighting. Values need not sum to 1 —
11
+ * they are normalized internally.
12
+ * @param space Interpolation color space (default "oklab").
13
+ * @param hueMethod Hue interpolation for cylindrical spaces (default "shorter").
14
+ * @returns A single Color in the interpolation space.
15
+ */
16
+ export declare function mixColorsN(colors: Color[], weights?: number[], space?: ColorSpace, hueMethod?: HueInterpolationMethod): Color;
@@ -1,10 +1,10 @@
1
1
  import { Color, ColorSpaceMap } from '.';
2
2
  import { ValueUnit } from '..';
3
3
  import { ColorSpace } from './constants';
4
- import { HueInterpolationMethod } from './utils';
4
+ import { HueInterpolationMethod } from './dispatch';
5
5
  export type { HueInterpolationMethod };
6
- export declare const normalizeColorUnitComponent: (v: number, unit: string, colorSpace: ColorSpace, component: string, inverse?: boolean) => ValueUnit<number, string>;
6
+ export declare const normalizeColorUnitComponent: (v: number, unit: string | undefined, colorSpace: ColorSpace, component: string, inverse?: boolean) => ValueUnit<number, string>;
7
7
  export declare const normalizeColor: (color: Color<ValueUnit<number> | number>, inverse?: boolean) => Color<ValueUnit<number>>;
8
8
  export declare const normalizeColorUnit: (color: ValueUnit<Color<ValueUnit<number>>, "color">, inverse?: boolean, inplace?: boolean) => ValueUnit<Color<ValueUnit<number>>, "color">;
9
9
  export declare const colorUnit2: <C extends ColorSpace>(color: ValueUnit<Color<ValueUnit<number>>, "color">, to?: C | null, normalized?: boolean, inverse?: boolean, inplace?: boolean) => ValueUnit<ColorSpaceMap<ValueUnit<number>>[C], "color">;
10
- export declare const normalizeColorUnits: (a: ValueUnit<Color<ValueUnit<number>>, "color">, b: ValueUnit<Color<ValueUnit<number>>, "color">, to?: ColorSpace, normalized?: boolean, inverse?: boolean, inplace?: boolean, hueMethod?: HueInterpolationMethod) => readonly [ValueUnit<import('.').RGBColor<ValueUnit<number, string | undefined>> | import('.').HSLColor<ValueUnit<number, string | undefined>> | import('.').HSVColor<ValueUnit<number, string | undefined>> | import('.').HWBColor<ValueUnit<number, string | undefined>> | import('.').LABColor<ValueUnit<number, string | undefined>> | import('.').LCHColor<ValueUnit<number, string | undefined>> | import('.').OKLABColor<ValueUnit<number, string | undefined>> | import('.').OKLCHColor<ValueUnit<number, string | undefined>> | import('.').XYZColor<ValueUnit<number, string | undefined>> | import('.').KelvinColor<ValueUnit<number, string | undefined>> | import('.').LinearSRGBColor<ValueUnit<number, string | undefined>> | import('.').DisplayP3Color<ValueUnit<number, string | undefined>> | import('.').AdobeRGBColor<ValueUnit<number, string | undefined>> | import('.').ProPhotoRGBColor<ValueUnit<number, string | undefined>> | import('.').Rec2020Color<ValueUnit<number, string | undefined>>, "color">, ValueUnit<import('.').RGBColor<ValueUnit<number, string | undefined>> | import('.').HSLColor<ValueUnit<number, string | undefined>> | import('.').HSVColor<ValueUnit<number, string | undefined>> | import('.').HWBColor<ValueUnit<number, string | undefined>> | import('.').LABColor<ValueUnit<number, string | undefined>> | import('.').LCHColor<ValueUnit<number, string | undefined>> | import('.').OKLABColor<ValueUnit<number, string | undefined>> | import('.').OKLCHColor<ValueUnit<number, string | undefined>> | import('.').XYZColor<ValueUnit<number, string | undefined>> | import('.').KelvinColor<ValueUnit<number, string | undefined>> | import('.').LinearSRGBColor<ValueUnit<number, string | undefined>> | import('.').DisplayP3Color<ValueUnit<number, string | undefined>> | import('.').AdobeRGBColor<ValueUnit<number, string | undefined>> | import('.').ProPhotoRGBColor<ValueUnit<number, string | undefined>> | import('.').Rec2020Color<ValueUnit<number, string | undefined>>, "color">, HueInterpolationMethod | undefined];
10
+ export declare const normalizeColorUnits: (a: ValueUnit<Color<ValueUnit<number>>, "color">, b: ValueUnit<Color<ValueUnit<number>>, "color">, to?: ColorSpace, normalized?: boolean, inverse?: boolean, inplace?: boolean, hueMethod?: HueInterpolationMethod) => readonly [ValueUnit<import('.').RGBColor<ValueUnit<number, string>> | import('.').HSLColor<ValueUnit<number, string>> | import('.').HSVColor<ValueUnit<number, string>> | import('.').HWBColor<ValueUnit<number, string>> | import('.').LABColor<ValueUnit<number, string>> | import('.').LCHColor<ValueUnit<number, string>> | import('.').OKLABColor<ValueUnit<number, string>> | import('.').OKLCHColor<ValueUnit<number, string>> | import('.').XYZColor<ValueUnit<number, string>> | import('.').KelvinColor<ValueUnit<number, string>> | import('.').LinearSRGBColor<ValueUnit<number, string>> | import('.').DisplayP3Color<ValueUnit<number, string>> | import('.').AdobeRGBColor<ValueUnit<number, string>> | import('.').ProPhotoRGBColor<ValueUnit<number, string>> | import('.').Rec2020Color<ValueUnit<number, string>>, "color">, ValueUnit<import('.').RGBColor<ValueUnit<number, string>> | import('.').HSLColor<ValueUnit<number, string>> | import('.').HSVColor<ValueUnit<number, string>> | import('.').HWBColor<ValueUnit<number, string>> | import('.').LABColor<ValueUnit<number, string>> | import('.').LCHColor<ValueUnit<number, string>> | import('.').OKLABColor<ValueUnit<number, string>> | import('.').OKLCHColor<ValueUnit<number, string>> | import('.').XYZColor<ValueUnit<number, string>> | import('.').KelvinColor<ValueUnit<number, string>> | import('.').LinearSRGBColor<ValueUnit<number, string>> | import('.').DisplayP3Color<ValueUnit<number, string>> | import('.').AdobeRGBColor<ValueUnit<number, string>> | import('.').ProPhotoRGBColor<ValueUnit<number, string>> | import('.').Rec2020Color<ValueUnit<number, string>>, "color">, HueInterpolationMethod | undefined];
@@ -1,6 +1,8 @@
1
1
  import { UNITS } from './constants';
2
+ import { ColorSpace } from './color/constants';
3
+ import { HueInterpolationMethod } from './color/dispatch';
2
4
  export { registerColorNames, clearCustomColorNames, getCustomColorNames } from '../parsing/color';
3
- export declare class ValueUnit<T = any, U = (typeof UNITS)[number] | string> {
5
+ export declare class ValueUnit<T = any, U extends string = Exclude<(typeof UNITS)[number], undefined> | string> {
4
6
  value: T;
5
7
  unit?: U | undefined;
6
8
  superType?: string[] | undefined;
@@ -8,6 +10,23 @@ export declare class ValueUnit<T = any, U = (typeof UNITS)[number] | string> {
8
10
  property?: string | undefined;
9
11
  targets?: HTMLElement[] | undefined;
10
12
  constructor(value: T, unit?: U | undefined, superType?: string[] | undefined, subProperty?: string | undefined, property?: string | undefined, targets?: HTMLElement[] | undefined);
13
+ /**
14
+ * Fully unwrap a (possibly nested) `ValueUnit`, returning the innermost
15
+ * non-`ValueUnit` value.
16
+ *
17
+ * **G.W2 Lane D (G-OPP-5)** — codifies the Mar 2026 iOS Safari
18
+ * stack-overflow fix as a first-class `ValueUnit` primitive. The
19
+ * `while (raw instanceof ValueUnit) raw = raw.value` idiom was inlined
20
+ * across the color pipeline + the parser; each inline copy was a place the
21
+ * `VU<VU<…>>` accumulation guard could silently drift. This static is the
22
+ * single source of truth — see `test/recursion-guard.test.ts`.
23
+ *
24
+ * The conditional return type peels exactly one `ValueUnit` layer at the
25
+ * type level (`ValueUnit` payloads can themselves be `ValueUnit` only via
26
+ * the bug class this guards against, so one peel is the honest static
27
+ * type); at runtime the `while` loop peels every layer.
28
+ */
29
+ static unwrapDeep<T>(x: T): T extends ValueUnit<infer V> ? V : T;
11
30
  setSubProperty(subProperty: any): void;
12
31
  setProperty(property: any): void;
13
32
  setTargets(targets: HTMLElement[]): void;
@@ -48,4 +67,23 @@ export type InterpolatedVar<T> = {
48
67
  stop: ValueUnit<T>;
49
68
  value: ValueUnit<T>;
50
69
  computed: boolean;
70
+ /**
71
+ * For color interpolation in cylindrical spaces (hsl/hsv/hwb/lch/oklch):
72
+ * which hue-direction method to use. Defaults to `"shorter"` per CSS
73
+ * Color 4 §12.4 when omitted.
74
+ */
75
+ hueMethod?: HueInterpolationMethod;
76
+ /**
77
+ * For color interpolation: the color space the start/stop endpoints have
78
+ * been normalised into. Drives the choice of which component is the hue
79
+ * channel in `lerpColorValue`.
80
+ */
81
+ colorSpace?: ColorSpace;
82
+ /**
83
+ * Pre-resolved dispatch function set by `prepareInterpVar`. Avoids three
84
+ * sequential type checks per `lerpValue` call in hot paths. Optional —
85
+ * external callers can construct an `InterpolatedVar` without it and the
86
+ * runtime dispatch in `lerpValue` will resolve at call time.
87
+ */
88
+ _lerp?: (t: number, iv: InterpolatedVar<any>) => ValueUnit<any>;
51
89
  };
@@ -0,0 +1,47 @@
1
+ import { Color } from './color';
2
+ import { ValueUnit, InterpolatedVar } from './index';
3
+ /**
4
+ * Interpolate a `ValueUnit` whose unit is *computed* (`var`, `calc`,
5
+ * `vh`, `vw`, etc.) by resolving both endpoints against a target
6
+ * element's live computed style and lerping the resulting numeric
7
+ * values.
8
+ *
9
+ * Mutates and returns the `value` field of the InterpolatedVar.
10
+ */
11
+ export declare function lerpComputedValue(t: number, { start, stop, value }: InterpolatedVar<any>): ValueUnit<any>;
12
+ /**
13
+ * A `Color` at interpolation time: its channels may be raw `number`s (numeric
14
+ * pipeline) or `ValueUnit<number>` wrappers (parser-produced colors). Both
15
+ * shapes flow through `lerpColorValue`; the per-channel `instanceof ValueUnit`
16
+ * branch dispatches between them.
17
+ */
18
+ type InterpColor = Color<ValueUnit<number> | number>;
19
+ /**
20
+ * Interpolate a colour `ValueUnit` (`unit === "color"`). Walks each
21
+ * channel of the parsed `Color` instance and lerps independently. The
22
+ * surrounding `normalizeColorUnits` step is responsible for putting
23
+ * `start` / `stop` in the same colour space (oklab, oklch, etc.) and
24
+ * for hue handling.
25
+ */
26
+ export declare function lerpColorValue(t: number, iv: InterpolatedVar<InterpColor>): ValueUnit<InterpColor>;
27
+ /** Lerp a simple numeric InterpolatedVar in-place. */
28
+ export declare function lerpNumericValue(t: number, { start, stop, value }: InterpolatedVar<number>): ValueUnit<number>;
29
+ /**
30
+ * Interpolate any `InterpolatedVar` at progress `t`.
31
+ *
32
+ * Uses a pre-resolved dispatch function (`_lerp`) when the
33
+ * InterpolatedVar was created via `prepareInterpVar` — avoids three
34
+ * sequential type checks per call in hot paths. Falls back to
35
+ * runtime dispatch for externally constructed values.
36
+ */
37
+ export declare function lerpValue(t: number, iv: InterpolatedVar<any>): ValueUnit<any> | undefined;
38
+ /**
39
+ * Pre-resolve the interpolation dispatch function on an
40
+ * `InterpolatedVar`. Sets a hidden `_lerp` property used by
41
+ * `lerpValue`'s fast path.
42
+ *
43
+ * Call once per InterpolatedVar after `normalizeValueUnits`; the
44
+ * dispatch is invariant for the lifetime of the InterpolatedVar.
45
+ */
46
+ export declare function prepareInterpVar(iv: InterpolatedVar<any>): InterpolatedVar<any>;
47
+ export {};
@@ -1,9 +1,49 @@
1
1
  import { ValueUnit, InterpolatedVar } from '.';
2
- export declare const getComputedValue: ((value: ValueUnit, target: HTMLElement) => ValueUnit<any, any>) & {
3
- cache: Map<string, {
4
- value: ValueUnit<any, any>;
5
- timestamp: number;
6
- }>;
7
- };
2
+ import { ColorSpace } from './color/constants';
3
+ import { HueInterpolationMethod } from './color/dispatch';
4
+ import { isColorUnit } from './utils';
5
+ /**
6
+ * Resolve a computed CSS value (`var()`, `calc()`, or other
7
+ * deferred-evaluation unit) against a target element by writing it
8
+ * into the target's inline style and reading back the computed
9
+ * style. Memoised by `(value.toString(), element-id)`.
10
+ *
11
+ * For `calc()` values whose `subProperty` names a transform axis
12
+ * (`translateX`, `scaleY`, etc.), the round-trip yields a `matrix()`
13
+ * or `matrix3d()`; this function decomposes the matrix to recover
14
+ * the resolved numeric value for that single axis.
15
+ *
16
+ * Caching is suppressed when the target is disconnected — layout
17
+ * units (`vh`, `cqw`, etc.) resolve to 0 outside the live tree, so
18
+ * caching that value would poison later reads.
19
+ */
20
+ export declare const getComputedValue: import('../utils').Memoized<(value: ValueUnit, target?: HTMLElement) => ValueUnit<any, any>>;
21
+ /**
22
+ * Convert two `ValueUnit`s of the same superType to a common base
23
+ * unit: length → px, angle → deg, time → ms, resolution → dpi.
24
+ *
25
+ * Throws if either input has a unit that isn't recognised for its
26
+ * superType — silent passthrough hides bugs upstream.
27
+ */
8
28
  export declare const normalizeNumericUnits: (a: ValueUnit, b: ValueUnit, inplace?: boolean) => [ValueUnit, ValueUnit];
9
- export declare function normalizeValueUnits(left: ValueUnit, right: ValueUnit): InterpolatedVar<any>;
29
+ export type NormalizeValueUnitsOptions = {
30
+ /** Color space for color interpolation. Default: `"oklab"`. */
31
+ colorSpace?: ColorSpace;
32
+ /** Hue interpolation method for cylindrical color spaces. */
33
+ hueMethod?: HueInterpolationMethod;
34
+ };
35
+ /**
36
+ * Compute an `InterpolatedVar` from two endpoint `ValueUnit`s,
37
+ * preparing them for later `lerpValue` calls.
38
+ *
39
+ * - Colors: collapsed to a common space via `normalizeColorUnits`.
40
+ * `colorSpace` defaults to `oklab` (perceptually uniform); `hueMethod`
41
+ * selects the cylindrical hue interpolation strategy.
42
+ * - Mixed units (e.g. `10px` ↔ `1em`): collapsed via
43
+ * `normalizeNumericUnits` to a common base unit.
44
+ * - Computed units (`var`, `calc`, `vh`, `cqw`, etc.): left as-is and
45
+ * marked `computed: true`. The actual numeric resolution happens
46
+ * later in `lerpComputedValue` against a live target.
47
+ */
48
+ export declare function normalizeValueUnits(left: ValueUnit, right: ValueUnit, options?: NormalizeValueUnitsOptions): InterpolatedVar<unknown>;
49
+ export { isColorUnit };
@@ -1,7 +1,7 @@
1
1
  import { FunctionValue, ValueUnit } from '.';
2
2
  import { Color } from './color';
3
3
  import { ABSOLUTE_LENGTH_UNITS, ANGLE_UNITS, FREQUENCY_UNITS, PERCENTAGE_UNITS, RELATIVE_LENGTH_UNITS, RESOLUTION_UNITS, STYLE_NAMES, TIME_UNITS, UNITS, MatrixValues } from './constants';
4
- export declare function isColorUnit(value: ValueUnit<Color<ValueUnit>>): value is ValueUnit<Color<ValueUnit>>;
4
+ export declare function isColorUnit(value: ValueUnit): value is ValueUnit<Color<ValueUnit>>;
5
5
  export declare const flattenObject: (obj: any) => Record<string, any>;
6
6
  export declare const unflattenObject: (flatObj: Record<string, any[]>) => any;
7
7
  export declare const unflattenObjectToString: (flatObj: Record<string, any[]>) => Record<string, string>;
package/dist/utils.d.ts CHANGED
@@ -15,14 +15,15 @@ export interface MemoizeOptions<T extends (...args: any[]) => any = (...args: an
15
15
  /** When provided, the result is only cached if this returns true. */
16
16
  shouldCache?: (result: ReturnType<T>, ...args: Parameters<T>) => boolean;
17
17
  }
18
- export declare function memoize<T extends (...args: any[]) => any>(func: T, options?: MemoizeOptions<T>): T & {
18
+ export type Memoized<T extends (...args: any[]) => any> = T & {
19
19
  cache: Map<string, {
20
20
  value: ReturnType<T>;
21
21
  timestamp: number;
22
22
  }>;
23
23
  };
24
+ export declare function memoize<T extends (...args: any[]) => any>(func: T, options?: MemoizeOptions<T>): Memoized<T>;
24
25
  export declare const hyphenToCamelCase: (str: string) => string;
25
26
  export declare function camelCaseToHyphen(str: string): string;
26
27
  export declare function seekPreviousValue<T>(ix: number, values: T[], pred: (f: T) => boolean): number | undefined;
27
- export declare function requestAnimationFrame(callback: FrameRequestCallback): number | NodeJS.Timeout;
28
+ export declare function requestAnimationFrame(callback: FrameRequestCallback): number;
28
29
  export declare function cancelAnimationFrame(handle: number | undefined | null | any): void;