@flatfile/listener 0.0.10 → 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.
@@ -1,5 +1,5 @@
1
1
 
2
- > @flatfile/listener@0.0.10 build
2
+ > @flatfile/listener@0.1.0 build
3
3
  > tsup src/index.ts --format esm,cjs --dts
4
4
 
5
5
  CLI Building entry: src/index.ts
@@ -8,10 +8,10 @@
8
8
  CLI Target: es2020
9
9
  ESM Build start
10
10
  CJS Build start
11
- ESM dist/index.js 25.20 KB
12
- ESM ⚡️ Build success in 111ms
13
- CJS dist/index.cjs 25.96 KB
14
- CJS ⚡️ Build success in 111ms
11
+ ESM dist/index.js 25.99 KB
12
+ ESM ⚡️ Build success in 112ms
13
+ CJS dist/index.cjs 26.82 KB
14
+ CJS ⚡️ Build success in 126ms
15
15
  DTS Build start
16
- DTS ⚡️ Build success in 5701ms
16
+ DTS ⚡️ Build success in 5649ms
17
17
  DTS dist/index.d.ts 7.41 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @flatfile/listener
2
2
 
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 4d88069: Adds a major improvement to event filtering, allowing you to grep the key path and defaulting to matching any key in the object
8
+
3
9
  ## 0.0.10
4
10
 
