-
Notifications
You must be signed in to change notification settings - Fork 19
/
match.test.js
257 lines (213 loc) · 6.93 KB
/
match.test.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
'use strict';
const when = require('./match').when;
const match = require('./match').match;
// var match = require('./');
const t = require('chai').assert;
describe('when multiple values can be hit', () => {
it("doesn't hit the first one", () => {
t.strictEqual(match({
[when.range(0, 43)]: 42,
[when(42)]: 72,
[when()]: 'should never be hit',
})(42), 42);
});
});
describe('match', () => {
const input = [{protocol: 'HTTP', i:10}, {protocol: 'AMQP', i:11}, {protocol: 'AMQP', i:5}, {protocol: 'WAT', i:3}];
it('does not require a catch-all at the definition of the match', () => {
t.doesNotThrow(() => match({
[when({protocol:'HTTP'})]: (o) => o.i+1,
[when({protocol:'AMQP'})]: (o) => o.i+2,
}));
});
describe('when matching without a catch-all', function () {
beforeEach(function () {
this.doMatch = match({
[when('value')]: 42,
[when('other value')]: 99
});
});
it('works when a match is hit', function () {
t.strictEqual(this.doMatch('value'), 42);
});
it('throws an Error when no matches are hit', function () {
t.throws(() => {
this.doMatch('not a value');
});
});
});
describe('match(<input>, specification)', () => {
it('instantly performs the match, rather than returning a function', function () {
t.strictEqual(
match('value', {
[when('value')]: 42,
[when()]: 99
}),
42
);
});
describe('the example in the docs', function () {
it('works correctly', function () {
function fact(n){
return match(n, {
[when(0)]: 1,
[when()]: (n) => n * fact(n-1)
});
}
t.deepEqual(fact(10), 3628800);
});
});
});
describe('matching', () => {
it('matches objects based on properties', () => {
const output = input.map(match({
[when({protocol:'HTTP', i:12})]: (o) => 1000,
[when({protocol:'HTTP'})]: (o) => o.i+1,
[when({protocol:'AMQP', i:12})]: (o) => 1001,
[when({protocol:'AMQP'})]: (o) => o.i+2,
[when()]: (o) => 0,
}));
t.deepEqual(output, [11, 13, 7, 0]);
});
it('matches arrays based on indexes and content', () => {
const output = [['a', 'b'], ['c'], ['d', 'e', 1]].map(match({
[when(['c'])]: 1000,
[when(['a', 'b'])]: 1001,
[when([])]: 1002,
[when(['d', 'e', 1])]: 1003,
[when()]: (o) => 0
}));
t.deepEqual(output, [1001, 1000, 1003]);
});
it('matches number as well', () => {
function fact(n){
return match({
[when(0)]: 1,
[when()]: (n) => n * fact(n-1) // when() === catch-all
})(n);
}
t.strictEqual(fact(10),3628800);
});
it('matches empty array', () => {
function length(list){
return match({
[when([])]: 0,
// [when()]: ([head, ...tail]) => 1 + length(tail) // still does not work in v5.3.0
[when()]: (arr) => 1 + length(arr.slice(1))
})(list);
}
t.strictEqual(length([1, 2, 3]), 3);
t.strictEqual(length([{}, {}, {}, {}]), 4);
});
it('supports regexp match', () => {
const output = [3, ' 2', 1, 'zEro', 90].map(match({
[when(/1/)]: 'one',
[when(/2/g)]: 'two',
[when(/3/)]: 'three',
[when(/zero/i)]: 'zero',
[when()]: v => v
}));
t.deepEqual(output, ['three', 'two', 'one', 'zero', 90]);
const invalidEmails = ['hey.com', 'fg@plop.com', 'fg+plop@plop.com', 'wat'].filter(match({
[when(/\S+@\S+\.\S+/)]: false, // **seems** to be a valid email
[when()]: true // the email may be invalid, return it
}));
t.deepEqual(invalidEmails, ['hey.com', 'wat']);
})
describe('when.and', () => {
it('supports AND conditional', () => {
const output = input.map(match({
[when.and({protocol:'AMQP'}, {i:5})]: o => o.i,
[when.and({protocol:'HTTP'}, {i:10})]: o => o.i,
[when()]: (o) => 0,
}));
t.deepEqual(output, [10, 0, 5, 0]);
})
});
describe('when.or', () => {
it('supports OR conditional matching', () => {
// example from https://kerflyn.wordpress.com/2011/02/14/playing-with-scalas-pattern-matching/
function parseArgument(arg){
return match({
[when.or("-h", "--help")]: () => displayHelp,
[when.or("-v", "--version")]: () => displayVersion,
[when()]: (whatever) => unknownArgument.bind(null, whatever)
})(arg);
}
function displayHelp(){
console.log('help.');
}
function displayVersion(){
console.log('v0.0.0');
}
function unknownArgument(whatever){
throw new Error(`command ${whatever} not found`);
}
t.strictEqual(parseArgument('-h'), displayHelp);
t.strictEqual(parseArgument('--help'), displayHelp);
t.strictEqual(parseArgument('-v'), displayVersion);
t.strictEqual(parseArgument('--version'), displayVersion);
t.throws(() => {
parseArgument('hey')();
});
});
})
});
describe('when.range', () => {
const rangeStart = 0,
rangeEnd = 5;
beforeEach(function () {
this.withinRange = match({
[when.range(rangeStart, rangeEnd)]: true,
[when()]: false
});
});
describe('given a value within the range', function () {
it('matches', function () {
t.isTrue(this.withinRange(rangeStart+1));
});
});
describe('given a value at the lower bound', function () {
it('matches', function () {
t.isTrue(this.withinRange(rangeStart));
});
});
describe('given a value at the upper bound', function () {
it('matches', function () {
t.isTrue(this.withinRange(rangeEnd));
});
});
describe('given a value above the upper bound', function () {
it('does not match', function () {
t.isFalse(this.withinRange(rangeEnd+1));
});
});
describe('given a value below the lower bound', function () {
it('does not match', function () {
t.isFalse(this.withinRange(rangeStart-1));
});
});
describe('the example in the docs', function () {
it('works correctly', function () {
var result = [12, 42, 99, 101].map(match({
[when.range(0, 41)]: '< answer',
[when.range(43, 100)]: '> answer',
[when(42)]: 'answer',
[when()]: '< 0, or > 100'
}));
var expected = ['< answer', 'answer', '> answer', '< 0, or > 100']
t.deepEqual(result, expected);
});
});
});
describe('yielding', () => {
it('yields primitive values', () => {
const output = input.map(match({
[when({protocol:'HTTP'})]: 1,
[when({protocol:'AMQP'})]: 2,
[when()]: 0,
}));
t.deepEqual(output, [1, 2, 2, 0]);
});
});
});