Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getFilteredByTags method and tests #711

Merged
merged 3 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/TemplateCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,28 @@ class TemplateCollection extends Sortable {
}

getFilteredByTag(tagName) {
return this.getAllSorted().filter(function(item) {
let match = false;
return this.getAllSorted().filter(item => {
if (!tagName) {
return true;
} else if (Array.isArray(item.data.tags)) {
item.data.tags.forEach(tag => {
if (tag === tagName) {
match = true;
}
});
return item.data.tags.some(tag => tag === tagName);
}
return match;
return false;
});
}

getFilteredByTags(...tags) {
return this.getAllSorted().filter(item =>
tags.every(requiredTag => {
const itemTags = item.data.tags;
if (Array.isArray(itemTags)) {
return itemTags.includes(requiredTag);
} else {
return itemTags === requiredTag;
}
})
);
}
}

module.exports = TemplateCollection;
45 changes: 45 additions & 0 deletions test/TemplateCollectionTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ test("getFilteredByTag", async t => {
let cats = c.getFilteredByTag("cat");
t.is(cats.length, 2);
t.deepEqual(cats[0].template, tmpl2);
t.deepEqual(cats[1].template, tmpl3);

let dogs = c.getFilteredByTag("dog");
t.is(dogs.length, 1);
Expand All @@ -97,12 +98,56 @@ test("getFilteredByTag (added out of order, sorted)", async t => {
t.truthy(cats.length);
t.is(cats.length, 2);
t.deepEqual(cats[0].template, tmpl2);
t.deepEqual(cats[1].template, tmpl3);

let dogs = c.getFilteredByTag("dog");
t.truthy(dogs.length);
t.deepEqual(dogs[0].template, tmpl1);
});

test("getFilteredByTags", async t => {
let c = new Collection();
await c._testAddTemplate(tmpl1);
await c._testAddTemplate(tmpl2);
await c._testAddTemplate(tmpl3);

let postsAndCats = c.getFilteredByTags("post", "cat");
t.is(postsAndCats.length, 1);
t.deepEqual(postsAndCats[0].template, tmpl3);

let cats = c.getFilteredByTags("cat");
t.is(cats.length, 2);
t.deepEqual(cats[0].template, tmpl2);
t.deepEqual(cats[1].template, tmpl3);

let dogs = c.getFilteredByTags("dog");
t.is(dogs.length, 1);
t.deepEqual(dogs[0].template, tmpl1);
});

test("getFilteredByTags (added out of order, sorted)", async t => {
let c = new Collection();
await c._testAddTemplate(tmpl3);
await c._testAddTemplate(tmpl2);
await c._testAddTemplate(tmpl1);

let postsAndCats = c.getFilteredByTags("post", "cat");
t.truthy(postsAndCats.length);
t.is(postsAndCats.length, 1);
t.deepEqual(postsAndCats[0].template, tmpl3);

let cats = c.getFilteredByTags("cat");
t.truthy(cats.length);
t.is(cats.length, 2);
t.deepEqual(cats[0].template, tmpl2);
t.deepEqual(cats[1].template, tmpl3);

let dogs = c.getFilteredByTags("dog");
t.truthy(dogs.length);
t.is(dogs.length, 1);
t.deepEqual(dogs[0].template, tmpl1);
});

test("getFilteredByGlob", async t => {
let c = new Collection();
await c._testAddTemplate(tmpl1);
Expand Down