forked from Jstarfish/JavaKeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
131 lines (111 loc) · 3.32 KB
/
utils.js
File metadata and controls
131 lines (111 loc) · 3.32 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
'use strict';
var DOM = require('./dom.js');
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
}
module.exports = {
// those methods are implemented differently
// depending on which build it is, using
// $... or angular... or Zepto... or require(...)
isArray: null,
isFunction: null,
isObject: null,
bind: null,
each: null,
map: null,
mixin: null,
isMsie: function(agentString) {
if (agentString === undefined) { agentString = navigator.userAgent; }
// from https://github.com/ded/bowser/blob/master/bowser.js
if ((/(msie|trident)/i).test(agentString)) {
var match = agentString.match(/(msie |rv:)(\d+(.\d+)?)/i);
if (match) { return match[2]; }
}
return false;
},
// http://stackoverflow.com/a/6969486
escapeRegExChars: function(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
},
isNumber: function(obj) { return typeof obj === 'number'; },
toStr: function toStr(s) {
return s === undefined || s === null ? '' : s + '';
},
cloneDeep: function cloneDeep(obj) {
var clone = this.mixin({}, obj);
var self = this;
this.each(clone, function(value, key) {
if (value) {
if (self.isArray(value)) {
clone[key] = [].concat(value);
} else if (self.isObject(value)) {
clone[key] = self.cloneDeep(value);
}
}
});
return clone;
},
error: function(msg) {
throw new Error(msg);
},
every: function(obj, test) {
var result = true;
if (!obj) {
return result;
}
this.each(obj, function(val, key) {
if (result) {
result = test.call(null, val, key, obj) && result;
}
});
return !!result;
},
any: function(obj, test) {
var found = false;
if (!obj) {
return found;
}
this.each(obj, function(val, key) {
if (test.call(null, val, key, obj)) {
found = true;
return false;
}
});
return found;
},
getUniqueId: (function() {
var counter = 0;
return function() { return counter++; };
})(),
templatify: function templatify(obj) {
if (this.isFunction(obj)) {
return obj;
}
var $template = DOM.element(obj);
if ($template.prop('tagName') === 'SCRIPT') {
return function template() { return $template.text(); };
}
return function template() { return String(obj); };
},
defer: function(fn) { setTimeout(fn, 0); },
noop: function() {},
formatPrefix: function(prefix, noPrefix) {
return noPrefix ? '' : prefix + '-';
},
className: function(prefix, clazz, skipDot) {
return (skipDot ? '' : '.') + prefix + clazz;
},
escapeHighlightedString: function(str, highlightPreTag, highlightPostTag) {
highlightPreTag = highlightPreTag || '<em>';
var pre = document.createElement('div');
pre.appendChild(document.createTextNode(highlightPreTag));
highlightPostTag = highlightPostTag || '</em>';
var post = document.createElement('div');
post.appendChild(document.createTextNode(highlightPostTag));
var div = document.createElement('div');
div.appendChild(document.createTextNode(str));
return div.innerHTML
.replace(RegExp(escapeRegExp(pre.innerHTML), 'g'), highlightPreTag)
.replace(RegExp(escapeRegExp(post.innerHTML), 'g'), highlightPostTag);
}
};