-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
glob.test.ts
129 lines (107 loc) · 5.72 KB
/
glob.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { globValues, globPaths } from "./glob";
describe("globValues", () => {
it("should return matched values in an object by path", () => {
expect(globValues("a", { a: 1, b: true, c: "3", d: [1, 2], f: { a: "b" } })).toEqual([1]);
expect(globValues("b", { a: 1, b: true, c: "3", d: [1, 2], f: { a: "b" } })).toEqual([true]);
expect(globValues("c", { a: 1, b: true, c: "3", d: [1, 2], f: { a: "b" } })).toEqual(["3"]);
expect(globValues("d", { a: 1, b: true, c: "3", d: [1, 2], f: { a: "b" } })).toEqual([[1, 2]]);
expect(globValues("f", { a: 1, b: true, c: "3", d: [1, 2], f: { a: "b" } })).toEqual([{ a: "b" }]);
// benchmark case
expect(globValues("prop0.nested0.value0", { prop0: { nested0: { value0: 0 } } })).toEqual([0]);
});
it("should return matched values in an array by index", () => {
expect(globValues("0", [1, true, "3", [1, 2], { a: "b" }])).toEqual([1]);
expect(globValues("1", [1, true, "3", [1, 2], { a: "b" }])).toEqual([true]);
expect(globValues("2", [1, true, "3", [1, 2], { a: "b" }])).toEqual(["3"]);
expect(globValues("3", [1, true, "3", [1, 2], { a: "b" }])).toEqual([[1, 2]]);
expect(globValues("4", [1, true, "3", [1, 2], { a: "b" }])).toEqual([{ a: "b" }]);
});
it("should return nested values by path", () => {
expect(globValues("a", { a: "bc" })).toEqual(["bc"]);
expect(globValues("a.b", { a: { b: 1 }, c: 2 })).toEqual([1]);
expect(globValues("a.b.c", { a: { b: { c: "deeply nested" } }, c: 2 })).toEqual(["deeply nested"]);
});
it("should return matched object properties with wildcard", () => {
expect(globValues("*", { a: 1, b: 2, c: 3 })).toEqual([1, 2, 3]);
expect(globValues("a.*.c", { a: [{ b: 1, c: 2 }, { c: 3 }] })).toEqual([2, 3]);
});
it("should return matched array items with wildcard", () => {
expect(globValues("*", [1, 2, 3])).toEqual([1, 2, 3]);
});
it("should return nested values with wildcard", () => {
expect(globValues("a.*.d", { a: { b: { d: 1, f: 2 }, c: { d: 3, f: 4 } } })).toEqual([1, 3]);
expect(globValues("a.*.f", { a: { b: { d: 1, f: 2 }, c: { d: 3, f: 4 } } })).toEqual([2, 4]);
expect(globValues("a.b.*", { a: { b: { d: 1, f: 2 }, c: { d: 3, f: 4 } } })).toEqual([1, 2]);
expect(globValues("a.c.*", { a: { b: { d: 1, f: 2 }, c: { d: 3, f: 4 } } })).toEqual([3, 4]);
});
it("should return deeply nested values with double wildcard", () => {
expect(globValues("**.d", { a: { b: { d: 1, f: 2 }, c: { d: 3, f: 4 } } })).toEqual([1, 3]);
expect(globValues("**.f", { a: { b: { d: 1, f: 2 }, c: { d: 3, f: 4 } } })).toEqual([2, 4]);
expect(globValues("a.**.d", { a: { b: { d: 1, f: 2 }, c: { d: 3, f: { d: 4 } } } })).toEqual([1, 3, 4]);
expect(globValues("z.**.f", { a: { b: { d: 1, f: 2 }, c: { d: 3, f: { d: 4 } } } })).toEqual([]);
});
it("should return values using wildcard attributes", () => {
expect(globValues("a*", { ab: 7, ac: 8, ba: 9, bc: 10 })).toEqual([7, 8]);
});
it("should return values using single character matcher attribute", () => {
expect(globValues("b?", { ab: 7, ac: 8, ba: 9, bc: 10 })).toEqual([9, 10]);
});
it("should return values using double wildcard attributes", () => {
const obj = {
wrapper: {
inner: [
{
id: "id-1",
},
],
},
};
expect(globValues("**.inner.*.id", obj)).toEqual(["id-1"]);
});
});
describe("globPaths", () => {
it("should return matched object property names with wildcard", () => {
expect(globPaths("*", { a: 1, b: 2, c: 3 })).toEqual(["a", "b", "c"]);
});
it("should return matched array indices with wildcard", () => {
expect(globPaths("*", [1, 2, 3])).toEqual(["0", "1", "2"]);
expect(globPaths("a.*.c", { a: [{ b: 1, c: 2 }, { c: 3 }] })).toEqual(["a.0.c", "a.1.c"]);
});
it("should return nested paths with wildcard", () => {
expect(globPaths("a.*.d", { a: { b: { d: 1, f: 2 }, c: { d: 3, f: 4 } } })).toEqual(["a.b.d", "a.c.d"]);
expect(globPaths("a.*.f", { a: { b: { d: 1, f: 2 }, c: { d: 3, f: 4 } } })).toEqual(["a.b.f", "a.c.f"]);
expect(globPaths("a.b.*", { a: { b: { d: 1, f: 2 }, c: { d: 3, f: 4 } } })).toEqual(["a.b.d", "a.b.f"]);
expect(globPaths("a.c.*", { a: { b: { d: 1, f: 2 }, c: { d: 3, f: 4 } } })).toEqual(["a.c.d", "a.c.f"]);
});
it("should return deeply nested paths with double wildcard", () => {
expect(globPaths("**.d", { a: { b: { d: 1, f: 2 }, c: { d: 3, f: 4 } } })).toEqual(["a.b.d", "a.c.d"]);
expect(globPaths("**.f", { a: { b: { d: 1, f: 2 }, c: { d: 3, f: 4 } } })).toEqual(["a.b.f", "a.c.f"]);
expect(globPaths("a.**.d", { a: { b: { d: 1, f: 2 }, c: { d: 3, f: { d: 4 } } } })).toEqual([
"a.b.d",
"a.c.d",
"a.c.f.d",
]);
expect(globPaths("z.**.f", { a: { b: { d: 1, f: 2 }, c: { d: 3, f: { d: 4 } } } })).toEqual([]);
});
it("should return paths using wildcard properties", () => {
expect(globPaths("a*", { abc: 7, ab: 8, ba: 9, bc: 10 })).toEqual(["abc", "ab"]);
expect(globPaths("b?", { abc: 7, ab: 8, ba: 9, bc: 10 })).toEqual(["ba", "bc"]);
});
it.each([null, { boom: null }])("should not blow up on null or undefined objects", (obj) => {
expect(globPaths("**.ba", obj)).toEqual([]);
});
});
describe("safe mode", () => {
it("globValues should not blow up on circular references", () => {
const obj: any = { a: 1 };
obj.b = obj;
expect(globValues("**.a", obj, { safeMode: true })).toEqual([1]);
expect(globValues("**.b", obj, { safeMode: true })).toEqual([obj]);
});
it("globPaths should not blow up on circular references", () => {
const obj: any = { a: 1 };
obj.b = obj;
expect(globPaths("**.a", obj, { safeMode: true })).toEqual(["b.a"]);
expect(globPaths("**.b", obj, { safeMode: true })).toEqual(["b.b"]);
});
});