-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path33.js
More file actions
272 lines (234 loc) · 6.83 KB
/
Copy path33.js
File metadata and controls
272 lines (234 loc) · 6.83 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
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
var toString = Object.prototype.toString,
arrayPrototype = Array.prototype,
slice = arrayPrototype.slice,
utils = exports,
_compat = require('./_compat'),
forEach = arrayPrototype.forEach || _compat.forEach;
var marked = '__marked';
// dummy subclass
/** @ignore */
function inheritance() {}
/**
* Sets or retrieves property on an object.
*
* If target has function with prop it will be called target[prop](value=)
* If no function present property will be set/get directly:
* target[prop] = value or return target[prop]</p>
*
* @example
* utils.prop(view, 'name', 'funny') // sets name to funny on view
* utils.prop(view, 'id') // gets id property of view
*
* @param {object} target
* @param {string} prop Attribute name
* @param {object=} value Value to set
* @returns {object} target if value is being set, retrieved value otherwise
*/
utils.prop = function(obj, prop, value, extra) {
if (arguments.length > 2) {
if (obj[prop] && obj[prop].apply) {
obj[prop](value, extra);
} else {
obj[prop] = value;
}
return obj;
} else {
if (obj[prop] && obj[prop].apply) {
return obj[prop]();
} else {
return obj[prop];
}
}
};
/**
* Checks if obj is a function
*
* @param {object} object Object to check
* @returns {boolean}
*/
utils.isFunction = function(obj) {
return toString.call(obj) === "[object Function]";
};
/**
* Checks if obj is an Array
*
* @param {object} object Object to check
* @returns {boolean}
*/
utils.isArray = function(obj) {
return toString.call(obj) === "[object Array]";
};
utils.toArray = function(array) {
return slice.call(array, 0);
};
utils.pluck = function(array, prop) {
function reader(v) {
return utils.prop(v, prop);
};
return array.map ?
array.map(reader) :
utils.map(array, reader);
};
utils.without = function(array, value) {
function filter(v) {
return v !== value;
};
return array.filter ?
array.filter(filter) :
utils.filter(array, filter);
};
utils.invoke = function(array, method) {
var args = slice.call(arguments, 2),
invoke = method ?
function(item) {
return item[method].apply(item, args);
} :
function(item) {
return item.apply(null, args);
};
return array.forEach ?
array.forEach(invoke) :
utils.forEach(array, invoke);
};
/**
* Iterates through all non empty values of object or an Array
*
* @param {object|Array} object Object to iterate through
* @param {function(object, number):boolean} callback Called for every item,
* may return false to stop iteration
* @param {object} context Context in which callback should called.
* If not specified context will be set to current item
* @returns {object}
*/
utils.forEach = function(object, callback, context) {
var name, i = 0, length = object.length;
if (length === undefined) {
for (name in object) {
if (!name || object[name] === undefined ||
!object.hasOwnProperty(name)) {
continue;
}
if (callback.call(context || object[name],
object[name], name) === false) { break; }
}
} else {
forEach.call(object, callback, context);
}
return object;
};
/**
* Returns unique elements in array
*
* @param {Array} array
* @returns {Array}
*/
utils.unique = function(array) {
var result = [],
i, length;
if (array.length && (typeof array[0] == 'object' ||
typeof array[0] == 'function')) {
for (i = 0; i < array.length; i++) {
if (!array[i][marked]) { result[result.length] = array[i]; }
array[i][marked] = true;
}
for (i = 0; i < result.length; i++) {
delete result[i][marked];
}
return result;
} else {
var done = {};
for (i = 0, length = array.length; i < length; i++) {
var id = array[i];
if (!done[id]) {
done[id] = true;
result.push(array[i]);
}
}
return result;
}
};
/**
* Copies properties from one object to another
* @example
* utils.extend(x, { width: 13, height: 14 }) // sets x.width = 13,
* // x.height = 14
* options = utils.extend({}, defaultOptions, options)
*
* @param {object} target Object to copy properties into
* @param {...object} sources Objects to take properties from
* @returns Describe what it returns
*/
utils.extend = function() {
var target = arguments[0] || {}, i = 1, length = arguments.length, options;
for (; i < length; i++) {
if ((options = arguments[i]) != null) {
for (var name in options) {
if (!options.hasOwnProperty(name)) {
continue;
}
var copy = options[name];
if (copy !== undefined) {
target[name] = copy;
}
}
}
}
return target;
};
/**
* Search closest value in a sorted array
* @param {nubmer} value to search
* @param {array} array sorted array
* @returns {number} index of closest value
*/
utils.binarySearch = function(array, value, low, high) {
low = low === undefined ? 0 : low;
high = high === undefined ? array.length : high;
var mid;
while (low < high) {
mid = (low + high) >> 1;
array[mid] < value ? low = mid + 1 : high = mid;
}
return low;
};
utils.camelize = function(string) {
return string.replace(/[_-]\S/g, function(v) {
return v.substr(1).toUpperCase();
});
};
utils.dasherize = function(string) {
return string.replace(/[A-Z]/g, function(v) {
return '-' + v.toLowerCase();
});
};
utils.path2obj = function(path, context) {
var parts = path.split('.');
context = context || global;
for (var i = 0, l = parts.length; context && i < l; i++) {
context = context[parts[i]];
}
return context;
};
utils.range = function(from, to) {
var result = new Array(to - from);
for (var idx = 0; from <= to; from++, idx++) {
result[idx] = from;
}
return result;
};
utils.applyCompat = _compat.applyCompat;
utils.forEach(_compat.arrayFunctions, function(name) {
if (!utils[name]) {
// using temp argument is faster than slicing
// arguments object
var method = Array.prototype[name] || _compat[name];
utils[name] = function(array, a, b) {
return method.call(array, a, b);
};
}
});
utils.keys = Object.keys || _compat.keys;
var trim = String.prototype.trim || _compat.trim;
utils.trim = function(s) {
return trim.call(s);
};