@yahoo/uds-create-config 5.57.0 → 5.58.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yahoo/uds-create-config",
3
- "version": "5.57.0",
3
+ "version": "5.58.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1,67 +0,0 @@
1
- import { StoredDefaultCondition } from "../../framework/default-conditions.js";
2
-
3
- //#region src/entities/system/conditional-defaults.d.ts
4
- declare const CONDITIONAL_DEFAULT: unique symbol;
5
- /** One condition of a {@link ConditionalDefault}: the value the prop defaults to while `when` holds. */
6
- interface ConditionalDefaultEntry<V = unknown> {
7
- readonly when: Readonly<Record<string, unknown>>;
8
- readonly value: V;
9
- }
10
- /** A default chosen by the component's other props — see {@link conditionalDefault}. */
11
- interface ConditionalDefault<V = unknown> {
12
- readonly [CONDITIONAL_DEFAULT]: true;
13
- readonly fallback: V;
14
- readonly entries: readonly ConditionalDefaultEntry<V>[];
15
- }
16
- /**
17
- * A default that depends on the component's other props, for `.defaultProps` and
18
- * `.extend({ defaultProps })` only.
19
- *
20
- * ```ts
21
- * .defaultProps({
22
- * modal: true,
23
- * includeScrim: conditionalDefault(true, [{ when: { modal: false }, value: false }]),
24
- * })
25
- * ```
26
- *
27
- * `fallback` applies when no entry's `when` holds; when several hold, the later entry wins. A `when`
28
- * is the grammar a style rule uses, over the component's own props. A value the call site passes
29
- * still wins over any of it.
30
- */
31
- declare function conditionalDefault<const V>(fallback: V, entries: readonly ConditionalDefaultEntry<V>[]): ConditionalDefault<V>;
32
- declare function isConditionalDefault(value: unknown): value is ConditionalDefault;
33
- /**
34
- * Stored defaults as authored: the inverse of {@link splitConditionalDefaults}, each prop
35
- * `defaultPropsWhen` names given back as a {@link conditionalDefault} over its fallback.
36
- */
37
- declare function joinConditionalDefaults({
38
- defaultProps,
39
- defaultPropsWhen
40
- }: {
41
- defaultProps: Readonly<Record<string, unknown>> | undefined;
42
- defaultPropsWhen: Readonly<Record<string, StoredDefaultCondition>> | undefined;
43
- }): Record<string, unknown>;
44
- /** The conditions one prop defaults under, in stored order, as `conditionalDefault` entries. */
45
- declare function propDefaultConditions({
46
- defaultPropsWhen,
47
- prop
48
- }: {
49
- defaultPropsWhen: Readonly<Record<string, StoredDefaultCondition>> | undefined;
50
- prop: string;
51
- }): ConditionalDefaultEntry[];
52
- /**
53
- * `defaultPropsWhen` with one prop's conditions replaced by `entries`: the prop leaves every entry it
54
- * was in, an entry left setting nothing goes, and each new condition joins the entry already keyed
55
- * by it or is added at the end. What a surface editing one prop's conditions writes back whole.
56
- */
57
- declare function withPropDefaultConditions({
58
- defaultPropsWhen,
59
- prop,
60
- entries
61
- }: {
62
- defaultPropsWhen: Readonly<Record<string, StoredDefaultCondition>> | undefined;
63
- prop: string;
64
- entries: readonly ConditionalDefaultEntry[];
65
- }): Record<string, StoredDefaultCondition>;
66
- //#endregion
67
- export { ConditionalDefault, ConditionalDefaultEntry, conditionalDefault, isConditionalDefault, joinConditionalDefaults, propDefaultConditions, withPropDefaultConditions };
@@ -1,179 +0,0 @@
1
- import { isPlainObject } from "../../framework/utils/field-path.js";
2
- import { canonicalCondition } from "../../framework/utils/refs.js";
3
- //#region src/entities/system/conditional-defaults.ts
4
- const CONDITIONAL_DEFAULT = Symbol.for("uds.conditionalDefault");
5
- /**
6
- * A default that depends on the component's other props, for `.defaultProps` and
7
- * `.extend({ defaultProps })` only.
8
- *
9
- * ```ts
10
- * .defaultProps({
11
- * modal: true,
12
- * includeScrim: conditionalDefault(true, [{ when: { modal: false }, value: false }]),
13
- * })
14
- * ```
15
- *
16
- * `fallback` applies when no entry's `when` holds; when several hold, the later entry wins. A `when`
17
- * is the grammar a style rule uses, over the component's own props. A value the call site passes
18
- * still wins over any of it.
19
- */
20
- function conditionalDefault(fallback, entries) {
21
- return {
22
- [CONDITIONAL_DEFAULT]: true,
23
- fallback,
24
- entries
25
- };
26
- }
27
- function isConditionalDefault(value) {
28
- return typeof value === "object" && value !== null && value[CONDITIONAL_DEFAULT] === true;
29
- }
30
- /**
31
- * Authored defaults as stored: each {@link conditionalDefault} leaves its fallback in `defaultProps`
32
- * and its entries in `defaultPropsWhen`. `defaultPropsWhen` is `undefined` when nothing was
33
- * conditional, so a body without one stores exactly what it did before.
34
- *
35
- * Stored by condition, every prop shares one order of conditions, while precedence is per prop. The
36
- * order taken keeps each prop's own; two props that order the same two conditions oppositely can't
37
- * share one, and are refused.
38
- */
39
- function splitConditionalDefaults(authored) {
40
- const defaultProps = {};
41
- const perProp = /* @__PURE__ */ new Map();
42
- for (const [prop, value] of Object.entries(authored)) {
43
- if (!isConditionalDefault(value)) {
44
- defaultProps[prop] = value;
45
- continue;
46
- }
47
- if (value.fallback !== void 0) defaultProps[prop] = value.fallback;
48
- const entries = /* @__PURE__ */ new Map();
49
- for (const entry of value.entries) {
50
- const key = canonicalCondition(entry.when);
51
- entries.delete(key);
52
- entries.set(key, entry);
53
- }
54
- perProp.set(prop, entries);
55
- }
56
- if (perProp.size === 0) return { defaultProps };
57
- const order = conditionOrder(perProp);
58
- const defaultPropsWhen = {};
59
- for (const key of order) {
60
- let when;
61
- const props = {};
62
- for (const [prop, entries] of perProp) {
63
- const entry = entries.get(key);
64
- if (!entry) continue;
65
- when ??= { ...entry.when };
66
- props[prop] = entry.value;
67
- }
68
- if (when) defaultPropsWhen[key] = {
69
- when,
70
- props
71
- };
72
- }
73
- return {
74
- defaultProps,
75
- defaultPropsWhen
76
- };
77
- }
78
- /**
79
- * `defaultProps` as stored: each `conditionalDefault` splits into its fallback, which stays in
80
- * `defaultProps`, and its conditions, which go to `defaultPropsWhen`. Empty for a body whose
81
- * defaults are all plain, so it stores what it did before.
82
- */
83
- function storedDefaults(body) {
84
- if (!isPlainObject(body.defaultProps)) return {};
85
- const { defaultProps, defaultPropsWhen } = splitConditionalDefaults(body.defaultProps);
86
- return defaultPropsWhen ? {
87
- defaultProps,
88
- defaultPropsWhen
89
- } : {};
90
- }
91
- /**
92
- * One order of every condition that keeps each prop's own order, by first appearance where props
93
- * don't constrain it.
94
- */
95
- function conditionOrder(perProp) {
96
- const seen = [];
97
- const after = /* @__PURE__ */ new Map();
98
- for (const [prop, entries] of perProp) {
99
- const keys = [...entries.keys()];
100
- keys.forEach((key, at) => {
101
- if (!seen.includes(key)) seen.push(key);
102
- const earlier = after.get(key) ?? /* @__PURE__ */ new Map();
103
- for (const before of keys.slice(0, at)) earlier.set(before, prop);
104
- after.set(key, earlier);
105
- });
106
- }
107
- for (const [key, earlier] of after) for (const [before, prop] of earlier) {
108
- const other = after.get(before)?.get(key);
109
- if (other === void 0) continue;
110
- throw new Error(`\`${prop}\` puts \`${before}\` before \`${key}\` and \`${other}\` puts them the other way round. Conditional defaults share one order of conditions, so give both props the same order.`);
111
- }
112
- const order = [];
113
- const placed = /* @__PURE__ */ new Set();
114
- const placing = /* @__PURE__ */ new Set();
115
- const place = (key) => {
116
- if (placed.has(key)) return;
117
- if (placing.has(key)) throw new Error(`Conditional defaults order \`${key}\` both before and after another condition across their props. They share one order of conditions, so give every prop the same order.`);
118
- placing.add(key);
119
- for (const before of after.get(key)?.keys() ?? []) place(before);
120
- placing.delete(key);
121
- placed.add(key);
122
- order.push(key);
123
- };
124
- for (const key of seen) place(key);
125
- return order;
126
- }
127
- /**
128
- * Stored defaults as authored: the inverse of {@link splitConditionalDefaults}, each prop
129
- * `defaultPropsWhen` names given back as a {@link conditionalDefault} over its fallback.
130
- */
131
- function joinConditionalDefaults({ defaultProps, defaultPropsWhen }) {
132
- const out = { ...defaultProps };
133
- const entries = /* @__PURE__ */ new Map();
134
- for (const { when, props } of Object.values(defaultPropsWhen ?? {})) for (const [prop, value] of Object.entries(props ?? {})) entries.set(prop, [...entries.get(prop) ?? [], {
135
- when,
136
- value
137
- }]);
138
- for (const [prop, list] of entries) out[prop] = conditionalDefault(out[prop], list);
139
- return out;
140
- }
141
- /** The conditions one prop defaults under, in stored order, as `conditionalDefault` entries. */
142
- function propDefaultConditions({ defaultPropsWhen, prop }) {
143
- return Object.values(defaultPropsWhen ?? {}).flatMap(({ when, props }) => props && Object.hasOwn(props, prop) ? [{
144
- when,
145
- value: props[prop]
146
- }] : []);
147
- }
148
- /**
149
- * `defaultPropsWhen` with one prop's conditions replaced by `entries`: the prop leaves every entry it
150
- * was in, an entry left setting nothing goes, and each new condition joins the entry already keyed
151
- * by it or is added at the end. What a surface editing one prop's conditions writes back whole.
152
- */
153
- function withPropDefaultConditions({ defaultPropsWhen, prop, entries }) {
154
- const out = {};
155
- for (const [key, { when, props }] of Object.entries(defaultPropsWhen ?? {})) {
156
- const { [prop]: _left, ...rest } = props ?? {};
157
- if (Object.keys(rest).length > 0) out[key] = {
158
- when,
159
- props: rest
160
- };
161
- }
162
- for (const entry of entries) {
163
- const key = canonicalCondition(entry.when);
164
- const existing = out[key];
165
- out[key] = existing ? {
166
- ...existing,
167
- props: {
168
- ...existing.props,
169
- [prop]: entry.value
170
- }
171
- } : {
172
- when: { ...entry.when },
173
- props: { [prop]: entry.value }
174
- };
175
- }
176
- return out;
177
- }
178
- //#endregion
179
- export { conditionalDefault, isConditionalDefault, joinConditionalDefaults, propDefaultConditions, storedDefaults, withPropDefaultConditions };