forked from eric-wieser/caius-meal-booking-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
76 lines (65 loc) · 1.86 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
// General purpose utilities
Date.extend({
toISODateString: function() { return this.toISOString().substring(0, 10); },
toLocalISODateString: function() { return this.toLocalISOString().substring(0, 10); },
toLocalISOString: function() {
function pad(n) { return n < 10 ? '0' + n : n }
var localIsoString = this.getFullYear() + '-'
+ pad(this.getMonth() + 1) + '-'
+ pad(this.getDate()) + 'T'
+ pad(this.getHours()) + ':'
+ pad(this.getMinutes()) + ':'
+ pad(this.getSeconds());
if(this.getTimezoneOffset() == 0) localIsoString += 'Z';
return localIsoString;
}
});
Array.extend({
// Behaves like itertools.groupby, maintaining sort order
orderedGroupBy: function(map) {
var keys = this.map(map);
var group;
var lastKey = function sentinel(){};
var overall = [];
this.each(function(el, index) {
var key = keys[index];
// no previous data for this key
if(!Object.equal(key, lastKey)) {
group = [];
overall.push([key, group]);
}
group.push(el);
lastKey = key;
});
return overall;
},
starMap: function(fn, context) {
return this.map(function(el) {
return fn.apply(context, el);
});
}
});
// jQuery configuration
$.ajaxPrefilter(function(options, originalOptions) {
if(!originalOptions.dataFilter) {
// encode root-level tags in a way that they can be interpreted
options.dataFilter = function(htmlString) {
return htmlString.replace(/<(\/?)(html|head|body|img)/g, '<$1ajax:$2')
};
}
});
$.whenAll = function(dfds) {
return this.when.apply(this, dfds).then(function() {
return [].slice.call(arguments);
}).promise();
};
$.defer = function(f) {
var d = $.Deferred();
var args = [].slice.call(arguments, 1);
args.push(d.resolve);
f.apply(null, args);
return d;
};
Object.method = function(o, name) {
return o[name].bind(o);
}