/
cli.js
executable file
·61 lines (49 loc) · 1.29 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env node
const parseArgs = require('minimist')
const jishon = require('./index.js')
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, {
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)