forked from tgalazka/fuzzymap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzzymap.js
More file actions
86 lines (60 loc) · 1.42 KB
/
fuzzymap.js
File metadata and controls
86 lines (60 loc) · 1.42 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Dependencies
*/
var _ = require('underscore');
/**
* Fuzzymap
*/
module.exports = (function() {
var Base = {};
Base.defineMap = function(mapObj, callback) {
if(!_.isFunction(callback))
return newFuzzyMap(mapObj);
return callback(null, newFuzzyMap(mapObj));
};
return Base;
})();
/**
* Private functions
*/
var newFuzzyMap = function(map) {
var Fuzzymap = {};
var Map = map;
var PureMap = map;
var LastValue = null;
if(!_.isArray(map))
Map = [Map]
Fuzzymap.Map = function() {
return PureMap;
}
Fuzzymap.last = function() {
return LastValue;
}
Fuzzymap.map = function(item) {
LastValue = item;
_.find(Map, function(MapGroup) {
return testGroup(item, MapGroup) !== false;;
});
return Fuzzymap.last();
}
var testGroup = function(item, MapGroup) {
if(MapGroup === null || MapGroup === undefined)
MapGroup = {};
var mapped = _.find(_.keys(MapGroup), function(mapString) {
var regexes = MapGroup[mapString];
if(!_.isArray(regexes))
regexes = [regexes];
var regex = _.find(regexes, function(regex) {
return nil(item.match(regex)) === false;
});
return nil(regex) === false;
});
if(!nil(mapped))
LastValue = mapped;
return nil(mapped) === false;
};
return Fuzzymap;
};
var nil = function(test) {
return (test === null || test === undefined);
};