-
Notifications
You must be signed in to change notification settings - Fork 1
/
matchers.js
102 lines (87 loc) · 2.24 KB
/
matchers.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
const TransformValue = require('./types/value');
const matchers = {
val: '(\\-?[\\d\\.]+)',
unit: '([^\\s]*)',
',': '\\,\\s*'
};
function extractVal(index, expectUnits) {
return function (match) {
var units = '';
if (typeof expectUnits == 'undefined' || expectUnits) {
// get the units
// default to undefined if an empty string which means the
// default units for the XYZ value type will be used
units = match[index + 1] || undefined;
}
// create the transform value
return new TransformValue(match[index], units);
};
}
function makeRegex(fnName, params) {
var regex = fnName + '\\(';
(params || '').split(/\s/).forEach(function (param) {
regex += matchers[param];
});
// return the regex
return new RegExp(regex + '\\)');
}
exports.translate = [
// standard 2d translation
{
regex: makeRegex('translate', 'val unit , val unit'),
x: extractVal(1),
y: extractVal(3)
},
// 2d/3d translation on a specific axis
{
regex: makeRegex('translate(X|Y|Z)', 'val unit'),
extract: function (match, data) {
data[match[1].toLowerCase()] = extractVal(2)(match);
},
multi: true
},
// 3d translation as the specific translate3d prop
{
regex: makeRegex('translate3d', 'val unit , val unit , val unit'),
x: extractVal(1),
y: extractVal(3),
z: extractVal(5)
}
];
exports.rotate = [
// standard 2d rotation
{
regex: makeRegex('rotate', 'val unit'),
z: extractVal(1)
},
// 3d rotations on a specific axis
{
regex: makeRegex('rotate(X|Y|Z)', 'val unit'),
extract: function (match, data) {
data[match[1].toLowerCase()] = extractVal(2)(match);
},
multi: true
}
];
exports.scale = [
// standard 2d scaling (single parameter version)
{
regex: makeRegex('scale', 'val'),
x: extractVal(1, false),
y: extractVal(1, false)
},
// standard 2d scaling (two parameter version)
{
regex: makeRegex('scale', 'val , val'),
x: extractVal(1, false),
y: extractVal(2, false)
},
// 2d/3d translation on a specific axis
{
regex: makeRegex('scale(X|Y|Z)', 'val'),
extract: function (match, data) {
data[match[1].toLowerCase()] = extractVal(2, false)(match);
},
multi: true
}
];