5
11
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -534,31 +534,48 @@ function wildcardMatch(pattern, options) {
534
534
  var index_es_default = wildcardMatch;
535
535
 
536
536
  // src/events/glob.match.ts
537
+ var import_flat = __toESM(require("flat"), 1);
537
538
  function glob(val, filter) {
538
539
  if (!val || typeof val !== "string") {
539
540
  return false;
540
541
  }
541
542
  return index_es_default(filter || "**", ":")(val);
542
543
  }
543
- function objectMatches(object, filter) {
544
+ function objectMatches(object, filterObject) {
545
+ const cleanFilter = !filterObject || typeof filterObject !== "object" ? { "**": filterObject } : filterObject;
546
+ if (typeof object !== "object") {
547
+ throw new Error("You cannot filter a non-object");
548
+ }
544
549
  let denied = false;
545
- for (const x in filter) {
546
- const prop = object[x];
547
- if (!object?.[x]) {
548
- return false;
549
- }
550
- if (typeof prop === "string" || Array.isArray(prop) && typeof prop[0] === "string") {
551
- if (Array.isArray(prop)) {
552
- denied || (denied = !prop.some((p) => {
553
- return glob(p, filter[x]);
554
- }));
555
- } else {
556
- denied || (denied = !glob(prop, filter[x]));
557
- }
558
- }
550
+ const filter = (0, import_flat.default)(cleanFilter, { safe: true });
551
+ const flattened = (0, import_flat.default)(object, { safe: true });
552
+ for (const keyPattern in filter) {
553
+ const keys = filterKeys(flattened, keyPattern);
554
+ const valuePattern = Array.isArray(filter[keyPattern]) ? filter[keyPattern] : [filter[keyPattern]];
555
+ denied || (denied = !keys.some((key) => {
556
+ const value = flattened[key];
557
+ return valuePattern.some((match) => globOrMatch(value, match));
558
+ }));
559
559
  }
560
560
  return !denied;
561
561
  }
562
+ function filterKeys(object, glob2) {
563
+ glob2 = glob2.includes("*") || glob2.includes(".") ? glob2 : `**.${glob2}`;
564
+ const matcher = index_es_default(glob2, ".");
565
+ return Object.keys(object).filter((key) => matcher(key));
566
+ }
567
+ function globOrMatch(val, filter) {
568
+ if (val === void 0 || val === null) {
569
+ return filter === null;
570
+ }
571
+ if (Array.isArray(val)) {
572
+ return val.some((v) => globOrMatch(v, filter));
573
+ }
574
+ if (typeof filter === "string") {
575
+ return glob(val.toString(), filter);
576
+ }
577
+ return val === filter;
578
+ }
562
579
 
563
580
  // src/events/event.handler.ts
564
581
  var EventHandler = class extends AuthenticatedClient {
package/dist/index.js CHANGED
@@ -515,31 +515,48 @@ function wildcardMatch(pattern, options) {
515
515
  var index_es_default = wildcardMatch;
516
516
 
517
517
  // src/events/glob.match.ts
518
+ import flat from "flat";
518
519
  function glob(val, filter) {
519
520
  if (!val || typeof val !== "string") {
520
521
  return false;
521
522
  }
522
523
  return index_es_default(filter || "**", ":")(val);
523
524
  }
524
- function objectMatches(object, filter) {
525
+ function objectMatches(object, filterObject) {
526
+ const cleanFilter = !filterObject || typeof filterObject !== "object" ? { "**": filterObject } : filterObject;
527
+ if (typeof object !== "object") {
528
+ throw new Error("You cannot filter a non-object");
529
+ }
525
530
  let denied = false;
526
- for (const x in filter) {
527
- const prop = object[x];
528
- if (!object?.[x]) {
529
- return false;
530
- }
531
- if (typeof prop === "string" || Array.isArray(prop) && typeof prop[0] === "string") {
532
- if (Array.isArray(prop)) {
533
- denied || (denied = !prop.some((p) => {
534
- return glob(p, filter[x]);
535
- }));
536
- } else {
537
- denied || (denied = !glob(prop, filter[x]));
538
- }
539
- }
531
+ const filter = flat(cleanFilter, { safe: true });
532
+ const flattened = flat(object, { safe: true });
533
+ for (const keyPattern in filter) {
534
+ const keys = filterKeys(flattened, keyPattern);
535
+ const valuePattern = Array.isArray(filter[keyPattern]) ? filter[keyPattern] : [filter[keyPattern]];
536
+ denied || (denied = !keys.some((key) => {
537
+ const value = flattened[key];
538
+ return valuePattern.some((match) => globOrMatch(value, match));
539
+ }));
540
540
  }
541
541
  return !denied;
542
542
  }
543
+ function filterKeys(object, glob2) {
544
+ glob2 = glob2.includes("*") || glob2.includes(".") ? glob2 : `**.${glob2}`;
545
+ const matcher = index_es_default(glob2, ".");
546
+ return Object.keys(object).filter((key) => matcher(key));
547
+ }
548
+ function globOrMatch(val, filter) {
549
+ if (val === void 0 || val === null) {
550
+ return filter === null;
551
+ }
552
+ if (Array.isArray(val)) {
553
+ return val.some((v) => globOrMatch(v, filter));
554
+ }
555
+ if (typeof filter === "string") {
556
+ return glob(val.toString(), filter);
557
+ }
558
+ return val === filter;
559
+ }
543
560
 
544
561
  // src/events/event.handler.ts
545
562
  var EventHandler = class extends AuthenticatedClient {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flatfile/listener",
3
- "version": "0.0.10",
3
+ "version": "0.1.0",
4
4
  "description": "A PubSub Listener for configuring and using Flatfile",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -17,6 +17,7 @@
17
17
  "@flatfile/eslint-config-platform-sdk": "*",
18
18
  "@flatfile/jest-preset-platform-sdk": "*",
19
19
  "@flatfile/ts-config-platform-sdk": "*",
20
+ "@types/flat": "^5.0.2",
20
21
  "@types/jest": "^29.5.1",
21
22
  "eslint": "^8.19.0",
22
23
  "jest": "^29.5.0",
@@ -27,6 +28,7 @@
27
28
  },
28
29
  "dependencies": {
29
30
  "@flatfile/api": "^0.0.19",
31
+ "flat": "^5.0.2",
30
32
  "node-fetch": "^2.6.7"
31
33
  }
32
34
  }
@@ -1,6 +1,16 @@
1
1
  import { objectMatches } from './glob.match'
2
2
 
3
3
  describe('objectMatches', () => {
4
+ test('matches a primitive anywhere in the object', () => {
5
+ expect(objectMatches({ foo: 'bar' }, 'bar')).toBe(true)
6
+ expect(objectMatches({ foo: 11 }, 11)).toBe(true)
7
+ expect(objectMatches({ foo: 11 }, 13)).toBe(false)
8
+ expect(objectMatches({ foo: 11, bar: 13 }, 13)).toBe(true)
9
+ expect(objectMatches({ foo: { bar: 'blue' } }, 'blue')).toBe(true)
10
+ expect(objectMatches({ foo: ['bar', 'baz'] }, 'baz')).toBe(true)
11
+ expect(objectMatches({ foo: ['bar', 'baz'] }, 'qux')).toBe(false)
12
+ })
13
+
4
14
  test('matches an object', () => {
5
15
  expect(objectMatches({ foo: 'bar' }, { foo: 'bar' })).toBe(true)
6
16
  expect(objectMatches({ foo: ['bar', 'baz'] }, { foo: 'bar' })).toBe(true)
@@ -74,4 +84,148 @@ describe('objectMatches', () => {
74
84
  )
75
85
  ).toBe(false)
76
86
  })
87
+
88
+ test('allows user to provide a nested match object as well', () => {
89
+ expect(
90
+ objectMatches({ one: { two: 'bar' } }, { one: { two: 'bar' } })
91
+ ).toBe(true)
92
+ })
93
+
94
+ test('matches a nested key', () => {
95
+ expect(objectMatches({ one: { two: 'bar' } }, { 'one.two': 'bar' })).toBe(
96
+ true
97
+ )
98
+ })
99
+
100
+ test('does not use end with logic when a nested pattern is provided', () => {
101
+ expect(
102
+ objectMatches({ zero: { one: { two: 'bar' } } }, { 'one.two': 'bar' })
103
+ ).toBe(false)
104
+ })
105
+
106
+ test('does not use end with logic when a glob pattern is provided', () => {
107
+ expect(
108
+ objectMatches({ zero: { one: { two: 'bar' } } }, { '*two': 'bar' })
109
+ ).toBe(false)
110
+ })
111
+
112
+ test('matches a glob pattern', () => {
113
+ expect(objectMatches({ one: { two: 'bar' } }, { '*.two': 'bar' })).toBe(
114
+ true
115
+ )
116
+ })
117
+
118
+ test('defaults to an `endsWith` pattern', () => {
119
+ expect(objectMatches({ one: { two: 'bar' } }, { two: 'bar' })).toBe(true)
120
+ })
121
+
122
+ test('false if non-existent key', () => {
123
+ expect(objectMatches({ one: { two: 'bar' } }, { three: 'bar' })).toBe(false)
124
+ })
125
+
126
+ test('matches a deep glob pattern', () => {
127
+ expect(
128
+ objectMatches({ one: { two: { three: 'bar' } } }, { '**.three': 'bar' })
129
+ ).toBe(true)
130
+ expect(
131
+ objectMatches({ one: { two: { three: 'bar' } } }, { '*.three': 'bar' })
132
+ ).toBe(false)
133
+ })
134
+
135
+ describe('additional edge cases', () => {
136
+ // Empty Objects
137
+ test('handles empty objects', () => {
138
+ expect(objectMatches({}, {})).toBe(true)
139
+ expect(objectMatches({ foo: 'bar' }, {})).toBe(true)
140
+ expect(objectMatches({}, { foo: 'bar' })).toBe(false)
141
+ })
142
+
143
+ // Non-Object Inputs
144
+ test('handles non-object inputs', () => {
145
+ // @ts-ignore
146
+ expect(() => objectMatches('foo', { foo: 'bar' })).toThrow()
147
+
148
+ // @ts-ignore
149
+ expect(() => objectMatches(123, { foo: 'bar' })).toThrow()
150
+
151
+ // @ts-ignore
152
+ expect(() => objectMatches(null, { foo: 'bar' })).toThrow()
153
+ })
154
+
155
+ // Special Characters in Keys
156
+ test('handles special characters in keys', () => {
157
+ expect(objectMatches({ 'foo*': 'bar' }, { 'foo*': 'bar' })).toBe(true)
158
+ expect(objectMatches({ 'foo.bar': 'baz' }, { 'foo.bar': 'baz' })).toBe(
159
+ true
160
+ )
161
+ expect(objectMatches({ foo$bar: 'baz' }, { foo$bar: 'baz' })).toBe(true)
162
+ })
163
+
164
+ // Matching Null Values
165
+ test('matches null values', () => {
166
+ expect(objectMatches({ foo: null }, { foo: null })).toBe(true)
167
+ expect(objectMatches({ foo: 'bar' }, { foo: null })).toBe(false)
168
+ })
169
+
170
+ // Matching Undefined Values
171
+ test('matches undefined values', () => {
172
+ expect(objectMatches({ foo: undefined }, { foo: null })).toBe(true)
173
+
174
+ // @ts-ignore
175
+ expect(objectMatches({ foo: 'bar' }, { foo: undefined })).toBe(false)
176
+ })
177
+
178
+ // Nested Array Matches
179
+ // test('handles nested array matches', () => {
180
+ // expect(
181
+ // objectMatches({ foo: [{ bar: 'baz' }] }, { foo: [{ bar: 'baz' }] })
182
+ // ).toBe(true)
183
+ // expect(
184
+ // objectMatches({ foo: [{ bar: 'qux' }] }, { foo: [{ bar: 'baz' }] })
185
+ // ).toBe(false)
186
+ // })
187
+
188
+ // Nested Wildcard Matches
189
+ test('handles nested wildcard matches', () => {
190
+ expect(
191
+ objectMatches({ foo: { bar: 'baz' } }, { foo: { bar: '*' } })
192
+ ).toBe(true)
193
+ expect(
194
+ objectMatches({ foo: { bar: 'qux' } }, { foo: { bar: 'baz*' } })
195
+ ).toBe(false)
196
+ })
197
+
198
+ // Nested Glob Pattern Matches
199
+ test('handles nested glob pattern matches', () => {
200
+ expect(objectMatches({ foo: { bar: 'baz' } }, { 'foo.*': 'baz' })).toBe(
201
+ true
202
+ )
203
+ expect(objectMatches({ foo: { bar: 'qux' } }, { 'foo.*': 'baz' })).toBe(
204
+ false
205
+ )
206
+ })
207
+
208
+ // Type Coercion
209
+ test('it attempts to perform type coercion when a string match is provided', () => {
210
+ expect(objectMatches({ foo: 1 }, { foo: '1' })).toBe(true)
211
+ })
212
+
213
+ // Type Coercion
214
+ test('it will not coerce types if a non-string matcher is provided', () => {
215
+ expect(objectMatches({ foo: '1' }, { foo: 1 })).toBe(false)
216
+ })
217
+
218
+ // Case Sensitivity
219
+ test('is case sensitive', () => {
220
+ expect(objectMatches({ foo: 'Bar' }, { foo: 'bar' })).toBe(false)
221
+ expect(objectMatches({ foo: 'bar' }, { foo: 'Bar' })).toBe(false)
222
+ })
223
+
224
+ // Order of Keys
225
+ test('does not care about order of keys', () => {
226
+ expect(
227
+ objectMatches({ foo: 'bar', baz: 'qux' }, { baz: 'qux', foo: 'bar' })
228
+ ).toBe(true)
229
+ })
230
+ })
77
231
  })
@@ -1,5 +1,6 @@
1
1
  import { Arrayable } from './event.handler'
2
2
  import wildMatch from 'wildcard-match'
3
+ import flat from 'flat'
3
4
 
4
5
  /**
5
6
  * Glob style matching of a value
@@ -18,32 +19,82 @@ export function glob(val: any, filter: string | string[]): boolean {
18
19
  * Glob style matching of values in an object
19
20
  *
20
21
  * @param object
21
- * @param filter
22
+ * @param filterObject
22
23
  */
23
24
  export function objectMatches(
24
25
  object: Record<string, any>,
25
- filter: Record<string, Arrayable<string>>
26
+ filterObject: JSONPrimitive | FilterObj
26
27
  ): boolean {
28
+ const cleanFilter: FilterObj =
29
+ !filterObject || typeof filterObject !== 'object'
30
+ ? { '**': filterObject }
31
+ : filterObject
32
+
33
+ if (typeof object !== 'object') {
34
+ throw new Error('You cannot filter a non-object')
35
+ }
27
36
  let denied = false
28
- for (const x in filter) {
29
- const prop: Arrayable<string> = object[x]
30
-
31
- if (!object?.[x]) {
32
- return false
33
- }
34
-
35
- if (
36
- typeof prop === 'string' ||
37
- (Array.isArray(prop) && typeof prop[0] === 'string')
38
- ) {
39
- if (Array.isArray(prop)) {
40
- denied ||= !prop.some((p) => {
41
- return glob(p, filter[x])
42
- })
43
- } else {
44
- denied ||= !glob(prop, filter[x])
45
- }
46
- }
37
+ const filter: FilterObj = flat(cleanFilter, { safe: true })
38
+ const flattened = flat(object, { safe: true }) as Record<
39
+ string,
40
+ JSONPrimitive
41
+ >
42
+
43
+ // all filters MUST resolve true
44
+ for (const keyPattern in filter) {
45
+ const keys = filterKeys(flattened, keyPattern)
46
+
47
+ const valuePattern = (
48
+ Array.isArray(filter[keyPattern])
49
+ ? filter[keyPattern]
50
+ : [filter[keyPattern]]
51
+ ) as JSONPrimitive[]
52
+
53
+ // only one filter must match
54
+ denied ||= !keys.some((key) => {
55
+ const value: JSONPrimitive = flattened[key]
56
+ return valuePattern.some((match) => globOrMatch(value, match))
57
+ })
47
58
  }
48
59
  return !denied
49
60
  }
61
+
62
+ /**
63
+ * Glob keys of an object and return the narrowed set
64
+ *
65
+ * @param object
66
+ * @param glob
67
+ */
68
+ function filterKeys<T extends Record<string, any>>(
69
+ object: Record<string, any>,
70
+ glob: string
71
+ ): Array<keyof T> {
72
+ glob = glob.includes('*') || glob.includes('.') ? glob : `**.${glob}`
73
+ const matcher = wildMatch(glob, '.')
74
+ return Object.keys(object).filter((key) => matcher(key))
75
+ }
76
+
77
+ function globOrMatch(
78
+ val: Arrayable<JSONPrimitive>,
79
+ filter: JSONPrimitive
80
+ ): boolean {
81
+ if (val === undefined || val === null) {
82
+ return filter === null
83
+ }
84
+ if (Array.isArray(val)) {
85
+ return val.some((v) => globOrMatch(v, filter))
86
+ }
87
+ if (typeof filter === 'string') {
88
+ return glob(val.toString(), filter)
89
+ }
90
+
91
+ // otherwise do a simple comparison
92
+ return val === filter
93
+ }
94
+
95
+ type JSONPrimitive = string | number | boolean | null
96
+
97
+ type FilterObj = Record<
98
+ string,
99
+ Arrayable<JSONPrimitive> | Record<string, Arrayable<JSONPrimitive>>
100
+ >