1 |
|
2 | (function(){
|
3 | var prelude, map, sortBy, fl, closestString, nameToRaw, dasherize, naturalJoin;
|
4 | prelude = require('prelude-ls'), map = prelude.map, sortBy = prelude.sortBy;
|
5 | fl = require('fast-levenshtein');
|
6 | closestString = function(possibilities, input){
|
7 | var distances, ref$, string, distance;
|
8 | if (!possibilities.length) {
|
9 | return;
|
10 | }
|
11 | distances = map(function(it){
|
12 | var ref$, longer, shorter;
|
13 | ref$ = input.length > it.length
|
14 | ? [input, it]
|
15 | : [it, input], longer = ref$[0], shorter = ref$[1];
|
16 | return {
|
17 | string: it,
|
18 | distance: fl.get(longer, shorter)
|
19 | };
|
20 | })(
|
21 | possibilities);
|
22 | ref$ = sortBy(function(it){
|
23 | return it.distance;
|
24 | }, distances)[0], string = ref$.string, distance = ref$.distance;
|
25 | return string;
|
26 | };
|
27 | nameToRaw = function(name){
|
28 | if (name.length === 1 || name === 'NUM') {
|
29 | return "-" + name;
|
30 | } else {
|
31 | return "--" + name;
|
32 | }
|
33 | };
|
34 | dasherize = function(string){
|
35 | if (/^[A-Z]/.test(string)) {
|
36 | return string;
|
37 | } else {
|
38 | return prelude.dasherize(string);
|
39 | }
|
40 | };
|
41 | naturalJoin = function(array){
|
42 | if (array.length < 3) {
|
43 | return array.join(' or ');
|
44 | } else {
|
45 | return array.slice(0, -1).join(', ') + ", or " + array[array.length - 1];
|
46 | }
|
47 | };
|
48 | module.exports = {
|
49 | closestString: closestString,
|
50 | nameToRaw: nameToRaw,
|
51 | dasherize: dasherize,
|
52 | naturalJoin: naturalJoin
|
53 | };
|
54 | }).call(this);
|