forked from FGRibreau/match-when
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.js
182 lines (143 loc) · 4.44 KB
/
match.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
'use strict';
const _catchAllSymbol = Symbol('match.pattern.catchAll');
const _patternOR = Symbol('match.pattern.OR');
const _patternORStr = _patternOR.toString(); // dirty hack
const _patternAND = Symbol('match.pattern.AND');
const _patternANDStr = _patternAND.toString(); // dirty hack
const _patternRANGE = Symbol('match.pattern.RANGE');
const _patternRANGEStr = _patternRANGE.toString(); // dirty hack
const _patternREGEXP = Symbol('match.pattern.REGEXP');
const _patternREGEXPStr = _patternREGEXP.toString(); // dirty hack
const EXTRACT_PATTERN_AND_FLAGS = /\/(.*)\/(.*)/;
function MissingCatchAllPattern() {
Error.call(this, 'Missing when() catch-all pattern as last match argument, add [when()]: void 0');
if (!('stack' in this)){
this.stack = (new Error()).stack;
}
}
MissingCatchAllPattern.prototype = Object.create(Error.prototype);
function match(/* args... */){
const args = Array.from(arguments),
obj = args[args.length-1];
// pre-compute matchers
let matchers = [];
for(let key in obj){
matchers.push(when.unserialize(key, obj[key]));
}
// since JS objects are unordered we need to reorder what for..in give us even if the order was already right
// because it depends on the JS engine implementation. See #2
matchers.sort(function(a, b){
return a.position < b.position ? -1 : 1;
});
if(Object.getOwnPropertySymbols(obj).indexOf(_catchAllSymbol) !== -1){
matchers.push(when.unserialize(_catchAllSymbol, obj[_catchAllSymbol]));
}
const calculateResult = function(input){
const matched = matchers.find((matcher) => matcher.match(input));
if (!matched) {
throw new MissingCatchAllPattern();
}
return typeof matched.result === 'function' ? matched.result(input) : matched.result;
};
return args.length === 2 ? calculateResult(args[0]) : calculateResult;
}
function when(props){
if(props === undefined){
return _catchAllSymbol;
}
if(props instanceof RegExp){
return _serialize([_patternREGEXP.toString(), props.toString()]);
}
return _serialize(props);
}
when.__uid = 0;
// Any -> String
function _serialize(mixed){
return JSON.stringify([when.__uid++, mixed]);
}
// String -> [Number, Any]
function _unserialize(str){
return JSON.parse(str);
}
function _true(){return true;}
// Any -> String
function _match(props){
if(Array.isArray(props)){
if(props[0] === _patternORStr){
props.shift();
return function(input){
return props[0].some((prop) => _matching(prop, input));
};
}
if(props[0] === _patternANDStr){
props.shift();
return function(input){
return props[0].every((prop) => _matching(prop, input));
};
}
if(props[0] === _patternRANGEStr){
props.shift();
return function(input){
return props[0] <= input && input <= props[1];
};
}
if(props[0] === _patternREGEXPStr){
const res = EXTRACT_PATTERN_AND_FLAGS.exec(props[1]);
return _matching.bind(null, new RegExp(res[1], res[2]));
}
}
function _matching(props, input){
// implement array matching
if(Array.isArray(input)){
// @todo yes this is a quick and dirty way, optimize this
return JSON.stringify(props) === JSON.stringify(input);
}
if(props instanceof RegExp){
return props.test(input);
}
if(typeof input === 'object'){
for(let prop in props){
if(input[prop] !== props[prop]){
return false;
}
}
return true;
}
return props === input;
}
return (input) => _matching(props, input);
}
// mixed -> String
when.or = function(/* args... */){
return _serialize([_patternOR.toString(), Array.prototype.slice.call(arguments)]);
};
// mixed -> String
// upcoming...
when.and = function(/* args... */){
return _serialize([_patternAND.toString(), Array.prototype.slice.call(arguments)]);
};
when.range = function(start, end){
return _serialize([_patternRANGE.toString(), start, end]);
};
when.unserialize = function(serializedKey, value){
if(serializedKey === _catchAllSymbol){
return {
match: _true,
result: value,
position: Infinity
};
}
// const {position, matcherConfiguration} = _unserialize(serializedKey);
const deserialized = _unserialize(serializedKey);
const matcherConfiguration = deserialized[1];
const position = deserialized[0];
return {
match: _match(matcherConfiguration),
result: value,
position: position
};
};
module.exports = {
match,
when
};