@charcoal-ui/utils 5.0.0-beta.1 → 5.0.0-beta.3
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/dist/index.cjs +236 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +128 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +118 -60
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +194 -0
- package/dist/index.js.map +1 -0
- package/package.json +15 -19
- package/src/index.ts +16 -13
- package/dist/index.cjs.js +0 -217
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.esm.js +0 -167
- package/dist/index.esm.js.map +0 -1
package/dist/index.js
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import rgba from "polished/lib/color/rgba";
|
|
2
|
+
import rgbToColorString from "polished/lib/color/rgbToColorString";
|
|
3
|
+
import parseToRgb from "polished/lib/color/parseToRgb";
|
|
4
|
+
import linearGradient from "polished/lib/mixins/linearGradient";
|
|
5
|
+
|
|
6
|
+
//#region src/index.ts
|
|
7
|
+
const GRADIENT_DIRECTIONS = [
|
|
8
|
+
"to top",
|
|
9
|
+
"to bottom",
|
|
10
|
+
"to left",
|
|
11
|
+
"to right"
|
|
12
|
+
];
|
|
13
|
+
function transparentGradient(color, defaultDirection = "to bottom") {
|
|
14
|
+
return function transparentGradient$1(direction = defaultDirection) {
|
|
15
|
+
const transparent = rgba(color, 0);
|
|
16
|
+
return linearGradient({
|
|
17
|
+
colorStops: [color, transparent],
|
|
18
|
+
fallback: transparent,
|
|
19
|
+
toDirection: typeof direction === "object" ? defaultDirection : direction
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function gradient(toDirection = "to bottom") {
|
|
24
|
+
return function toLinearGradient(value) {
|
|
25
|
+
return linearGradient({
|
|
26
|
+
colorStops: value.map(({ color, ratio }) => `${color} ${ratio}%`),
|
|
27
|
+
fallback: value[0]?.color,
|
|
28
|
+
toDirection
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function applyEffectToGradient(effect) {
|
|
33
|
+
return function toGradient(value) {
|
|
34
|
+
return value.map(({ color, ratio }) => ({
|
|
35
|
+
color: applyEffect(color, effect),
|
|
36
|
+
ratio
|
|
37
|
+
}));
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function applyEffect(baseColor, effects) {
|
|
41
|
+
const color = baseColor ?? "#00000000";
|
|
42
|
+
if (Array.isArray(effects)) return effects.reduce(applySingleEffect, color);
|
|
43
|
+
return applySingleEffect(color, effects);
|
|
44
|
+
}
|
|
45
|
+
function applySingleEffect(baseColor, effect) {
|
|
46
|
+
switch (effect.type) {
|
|
47
|
+
case "alpha": return applyAlpha(baseColor, effect);
|
|
48
|
+
case "opacity": return applyOpacity(baseColor, effect);
|
|
49
|
+
case "replace": return applyReplace(baseColor, effect);
|
|
50
|
+
default: throw new RangeError(`Unknown effect type ${effect.type}, upgrade @charcoal-ui/utils`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function applyAlpha(baseColor, { color, opacity }) {
|
|
54
|
+
const base = parseToRgb(baseColor);
|
|
55
|
+
const effect = parseToRgb(color);
|
|
56
|
+
return rgba(...alphaBlend([
|
|
57
|
+
base.red,
|
|
58
|
+
base.green,
|
|
59
|
+
base.blue,
|
|
60
|
+
base.alpha ?? 1
|
|
61
|
+
], [
|
|
62
|
+
effect.red,
|
|
63
|
+
effect.green,
|
|
64
|
+
effect.blue,
|
|
65
|
+
clamp(0, 1, (effect.alpha ?? 1) * (opacity ?? 1))
|
|
66
|
+
]));
|
|
67
|
+
}
|
|
68
|
+
function applyOpacity(baseColor, { opacity }) {
|
|
69
|
+
const parsed = parseToRgb(baseColor);
|
|
70
|
+
parsed.alpha = clamp(0, 1, (parsed.alpha ?? 1) * opacity);
|
|
71
|
+
return rgbToColorString(parsed);
|
|
72
|
+
}
|
|
73
|
+
function applyReplace(baseColor, { color = baseColor, opacity }) {
|
|
74
|
+
if (opacity === void 0) return color;
|
|
75
|
+
const parsed = parseToRgb(color);
|
|
76
|
+
parsed.alpha = opacity;
|
|
77
|
+
return rgbToColorString(parsed);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* NOTE: alpha component must be in range from 0.0 to 1.0. (0.0 represents a fully transparent)
|
|
81
|
+
*
|
|
82
|
+
* @param src `[r, g, b, alpha]` Background
|
|
83
|
+
* @param dst `[r, g, b, alpha]` Foreground
|
|
84
|
+
*/
|
|
85
|
+
function alphaBlend(src, dst) {
|
|
86
|
+
const srcA = src[3];
|
|
87
|
+
const dstA = dst[3];
|
|
88
|
+
const outA = srcA + dstA * (1 - srcA);
|
|
89
|
+
if (outA < EPS) return [
|
|
90
|
+
0,
|
|
91
|
+
0,
|
|
92
|
+
0,
|
|
93
|
+
0
|
|
94
|
+
];
|
|
95
|
+
return [
|
|
96
|
+
Math.round((src[0] * srcA * (1 - dstA) + dst[0] * dstA) / outA),
|
|
97
|
+
Math.round((src[1] * srcA * (1 - dstA) + dst[1] * dstA) / outA),
|
|
98
|
+
Math.round((src[2] * srcA * (1 - dstA) + dst[2] * dstA) / outA),
|
|
99
|
+
outA
|
|
100
|
+
];
|
|
101
|
+
}
|
|
102
|
+
const EPS = 1e-6;
|
|
103
|
+
function clamp(min, max, value) {
|
|
104
|
+
return Math.min(Math.max(value, min), max);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* affix `px` unit
|
|
108
|
+
*
|
|
109
|
+
* @param value pixel
|
|
110
|
+
*/
|
|
111
|
+
function px(value) {
|
|
112
|
+
return `${value}px`;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* affix `s` unit
|
|
116
|
+
*
|
|
117
|
+
* @param value second
|
|
118
|
+
*/
|
|
119
|
+
function dur(value) {
|
|
120
|
+
return `${value}s`;
|
|
121
|
+
}
|
|
122
|
+
const notDisabledSelector = `&:not(:disabled):not([aria-disabled]), &[aria-disabled=false]`;
|
|
123
|
+
const disabledSelector = `&:disabled, &[aria-disabled]:not([aria-disabled=false])`;
|
|
124
|
+
/**
|
|
125
|
+
* Construct media query from breakpoint
|
|
126
|
+
*/
|
|
127
|
+
function maxWidth(breakpoint) {
|
|
128
|
+
return `(max-width: ${breakpoint - 1}px)`;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Derive half-leading from typography size
|
|
132
|
+
*/
|
|
133
|
+
const halfLeading = ({ fontSize, lineHeight }) => (lineHeight - fontSize) / 2;
|
|
134
|
+
/**
|
|
135
|
+
* Namespaced custom property
|
|
136
|
+
*/
|
|
137
|
+
const customPropertyToken = (id, modifiers = []) => `--charcoal-${id}${modifiers.length === 0 ? "" : ["", modifiers].join("-")}`;
|
|
138
|
+
/**
|
|
139
|
+
* @example
|
|
140
|
+
* ```js
|
|
141
|
+
* mapKeys({ a: 'aa', b: 'bb' }, (key) => key.toUpperCase()) // => { A: "aa", B: "bb" }
|
|
142
|
+
* ````
|
|
143
|
+
*/
|
|
144
|
+
function mapKeys(object, callback) {
|
|
145
|
+
return Object.fromEntries(Object.entries(object).map(([key, value]) => [callback(key), value]));
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* @example
|
|
149
|
+
* ```js
|
|
150
|
+
* mapObject({ a: 'aa', b: 'bb', c: 'cc' }, (key, value) =>
|
|
151
|
+
* key === 'b' ? undefined : [key + '1', value.toUpperCase()]
|
|
152
|
+
* ) // => { a1: "AA", c1: "CC" }
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
function mapObject(source, callback) {
|
|
156
|
+
return Object.fromEntries(Object.entries(source).flatMap(([key, value]) => {
|
|
157
|
+
const entry = callback(key, value);
|
|
158
|
+
if (entry) return [entry];
|
|
159
|
+
else return [];
|
|
160
|
+
}));
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* @example
|
|
164
|
+
* ```js
|
|
165
|
+
* flatMapObject({ a: 'aa', b: 'bb' }, (key, value) => [
|
|
166
|
+
* [key + '1', value + '1'],
|
|
167
|
+
* [key + '2', value + '2'],
|
|
168
|
+
* ]) // => { a1: "aa1", a2: "aa2", b1: "bb1", b2: "bb2" }
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
function flatMapObject(source, callback) {
|
|
172
|
+
return Object.fromEntries(Object.entries(source).flatMap(([key, value]) => {
|
|
173
|
+
return callback(key, value);
|
|
174
|
+
}));
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* filterObject(
|
|
180
|
+
* { a: 'aa', b: 'bb', c: 'cc' },
|
|
181
|
+
* (value): value is string => value !== 'bb'
|
|
182
|
+
* ) // => { a: "aa", c: "cc" }
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
function filterObject(source, fn) {
|
|
186
|
+
return mapObject(source, (key, value) => {
|
|
187
|
+
if (fn(value) === true) return [key, value];
|
|
188
|
+
else return null;
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
//#endregion
|
|
193
|
+
export { GRADIENT_DIRECTIONS, applyEffect, applyEffectToGradient, customPropertyToken, disabledSelector, dur, filterObject, flatMapObject, gradient, halfLeading, mapKeys, mapObject, maxWidth, notDisabledSelector, px, transparentGradient };
|
|
194
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["transparentGradient","base: RgbaColor","effect: RgbaColor","parsed: RgbaColor"],"sources":["../src/index.ts"],"sourcesContent":["import rgba from 'polished/lib/color/rgba'\nimport rgbToColorString from 'polished/lib/color/rgbToColorString'\nimport parseToRgb from 'polished/lib/color/parseToRgb'\nimport linearGradient from 'polished/lib/mixins/linearGradient'\nimport type { RgbColor } from 'polished/lib/types/color'\n\nimport type {\n AlphaEffect,\n Effect,\n Effects,\n GradientMaterial,\n OpacityEffect,\n ReplaceEffect,\n TypographyDescriptor,\n} from '@charcoal-ui/foundation'\nimport type { Styles } from 'polished/lib/types/style'\n\nexport const GRADIENT_DIRECTIONS = [\n 'to top',\n 'to bottom',\n 'to left',\n 'to right',\n] as const\n\nexport type GradientDirection = (typeof GRADIENT_DIRECTIONS)[number]\n\nexport function transparentGradient(\n color: string,\n defaultDirection: GradientDirection = 'to bottom'\n) {\n return function transparentGradient(\n direction: GradientDirection | object = defaultDirection\n ): Styles {\n const transparent = rgba(color, 0)\n return linearGradient({\n colorStops: [color, transparent],\n fallback: transparent,\n toDirection: typeof direction === 'object' ? defaultDirection : direction,\n })\n }\n}\n\nexport function gradient(toDirection: GradientDirection = 'to bottom') {\n return function toLinearGradient(value: GradientMaterial): Styles {\n return linearGradient({\n colorStops: value.map(({ color, ratio }) => `${color} ${ratio}%`),\n fallback: value[0]?.color,\n toDirection,\n })\n }\n}\n\nexport function applyEffectToGradient(effect: Effects) {\n return function toGradient(value: GradientMaterial): GradientMaterial {\n return value.map(({ color, ratio }) => ({\n color: applyEffect(color, effect),\n ratio,\n }))\n }\n}\n\ninterface RgbaColor extends RgbColor {\n alpha?: number\n}\n\ninterface ReadonlyArrayConstructor {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n isArray(value: any): value is readonly any[]\n}\n\nexport function applyEffect(\n baseColor: string | null,\n effects: Effects\n): string {\n const color = baseColor ?? '#00000000'\n if ((Array as ReadonlyArrayConstructor).isArray(effects)) {\n return effects.reduce(applySingleEffect, color)\n }\n return applySingleEffect(color, effects)\n}\n\nfunction applySingleEffect(baseColor: string, effect: Effect): string {\n switch (effect.type) {\n case 'alpha':\n return applyAlpha(baseColor, effect)\n case 'opacity':\n return applyOpacity(baseColor, effect)\n case 'replace':\n return applyReplace(baseColor, effect)\n default:\n throw new RangeError(\n `Unknown effect type ${\n (effect as Effect).type\n }, upgrade @charcoal-ui/utils`\n )\n }\n}\n\nfunction applyAlpha(baseColor: string, { color, opacity }: AlphaEffect) {\n const base: RgbaColor = parseToRgb(baseColor)\n const effect: RgbaColor = parseToRgb(color)\n const src = [base.red, base.green, base.blue, base.alpha ?? 1.0] as const\n const dst = [\n effect.red,\n effect.green,\n effect.blue,\n clamp(0, 1, (effect.alpha ?? 1.0) * (opacity ?? 1.0)),\n ] as const\n return rgba(...alphaBlend(src, dst))\n}\n\nfunction applyOpacity(baseColor: string, { opacity }: OpacityEffect) {\n const parsed: RgbaColor = parseToRgb(baseColor)\n parsed.alpha = clamp(0, 1, (parsed.alpha ?? 1.0) * opacity)\n return rgbToColorString(parsed)\n}\n\nfunction applyReplace(\n baseColor: string,\n { color = baseColor, opacity }: ReplaceEffect\n) {\n if (opacity === undefined) {\n return color\n }\n const parsed: RgbaColor = parseToRgb(color)\n // NOTE: intentionally ignores any alpha value in the baseColor\n parsed.alpha = opacity\n return rgbToColorString(parsed)\n}\n\ntype Color4 = readonly [number, number, number, number]\n\n/**\n * NOTE: alpha component must be in range from 0.0 to 1.0. (0.0 represents a fully transparent)\n *\n * @param src `[r, g, b, alpha]` Background\n * @param dst `[r, g, b, alpha]` Foreground\n */\nfunction alphaBlend(src: Color4, dst: Color4): Color4 {\n const srcA = src[3]\n const dstA = dst[3]\n const outA = srcA + dstA * (1 - srcA)\n if (outA < EPS) {\n // blending 0% alpha with 0% alpha\n return [0, 0, 0, 0]\n }\n return [\n Math.round((src[0] * srcA * (1 - dstA) + dst[0] * dstA) / outA),\n Math.round((src[1] * srcA * (1 - dstA) + dst[1] * dstA) / outA),\n Math.round((src[2] * srcA * (1 - dstA) + dst[2] * dstA) / outA),\n outA,\n ]\n}\nconst EPS = 1e-6\n\nfunction clamp(min: number, max: number, value: number) {\n return Math.min(Math.max(value, min), max)\n}\n\n/**\n * affix `px` unit\n *\n * @param value pixel\n */\nexport function px(value: number) {\n return `${value}px`\n}\n\n/**\n * affix `s` unit\n *\n * @param value second\n */\nexport function dur(value: number) {\n return `${value}s`\n}\n\nexport const notDisabledSelector = `&:not(:disabled):not([aria-disabled]), &[aria-disabled=false]`\n\nexport const disabledSelector = `&:disabled, &[aria-disabled]:not([aria-disabled=false])`\n\n/**\n * Construct media query from breakpoint\n */\nexport function maxWidth(breakpoint: number) {\n return `(max-width: ${breakpoint - 1}px)`\n}\n\n/**\n * Derive half-leading from typography size\n */\nexport const halfLeading = ({\n fontSize,\n lineHeight,\n}: TypographyDescriptor): number => (lineHeight - fontSize) / 2\n\n/**\n * Namespaced custom property\n */\nexport const customPropertyToken = (\n id: string,\n modifiers: readonly string[] = []\n): `--charcoal-${string}` =>\n `--charcoal-${id}${modifiers.length === 0 ? '' : ['', modifiers].join('-')}`\n\n/**\n * @example\n * ```js\n * mapKeys({ a: 'aa', b: 'bb' }, (key) => key.toUpperCase()) // => { A: \"aa\", B: \"bb\" }\n * ````\n */\nexport function mapKeys<V, K extends string>(\n object: Record<string, V>,\n callback: (key: string) => K\n) {\n return Object.fromEntries(\n Object.entries(object).map(([key, value]) => [callback(key), value])\n ) as Record<K, V>\n}\n\n/**\n * @example\n * ```js\n * mapObject({ a: 'aa', b: 'bb', c: 'cc' }, (key, value) =>\n * key === 'b' ? undefined : [key + '1', value.toUpperCase()]\n * ) // => { a1: \"AA\", c1: \"CC\" }\n * ```\n */\nexport function mapObject<\n SourceKey extends string,\n SourceValue,\n DestKey extends string,\n DestValue\n>(\n source: Record<SourceKey, SourceValue>,\n callback: (\n key: SourceKey,\n value: SourceValue\n ) => [DestKey, DestValue] | null | undefined\n) {\n return Object.fromEntries(\n Object.entries(source).flatMap(([key, value]) => {\n const entry = callback(key as SourceKey, value as SourceValue)\n if (entry) {\n return [entry]\n } else {\n return []\n }\n })\n ) as Record<DestKey, DestValue>\n}\n\n/**\n * @example\n * ```js\n * flatMapObject({ a: 'aa', b: 'bb' }, (key, value) => [\n * [key + '1', value + '1'],\n * [key + '2', value + '2'],\n * ]) // => { a1: \"aa1\", a2: \"aa2\", b1: \"bb1\", b2: \"bb2\" }\n * ```\n */\nexport function flatMapObject<\n SourceKey extends string,\n SourceValue,\n DestKey extends string,\n DestValue\n>(\n source: Record<SourceKey, SourceValue>,\n callback: (key: SourceKey, value: SourceValue) => [DestKey, DestValue][]\n) {\n return Object.fromEntries(\n Object.entries(source).flatMap(([key, value]) => {\n return callback(key as SourceKey, value as SourceValue)\n })\n ) as Record<DestKey, DestValue>\n}\n\n/**\n * @example\n * ```ts\n * filterObject(\n * { a: 'aa', b: 'bb', c: 'cc' },\n * (value): value is string => value !== 'bb'\n * ) // => { a: \"aa\", c: \"cc\" }\n * ```\n */\nexport function filterObject<Source, Dest extends Source>(\n source: Record<string, Source>,\n fn: (value: Source) => value is Dest\n) {\n return mapObject(source, (key, value) => {\n if (fn(value) === true) {\n return [key, value]\n } else {\n return null\n }\n }) as Record<string, Dest>\n}\n"],"mappings":";;;;;;AAiBA,MAAa,sBAAsB;CACjC;CACA;CACA;CACA;CACD;AAID,SAAgB,oBACd,OACA,mBAAsC,aACtC;AACA,QAAO,SAASA,sBACd,YAAwC,kBAChC;EACR,MAAM,cAAc,KAAK,OAAO,EAAE;AAClC,SAAO,eAAe;GACpB,YAAY,CAAC,OAAO,YAAY;GAChC,UAAU;GACV,aAAa,OAAO,cAAc,WAAW,mBAAmB;GACjE,CAAC;;;AAIN,SAAgB,SAAS,cAAiC,aAAa;AACrE,QAAO,SAAS,iBAAiB,OAAiC;AAChE,SAAO,eAAe;GACpB,YAAY,MAAM,KAAK,EAAE,OAAO,YAAY,GAAG,MAAM,GAAG,MAAM,GAAG;GACjE,UAAU,MAAM,IAAI;GACpB;GACD,CAAC;;;AAIN,SAAgB,sBAAsB,QAAiB;AACrD,QAAO,SAAS,WAAW,OAA2C;AACpE,SAAO,MAAM,KAAK,EAAE,OAAO,aAAa;GACtC,OAAO,YAAY,OAAO,OAAO;GACjC;GACD,EAAE;;;AAaP,SAAgB,YACd,WACA,SACQ;CACR,MAAM,QAAQ,aAAa;AAC3B,KAAK,MAAmC,QAAQ,QAAQ,CACtD,QAAO,QAAQ,OAAO,mBAAmB,MAAM;AAEjD,QAAO,kBAAkB,OA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KAAK,KAChB,QAAO,CAAC,KAAK,MAAM;MAEnB,QAAO;GAET"}
|
package/package.json
CHANGED
|
@@ -1,26 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@charcoal-ui/utils",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
3
|
+
"version": "5.0.0-beta.3",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
7
8
|
"exports": {
|
|
8
|
-
"types":
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
"types": {
|
|
10
|
+
"import": "./dist/index.d.ts",
|
|
11
|
+
"require": "./dist/index.d.cts",
|
|
12
|
+
"default": "./dist/index.d.ts"
|
|
13
|
+
},
|
|
14
|
+
"require": "./dist/index.cjs",
|
|
15
|
+
"default": "./dist/index.js"
|
|
11
16
|
},
|
|
12
17
|
"types": "./dist/index.d.ts",
|
|
13
18
|
"sideEffects": false,
|
|
14
|
-
"devDependencies": {
|
|
15
|
-
"npm-run-all": "^4.1.5",
|
|
16
|
-
"rimraf": "^3.0.2",
|
|
17
|
-
"tsup": "^6.5.0",
|
|
18
|
-
"typescript": "^4.9.5",
|
|
19
|
-
"vitest": "^2.0.2"
|
|
20
|
-
},
|
|
21
19
|
"dependencies": {
|
|
22
20
|
"polished": "^4.1.4",
|
|
23
|
-
"@charcoal-ui/foundation": "5.0.0-beta.
|
|
21
|
+
"@charcoal-ui/foundation": "5.0.0-beta.3"
|
|
24
22
|
},
|
|
25
23
|
"files": [
|
|
26
24
|
"src",
|
|
@@ -36,11 +34,9 @@
|
|
|
36
34
|
},
|
|
37
35
|
"gitHead": "e1ece2e43901ae667afdd5c178040607d939dcd5",
|
|
38
36
|
"scripts": {
|
|
39
|
-
"build": "
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"typecheck": "tsc --project tsconfig.build.json --pretty --noEmit",
|
|
43
|
-
"clean": "rimraf dist .tsbuildinfo",
|
|
37
|
+
"build": "tsdown",
|
|
38
|
+
"typecheck": "tsc --pretty --noEmit",
|
|
39
|
+
"clean": "rimraf dist",
|
|
44
40
|
"test": "vitest --passWithNoTests"
|
|
45
41
|
}
|
|
46
42
|
}
|
package/src/index.ts
CHANGED
|
@@ -2,17 +2,18 @@ import rgba from 'polished/lib/color/rgba'
|
|
|
2
2
|
import rgbToColorString from 'polished/lib/color/rgbToColorString'
|
|
3
3
|
import parseToRgb from 'polished/lib/color/parseToRgb'
|
|
4
4
|
import linearGradient from 'polished/lib/mixins/linearGradient'
|
|
5
|
-
import {
|
|
5
|
+
import type { RgbColor } from 'polished/lib/types/color'
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
import type {
|
|
8
|
+
AlphaEffect,
|
|
9
|
+
Effect,
|
|
10
|
+
Effects,
|
|
11
|
+
GradientMaterial,
|
|
12
|
+
OpacityEffect,
|
|
13
|
+
ReplaceEffect,
|
|
14
|
+
TypographyDescriptor,
|
|
15
15
|
} from '@charcoal-ui/foundation'
|
|
16
|
+
import type { Styles } from 'polished/lib/types/style'
|
|
16
17
|
|
|
17
18
|
export const GRADIENT_DIRECTIONS = [
|
|
18
19
|
'to top',
|
|
@@ -29,7 +30,7 @@ export function transparentGradient(
|
|
|
29
30
|
) {
|
|
30
31
|
return function transparentGradient(
|
|
31
32
|
direction: GradientDirection | object = defaultDirection
|
|
32
|
-
) {
|
|
33
|
+
): Styles {
|
|
33
34
|
const transparent = rgba(color, 0)
|
|
34
35
|
return linearGradient({
|
|
35
36
|
colorStops: [color, transparent],
|
|
@@ -40,7 +41,7 @@ export function transparentGradient(
|
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
export function gradient(toDirection: GradientDirection = 'to bottom') {
|
|
43
|
-
return function toLinearGradient(value: GradientMaterial) {
|
|
44
|
+
return function toLinearGradient(value: GradientMaterial): Styles {
|
|
44
45
|
return linearGradient({
|
|
45
46
|
colorStops: value.map(({ color, ratio }) => `${color} ${ratio}%`),
|
|
46
47
|
fallback: value[0]?.color,
|
|
@@ -188,8 +189,10 @@ export function maxWidth(breakpoint: number) {
|
|
|
188
189
|
/**
|
|
189
190
|
* Derive half-leading from typography size
|
|
190
191
|
*/
|
|
191
|
-
export const halfLeading = ({
|
|
192
|
-
|
|
192
|
+
export const halfLeading = ({
|
|
193
|
+
fontSize,
|
|
194
|
+
lineHeight,
|
|
195
|
+
}: TypographyDescriptor): number => (lineHeight - fontSize) / 2
|
|
193
196
|
|
|
194
197
|
/**
|
|
195
198
|
* Namespaced custom property
|
package/dist/index.cjs.js
DELETED
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/index.ts
|
|
31
|
-
var src_exports = {};
|
|
32
|
-
__export(src_exports, {
|
|
33
|
-
GRADIENT_DIRECTIONS: () => GRADIENT_DIRECTIONS,
|
|
34
|
-
applyEffect: () => applyEffect,
|
|
35
|
-
applyEffectToGradient: () => applyEffectToGradient,
|
|
36
|
-
customPropertyToken: () => customPropertyToken,
|
|
37
|
-
disabledSelector: () => disabledSelector,
|
|
38
|
-
dur: () => dur,
|
|
39
|
-
filterObject: () => filterObject,
|
|
40
|
-
flatMapObject: () => flatMapObject,
|
|
41
|
-
gradient: () => gradient,
|
|
42
|
-
halfLeading: () => halfLeading,
|
|
43
|
-
mapKeys: () => mapKeys,
|
|
44
|
-
mapObject: () => mapObject,
|
|
45
|
-
maxWidth: () => maxWidth,
|
|
46
|
-
notDisabledSelector: () => notDisabledSelector,
|
|
47
|
-
px: () => px,
|
|
48
|
-
transparentGradient: () => transparentGradient
|
|
49
|
-
});
|
|
50
|
-
module.exports = __toCommonJS(src_exports);
|
|
51
|
-
var import_rgba = __toESM(require("polished/lib/color/rgba"));
|
|
52
|
-
var import_rgbToColorString = __toESM(require("polished/lib/color/rgbToColorString"));
|
|
53
|
-
var import_parseToRgb = __toESM(require("polished/lib/color/parseToRgb"));
|
|
54
|
-
var import_linearGradient = __toESM(require("polished/lib/mixins/linearGradient"));
|
|
55
|
-
var GRADIENT_DIRECTIONS = [
|
|
56
|
-
"to top",
|
|
57
|
-
"to bottom",
|
|
58
|
-
"to left",
|
|
59
|
-
"to right"
|
|
60
|
-
];
|
|
61
|
-
function transparentGradient(color, defaultDirection = "to bottom") {
|
|
62
|
-
return function transparentGradient2(direction = defaultDirection) {
|
|
63
|
-
const transparent = (0, import_rgba.default)(color, 0);
|
|
64
|
-
return (0, import_linearGradient.default)({
|
|
65
|
-
colorStops: [color, transparent],
|
|
66
|
-
fallback: transparent,
|
|
67
|
-
toDirection: typeof direction === "object" ? defaultDirection : direction
|
|
68
|
-
});
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
function gradient(toDirection = "to bottom") {
|
|
72
|
-
return function toLinearGradient(value) {
|
|
73
|
-
return (0, import_linearGradient.default)({
|
|
74
|
-
colorStops: value.map(({ color, ratio }) => `${color} ${ratio}%`),
|
|
75
|
-
fallback: value[0]?.color,
|
|
76
|
-
toDirection
|
|
77
|
-
});
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
function applyEffectToGradient(effect) {
|
|
81
|
-
return function toGradient(value) {
|
|
82
|
-
return value.map(({ color, ratio }) => ({
|
|
83
|
-
color: applyEffect(color, effect),
|
|
84
|
-
ratio
|
|
85
|
-
}));
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
function applyEffect(baseColor, effects) {
|
|
89
|
-
const color = baseColor ?? "#00000000";
|
|
90
|
-
if (Array.isArray(effects)) {
|
|
91
|
-
return effects.reduce(applySingleEffect, color);
|
|
92
|
-
}
|
|
93
|
-
return applySingleEffect(color, effects);
|
|
94
|
-
}
|
|
95
|
-
function applySingleEffect(baseColor, effect) {
|
|
96
|
-
switch (effect.type) {
|
|
97
|
-
case "alpha":
|
|
98
|
-
return applyAlpha(baseColor, effect);
|
|
99
|
-
case "opacity":
|
|
100
|
-
return applyOpacity(baseColor, effect);
|
|
101
|
-
case "replace":
|
|
102
|
-
return applyReplace(baseColor, effect);
|
|
103
|
-
default:
|
|
104
|
-
throw new RangeError(
|
|
105
|
-
`Unknown effect type ${effect.type}, upgrade @charcoal-ui/utils`
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
function applyAlpha(baseColor, { color, opacity }) {
|
|
110
|
-
const base = (0, import_parseToRgb.default)(baseColor);
|
|
111
|
-
const effect = (0, import_parseToRgb.default)(color);
|
|
112
|
-
const src = [base.red, base.green, base.blue, base.alpha ?? 1];
|
|
113
|
-
const dst = [
|
|
114
|
-
effect.red,
|
|
115
|
-
effect.green,
|
|
116
|
-
effect.blue,
|
|
117
|
-
clamp(0, 1, (effect.alpha ?? 1) * (opacity ?? 1))
|
|
118
|
-
];
|
|
119
|
-
return (0, import_rgba.default)(...alphaBlend(src, dst));
|
|
120
|
-
}
|
|
121
|
-
function applyOpacity(baseColor, { opacity }) {
|
|
122
|
-
const parsed = (0, import_parseToRgb.default)(baseColor);
|
|
123
|
-
parsed.alpha = clamp(0, 1, (parsed.alpha ?? 1) * opacity);
|
|
124
|
-
return (0, import_rgbToColorString.default)(parsed);
|
|
125
|
-
}
|
|
126
|
-
function applyReplace(baseColor, { color = baseColor, opacity }) {
|
|
127
|
-
if (opacity === void 0) {
|
|
128
|
-
return color;
|
|
129
|
-
}
|
|
130
|
-
const parsed = (0, import_parseToRgb.default)(color);
|
|
131
|
-
parsed.alpha = opacity;
|
|
132
|
-
return (0, import_rgbToColorString.default)(parsed);
|
|
133
|
-
}
|
|
134
|
-
function alphaBlend(src, dst) {
|
|
135
|
-
const srcA = src[3];
|
|
136
|
-
const dstA = dst[3];
|
|
137
|
-
const outA = srcA + dstA * (1 - srcA);
|
|
138
|
-
if (outA < EPS) {
|
|
139
|
-
return [0, 0, 0, 0];
|
|
140
|
-
}
|
|
141
|
-
return [
|
|
142
|
-
Math.round((src[0] * srcA * (1 - dstA) + dst[0] * dstA) / outA),
|
|
143
|
-
Math.round((src[1] * srcA * (1 - dstA) + dst[1] * dstA) / outA),
|
|
144
|
-
Math.round((src[2] * srcA * (1 - dstA) + dst[2] * dstA) / outA),
|
|
145
|
-
outA
|
|
146
|
-
];
|
|
147
|
-
}
|
|
148
|
-
var EPS = 1e-6;
|
|
149
|
-
function clamp(min, max, value) {
|
|
150
|
-
return Math.min(Math.max(value, min), max);
|
|
151
|
-
}
|
|
152
|
-
function px(value) {
|
|
153
|
-
return `${value}px`;
|
|
154
|
-
}
|
|
155
|
-
function dur(value) {
|
|
156
|
-
return `${value}s`;
|
|
157
|
-
}
|
|
158
|
-
var notDisabledSelector = `&:not(:disabled):not([aria-disabled]), &[aria-disabled=false]`;
|
|
159
|
-
var disabledSelector = `&:disabled, &[aria-disabled]:not([aria-disabled=false])`;
|
|
160
|
-
function maxWidth(breakpoint) {
|
|
161
|
-
return `(max-width: ${breakpoint - 1}px)`;
|
|
162
|
-
}
|
|
163
|
-
var halfLeading = ({ fontSize, lineHeight }) => (lineHeight - fontSize) / 2;
|
|
164
|
-
var customPropertyToken = (id, modifiers = []) => `--charcoal-${id}${modifiers.length === 0 ? "" : ["", modifiers].join("-")}`;
|
|
165
|
-
function mapKeys(object, callback) {
|
|
166
|
-
return Object.fromEntries(
|
|
167
|
-
Object.entries(object).map(([key, value]) => [callback(key), value])
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
function mapObject(source, callback) {
|
|
171
|
-
return Object.fromEntries(
|
|
172
|
-
Object.entries(source).flatMap(([key, value]) => {
|
|
173
|
-
const entry = callback(key, value);
|
|
174
|
-
if (entry) {
|
|
175
|
-
return [entry];
|
|
176
|
-
} else {
|
|
177
|
-
return [];
|
|
178
|
-
}
|
|
179
|
-
})
|
|
180
|
-
);
|
|
181
|
-
}
|
|
182
|
-
function flatMapObject(source, callback) {
|
|
183
|
-
return Object.fromEntries(
|
|
184
|
-
Object.entries(source).flatMap(([key, value]) => {
|
|
185
|
-
return callback(key, value);
|
|
186
|
-
})
|
|
187
|
-
);
|
|
188
|
-
}
|
|
189
|
-
function filterObject(source, fn) {
|
|
190
|
-
return mapObject(source, (key, value) => {
|
|
191
|
-
if (fn(value) === true) {
|
|
192
|
-
return [key, value];
|
|
193
|
-
} else {
|
|
194
|
-
return null;
|
|
195
|
-
}
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
199
|
-
0 && (module.exports = {
|
|
200
|
-
GRADIENT_DIRECTIONS,
|
|
201
|
-
applyEffect,
|
|
202
|
-
applyEffectToGradient,
|
|
203
|
-
customPropertyToken,
|
|
204
|
-
disabledSelector,
|
|
205
|
-
dur,
|
|
206
|
-
filterObject,
|
|
207
|
-
flatMapObject,
|
|
208
|
-
gradient,
|
|
209
|
-
halfLeading,
|
|
210
|
-
mapKeys,
|
|
211
|
-
mapObject,
|
|
212
|
-
maxWidth,
|
|
213
|
-
notDisabledSelector,
|
|
214
|
-
px,
|
|
215
|
-
transparentGradient
|
|
216
|
-
});
|
|
217
|
-
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.cjs.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import rgba from 'polished/lib/color/rgba'\nimport rgbToColorString from 'polished/lib/color/rgbToColorString'\nimport parseToRgb from 'polished/lib/color/parseToRgb'\nimport linearGradient from 'polished/lib/mixins/linearGradient'\nimport { type RgbColor } from 'polished/lib/types/color'\n\nimport {\n type AlphaEffect,\n type Effect,\n type Effects,\n type GradientMaterial,\n type OpacityEffect,\n type ReplaceEffect,\n type TypographyDescriptor,\n} from '@charcoal-ui/foundation'\n\nexport const GRADIENT_DIRECTIONS = [\n 'to top',\n 'to bottom',\n 'to left',\n 'to right',\n] as const\n\nexport type GradientDirection = (typeof GRADIENT_DIRECTIONS)[number]\n\nexport function transparentGradient(\n color: string,\n defaultDirection: GradientDirection = 'to bottom'\n) {\n return function transparentGradient(\n direction: GradientDirection | object = defaultDirection\n ) {\n const transparent = rgba(color, 0)\n return linearGradient({\n colorStops: [color, transparent],\n fallback: transparent,\n toDirection: typeof direction === 'object' ? defaultDirection : direction,\n })\n }\n}\n\nexport function gradient(toDirection: GradientDirection = 'to bottom') {\n return function toLinearGradient(value: GradientMaterial) {\n return linearGradient({\n colorStops: value.map(({ color, ratio }) => `${color} ${ratio}%`),\n fallback: value[0]?.color,\n toDirection,\n })\n }\n}\n\nexport function applyEffectToGradient(effect: Effects) {\n return function toGradient(value: GradientMaterial): GradientMaterial {\n return value.map(({ color, ratio }) => ({\n color: applyEffect(color, effect),\n ratio,\n }))\n }\n}\n\ninterface RgbaColor extends RgbColor {\n alpha?: number\n}\n\ninterface ReadonlyArrayConstructor {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n isArray(value: any): value is readonly any[]\n}\n\nexport function applyEffect(\n baseColor: string | null,\n effects: Effects\n): string {\n const color = baseColor ?? '#00000000'\n if ((Array as ReadonlyArrayConstructor).isArray(effects)) {\n return effects.reduce(applySingleEffect, color)\n }\n return applySingleEffect(color, effects)\n}\n\nfunction applySingleEffect(baseColor: string, effect: Effect): string {\n switch (effect.type) {\n case 'alpha':\n return applyAlpha(baseColor, effect)\n case 'opacity':\n return applyOpacity(baseColor, effect)\n case 'replace':\n return applyReplace(baseColor, effect)\n default:\n throw new RangeError(\n `Unknown effect type ${\n (effect as Effect).type\n }, upgrade @charcoal-ui/utils`\n )\n }\n}\n\nfunction applyAlpha(baseColor: string, { color, opacity }: AlphaEffect) {\n const base: RgbaColor = parseToRgb(baseColor)\n const effect: RgbaColor = parseToRgb(color)\n const src = [base.red, base.green, base.blue, base.alpha ?? 1.0] as const\n const dst = [\n effect.red,\n effect.green,\n effect.blue,\n clamp(0, 1, (effect.alpha ?? 1.0) * (opacity ?? 1.0)),\n ] as const\n return rgba(...alphaBlend(src, dst))\n}\n\nfunction applyOpacity(baseColor: string, { opacity }: OpacityEffect) {\n const parsed: RgbaColor = parseToRgb(baseColor)\n parsed.alpha = clamp(0, 1, (parsed.alpha ?? 1.0) * opacity)\n return rgbToColorString(parsed)\n}\n\nfunction applyReplace(\n baseColor: string,\n { color = baseColor, opacity }: ReplaceEffect\n) {\n if (opacity === undefined) {\n return color\n }\n const parsed: RgbaColor = parseToRgb(color)\n // NOTE: intentionally ignores any alpha value in the baseColor\n parsed.alpha = opacity\n return rgbToColorString(parsed)\n}\n\ntype Color4 = readonly [number, number, number, number]\n\n/**\n * NOTE: alpha component must be in range from 0.0 to 1.0. (0.0 represents a fully transparent)\n *\n * @param src `[r, g, b, alpha]` Background\n * @param dst `[r, g, b, alpha]` Foreground\n */\nfunction alphaBlend(src: Color4, dst: Color4): Color4 {\n const srcA = src[3]\n const dstA = dst[3]\n const outA = srcA + dstA * (1 - srcA)\n if (outA < EPS) {\n // blending 0% alpha with 0% alpha\n return [0, 0, 0, 0]\n }\n return [\n Math.round((src[0] * srcA * (1 - dstA) + dst[0] * dstA) / outA),\n Math.round((src[1] * srcA * (1 - dstA) + dst[1] * dstA) / outA),\n Math.round((src[2] * srcA * (1 - dstA) + dst[2] * dstA) / outA),\n outA,\n ]\n}\nconst EPS = 1e-6\n\nfunction clamp(min: number, max: number, value: number) {\n return Math.min(Math.max(value, min), max)\n}\n\n/**\n * affix `px` unit\n *\n * @param value pixel\n */\nexport function px(value: number) {\n return `${value}px`\n}\n\n/**\n * affix `s` unit\n *\n * @param value second\n */\nexport function dur(value: number) {\n return `${value}s`\n}\n\nexport const notDisabledSelector = `&:not(:disabled):not([aria-disabled]), &[aria-disabled=false]`\n\nexport const disabledSelector = `&:disabled, &[aria-disabled]:not([aria-disabled=false])`\n\n/**\n * Construct media query from breakpoint\n */\nexport function maxWidth(breakpoint: number) {\n return `(max-width: ${breakpoint - 1}px)`\n}\n\n/**\n * Derive half-leading from typography size\n */\nexport const halfLeading = ({ fontSize, lineHeight }: TypographyDescriptor) =>\n (lineHeight - fontSize) / 2\n\n/**\n * Namespaced custom property\n */\nexport const customPropertyToken = (\n id: string,\n modifiers: readonly string[] = []\n): `--charcoal-${string}` =>\n `--charcoal-${id}${modifiers.length === 0 ? '' : ['', modifiers].join('-')}`\n\n/**\n * @example\n * ```js\n * mapKeys({ a: 'aa', b: 'bb' }, (key) => key.toUpperCase()) // => { A: \"aa\", B: \"bb\" }\n * ````\n */\nexport function mapKeys<V, K extends string>(\n object: Record<string, V>,\n callback: (key: string) => K\n) {\n return Object.fromEntries(\n Object.entries(object).map(([key, value]) => [callback(key), value])\n ) as Record<K, V>\n}\n\n/**\n * @example\n * ```js\n * mapObject({ a: 'aa', b: 'bb', c: 'cc' }, (key, value) =>\n * key === 'b' ? undefined : [key + '1', value.toUpperCase()]\n * ) // => { a1: \"AA\", c1: \"CC\" }\n * ```\n */\nexport function mapObject<\n SourceKey extends string,\n SourceValue,\n DestKey extends string,\n DestValue\n>(\n source: Record<SourceKey, SourceValue>,\n callback: (\n key: SourceKey,\n value: SourceValue\n ) => [DestKey, DestValue] | null | undefined\n) {\n return Object.fromEntries(\n Object.entries(source).flatMap(([key, value]) => {\n const entry = callback(key as SourceKey, value as SourceValue)\n if (entry) {\n return [entry]\n } else {\n return []\n }\n })\n ) as Record<DestKey, DestValue>\n}\n\n/**\n * @example\n * ```js\n * flatMapObject({ a: 'aa', b: 'bb' }, (key, value) => [\n * [key + '1', value + '1'],\n * [key + '2', value + '2'],\n * ]) // => { a1: \"aa1\", a2: \"aa2\", b1: \"bb1\", b2: \"bb2\" }\n * ```\n */\nexport function flatMapObject<\n SourceKey extends string,\n SourceValue,\n DestKey extends string,\n DestValue\n>(\n source: Record<SourceKey, SourceValue>,\n callback: (key: SourceKey, value: SourceValue) => [DestKey, DestValue][]\n) {\n return Object.fromEntries(\n Object.entries(source).flatMap(([key, value]) => {\n return callback(key as SourceKey, value as SourceValue)\n })\n ) as Record<DestKey, DestValue>\n}\n\n/**\n * @example\n * ```ts\n * filterObject(\n * { a: 'aa', b: 'bb', c: 'cc' },\n * (value): value is string => value !== 'bb'\n * ) // => { a: \"aa\", c: \"cc\" }\n * ```\n */\nexport function filterObject<Source, Dest extends Source>(\n source: Record<string, Source>,\n fn: (value: Source) => value is Dest\n) {\n return mapObject(source, (key, value) => {\n if (fn(value) === true) {\n return [key, value]\n } else {\n return null\n }\n }) as Record<string, Dest>\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAiB;AACjB,8BAA6B;AAC7B,wBAAuB;AACvB,4BAA2B;AAapB,IAAM,sBAAsB;AAAA,EACjC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAIO,SAAS,oBACd,OACA,mBAAsC,aACtC;AACA,SAAO,SAASA,qBACd,YAAwC,kBACxC;AACA,UAAM,kBAAc,YAAAC,SAAK,OAAO,CAAC;AACjC,eAAO,sBAAAC,SAAe;AAAA,MACpB,YAAY,CAAC,OAAO,WAAW;AAAA,MAC/B,UAAU;AAAA,MACV,aAAa,OAAO,cAAc,WAAW,mBAAmB;AAAA,IAClE,CAAC;AAAA,EACH;AACF;AAEO,SAAS,SAAS,cAAiC,aAAa;AACrE,SAAO,SAAS,iBAAiB,OAAyB;AACxD,eAAO,sBAAAA,SAAe;AAAA,MACpB,YAAY,MAAM,IAAI,CAAC,EAAE,OAAO,MAAM,MAAM,GAAG,SAAS,QAAQ;AAAA,MAChE,UAAU,MAAM,CAAC,GAAG;AAAA,MACpB;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAEO,SAAS,sBAAsB,QAAiB;AACrD,SAAO,SAAS,WAAW,OAA2C;AACpE,WAAO,MAAM,IAAI,CAAC,EAAE,OAAO,MAAM,OAAO;AAAA,MACtC,OAAO,YAAY,OAAO,MAAM;AAAA,MAChC;AAAA,IACF,EAAE;AAAA,EACJ;AACF;AAWO,SAAS,YACd,WACA,SACQ;AACR,QAAM,QAAQ,aAAa;AAC3B,MAAK,MAAmC,QAAQ,OAAO,GAAG;AACxD,WAAO,QAAQ,OAAO,mBAAmB,KAAK;AAAA,EAChD;AACA,SAA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