Skip to content

Instantly share code, notes, and snippets.

@16Yongjin
Created May 4, 2018 08:45
Show Gist options
  • Select an option

  • Save 16Yongjin/38acbf4442f4e8eec19c865878269a32 to your computer and use it in GitHub Desktop.

Select an option

Save 16Yongjin/38acbf4442f4e8eec19c865878269a32 to your computer and use it in GitHub Desktop.
const fs = require('mz/fs')
const _ = require('partial-js')
const trim = s => s.trim()
const verbPath = verb => `content/${verb[0]}/${verb}.json`
const getJson = _.pipe(fs.readFile, JSON.parse)
const remove2ndPerson = _.reject(c => ![0, 2, 3, 4].includes(c.group_sort) && c.sort == 1 || c.sort == 4)
const getConj = _.pipe(trim, verbPath, getJson, v => v.conjugations, remove2ndPerson, _.groupBy('group'))
const categoryPath = verb => `categories/${verb[0]}.json`
const verbCategories = {}
const cachedCategory = verb => verbCategories[verb[0]]
const memoize = k => v => (verbCategories[k] = v, v)
const startsWith = w => str => str.startsWith(w)
const getCategory = _.pipe(trim, _.first)
const getVerbIndex = category => cachedCategory(category) || _.go(category, categoryPath, getJson, memoize(category))
const takeFive = verb => _.pipe(L.filter(startsWith(verb)), L.take(5))
const searchVerb = verb => _.go(verb, getCategory, getVerbIndex, takeFive(verb))
const rootIndex = {}
_.go(getJson('roots/roots.json'), _(Object.assign, rootIndex, _))
const getRoots = verbs => rootIndex[verbs]
// getConj('passar').then(console.log).catch(console.warn)
// searchVerb('pas').then(console.log).catch(console.warn)
// getRoots('passaria').then(console.log).catch(console.warn)
module.exports = { getConj, searchVerb, getRoots }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment