Skip to content

Instantly share code, notes, and snippets.

Created February 6, 2016 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d80d1550d73538d0ce42 to your computer and use it in GitHub Desktop.
Save anonymous/d80d1550d73538d0ce42 to your computer and use it in GitHub Desktop.
const LANG_SCORE = 128;
const FORMAT_SCORE = 64;
const weighTemplate = curry((format, lang, item) =>
(item.lang === lang ? LANG_SCORE : 0) +
(item.format === format ? FORMAT_SCORE : 0));
const filterMatchingTemplates = curry((templates, format, lang, type) =>
pipe(
// First, find the template items that match the type, lang and format,
// or where lang and format are 'ANY', i.e. wildcards
filter(where({
type : equals(type),
lang : equalOrAny(lang),
// markdown is a special format that acts like ANY
format : anyPass([ equalOrAny(format), equals('md') ])
})),
// Group by name, and pick the values that are most appropriate for
// given format and lang
groupBy(prop('name')),
mapObj(maximumBy(weighTemplate(format, lang))),
values
)(templates));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment