Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support command line flags for words, names and tags
and sentences
- Loading branch information
chee
committed
Sep 23, 2017
1 parent
c4e4e3a
commit 3760f59
Showing
3 changed files
with
69 additions
and
6 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 |
---|---|---|
@@ -1,7 +1,62 @@ | ||
#!/usr/bin/env node | ||
|
||
const parseArgs = require('minimist') | ||
const jishon = require('./index.js') | ||
|
||
jishon(process.argv.slice(2).join(' ')) | ||
const WORDS = 'words' | ||
const SENTENCES = 'sentences' | ||
const NAMES = 'names' | ||
const TAGS = 'tags' | ||
|
||
const argv = parseArgs(process.argv.slice(2), { | ||
alias: { | ||
w: WORDS, | ||
s: SENTENCES, | ||
n: NAMES, | ||
t: TAGS | ||
} | ||
}) | ||
|
||
const tagFilter = tags => word => | ||
tags.some(tag => word.tags.includes(tag.toLowerCase())) | ||
|
||
const filterForTags = tags => response => | ||
tags | ||
? Object.assign({}, response, { | ||
x: console.log(tags), | ||
words: response.words.filter(tagFilter(tags)) | ||
}) | ||
: response | ||
|
||
const selectSections = options => response => { | ||
const sections = [WORDS, SENTENCES, NAMES] | ||
const requestedSections = sections | ||
.map(section => options[section] && section) | ||
.filter(Boolean) | ||
|
||
const sectionRequested = !!requestedSections.length | ||
|
||
if (!sectionRequested) { | ||
return response | ||
} | ||
|
||
if (requestedSections.length === 1) { | ||
return response[requestedSections] | ||
} | ||
|
||
return requestedSections.reduce((results, section) => { | ||
results[section] = response[section] | ||
return results | ||
}, {}) | ||
} | ||
|
||
const term = argv._.join(' ') | ||
|
||
const tags = Array.isArray(argv.tags) | ||
? argv.tags | ||
: argv.tags && [argv.tags] | ||
|
||
jishon(term) | ||
.then(filterForTags(tags)) | ||
.then(selectSections(argv)) | ||
.then(JSON.stringify) | ||
.then(console.log) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
}, | ||
"bin": "cli.js", | ||
"dependencies": { | ||
"minimist": "^1.2.0", | ||
"scrape-it": "^3.3.3" | ||
} | ||
} |