@easy-cms/core 0.45.0 → 0.47.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/chunk-C4KJ5TFK.js +48 -0
- package/dist/conditions.d.ts +31 -0
- package/dist/conditions.js +8 -0
- package/dist/index.d.ts +441 -28
- package/dist/index.js +905 -74
- package/package.json +5 -1
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// src/conditions.ts
|
|
2
|
+
var empty = (value) => value === void 0 || value === null || value === "" || value === false || Array.isArray(value) && value.length === 0;
|
|
3
|
+
var plain = (value) => value && typeof value === "object" && !Array.isArray(value) && "id" in value ? value.id : value;
|
|
4
|
+
var same = (a, b) => {
|
|
5
|
+
const x = plain(a);
|
|
6
|
+
const y = plain(b);
|
|
7
|
+
if (Array.isArray(x)) return x.some((v) => same(v, y));
|
|
8
|
+
return x === y || x !== null && y !== null && x !== void 0 && String(x) === String(y);
|
|
9
|
+
};
|
|
10
|
+
function read(data, path) {
|
|
11
|
+
let current = data;
|
|
12
|
+
for (const key of path.split(".")) {
|
|
13
|
+
if (typeof current !== "object" || current === null) return void 0;
|
|
14
|
+
current = current[key];
|
|
15
|
+
}
|
|
16
|
+
return current;
|
|
17
|
+
}
|
|
18
|
+
function matchesCondition(condition, data) {
|
|
19
|
+
if ("and" in condition) return condition.and.every((c) => matchesCondition(c, data));
|
|
20
|
+
if ("or" in condition) return condition.or.some((c) => matchesCondition(c, data));
|
|
21
|
+
if ("not" in condition) return !matchesCondition(condition.not, data);
|
|
22
|
+
const value = read(data, condition.field);
|
|
23
|
+
if (condition.exists !== void 0 && empty(value) === condition.exists) return false;
|
|
24
|
+
if ("equals" in condition && !same(value, condition.equals)) return false;
|
|
25
|
+
if ("notEquals" in condition && same(value, condition.notEquals)) return false;
|
|
26
|
+
if (condition.in && !condition.in.some((v) => same(value, v))) return false;
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
function conditionIssues(condition, siblings) {
|
|
30
|
+
if (typeof condition !== "object" || condition === null || Array.isArray(condition))
|
|
31
|
+
return ['must be an object, e.g. { field: "type", equals: "link" }'];
|
|
32
|
+
const c = condition;
|
|
33
|
+
if (Array.isArray(c.and)) return c.and.flatMap((x) => conditionIssues(x, siblings));
|
|
34
|
+
if (Array.isArray(c.or)) return c.or.flatMap((x) => conditionIssues(x, siblings));
|
|
35
|
+
if ("not" in c) return conditionIssues(c.not, siblings);
|
|
36
|
+
if (typeof c.field !== "string" || c.field === "") return ["needs `field`, a sibling field"];
|
|
37
|
+
const first = c.field.split(".")[0];
|
|
38
|
+
if (!siblings.includes(first)) return [`"${c.field}" is not a field beside it`];
|
|
39
|
+
if (c.in !== void 0 && !Array.isArray(c.in)) return ["`in` must be a list"];
|
|
40
|
+
if (c.exists !== void 0 && typeof c.exists !== "boolean")
|
|
41
|
+
return ["`exists` must be true or false"];
|
|
42
|
+
return [];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export {
|
|
46
|
+
matchesCondition,
|
|
47
|
+
conditionIssues
|
|
48
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When a field is shown, from its sibling fields' values (the fields beside it, in its group,
|
|
3
|
+
* array row or block):
|
|
4
|
+
*
|
|
5
|
+
* - `{ field: 'linkType', equals: 'external' }`, also `notEquals`, `in: [...]`, `exists: true`
|
|
6
|
+
* - `{ and: [...] }`, `{ or: [...] }`, `{ not: … }`
|
|
7
|
+
*
|
|
8
|
+
* A hidden field isn't required, and keeps its value.
|
|
9
|
+
*/
|
|
10
|
+
type FieldCondition = {
|
|
11
|
+
/** A sibling field's name; a path with `.` reads inside a group, e.g. `link.type`. */
|
|
12
|
+
readonly field: string;
|
|
13
|
+
readonly equals?: unknown;
|
|
14
|
+
readonly notEquals?: unknown;
|
|
15
|
+
readonly in?: readonly unknown[];
|
|
16
|
+
/** `true`: has a value (not empty); `false`: empty. */
|
|
17
|
+
readonly exists?: boolean;
|
|
18
|
+
} | {
|
|
19
|
+
readonly and: readonly FieldCondition[];
|
|
20
|
+
} | {
|
|
21
|
+
readonly or: readonly FieldCondition[];
|
|
22
|
+
} | {
|
|
23
|
+
readonly not: FieldCondition;
|
|
24
|
+
};
|
|
25
|
+
type Data = Record<string, unknown>;
|
|
26
|
+
/** Whether a condition holds for the sibling data. */
|
|
27
|
+
declare function matchesCondition(condition: FieldCondition, data: Data): boolean;
|
|
28
|
+
/** Problems with a condition, for the config's checks; `[]` when it's fine. */
|
|
29
|
+
declare function conditionIssues(condition: unknown, siblings: readonly string[]): string[];
|
|
30
|
+
|
|
31
|
+
export { type FieldCondition, conditionIssues, matchesCondition };
|