diff --git a/cli.js b/cli.js index 08dc2eb..708abbd 100755 --- a/cli.js +++ b/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) diff --git a/package-lock.json b/package-lock.json index 83bc975..4a42b69 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1586,10 +1586,9 @@ } }, "minimist": { - "version": "0.0.8", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", - "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", - "dev": true + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=" }, "mkdirp": { "version": "0.5.1", @@ -1598,6 +1597,14 @@ "dev": true, "requires": { "minimist": "0.0.8" + }, + "dependencies": { + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", + "dev": true + } } }, "ms": { diff --git a/package.json b/package.json index 8da446a..62abf42 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ }, "bin": "cli.js", "dependencies": { + "minimist": "^1.2.0", "scrape-it": "^3.3.3" } }