forked from mdeforall/atompm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
370 lines (291 loc) · 7.59 KB
/
utils.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* This file is part of AToMPM - A Tool for Multi-Paradigm Modelling
* Copyright 2011 by the AToMPM team and licensed under the LGPL
* See COPYING.lesser and README.md in the root of this project for full details
*/
var utils = {};
/* return a vobject geometric attribute (i.e., position, etc.) value given its
current and new values...
oldVal newVal result
aa;bbbb ccccc aa;cccc
aa ccccc aa;cccc */
utils.buildVobjGeomAttrVal =
function(oldVal,newVal)
{
if( (matches = String(oldVal).match(/^(.*?)(;.*){0,1}$/)) )
oldVal = matches[1];
return oldVal+';'+newVal;
};
utils.clone =
function(obj)
{
return (obj == undefined ? undefined : utils.jsonp(utils.jsons(obj)));
};
utils.contains =
function(arr,x)
{
return arr.indexOf(x) > -1;
};
/* cause one type to inherit properties from another */
utils.extend =
function(child,parent)
{
for( var prop in parent.prototype )
if( !(prop in child.prototype) )
child.prototype[prop] = parent.prototype[prop];
};
/* remove specified elements from the array in-place, and return array */
utils.filter =
function(arr,items)
{
for( var i=0; i<arr.length; )
if( utils.contains(items,arr[i]) )
arr.splice(i,1);
else
i++;
return arr;
};
/* flatten an array of arrays into a single array */
utils.flatten =
function(arrays)
{
return (arrays.length == 0 ?
[] :
[].concat.apply([], arrays));
};
/* return the given array's first element */
utils.head =
function(arr)
{
return arr[0];
};
utils.isArray =
function(obj)
{
return Object.prototype.toString.call(obj) == '[object Array]';
};
utils.isObject =
function(obj)
{
return Object.prototype.toString.call(obj) == '[object Object]';
};
/* increment the numeric part of a sequence# of the form 'src#number' */
utils.incrementSequenceNumber =
function(sn,inc)
{
var matches = sn.match(/(.*)#(\d*)/);
return matches[1]+'#'+(parseInt(matches[2])+(inc == undefined ? 1 : inc));
};
utils.isHttpSuccessCode =
function(statusCode)
{
return Math.floor(statusCode/100.0) == 2;
};
/* decode/encode a json string... in short, replace marked line breaks ('\\n')
by placeholders so the source json string can be parsed... this enables
multiline json values */
utils.jsond =
function(str,rep)
{
if( rep == undefined )
rep = '\\\n';
return str.replace(/\/\*newline\*\//g,rep);
};
utils.jsone =
function(str)
{
return str.replace(/\\\n/g,'/*newline*/');
};
/* shortcuts for JSON.* functions */
utils.jsonp =
function(str)
{
return JSON.parse(str);
};
utils.jsons =
function(obj,replacer,space)
{
return JSON.stringify(obj,replacer,space);
};
/* return an array containing all the keys of a hash */
utils.keys =
function(hash)
{
var keys = [];
for( var k in hash )
keys.push(k);
return keys;
};
/* return the maximal value in an array given a measurement function */
utils.max =
function(arr,measure)
{
var max = -Infinity;
arr.forEach(
function(_)
{
var meas = measure(_);
if( meas > max )
max = meas;
});
return max;
};
/* merge an array of dictionaries into a single dictionary... in case of key
clashes, the value in the furthest dictionary is taken */
utils.mergeDicts =
function(dicts)
{
if( dicts.length == 0 )
return {};
var merged = {};
dicts.forEach(
function(d)
{
for( var key in d )
merged[key] = d[key];
});
return merged;
};
/* escapes regexp special characters from a string (so the string can be used as
data within a regexp) */
utils.regexpe =
function(str)
{
return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};
/* return a dicts keys sorted by value according to the given function */
utils.sortDict =
function(dict,sortf)
{
var tuples = [];
for(var key in dict)
tuples.push([key, dict[key]]);
tuples.sort( function(a,b) {return sortf(a[1],b[1]);} );
return tuples.map( function(t) {return t[0];} );
};
/* remove specified keys from given dictionary (in-place) and return dictionary
of removed entries */
utils.splitDict =
function(dict,keys)
{
var other = {};
keys.forEach(
function(k)
{
if( k in dict )
{
other[k] = dict[k];
delete dict[k];
}
});
return other;
};
/* returns the numeric part of a sequence# of the form 'src#number' */
utils.sn2int =
function(sn)
{
return parseInt(sn.match(/.*#(\d*)/)[1]);
};
/* return the given array's last element */
utils.tail =
function(arr)
{
return arr[arr.length-1];
};
/* transform the given array into a set (i.e., remove duplicate elements) */
utils.toSet =
function(arr)
{
var set = [];
arr.forEach(
function(_)
{
if( ! utils.contains(set,_) )
set.push(_);
});
return set;
};
/* return an array containing all the values of a hash */
utils.values =
function(hash)
{
var values = [];
for( var k in hash )
values.push(hash[k]);
return values;
};
/* creates a cookie with given name and value, which expires after the given
amount of days (undefined == expire when browser closes) */
utils.createCookie =
function(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
};
/* returns the value of the cookie with given name */
utils.readCookie =
function(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
};
/* erases the cookie with given name */
utils.eraseCookie =
function(name) {
createCookie(name,"",-1);
};
__pendingCalls = {};
/* performs a function after the specified amount of milleseconds
unless this call is repeated during that time interval. If it is,
the timer is reset. 'args' is an array containing arguments for the
function call. */
utils.doAfterUnlessRepeated =
function(func, args, ms) {
function doIt() {
func.apply(undefined, args);
}
if (__pendingCalls[func]) {
clearTimeout(__pendingCalls[func]);
}
__pendingCalls[func] = setTimeout(doIt, ms);
};
/* NOTE: 'exports' exists in back-end 'require', but not in browser import...
this ensures no errors are reported during browser imports */
var exports = exports || {};
exports.buildVobjGeomAttrVal = utils.buildVobjGeomAttrVal;
exports.clone = utils.clone;
exports.contains = utils.contains;
exports.extend = utils.extend;
exports.filter = utils.filter;
exports.flatten = utils.flatten;
exports.head = utils.head;
exports.isArray = utils.isArray;
exports.isObject = utils.isObject;
exports.incrementSequenceNumber = utils.incrementSequenceNumber;
exports.isHttpSuccessCode = utils.isHttpSuccessCode;
exports.jsond = utils.jsond;
exports.jsone = utils.jsone;
exports.jsonp = utils.jsonp;
exports.jsons = utils.jsons;
exports.keys = utils.keys;
exports.max = utils.max;
exports.mergeDicts = utils.mergeDicts;
exports.regexpe = utils.regexpe;
exports.sortDict = utils.sortDict;
exports.splitDict = utils.splitDict;
exports.sn2int = utils.sn2int;
exports.tail = utils.tail;
exports.toSet = utils.toSet;
exports.values = utils.values;
exports.createCookie = utils.createCookie;
exports.readCookie = utils.readCookie;
exports.eraseCookie = utils.eraseCookie;
exports.doAfterUnlessRepeated = utils.doAfterUnlessRepeated;