@usebacklit/ui-core 0.0.0-stage → 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/dist/main.d.ts +42 -0
- package/dist/main.js +223 -0
- package/package.json +48 -3
- package/README.md +0 -3
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Condition } from "@usebacklit/protocol";
|
|
2
|
+
//#region src/defect_reporters.d.ts
|
|
3
|
+
/** A problem in the data that the client received. The client continues, and reports it. */
|
|
4
|
+
interface Defect {
|
|
5
|
+
/** What went wrong, written by the code that found it. */
|
|
6
|
+
message: string;
|
|
7
|
+
}
|
|
8
|
+
/** Receives each defect. The app decides where it goes. */
|
|
9
|
+
type DefectReporter = (defect: Defect) => void;
|
|
10
|
+
/**
|
|
11
|
+
* Creates a reporter that writes each defect to `console.warn`, once for each
|
|
12
|
+
* message. Code that runs on every change, such as a form condition, would
|
|
13
|
+
* otherwise print the same defect on every keystroke. Each reporter remembers
|
|
14
|
+
* only its own messages.
|
|
15
|
+
*/
|
|
16
|
+
declare function createDefectReporter(): DefectReporter;
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/conditions.d.ts
|
|
19
|
+
/** What `evaluateCondition` needs from its caller. */
|
|
20
|
+
interface EvaluateConditionOptions {
|
|
21
|
+
/** Reads the value at a path of the form data. */
|
|
22
|
+
read: (path: string) => unknown;
|
|
23
|
+
/** Reads the value at a path inside one array member, for a quantifier. */
|
|
24
|
+
readIn: (member: unknown, path: string) => unknown;
|
|
25
|
+
/** Receives each defect. */
|
|
26
|
+
onDefect: DefectReporter;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Evaluates a condition tree against the values `read` returns for each path.
|
|
30
|
+
* The readers are the caller's: a gate reads the form's store, and a
|
|
31
|
+
* quantifier reads one array member through `readIn`. Groups stop at the first
|
|
32
|
+
* member that decides them.
|
|
33
|
+
*/
|
|
34
|
+
declare function evaluateCondition(condition: Condition, options: EvaluateConditionOptions): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Collects the paths a condition reads, so a subscriber can watch exactly
|
|
37
|
+
* those. A quantifier contributes its array path only: a change to any
|
|
38
|
+
* member is a change to the array.
|
|
39
|
+
*/
|
|
40
|
+
declare function collectConditionPaths(condition: Condition): string[];
|
|
41
|
+
//#endregion
|
|
42
|
+
export { type Defect, type DefectReporter, type EvaluateConditionOptions, collectConditionPaths, createDefectReporter, evaluateCondition };
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
//#region src/conditions.ts
|
|
2
|
+
/** Reports a predicate or a quantifier whose runtime values do not fit its operation. */
|
|
3
|
+
function reportDefect(onDefect, member, detail) {
|
|
4
|
+
onDefect({ message: `condition "${member.op}" on "${member.path}" ${detail}; the predicate is false.` });
|
|
5
|
+
}
|
|
6
|
+
/** The word a defect report uses for a value's shape. */
|
|
7
|
+
function shapeOf(value) {
|
|
8
|
+
if (value === null) return "null";
|
|
9
|
+
return Array.isArray(value) ? "array" : typeof value;
|
|
10
|
+
}
|
|
11
|
+
/** Whether a value is absent: the path does not exist or holds `undefined`. */
|
|
12
|
+
function isMissing(value) {
|
|
13
|
+
return value === void 0;
|
|
14
|
+
}
|
|
15
|
+
/** Whether a value is an empty string or an empty array. */
|
|
16
|
+
function isEmpty(value) {
|
|
17
|
+
return value === "" || Array.isArray(value) && value.length === 0;
|
|
18
|
+
}
|
|
19
|
+
/** Whether a value is present, not `null` and not empty. `false` and `0` are filled. */
|
|
20
|
+
function isFilled(value) {
|
|
21
|
+
return !isMissing(value) && value !== null && !isEmpty(value);
|
|
22
|
+
}
|
|
23
|
+
/** Whether a value is one of the two shapes that carry a length. */
|
|
24
|
+
function hasLength(value) {
|
|
25
|
+
return typeof value === "string" || Array.isArray(value);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Whether a value counts as no value for a comparison: the same values as
|
|
29
|
+
* `unfilled`. A comparison with no value on either side is false.
|
|
30
|
+
*/
|
|
31
|
+
function hasNoValue(value) {
|
|
32
|
+
return !isFilled(value);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Compares two values of the same ordered type. Two numbers and two strings
|
|
36
|
+
* compare; a mixed pair or any other type has no order and returns `undefined`
|
|
37
|
+
* so the caller can report it.
|
|
38
|
+
*/
|
|
39
|
+
function compareOrdered(op, left, right) {
|
|
40
|
+
if (!(typeof left === "number" && typeof right === "number") && !(typeof left === "string" && typeof right === "string")) return;
|
|
41
|
+
switch (op) {
|
|
42
|
+
case "gt": return left > right;
|
|
43
|
+
case "gte": return left >= right;
|
|
44
|
+
case "lt": return left < right;
|
|
45
|
+
case "lte": return left <= right;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Tests one predicate against the values its reader returns. The value-state
|
|
50
|
+
* operations read only the left path and have exact meanings. Every other
|
|
51
|
+
* operation reads its right operand — a literal, or another path — and
|
|
52
|
+
* compares without coercion.
|
|
53
|
+
*
|
|
54
|
+
* A comparison with no value on either side is false, `eq` and `neq`
|
|
55
|
+
* included, and it is not a defect: an unfilled control is a normal state,
|
|
56
|
+
* and it must not trigger a condition. No value is the same as `unfilled`:
|
|
57
|
+
* `undefined`, `null`, `''` or `[]`. A comparison whose sides have the wrong
|
|
58
|
+
* shape for the operation is false and is reported, because the SDK typed the
|
|
59
|
+
* literal against the field and a mismatch means a control wrote the wrong
|
|
60
|
+
* shape.
|
|
61
|
+
*/
|
|
62
|
+
function testPredicate(predicate, options) {
|
|
63
|
+
const { read, onDefect } = options;
|
|
64
|
+
const left = read(predicate.path);
|
|
65
|
+
const report = (detail) => reportDefect(onDefect, predicate, detail);
|
|
66
|
+
switch (predicate.op) {
|
|
67
|
+
case "missing": return isMissing(left);
|
|
68
|
+
case "present": return !isMissing(left);
|
|
69
|
+
case "null": return left === null;
|
|
70
|
+
case "not-null": return left !== null;
|
|
71
|
+
case "empty": return isEmpty(left);
|
|
72
|
+
case "not-empty": return !isEmpty(left);
|
|
73
|
+
case "filled": return isFilled(left);
|
|
74
|
+
case "unfilled": return !isFilled(left);
|
|
75
|
+
}
|
|
76
|
+
const right = "value" in predicate.right ? predicate.right.value : read(predicate.right.path);
|
|
77
|
+
if (hasNoValue(left) || hasNoValue(right)) return false;
|
|
78
|
+
switch (predicate.op) {
|
|
79
|
+
case "eq": return left === right;
|
|
80
|
+
case "neq": return left !== right;
|
|
81
|
+
case "gt":
|
|
82
|
+
case "gte":
|
|
83
|
+
case "lt":
|
|
84
|
+
case "lte": {
|
|
85
|
+
const ordered = compareOrdered(predicate.op, left, right);
|
|
86
|
+
if (ordered === void 0) {
|
|
87
|
+
report(`compared a ${shapeOf(left)} with a ${shapeOf(right)}`);
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
return ordered;
|
|
91
|
+
}
|
|
92
|
+
case "in":
|
|
93
|
+
case "not-in": {
|
|
94
|
+
if (!Array.isArray(right)) {
|
|
95
|
+
report(`expected an array on the right, got a ${shapeOf(right)}`);
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
const member = right.includes(left);
|
|
99
|
+
return predicate.op === "in" ? member : !member;
|
|
100
|
+
}
|
|
101
|
+
case "text-contains":
|
|
102
|
+
case "text-starts-with":
|
|
103
|
+
case "text-ends-with":
|
|
104
|
+
if (typeof left !== "string" || typeof right !== "string") {
|
|
105
|
+
report(`compared a ${shapeOf(left)} with a ${shapeOf(right)}`);
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
if (predicate.op === "text-contains") return left.includes(right);
|
|
109
|
+
return predicate.op === "text-starts-with" ? left.startsWith(right) : left.endsWith(right);
|
|
110
|
+
case "array-contains":
|
|
111
|
+
if (!Array.isArray(left)) {
|
|
112
|
+
report(`expected an array on the left, got a ${shapeOf(left)}`);
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
return left.includes(right);
|
|
116
|
+
case "array-overlaps":
|
|
117
|
+
case "array-contains-all":
|
|
118
|
+
if (!Array.isArray(left) || !Array.isArray(right)) {
|
|
119
|
+
report(`compared a ${shapeOf(left)} with a ${shapeOf(right)}`);
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
return predicate.op === "array-overlaps" ? right.some((member) => left.includes(member)) : right.every((member) => left.includes(member));
|
|
123
|
+
case "length-eq":
|
|
124
|
+
case "length-gt":
|
|
125
|
+
case "length-gte":
|
|
126
|
+
case "length-lt":
|
|
127
|
+
case "length-lte": {
|
|
128
|
+
if (!hasLength(left) || typeof right !== "number") {
|
|
129
|
+
report(`compared the length of a ${shapeOf(left)} with a ${shapeOf(right)}`);
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
const length = left.length;
|
|
133
|
+
switch (predicate.op) {
|
|
134
|
+
case "length-eq": return length === right;
|
|
135
|
+
case "length-gt": return length > right;
|
|
136
|
+
case "length-gte": return length >= right;
|
|
137
|
+
case "length-lt": return length < right;
|
|
138
|
+
case "length-lte": return length <= right;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Tests a quantifier over the members of the array at its path. Each member is
|
|
145
|
+
* the root of the child condition, so a path inside it is read against the
|
|
146
|
+
* member alone, through `readIn`. An array with no value (`undefined`,
|
|
147
|
+
* `null` or `[]`) makes `some` and `every` false, like a comparison with no
|
|
148
|
+
* value. Any other non-array value is a defect and makes the quantifier false.
|
|
149
|
+
*/
|
|
150
|
+
function testQuantifier(quantifier, options) {
|
|
151
|
+
const members = options.read(quantifier.path);
|
|
152
|
+
if (members === void 0 || members === null) return false;
|
|
153
|
+
if (!Array.isArray(members)) {
|
|
154
|
+
reportDefect(options.onDefect, quantifier, `expected an array, got a ${shapeOf(members)}`);
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
if (members.length === 0) return false;
|
|
158
|
+
const test = (member) => evaluateCondition(quantifier.condition, {
|
|
159
|
+
...options,
|
|
160
|
+
read: (path) => options.readIn(member, path)
|
|
161
|
+
});
|
|
162
|
+
return quantifier.op === "some" ? members.some(test) : members.every(test);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Evaluates a condition tree against the values `read` returns for each path.
|
|
166
|
+
* The readers are the caller's: a gate reads the form's store, and a
|
|
167
|
+
* quantifier reads one array member through `readIn`. Groups stop at the first
|
|
168
|
+
* member that decides them.
|
|
169
|
+
*/
|
|
170
|
+
function evaluateCondition(condition, options) {
|
|
171
|
+
switch (condition.kind) {
|
|
172
|
+
case "condition-predicate": return testPredicate(condition, options);
|
|
173
|
+
case "condition-quantifier": return testQuantifier(condition, options);
|
|
174
|
+
case "condition-group": {
|
|
175
|
+
const test = (member) => evaluateCondition(member, options);
|
|
176
|
+
switch (condition.op) {
|
|
177
|
+
case "all": return condition.conditions.every(test);
|
|
178
|
+
case "any": return condition.conditions.some(test);
|
|
179
|
+
case "none": return !condition.conditions.some(test);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Collects the paths a condition reads, so a subscriber can watch exactly
|
|
186
|
+
* those. A quantifier contributes its array path only: a change to any
|
|
187
|
+
* member is a change to the array.
|
|
188
|
+
*/
|
|
189
|
+
function collectConditionPaths(condition) {
|
|
190
|
+
const paths = /* @__PURE__ */ new Set();
|
|
191
|
+
const visit = (member) => {
|
|
192
|
+
switch (member.kind) {
|
|
193
|
+
case "condition-predicate":
|
|
194
|
+
paths.add(member.path);
|
|
195
|
+
if ("right" in member && "path" in member.right) paths.add(member.right.path);
|
|
196
|
+
return;
|
|
197
|
+
case "condition-quantifier":
|
|
198
|
+
paths.add(member.path);
|
|
199
|
+
return;
|
|
200
|
+
case "condition-group": member.conditions.forEach(visit);
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
visit(condition);
|
|
204
|
+
return [...paths];
|
|
205
|
+
}
|
|
206
|
+
//#endregion
|
|
207
|
+
//#region src/defect_reporters.ts
|
|
208
|
+
/**
|
|
209
|
+
* Creates a reporter that writes each defect to `console.warn`, once for each
|
|
210
|
+
* message. Code that runs on every change, such as a form condition, would
|
|
211
|
+
* otherwise print the same defect on every keystroke. Each reporter remembers
|
|
212
|
+
* only its own messages.
|
|
213
|
+
*/
|
|
214
|
+
function createDefectReporter() {
|
|
215
|
+
const reported = /* @__PURE__ */ new Set();
|
|
216
|
+
return ({ message }) => {
|
|
217
|
+
if (reported.has(message)) return;
|
|
218
|
+
reported.add(message);
|
|
219
|
+
console.warn(`[backlit] ${message}`);
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
//#endregion
|
|
223
|
+
export { collectConditionPaths, createDefectReporter, evaluateCondition };
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@usebacklit/ui-core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"homepage": "https://usebacklit.com",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/crossoverlabs/backlit.git",
|
|
8
|
+
"directory": "packages/ui-core"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=26"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public",
|
|
17
|
+
"provenance": true
|
|
18
|
+
},
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/main.d.ts",
|
|
22
|
+
"default": "./dist/main.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@usebacklit/protocol": "0.1.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@vitest/browser-playwright": "5.1.2",
|
|
33
|
+
"oxlint": "1.96.0",
|
|
34
|
+
"oxlint-tsgolint": "7.0.2009",
|
|
35
|
+
"playwright": "1.65.2",
|
|
36
|
+
"react": "19.3.1",
|
|
37
|
+
"react-hook-form": "7.92.0",
|
|
38
|
+
"tsdown": "0.22.7",
|
|
39
|
+
"typescript": "6.0.3",
|
|
40
|
+
"vitest": "5.1.2",
|
|
41
|
+
"@usebacklit/tsconfig": "0.0.0",
|
|
42
|
+
"@usebacklit/oxlint-config": "0.0.0"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsdown",
|
|
46
|
+
"dev": "tsdown --watch",
|
|
47
|
+
"lint": "oxlint",
|
|
48
|
+
"typecheck": "tsc --noEmit",
|
|
49
|
+
"test": "vitest run"
|
|
50
|
+
}
|
|
6
51
|
}
|
package/README.md
DELETED