@loancrate/json-selector 3.0.0 → 4.0.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/src/util.ts DELETED
@@ -1,70 +0,0 @@
1
- import { JsonValue } from "type-fest";
2
-
3
- export function isArray(value: unknown): value is unknown[] {
4
- return Array.isArray(value);
5
- }
6
-
7
- export function asArray(value: unknown): unknown[] {
8
- return value == null ? [] : isArray(value) ? value : [value];
9
- }
10
-
11
- export function isObject(value: unknown): value is Record<string, unknown> {
12
- return typeof value === "object" && value != null;
13
- }
14
-
15
- export function isFalseOrEmpty(value: unknown): boolean {
16
- return (
17
- value == null ||
18
- value === false ||
19
- value === "" ||
20
- (isArray(value) && value.length === 0) ||
21
- (isObject(value) && !hasOwnProperties(value))
22
- );
23
- }
24
-
25
- function hasOwnProperties(value: Record<string, unknown>): boolean {
26
- for (const key in value) {
27
- if (Object.prototype.hasOwnProperty.call(value, key)) {
28
- return true;
29
- }
30
- }
31
- return false;
32
- }
33
-
34
- export function getField(obj: unknown, name: string): unknown {
35
- return isObject(obj) ? obj[name] ?? null : null;
36
- }
37
-
38
- export function getIndex(obj: unknown, index: number): unknown {
39
- return isArray(obj)
40
- ? obj[index < 0 ? obj.length + index : index] ?? null
41
- : null;
42
- }
43
-
44
- export function findId(obj: unknown, id: string | number): unknown {
45
- return isArray(obj)
46
- ? obj.find((e) => isObject(e) && e.id === id) ?? null
47
- : null;
48
- }
49
-
50
- export function findIdIndex(arr: unknown[], id: string | number): number {
51
- return arr.findIndex((e) => isObject(e) && e.id === id);
52
- }
53
-
54
- export function formatLiteral(value: JsonValue): string {
55
- return "`" + JSON.stringify(value).replace(/`/g, "\\`") + "`";
56
- }
57
-
58
- export function isValidIdentifier(s: string): boolean {
59
- return /^[a-z_][0-9a-z_]*$/i.test(s);
60
- }
61
-
62
- export function formatIdentifier(s: string): string {
63
- return isValidIdentifier(s) ? s : JSON.stringify(s);
64
- }
65
-
66
- export function formatRawString(s: string): string {
67
- // https://jmespath.org/specification.html#raw-string-literals
68
- // eslint-disable-next-line no-control-regex
69
- return `'${s.replace(/[\0-\x1F]/g, "").replace(/['\\]/g, (c) => `\\${c}`)}'`;
70
- }
package/src/visitor.ts DELETED
@@ -1,79 +0,0 @@
1
- import {
2
- JsonSelector,
3
- JsonSelectorAnd,
4
- JsonSelectorCompare,
5
- JsonSelectorCurrent,
6
- JsonSelectorFieldAccess,
7
- JsonSelectorFilter,
8
- JsonSelectorFlatten,
9
- JsonSelectorIdAccess,
10
- JsonSelectorIdentifier,
11
- JsonSelectorIndexAccess,
12
- JsonSelectorLiteral,
13
- JsonSelectorNot,
14
- JsonSelectorOr,
15
- JsonSelectorPipe,
16
- JsonSelectorProject,
17
- JsonSelectorRoot,
18
- JsonSelectorSlice,
19
- } from "./ast";
20
-
21
- export interface Visitor<R, C> {
22
- current(node: JsonSelectorCurrent, context: C): R;
23
- root(node: JsonSelectorRoot, context: C): R;
24
- literal(node: JsonSelectorLiteral, context: C): R;
25
- identifier(node: JsonSelectorIdentifier, context: C): R;
26
- fieldAccess(node: JsonSelectorFieldAccess, context: C): R;
27
- indexAccess(node: JsonSelectorIndexAccess, context: C): R;
28
- idAccess(node: JsonSelectorIdAccess, context: C): R;
29
- project(node: JsonSelectorProject, context: C): R;
30
- filter(node: JsonSelectorFilter, context: C): R;
31
- slice(node: JsonSelectorSlice, context: C): R;
32
- flatten(node: JsonSelectorFlatten, context: C): R;
33
- not(node: JsonSelectorNot, context: C): R;
34
- compare(node: JsonSelectorCompare, context: C): R;
35
- and(node: JsonSelectorAnd, context: C): R;
36
- or(node: JsonSelectorOr, context: C): R;
37
- pipe(node: JsonSelectorPipe, context: C): R;
38
- }
39
-
40
- export function visitJsonSelector<R, C>(
41
- selector: JsonSelector,
42
- visitor: Visitor<R, C>,
43
- context: C
44
- ): R {
45
- switch (selector.type) {
46
- case "current":
47
- return visitor.current(selector, context);
48
- case "root":
49
- return visitor.root(selector, context);
50
- case "literal":
51
- return visitor.literal(selector, context);
52
- case "identifier":
53
- return visitor.identifier(selector, context);
54
- case "fieldAccess":
55
- return visitor.fieldAccess(selector, context);
56
- case "indexAccess":
57
- return visitor.indexAccess(selector, context);
58
- case "idAccess":
59
- return visitor.idAccess(selector, context);
60
- case "project":
61
- return visitor.project(selector, context);
62
- case "filter":
63
- return visitor.filter(selector, context);
64
- case "slice":
65
- return visitor.slice(selector, context);
66
- case "flatten":
67
- return visitor.flatten(selector, context);
68
- case "not":
69
- return visitor.not(selector, context);
70
- case "compare":
71
- return visitor.compare(selector, context);
72
- case "and":
73
- return visitor.and(selector, context);
74
- case "or":
75
- return visitor.or(selector, context);
76
- case "pipe":
77
- return visitor.pipe(selector, context);
78
- }
79
- }