-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtonic_example.js
More file actions
49 lines (43 loc) · 1.33 KB
/
tonic_example.js
File metadata and controls
49 lines (43 loc) · 1.33 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
var matchPattern = require('lodash-match-pattern');
var lodash = matchPattern.getLodashModule(); // Use our lodash extensions (recommended)
var isString = lodash.isString;
// ^^^ setting isString explicitly to overcome a REPL issue with using '_'
// Trivial example
var testValue = {a: 1, b: 'abc'};
var successResult = matchPattern(testValue, {a: 1, b: isString});
// returns null for a successful match.
var failResult = matchPattern(testValue, {a: isString, b: 'abc'});
// returns "{a: 1} didn't match target {a: \'function isString(value) {...}\'}"
// Fancy test value
var fancyValue = {
name: 'Gale',
email: 'gale.force@winds.com',
age: 23,
friends: [
{ name: 'Breeze', age: 14 },
{ name: 'Cyclone', age: 29 },
{ name: 'Gust', age: 22 }
]
};
// fancy match with partial match
var partialMatchResult = matchPattern(fancyValue, `{
name: _.isString,
email: _.isEmail,
age: _.isBetween|20|30
...
}`);
// extra fancy match with filterPattern transform.
// This checks that "Gale" has two friends between the ages of 20 and 30
// and that one of them is named "Breeze".
var extraFancyResult = matchPattern(fancyValue, `{
name: /^[A-Z]\w+$/,
email: _.isEmail,
age: _.isBetween|20|30,
friends: {
<-.filterPattern|'{age: _.isBetween|20|30 ...}': _.isSize|2,
<=.get|name: [
'Breeze',
...
]
}
}`);