Skip to content

Commit

Permalink
Merge pull request #711 from saenglert/master
Browse files Browse the repository at this point in the history
Add getFilteredByTags method and tests
  • Loading branch information
zachleat authored Dec 20, 2019
2 parents 727607c + 3da2f86 commit f30523f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
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

0 comments on commit f30523f

Please sign in to comment.