-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperations.js
More file actions
142 lines (136 loc) · 3.53 KB
/
operations.js
File metadata and controls
142 lines (136 loc) · 3.53 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
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
/* eslint-disable no-var */
/* eslint-disable camelcase */
const helpers = require('./helpers')
module.exports = {
'==': function (a, b) {
return a === b
},
'===': function (a, b) {
return a === b
},
'!=': function (a, b) {
return a !== b
},
'!==': function (a, b) {
return a !== b
},
'>': function (a, b) {
return a > b
},
'>=': function (a, b) {
return a >= b
},
'<': function (a, b, c) {
return (c === undefined) ? a < b : (a < b) && (b < c)
},
'<=': function (a, b, c) {
return (c === undefined) ? a <= b : (a <= b) && (b <= c)
},
'!!': function (a) {
return helpers.truthy(a)
},
'!': function (a) {
return !helpers.truthy(a)
},
'%': function (a, b) {
return a % b
},
log: function (a) {
console.log(a); return a
},
in: function (a, b) {
if (!b || typeof b.indexOf === 'undefined') return false
return (b.indexOf(a) !== -1)
},
cat: function () {
return Array.prototype.join.call(arguments, '')
},
substr: function (source, start, end) {
if (end < 0) {
// JavaScript doesn't support negative end, this emulates PHP behavior
var temp = String(source).substr(start)
return temp.substr(0, temp.length + end)
}
return String(source).substr(start, end)
},
'+': function () {
return Array.prototype.reduce.call(arguments, function (a, b) {
return parseFloat(a, 10) + parseFloat(b, 10)
}, 0)
},
'*': function () {
return Array.prototype.reduce.call(arguments, function (a, b) {
return parseFloat(a, 10) * parseFloat(b, 10)
})
},
'-': function (a, b) {
if (b === undefined) {
return -a
} else {
return a - b
}
},
'/': function (a, b) {
return a / b
},
min: function () {
return Math.min.apply(this, arguments)
},
max: function () {
return Math.max.apply(this, arguments)
},
merge: function () {
return Array.prototype.reduce.call(arguments, function (a, b) {
return a.concat(b)
}, [])
},
var: function (a, b) {
var not_found = (b === undefined) ? null : b
var data = this
if (typeof a === 'undefined' || a === '' || a === null) {
return data
}
var sub_props = String(a).split('.')
for (var i = 0; i < sub_props.length; i++) {
if (data === null) {
return not_found
}
// Descending into data
data = data[sub_props[i]]
if (data === undefined) {
return not_found
}
}
return data
},
missing: function () {
/*
Missing can receive many keys as many arguments, like {"missing:[1,2]}
Missing can also receive *one* argument that is an array of keys,
which typically happens if it's actually acting on the output of another command
(like 'if' or 'merge')
*/
var missing = []
var keys = Array.isArray(arguments[0]) ? arguments[0] : arguments
for (var i = 0; i < keys.length; i++) {
var key = keys[i]
var value = helpers.apply({ var: key }, this)
if (value === null || value === '') {
missing.push(key)
}
}
return missing
},
missing_some: function (need_count, options) {
// missing_some takes two arguments, how many (minimum) items must be present, and an array of keys (just like 'missing') to check for presence.
var are_missing = helpers.apply({ missing: options }, this)
if (options.length - are_missing.length >= need_count) {
return []
} else {
return are_missing
}
},
method: function (obj, method, args) {
return obj[method].apply(obj, args)
}
}