Created
February 6, 2016 21:49
-
-
Save anonymous/d80d1550d73538d0ce42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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