-
Notifications
You must be signed in to change notification settings - Fork 110
/
helpers_js
290 lines (246 loc) · 6.32 KB
/
helpers_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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
* JavaScript Helpers
*
* Author: Rodrigo E. Principe
* email: fitoprincipe82@gmail.com
* license: MIT
**/
var PixelSize = function(number) {
if (object.is(number, 'Number')) {
this.number = Number(number)
} else {
this.number = Number(number.replace('px', ''))
}
this.value = function() {
return this.number.toString()+'px'
}
this.add = function(another) {
var other = another.number || another
return new PixelSize(this.number + other)
}
this.multiply = function(another) {
var other = another.number || another
return new PixelSize(this.number * other)
}
this.divide = function(another) {
var other = another.number || another
return new PixelSize(this.number / other)
}
this.subtract = function(another) {
var other = another.number || another
return new PixelSize(this.number - other)
}
}
exports.PixelSize = PixelSize
// UUID4
// from https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript#answer-21963136
var uuid4 = function() {
var lut = []; for (var i=0; i<256; i++) { lut[i] = (i<16?'0':'')+(i).toString(16); }
var d0 = Math.random()*0x100000000|0;
var d1 = Math.random()*0x100000000|0;
var d2 = Math.random()*0x100000000|0;
var d3 = Math.random()*0x100000000|0;
return lut[d0&0xff]+lut[d0>>8&0xff]+lut[d0>>16&0xff]+lut[d0>>24&0xff]+'-'+
lut[d1&0xff]+lut[d1>>8&0xff]+'-'+lut[d1>>16&0x0f|0x40]+lut[d1>>24&0xff]+'-'+
lut[d2&0x3f|0x80]+lut[d2>>8&0xff]+'-'+lut[d2>>16&0xff]+lut[d2>>24&0xff]+
lut[d3&0xff]+lut[d3>>8&0xff]+lut[d3>>16&0xff]+lut[d3>>24&0xff];
}
exports.uuid4 = uuid4
// HASH
// from https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript
var hash = function(str) {
var h = 0, i, chr;
if (str.length === 0) return h;
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
h = ((h << 5) - h) + chr;
h |= 0; // Convert to 32bit integer
}
return h;
}
exports.hash = hash
// Catch Widget Object Type
var isWidget = function(obj) {
try {
var o = obj.Ab().split('.')[0]
if (o === 'ui') {
n = true
}
else {
n = false
}
} catch(err) {
var n = false
}
return n
}
exports.isWidget = isWidget
// KEEP IN CASE BROKEN APPS
var get_options_old = function(def, options) {
// fill options dict with default values if not present
if (options !== undefined) {
var opt = options
for (var key in def) {
var value = def[key]
if (opt[key] === undefined) {opt[key] = value}
}
} else {var opt = def}
return opt
}
exports.get_options_simple = get_options_old
// Get Options
var get_options = function(defaults, options) {
// fill options dict with default values if not present
if (options !== undefined) {
var opt = options
for (var key in defaults) {
var def_value = defaults[key]
var opt_value = opt[key]
var def_type = object.getType(def_value)
var opt_type = object.getType(opt_value)
if (def_type === 'Object' && opt_type === 'Object' && !isWidget(def_value)) {
def_value = get_options(def_value, opt_value)
}
if (opt[key] === undefined) {
opt[key] = def_value
}
}
} else {var opt = defaults}
return opt
}
exports.get_options = get_options
//
// Objects
var object = {}
object.getType = function(obj) {
return Object.prototype.toString.call(obj).slice(8, -1);
}
object.is = function (obj, type) {
var clas = object.getType(obj)
return obj !== undefined && obj !== null && clas === type;
}
object.keys = function(obj) {
return Object.keys(obj)
}
object.values = function(obj) {
var v = []
for (var key in obj) {
var value = obj[key]
v.push(value)
}
return v
}
object.remove = function(obj, key) {
var newobj = {}
for (var k in obj) {
var val = obj[k]
if (k !== key) {
newobj[k] = val
}
}
return newobj
}
object.insert = function(obj, key, value) {
var newobj = {}
for (var k in obj) {
newobj[k] = obj[k]
}
newobj[key] = value
return newobj
}
exports.object = object
// Arrays
var array = {}
array.insert = function(array, index, element) {
var firstPart = array.slice(0, index)
var lastPart = array.slice(index, array.length)
firstPart.push(element)
for (var i in lastPart) {
var rest = lastPart[i]
firstPart.push(rest)
}
return firstPart
}
array.toString = function(array) {
var str = array.toString()
return '['+str+']'
}
array.countArraysInside = function(array) {
// Count the number of arrays inside another array
var arrays = 0
for (var i in array) {
var element = array[i]
if (object.is(element, 'Array')) {
arrays += 1
}
}
return arrays
}
array.contains = function(array, element) {
var r = false
array.map(function(el){
if (element === el) {r = true}
})
return r
}
var writeableArray = function(array) {
// Make an array writeable
var ini = []
for (var i in array) {ini.push(0)}
for (var i in array) {
var e = array[i]
if (object.is(e, 'Array')) {
ini[i] = writeableArray(e)
} else {
ini[i] = e
}
}
return ini
}
array.writeableArray = writeableArray
var arrayToString = function(arr) {
// make sure it is writeable
arr = array.writeableArray(arr)
for (var i in arr) {
var element = arr[i]
if (object.is(element, 'Array')) {
var n = array.countArraysInside(element)
if (n === 0) {
var str = element.toString()
str = '['+str+']'
arr[i] = str
} else {
arr[i] = arrayToString(element)
}
}
}
var str = arr.toString()
str = '['+str+']'
return str
}
array.toString = arrayToString
exports.array = array
// Strings
var string = {}
string.format = function(str, arr) {
if (typeof(arr) === 'array') {
return str.replace(/{(\d+)}/g, function (match, number) {
return typeof arr[number] != 'undefined' ? arr[number] : match;
})}
else if (typeof(arr) === 'object') {
return str.replace(/{(\w+)}/g, function (match, str) {
return typeof arr[str] != 'undefined' ? arr[str] : match;
});
};
}
string._str = function(str) {
return str.replace(/(\s)/g, function(match, result) {
return '_'
})
}
string.formatTask = function(str) {
return str.replace(/(\/)/g, function(match, result) {
return '_'
})
}
exports.string = string