Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
Added: statinamic/lib/enhance-collection can now filter keys using …
Browse files Browse the repository at this point in the history
…a RegExp
  • Loading branch information
MoOx committed Nov 6, 2015
1 parent 348a76f commit 4f2fdea
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
- Removed: `statinamic/lib/enhance-collection` do not add siblings by default.
You will need to pass `{ addSiblingReferences: true }` in the options to get
next and previous references to collection items.
- Added: `statinamic/lib/enhance-collection` can now filter keys using a RegExp.
- Added: when `--production` is used, `process.env.NODE_ENV` is automatically
set to `production`.

Expand Down
42 changes: 42 additions & 0 deletions src/enhance-collection/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import tape from "tape"

import enhanceCollection from ".."

tape("statinamic/lib/enhance-collection", (test) => {

const collec = [
{ k: "ey" },
{ k: "ay" },
{ k: "ei" },
{ k: "eye" },
]

test.deepEqual(
enhanceCollection(
collec,
{
filter: { k: "ey" },
}
),
[
{ k: "ey" },
],
"should filter by object { key: string }"
)

test.deepEqual(
enhanceCollection(
collec,
{
filter: { k: /y$/ },
}
),
[
{ k: "ey" },
{ k: "ay" },
],
"should filter by object { key: regexp }"
)

test.end()
})
12 changes: 11 additions & 1 deletion src/enhance-collection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ export function filter(collection, filters) {
const keys = Object.keys(filter)
if (
keys.reduce(
(acc, key) => acc && item[key] === filter[key],
(acc, key) => acc && (
(
typeof filter[key] === "string" &&
item[key] === filter[key]
)
||
(
filter[key] instanceof RegExp &&
item[key].match(filter[key])
)
),
true
)
) {
Expand Down

0 comments on commit 4f2fdea

Please sign in to comment.