utils.js
4.2 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
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
var isArray;
/**
* Strip leading and trailing whitespace from a string.
* @param {string} input
* @return {string} Stripped input.
*/
exports.strip = function (input) {
return input.replace(/^\s+|\s+$/g, '');
};
/**
* Test if a string starts with a given prefix.
* @param {string} str String to test against.
* @param {string} prefix Prefix to check for.
* @return {boolean}
*/
exports.startsWith = function (str, prefix) {
return str.indexOf(prefix) === 0;
};
/**
* Test if a string ends with a given suffix.
* @param {string} str String to test against.
* @param {string} suffix Suffix to check for.
* @return {boolean}
*/
exports.endsWith = function (str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
};
/**
* Iterate over an array or object.
* @param {array|object} obj Enumerable object.
* @param {Function} fn Callback function executed for each item.
* @return {array|object} The original input object.
*/
exports.each = function (obj, fn) {
var i, l;
if (isArray(obj)) {
i = 0;
l = obj.length;
for (i; i < l; i += 1) {
if (fn(obj[i], i, obj) === false) {
break;
}
}
} else {
for (i in obj) {
if (obj.hasOwnProperty(i)) {
if (fn(obj[i], i, obj) === false) {
break;
}
}
}
}
return obj;
};
/**
* Test if an object is an Array.
* @param {object} obj
* @return {boolean}
*/
exports.isArray = isArray = (Array.hasOwnProperty('isArray')) ? Array.isArray : function (obj) {
return (obj) ? (typeof obj === 'object' && Object.prototype.toString.call(obj).indexOf() !== -1) : false;
};
/**
* Test if an item in an enumerable matches your conditions.
* @param {array|object} obj Enumerable object.
* @param {Function} fn Executed for each item. Return true if your condition is met.
* @return {boolean}
*/
exports.some = function (obj, fn) {
var i = 0,
result,
l;
if (isArray(obj)) {
l = obj.length;
for (i; i < l; i += 1) {
result = fn(obj[i], i, obj);
if (result) {
break;
}
}
} else {
exports.each(obj, function (value, index) {
result = fn(value, index, obj);
return !(result);
});
}
return !!result;
};
/**
* Return a new enumerable, mapped by a given iteration function.
* @param {object} obj Enumerable object.
* @param {Function} fn Executed for each item. Return the item to replace the original item with.
* @return {object} New mapped object.
*/
exports.map = function (obj, fn) {
var i = 0,
result = [],
l;
if (isArray(obj)) {
l = obj.length;
for (i; i < l; i += 1) {
result[i] = fn(obj[i], i);
}
} else {
for (i in obj) {
if (obj.hasOwnProperty(i)) {
result[i] = fn(obj[i], i);
}
}
}
return result;
};
/**
* Copy all of the properties in the source objects over to the destination object, and return the destination object. It's in-order, so the last source will override properties of the same name in previous arguments.
* @param {...object} arguments
* @return {object}
*/
exports.extend = function () {
var args = arguments,
target = args[0],
objs = (args.length > 1) ? Array.prototype.slice.call(args, 1) : [],
i = 0,
l = objs.length,
key,
obj;
for (i; i < l; i += 1) {
obj = objs[i] || {};
for (key in obj) {
if (obj.hasOwnProperty(key)) {
target[key] = obj[key];
}
}
}
return target;
};
/**
* Get all of the keys on an object.
* @param {object} obj
* @return {array}
*/
exports.keys = function (obj) {
if (!obj) {
return [];
}
if (Object.keys) {
return Object.keys(obj);
}
return exports.map(obj, function (v, k) {
return k;
});
};
/**
* Throw an error with possible line number and source file.
* @param {string} message Error message
* @param {number} [line] Line number in template.
* @param {string} [file] Template file the error occured in.
* @throws {Error} No seriously, the point is to throw an error.
*/
exports.throwError = function (message, line, file) {
if (line) {
message += ' on line ' + line;
}
if (file) {
message += ' in file ' + file;
}
throw new Error(message + '.');
};