Skip to content

Commit

Permalink
support command line flags for words, names and tags
Browse files Browse the repository at this point in the history
and sentences
  • Loading branch information
chee committed Sep 23, 2017
1 parent c4e4e3a commit 3760f59
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
59 changes: 57 additions & 2 deletions cli.js
@@ -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)
15 changes: 11 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -27,6 +27,7 @@
},
"bin": "cli.js",
"dependencies": {
"minimist": "^1.2.0",
"scrape-it": "^3.3.3"
}
}

0 comments on commit 3760f59

Please sign in to comment.