This repository has been archived by the owner on Mar 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(proofdict-tester): support whitelist and blacklist
- Loading branch information
Showing
5 changed files
with
123 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// MIT © 2017 azu | ||
import { ProofdictItem, Proofdict } from "./proofdict-tester"; | ||
|
||
const NOUN_TAG = "noun"; | ||
|
||
/** | ||
* Does the `dict` has "noun" tag? | ||
* @param {ProofdictItem} dict | ||
* @returns {boolean} | ||
*/ | ||
export function isNoun(dict: ProofdictItem): boolean { | ||
return dict.tags.indexOf(NOUN_TAG) !== -1; | ||
} | ||
|
||
export function filterByTags(dictionary: Proofdict, whitelistTags: string[] = [], blacklistTags: string[] = []) { | ||
if (whitelistTags.length > 0) { | ||
return dictionary.filter(item => { | ||
return whitelistTags.every(whitelistTags => item.tags.indexOf(whitelistTags) !== -1); | ||
}); | ||
} | ||
if (blacklistTags.length > 0) { | ||
return dictionary.filter(item => { | ||
return !item.tags.some(tag => blacklistTags.indexOf(tag) !== -1); | ||
}); | ||
} | ||
return dictionary; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// MIT © 2017 azu | ||
import { Proofdict, ProofdictItem } from "../src/proofdict-tester"; | ||
import { filterByTags } from "../src/TagFilter"; | ||
import * as assert from "assert"; | ||
|
||
const proofdict: Proofdict = require("./fixtures/proofdict.json"); | ||
const shouldHaveTag = (tag: string) => { | ||
return (item: ProofdictItem) => { | ||
assert.ok(item.tags.indexOf(tag) !== -1, `item should have tag(${tag}). item: ${JSON.stringify(item)}`); | ||
}; | ||
}; | ||
const shouldNotHaveTag = (tag: string) => { | ||
return (item: ProofdictItem) => { | ||
assert.ok(item.tags.indexOf(tag) === -1, `item should not have tag(${tag}). item: ${JSON.stringify(item)}`); | ||
}; | ||
}; | ||
describe("TagFilter", () => { | ||
describe("#filterByTags", () => { | ||
it("whitelist options filter items", () => { | ||
const whitelistTags = ["noun"]; | ||
const items = filterByTags(proofdict, whitelistTags); | ||
items.forEach(item => { | ||
shouldHaveTag("noun")(item); | ||
}); | ||
}); | ||
it("whitelist options filter items by multiple tags", () => { | ||
const whitelistTags = ["noun", "JavaScript"]; | ||
const items = filterByTags(proofdict, whitelistTags); | ||
items.forEach(item => { | ||
shouldHaveTag("noun")(item); | ||
shouldHaveTag("JavaScript")(item); | ||
}); | ||
}); | ||
it("blacklist options reject items", () => { | ||
const blacklistTags = ["noun"]; | ||
const items = filterByTags(proofdict, [], blacklistTags); | ||
items.forEach(item => { | ||
shouldNotHaveTag("noun")(item); | ||
}); | ||
}); | ||
it("blacklist options reject items by multiple tags", () => { | ||
const blacklistTags = ["noun", "JavaScript"]; | ||
const items = filterByTags(proofdict, [], blacklistTags); | ||
items.forEach(item => { | ||
shouldNotHaveTag("noun")(item); | ||
shouldNotHaveTag("JavaScript")(item); | ||
}); | ||
}); | ||
it("whitelist options filter items", () => { | ||
const whitelistTags = ["noun"]; | ||
const items = filterByTags(proofdict, whitelistTags); | ||
items.forEach(item => { | ||
shouldHaveTag("noun")(item); | ||
}); | ||
}); | ||
it("whitelist/blacklist options, prefer to use whitelist", () => { | ||
const whitelist = ["noun"]; | ||
const blacklistTags = ["noun"]; | ||
const items = filterByTags(proofdict, whitelist, blacklistTags); | ||
items.forEach(item => { | ||
shouldHaveTag("noun")(item); | ||
}); | ||
}); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters