Skip to content

Commit f836563

Browse files
committed
feat(filters): add better way to filter/keep based on undefined attr
Makes it possible for entity whitelist rules to take the value `true` or `false` to mean "keep if present" or "discard if present".
1 parent 8d003aa commit f836563

File tree

2 files changed

+96
-18
lines changed

2 files changed

+96
-18
lines changed

src/lib/filters/entities.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,15 @@ export const shouldKeepEntityByAttribute = (
9191
}
9292

9393
const isValid = Object.keys(whitelist).every((attr) => {
94-
const regex = new RegExp(whitelist[attr])
95-
const hasData = data.hasOwnProperty(attr)
94+
const check = whitelist[attr]
9695

97-
return hasData && regex.test(data[attr])
96+
if (typeof check === "boolean") {
97+
const hasData = data.hasOwnProperty(attr)
98+
99+
return check ? hasData : !hasData
100+
}
101+
102+
return new RegExp(check).test(data[attr])
98103
})
99104

100105
return isValid

src/lib/filters/entities.test.js

+88-15
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,25 @@ describe("entities", () => {
180180
).toBe(false)
181181
})
182182

183+
it("attribute not defined on entities", () => {
184+
expect(
185+
shouldKeepEntityByAttribute(
186+
[
187+
{
188+
type: "LINK",
189+
whitelist: {
190+
href: "^(?!#)",
191+
},
192+
},
193+
],
194+
"LINK",
195+
{
196+
url: "http://example.com",
197+
},
198+
),
199+
).toBe(true)
200+
})
201+
183202
describe("multiple attributes", () => {
184203
it("valid", () => {
185204
expect(
@@ -224,24 +243,78 @@ describe("entities", () => {
224243
})
225244
})
226245

227-
it("attribute not defined on entities", () => {
228-
expect(
229-
shouldKeepEntityByAttribute(
230-
[
246+
describe("attribute presence", () => {
247+
it("requested, absent", () => {
248+
expect(
249+
shouldKeepEntityByAttribute(
250+
[
251+
{
252+
type: "LINK",
253+
whitelist: {
254+
id: true,
255+
},
256+
},
257+
],
258+
"LINK",
259+
{},
260+
),
261+
).toBe(false)
262+
})
263+
264+
it("requested, present", () => {
265+
expect(
266+
shouldKeepEntityByAttribute(
267+
[
268+
{
269+
type: "LINK",
270+
whitelist: {
271+
id: true,
272+
},
273+
},
274+
],
275+
"LINK",
231276
{
232-
type: "LINK",
233-
whitelist: {
234-
id: ".*",
277+
id: "5",
278+
},
279+
),
280+
).toBe(true)
281+
})
282+
283+
it("requested out, absent", () => {
284+
expect(
285+
shouldKeepEntityByAttribute(
286+
[
287+
{
288+
type: "LINK",
289+
whitelist: {
290+
id: false,
291+
},
235292
},
293+
],
294+
"LINK",
295+
{},
296+
),
297+
).toBe(true)
298+
})
299+
300+
it("requested out, present", () => {
301+
expect(
302+
shouldKeepEntityByAttribute(
303+
[
304+
{
305+
type: "LINK",
306+
whitelist: {
307+
id: false,
308+
},
309+
},
310+
],
311+
"LINK",
312+
{
313+
id: "5",
236314
},
237-
],
238-
"LINK",
239-
{
240-
href: "#_msocom_1",
241-
target: "_blank",
242-
},
243-
),
244-
).toBe(false)
315+
),
316+
).toBe(false)
317+
})
245318
})
246319

247320
describe("defaults", () => {

0 commit comments

Comments
 (0)