@mgarlik/json-filter 1.0.0 → 1.0.1
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/matchesFilter.js +2 -17
- package/dist/types.d.ts +9 -28
- package/package.json +1 -1
package/dist/matchesFilter.js
CHANGED
|
@@ -27,11 +27,6 @@ function compareResolvedValues(fieldVal, resolved) {
|
|
|
27
27
|
return resolved.includes(fieldVal);
|
|
28
28
|
return fieldVal === resolved;
|
|
29
29
|
}
|
|
30
|
-
function buildSourceKey(source) {
|
|
31
|
-
if (typeof source !== "object" || source === null)
|
|
32
|
-
return String(source);
|
|
33
|
-
return JSON.stringify(source);
|
|
34
|
-
}
|
|
35
30
|
function getCurrentDate() {
|
|
36
31
|
return new Date().toISOString().slice(0, 10);
|
|
37
32
|
}
|
|
@@ -73,7 +68,7 @@ function evalCondition(fieldVal, condition, target, context) {
|
|
|
73
68
|
}
|
|
74
69
|
const keys = Object.keys(condition);
|
|
75
70
|
if (keys.length === 1 && keys[0] === "$source") {
|
|
76
|
-
|
|
71
|
+
throw new Error("Unresolved $source in filter condition. Resolve $source before calling json-filter.");
|
|
77
72
|
}
|
|
78
73
|
for (const op of keys) {
|
|
79
74
|
const arg = condition[op];
|
|
@@ -224,17 +219,7 @@ export function evaluateValue(expr, context = {}, target = {}) {
|
|
|
224
219
|
const obj = expr;
|
|
225
220
|
const keys = Object.keys(obj);
|
|
226
221
|
if (keys.length === 1 && keys[0] === "$source") {
|
|
227
|
-
|
|
228
|
-
const sourceResolver = context.sourceResolver;
|
|
229
|
-
if (!sourceResolver) {
|
|
230
|
-
throw new Error("$source requires context.sourceResolver");
|
|
231
|
-
}
|
|
232
|
-
const stack = context.sourceStack ?? [];
|
|
233
|
-
const sourceKey = buildSourceKey(source);
|
|
234
|
-
if (stack.includes(sourceKey)) {
|
|
235
|
-
throw new Error(`Cyclic $source reference detected for ${sourceKey}`);
|
|
236
|
-
}
|
|
237
|
-
return sourceResolver(source, { ...context, sourceStack: [...stack, sourceKey] });
|
|
222
|
+
throw new Error("Unresolved $source expression. Resolve $source before calling json-filter.");
|
|
238
223
|
}
|
|
239
224
|
if (keys.includes("$first")) {
|
|
240
225
|
return evalFirst(obj["$first"], context, target);
|
package/dist/types.d.ts
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
/** Zakladni skalarny typ podporovany ve filtrech. */
|
|
2
2
|
export type scalarType = string | number | boolean | null;
|
|
3
3
|
/** Kontext predavany jako zdroj hodnot pro reference `$ctx.*`. */
|
|
4
|
-
export type contextType = Record<string, unknown
|
|
5
|
-
/** Volitelny resolver pro dynamicke reference typu `$source`. */
|
|
6
|
-
sourceResolver?: (source: unknown, context: contextType) => unknown;
|
|
7
|
-
/** Interni stack pro detekci cyklu pri reseni `$source`. */
|
|
8
|
-
sourceStack?: string[];
|
|
9
|
-
};
|
|
4
|
+
export type contextType = Record<string, unknown>;
|
|
10
5
|
/** Cilovy JSON dokument, nad kterym se filtr vyhodnocuje. */
|
|
11
6
|
export type targetType = Record<string, unknown>;
|
|
12
7
|
type objectLikeType = Record<string, unknown>;
|
|
@@ -24,30 +19,14 @@ export type targetReferenceType<TTarget extends targetType> = `$this.${dotPathTy
|
|
|
24
19
|
/** Reference na hodnotu z kontextu, napr. `$ctx.user.role`. */
|
|
25
20
|
export type contextReferenceType<TContext extends contextType = contextType> = `$ctx.${dotPathType<TContext>}`;
|
|
26
21
|
export type valueReferenceType<TTarget extends targetType = targetType, TContext extends contextType = contextType> = targetReferenceType<TTarget> | contextReferenceType<TContext>;
|
|
27
|
-
/** Zdrojova reference, ktera se vyhodnoti na jednu hodnotu nebo pole hodnot. */
|
|
28
|
-
export type sourceValueType<TTarget extends targetType = targetType, TContext extends contextType = contextType> = {
|
|
29
|
-
$source: {
|
|
30
|
-
dataProvider: string;
|
|
31
|
-
filter?: filterType<TTarget, TContext>;
|
|
32
|
-
val: string;
|
|
33
|
-
map?: never;
|
|
34
|
-
};
|
|
35
|
-
} | {
|
|
36
|
-
$source: {
|
|
37
|
-
dataProvider: string;
|
|
38
|
-
filter?: filterType<TTarget, TContext>;
|
|
39
|
-
map: string;
|
|
40
|
-
val?: never;
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
22
|
/**
|
|
44
23
|
* Stringová hodnota nebo reference s validací.
|
|
45
24
|
* Pokud začíná `$this.` nebo `$ctx.`, musí být validní cesta.
|
|
46
25
|
* Jinak je to libovolný string (ne-reference).
|
|
47
26
|
*/
|
|
48
27
|
type stringOrValidReferenceType<TTarget extends targetType, TContext extends contextType> = `$this.${dotPathType<TTarget>}` | `$ctx.${dotPathType<TContext>}` | (string & {});
|
|
49
|
-
type scalarOrReferenceType<TValue, TTarget extends targetType, TContext extends contextType> = TValue extends string ? stringOrValidReferenceType<TTarget, TContext> : scalarFieldValueType<TValue> | valueReferenceType<TTarget, TContext
|
|
50
|
-
export type conditionType<TValue = unknown, TTarget extends targetType = targetType, TContext extends contextType = contextType> = scalarType | undefined |
|
|
28
|
+
type scalarOrReferenceType<TValue, TTarget extends targetType, TContext extends contextType> = TValue extends string ? stringOrValidReferenceType<TTarget, TContext> : scalarFieldValueType<TValue> | valueReferenceType<TTarget, TContext>;
|
|
29
|
+
export type conditionType<TValue = unknown, TTarget extends targetType = targetType, TContext extends contextType = contextType> = scalarType | undefined | conditionObjectType<TValue, TTarget, TContext> | Array<conditionType<TValue, TTarget, TContext>>;
|
|
51
30
|
/** Objektova podoba podminky (operator -> hodnota). */
|
|
52
31
|
export type conditionObjectType<TValue = unknown, TTarget extends targetType = targetType, TContext extends contextType = contextType> = {
|
|
53
32
|
/**
|
|
@@ -68,13 +47,13 @@ export type conditionObjectType<TValue = unknown, TTarget extends targetType = t
|
|
|
68
47
|
/** Rozsah vcetne hranic (`min <= field <= max`). */
|
|
69
48
|
$between?: [unknown, unknown];
|
|
70
49
|
/** Hodnota je v seznamu (`field in list`). */
|
|
71
|
-
$in?: Array<scalarOrReferenceType<TValue, TTarget, TContext
|
|
50
|
+
$in?: Array<scalarOrReferenceType<TValue, TTarget, TContext>>;
|
|
72
51
|
/** Hodnota neni v seznamu (`field not in list`). */
|
|
73
|
-
$nin?: Array<scalarOrReferenceType<TValue, TTarget, TContext
|
|
52
|
+
$nin?: Array<scalarOrReferenceType<TValue, TTarget, TContext>>;
|
|
74
53
|
/** Vsechny prvky pole jsou v seznamu. */
|
|
75
|
-
$ein?: TValue extends readonly unknown[] ? Array<scalarOrReferenceType<arrayElementType<TValue>, TTarget, TContext
|
|
54
|
+
$ein?: TValue extends readonly unknown[] ? Array<scalarOrReferenceType<arrayElementType<TValue>, TTarget, TContext>> : never;
|
|
76
55
|
/** Alespon jeden prvek pole neni v seznamu. */
|
|
77
|
-
$nein?: TValue extends readonly unknown[] ? Array<scalarOrReferenceType<arrayElementType<TValue>, TTarget, TContext
|
|
56
|
+
$nein?: TValue extends readonly unknown[] ? Array<scalarOrReferenceType<arrayElementType<TValue>, TTarget, TContext>> : never;
|
|
78
57
|
/** String obsahuje podretezec (case-sensitive). */
|
|
79
58
|
$has?: scalarOrReferenceType<string, TTarget, TContext>;
|
|
80
59
|
/** String neobsahuje podretezec (case-sensitive). */
|
|
@@ -154,6 +133,8 @@ export type filterType<TTarget extends targetType = targetType, TContext extends
|
|
|
154
133
|
[K in optionalFieldKeyType<TTarget>]?: conditionType<pathValueType<TTarget, K extends `${infer P}?` ? P : never>, TTarget, TContext>;
|
|
155
134
|
} & {
|
|
156
135
|
[K in contextReferenceType<TContext>]?: conditionType<any, TTarget, TContext>;
|
|
136
|
+
} & {
|
|
137
|
+
[key: string]: conditionType<any, TTarget, TContext> | undefined;
|
|
157
138
|
};
|
|
158
139
|
/** Pole JSON dokumentu. */
|
|
159
140
|
export type documentsArrayType = Array<Record<string, unknown>>;
|