talkie 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +4 -0
- data/README.md +25 -0
- data/app/assets/javascripts/talkie/application.js +6 -0
- data/app/assets/javascripts/talkie/talkie.mentions.js +25 -0
- data/app/assets/stylesheets/talkie/_form.scss +7 -1
- data/app/assets/stylesheets/talkie/_variables.scss +6 -1
- data/app/assets/stylesheets/talkie/application.scss +2 -0
- data/app/blueprints/mentionees_blueprint.rb +15 -0
- data/app/controllers/talkie/comments_controller.rb +3 -1
- data/app/controllers/talkie/mentions_controller.rb +24 -0
- data/app/mailers/talkie/notifications_mailer.rb +15 -0
- data/app/models/concerns/talkie/mentionable.rb +55 -0
- data/app/models/talkie/comment.rb +6 -0
- data/app/models/talkie/subscription.rb +11 -0
- data/app/views/talkie/comments/_comment.html.erb +7 -3
- data/app/views/talkie/comments/_form.html.erb +6 -2
- data/app/views/talkie/notifications_mailer/mentioned.html.erb +15 -0
- data/app/views/talkie/notifications_mailer/mentioned.text.erb +5 -0
- data/config/locales/en.yml +8 -0
- data/config/locales/es.yml +8 -0
- data/config/routes.rb +4 -0
- data/lib/generators/talkie/templates/create_talkie_comments.rb +14 -0
- data/lib/generators/talkie/templates/talkie.rb +28 -1
- data/lib/generators/talkie/views_generator.rb +15 -0
- data/lib/talkie/acts_as_talker.rb +7 -0
- data/lib/talkie/blueprinter.rb +5 -0
- data/lib/talkie/nil_mention_tokens.rb +18 -0
- data/lib/talkie/permission.rb +3 -3
- data/lib/talkie/subscription_error.rb +6 -0
- data/lib/talkie/version.rb +1 -1
- data/lib/talkie.rb +28 -0
- data/talkie.gemspec +2 -0
- data/vendor/assets/javascripts/jquery.elastic.js +151 -0
- data/vendor/assets/javascripts/jquery.mentionsinput.js +543 -0
- data/vendor/assets/javascripts/underscore.js +1590 -0
- data/vendor/assets/stylesheets/jquery.mentionsinput.css +112 -0
- metadata +46 -2
@@ -0,0 +1,1590 @@
|
|
1
|
+
// Underscore.js 1.8.3
|
2
|
+
// http://underscorejs.org
|
3
|
+
// (c) 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
|
4
|
+
// Underscore may be freely distributed under the MIT license.
|
5
|
+
|
6
|
+
(function() {
|
7
|
+
|
8
|
+
// Baseline setup
|
9
|
+
// --------------
|
10
|
+
|
11
|
+
// Establish the root object, `window` (`self`) in the browser, or `global` on the server.
|
12
|
+
// We use `self` instead of `window` for `WebWorker` support.
|
13
|
+
var root = typeof self === 'object' && self.self === self && self ||
|
14
|
+
typeof global === 'object' && global.global === global && global;
|
15
|
+
|
16
|
+
// Save the previous value of the `_` variable.
|
17
|
+
var previousUnderscore = root._;
|
18
|
+
|
19
|
+
// Save bytes in the minified (but not gzipped) version:
|
20
|
+
var ArrayProto = Array.prototype, ObjProto = Object.prototype;
|
21
|
+
|
22
|
+
// Create quick reference variables for speed access to core prototypes.
|
23
|
+
var
|
24
|
+
push = ArrayProto.push,
|
25
|
+
slice = ArrayProto.slice,
|
26
|
+
toString = ObjProto.toString,
|
27
|
+
hasOwnProperty = ObjProto.hasOwnProperty;
|
28
|
+
|
29
|
+
// All **ECMAScript 5** native function implementations that we hope to use
|
30
|
+
// are declared here.
|
31
|
+
var
|
32
|
+
nativeIsArray = Array.isArray,
|
33
|
+
nativeKeys = Object.keys,
|
34
|
+
nativeCreate = Object.create;
|
35
|
+
|
36
|
+
// Naked function reference for surrogate-prototype-swapping.
|
37
|
+
var Ctor = function(){};
|
38
|
+
|
39
|
+
// Create a safe reference to the Underscore object for use below.
|
40
|
+
var _ = function(obj) {
|
41
|
+
if (obj instanceof _) return obj;
|
42
|
+
if (!(this instanceof _)) return new _(obj);
|
43
|
+
this._wrapped = obj;
|
44
|
+
};
|
45
|
+
|
46
|
+
// Export the Underscore object for **Node.js**, with
|
47
|
+
// backwards-compatibility for their old module API. If we're in
|
48
|
+
// the browser, add `_` as a global object.
|
49
|
+
if (typeof exports !== 'undefined') {
|
50
|
+
if (typeof module !== 'undefined' && module.exports) {
|
51
|
+
exports = module.exports = _;
|
52
|
+
}
|
53
|
+
exports._ = _;
|
54
|
+
} else {
|
55
|
+
root._ = _;
|
56
|
+
}
|
57
|
+
|
58
|
+
// Current version.
|
59
|
+
_.VERSION = '1.8.3';
|
60
|
+
|
61
|
+
// Internal function that returns an efficient (for current engines) version
|
62
|
+
// of the passed-in callback, to be repeatedly applied in other Underscore
|
63
|
+
// functions.
|
64
|
+
var optimizeCb = function(func, context, argCount) {
|
65
|
+
if (context === void 0) return func;
|
66
|
+
switch (argCount == null ? 3 : argCount) {
|
67
|
+
case 1: return function(value) {
|
68
|
+
return func.call(context, value);
|
69
|
+
};
|
70
|
+
case 2: return function(value, other) {
|
71
|
+
return func.call(context, value, other);
|
72
|
+
};
|
73
|
+
case 3: return function(value, index, collection) {
|
74
|
+
return func.call(context, value, index, collection);
|
75
|
+
};
|
76
|
+
case 4: return function(accumulator, value, index, collection) {
|
77
|
+
return func.call(context, accumulator, value, index, collection);
|
78
|
+
};
|
79
|
+
}
|
80
|
+
return function() {
|
81
|
+
return func.apply(context, arguments);
|
82
|
+
};
|
83
|
+
};
|
84
|
+
|
85
|
+
// A mostly-internal function to generate callbacks that can be applied
|
86
|
+
// to each element in a collection, returning the desired result — either
|
87
|
+
// identity, an arbitrary callback, a property matcher, or a property accessor.
|
88
|
+
var cb = function(value, context, argCount) {
|
89
|
+
if (value == null) return _.identity;
|
90
|
+
if (_.isFunction(value)) return optimizeCb(value, context, argCount);
|
91
|
+
if (_.isObject(value)) return _.matcher(value);
|
92
|
+
return _.property(value);
|
93
|
+
};
|
94
|
+
_.iteratee = function(value, context) {
|
95
|
+
return cb(value, context, Infinity);
|
96
|
+
};
|
97
|
+
|
98
|
+
// Similar to ES6's rest param (http://ariya.ofilabs.com/2013/03/es6-and-rest-parameter.html)
|
99
|
+
// This accumulates the arguments passed into an array, after a given index.
|
100
|
+
var restArgs = function(func, startIndex) {
|
101
|
+
startIndex = startIndex == null ? func.length - 1 : +startIndex;
|
102
|
+
return function() {
|
103
|
+
var length = Math.max(arguments.length - startIndex, 0);
|
104
|
+
var rest = Array(length);
|
105
|
+
var index;
|
106
|
+
for (index = 0; index < length; index++) {
|
107
|
+
rest[index] = arguments[index + startIndex];
|
108
|
+
}
|
109
|
+
switch (startIndex) {
|
110
|
+
case 0: return func.call(this, rest);
|
111
|
+
case 1: return func.call(this, arguments[0], rest);
|
112
|
+
case 2: return func.call(this, arguments[0], arguments[1], rest);
|
113
|
+
}
|
114
|
+
var args = Array(startIndex + 1);
|
115
|
+
for (index = 0; index < startIndex; index++) {
|
116
|
+
args[index] = arguments[index];
|
117
|
+
}
|
118
|
+
args[startIndex] = rest;
|
119
|
+
return func.apply(this, args);
|
120
|
+
};
|
121
|
+
};
|
122
|
+
|
123
|
+
// An internal function for creating a new object that inherits from another.
|
124
|
+
var baseCreate = function(prototype) {
|
125
|
+
if (!_.isObject(prototype)) return {};
|
126
|
+
if (nativeCreate) return nativeCreate(prototype);
|
127
|
+
Ctor.prototype = prototype;
|
128
|
+
var result = new Ctor;
|
129
|
+
Ctor.prototype = null;
|
130
|
+
return result;
|
131
|
+
};
|
132
|
+
|
133
|
+
var property = function(key) {
|
134
|
+
return function(obj) {
|
135
|
+
return obj == null ? void 0 : obj[key];
|
136
|
+
};
|
137
|
+
};
|
138
|
+
|
139
|
+
// Helper for collection methods to determine whether a collection
|
140
|
+
// should be iterated as an array or as an object
|
141
|
+
// Related: http://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength
|
142
|
+
// Avoids a very nasty iOS 8 JIT bug on ARM-64. #2094
|
143
|
+
var MAX_ARRAY_INDEX = Math.pow(2, 53) - 1;
|
144
|
+
var getLength = property('length');
|
145
|
+
var isArrayLike = function(collection) {
|
146
|
+
var length = getLength(collection);
|
147
|
+
return typeof length == 'number' && length >= 0 && length <= MAX_ARRAY_INDEX;
|
148
|
+
};
|
149
|
+
|
150
|
+
// Collection Functions
|
151
|
+
// --------------------
|
152
|
+
|
153
|
+
// The cornerstone, an `each` implementation, aka `forEach`.
|
154
|
+
// Handles raw objects in addition to array-likes. Treats all
|
155
|
+
// sparse array-likes as if they were dense.
|
156
|
+
_.each = _.forEach = function(obj, iteratee, context) {
|
157
|
+
iteratee = optimizeCb(iteratee, context);
|
158
|
+
var i, length;
|
159
|
+
if (isArrayLike(obj)) {
|
160
|
+
for (i = 0, length = obj.length; i < length; i++) {
|
161
|
+
iteratee(obj[i], i, obj);
|
162
|
+
}
|
163
|
+
} else {
|
164
|
+
var keys = _.keys(obj);
|
165
|
+
for (i = 0, length = keys.length; i < length; i++) {
|
166
|
+
iteratee(obj[keys[i]], keys[i], obj);
|
167
|
+
}
|
168
|
+
}
|
169
|
+
return obj;
|
170
|
+
};
|
171
|
+
|
172
|
+
// Return the results of applying the iteratee to each element.
|
173
|
+
_.map = _.collect = function(obj, iteratee, context) {
|
174
|
+
iteratee = cb(iteratee, context);
|
175
|
+
var keys = !isArrayLike(obj) && _.keys(obj),
|
176
|
+
length = (keys || obj).length,
|
177
|
+
results = Array(length);
|
178
|
+
for (var index = 0; index < length; index++) {
|
179
|
+
var currentKey = keys ? keys[index] : index;
|
180
|
+
results[index] = iteratee(obj[currentKey], currentKey, obj);
|
181
|
+
}
|
182
|
+
return results;
|
183
|
+
};
|
184
|
+
|
185
|
+
// Create a reducing function iterating left or right.
|
186
|
+
var createReduce = function(dir) {
|
187
|
+
// Optimized iterator function as using arguments.length
|
188
|
+
// in the main function will deoptimize the, see #1991.
|
189
|
+
var reducer = function(obj, iteratee, memo, initial) {
|
190
|
+
var keys = !isArrayLike(obj) && _.keys(obj),
|
191
|
+
length = (keys || obj).length,
|
192
|
+
index = dir > 0 ? 0 : length - 1;
|
193
|
+
if (!initial) {
|
194
|
+
memo = obj[keys ? keys[index] : index];
|
195
|
+
index += dir;
|
196
|
+
}
|
197
|
+
for (; index >= 0 && index < length; index += dir) {
|
198
|
+
var currentKey = keys ? keys[index] : index;
|
199
|
+
memo = iteratee(memo, obj[currentKey], currentKey, obj);
|
200
|
+
}
|
201
|
+
return memo;
|
202
|
+
};
|
203
|
+
|
204
|
+
return function(obj, iteratee, memo, context) {
|
205
|
+
var initial = arguments.length >= 3;
|
206
|
+
return reducer(obj, optimizeCb(iteratee, context, 4), memo, initial);
|
207
|
+
};
|
208
|
+
};
|
209
|
+
|
210
|
+
// **Reduce** builds up a single result from a list of values, aka `inject`,
|
211
|
+
// or `foldl`.
|
212
|
+
_.reduce = _.foldl = _.inject = createReduce(1);
|
213
|
+
|
214
|
+
// The right-associative version of reduce, also known as `foldr`.
|
215
|
+
_.reduceRight = _.foldr = createReduce(-1);
|
216
|
+
|
217
|
+
// Return the first value which passes a truth test. Aliased as `detect`.
|
218
|
+
_.find = _.detect = function(obj, predicate, context) {
|
219
|
+
var key;
|
220
|
+
if (isArrayLike(obj)) {
|
221
|
+
key = _.findIndex(obj, predicate, context);
|
222
|
+
} else {
|
223
|
+
key = _.findKey(obj, predicate, context);
|
224
|
+
}
|
225
|
+
if (key !== void 0 && key !== -1) return obj[key];
|
226
|
+
};
|
227
|
+
|
228
|
+
// Return all the elements that pass a truth test.
|
229
|
+
// Aliased as `select`.
|
230
|
+
_.filter = _.select = function(obj, predicate, context) {
|
231
|
+
var results = [];
|
232
|
+
predicate = cb(predicate, context);
|
233
|
+
_.each(obj, function(value, index, list) {
|
234
|
+
if (predicate(value, index, list)) results.push(value);
|
235
|
+
});
|
236
|
+
return results;
|
237
|
+
};
|
238
|
+
|
239
|
+
// Return all the elements for which a truth test fails.
|
240
|
+
_.reject = function(obj, predicate, context) {
|
241
|
+
return _.filter(obj, _.negate(cb(predicate)), context);
|
242
|
+
};
|
243
|
+
|
244
|
+
// Determine whether all of the elements match a truth test.
|
245
|
+
// Aliased as `all`.
|
246
|
+
_.every = _.all = function(obj, predicate, context) {
|
247
|
+
predicate = cb(predicate, context);
|
248
|
+
var keys = !isArrayLike(obj) && _.keys(obj),
|
249
|
+
length = (keys || obj).length;
|
250
|
+
for (var index = 0; index < length; index++) {
|
251
|
+
var currentKey = keys ? keys[index] : index;
|
252
|
+
if (!predicate(obj[currentKey], currentKey, obj)) return false;
|
253
|
+
}
|
254
|
+
return true;
|
255
|
+
};
|
256
|
+
|
257
|
+
// Determine if at least one element in the object matches a truth test.
|
258
|
+
// Aliased as `any`.
|
259
|
+
_.some = _.any = function(obj, predicate, context) {
|
260
|
+
predicate = cb(predicate, context);
|
261
|
+
var keys = !isArrayLike(obj) && _.keys(obj),
|
262
|
+
length = (keys || obj).length;
|
263
|
+
for (var index = 0; index < length; index++) {
|
264
|
+
var currentKey = keys ? keys[index] : index;
|
265
|
+
if (predicate(obj[currentKey], currentKey, obj)) return true;
|
266
|
+
}
|
267
|
+
return false;
|
268
|
+
};
|
269
|
+
|
270
|
+
// Determine if the array or object contains a given item (using `===`).
|
271
|
+
// Aliased as `includes` and `include`.
|
272
|
+
_.contains = _.includes = _.include = function(obj, item, fromIndex, guard) {
|
273
|
+
if (!isArrayLike(obj)) obj = _.values(obj);
|
274
|
+
if (typeof fromIndex != 'number' || guard) fromIndex = 0;
|
275
|
+
return _.indexOf(obj, item, fromIndex) >= 0;
|
276
|
+
};
|
277
|
+
|
278
|
+
// Invoke a method (with arguments) on every item in a collection.
|
279
|
+
_.invoke = restArgs(function(obj, method, args) {
|
280
|
+
var isFunc = _.isFunction(method);
|
281
|
+
return _.map(obj, function(value) {
|
282
|
+
var func = isFunc ? method : value[method];
|
283
|
+
return func == null ? func : func.apply(value, args);
|
284
|
+
});
|
285
|
+
});
|
286
|
+
|
287
|
+
// Convenience version of a common use case of `map`: fetching a property.
|
288
|
+
_.pluck = function(obj, key) {
|
289
|
+
return _.map(obj, _.property(key));
|
290
|
+
};
|
291
|
+
|
292
|
+
// Convenience version of a common use case of `filter`: selecting only objects
|
293
|
+
// containing specific `key:value` pairs.
|
294
|
+
_.where = function(obj, attrs) {
|
295
|
+
return _.filter(obj, _.matcher(attrs));
|
296
|
+
};
|
297
|
+
|
298
|
+
// Convenience version of a common use case of `find`: getting the first object
|
299
|
+
// containing specific `key:value` pairs.
|
300
|
+
_.findWhere = function(obj, attrs) {
|
301
|
+
return _.find(obj, _.matcher(attrs));
|
302
|
+
};
|
303
|
+
|
304
|
+
// Return the maximum element (or element-based computation).
|
305
|
+
_.max = function(obj, iteratee, context) {
|
306
|
+
var result = -Infinity, lastComputed = -Infinity,
|
307
|
+
value, computed;
|
308
|
+
if (iteratee == null && obj != null) {
|
309
|
+
obj = isArrayLike(obj) ? obj : _.values(obj);
|
310
|
+
for (var i = 0, length = obj.length; i < length; i++) {
|
311
|
+
value = obj[i];
|
312
|
+
if (value > result) {
|
313
|
+
result = value;
|
314
|
+
}
|
315
|
+
}
|
316
|
+
} else {
|
317
|
+
iteratee = cb(iteratee, context);
|
318
|
+
_.each(obj, function(v, index, list) {
|
319
|
+
computed = iteratee(v, index, list);
|
320
|
+
if (computed > lastComputed || computed === -Infinity && result === -Infinity) {
|
321
|
+
result = v;
|
322
|
+
lastComputed = computed;
|
323
|
+
}
|
324
|
+
});
|
325
|
+
}
|
326
|
+
return result;
|
327
|
+
};
|
328
|
+
|
329
|
+
// Return the minimum element (or element-based computation).
|
330
|
+
_.min = function(obj, iteratee, context) {
|
331
|
+
var result = Infinity, lastComputed = Infinity,
|
332
|
+
value, computed;
|
333
|
+
if (iteratee == null && obj != null) {
|
334
|
+
obj = isArrayLike(obj) ? obj : _.values(obj);
|
335
|
+
for (var i = 0, length = obj.length; i < length; i++) {
|
336
|
+
value = obj[i];
|
337
|
+
if (value < result) {
|
338
|
+
result = value;
|
339
|
+
}
|
340
|
+
}
|
341
|
+
} else {
|
342
|
+
iteratee = cb(iteratee, context);
|
343
|
+
_.each(obj, function(v, index, list) {
|
344
|
+
computed = iteratee(v, index, list);
|
345
|
+
if (computed < lastComputed || computed === Infinity && result === Infinity) {
|
346
|
+
result = v;
|
347
|
+
lastComputed = computed;
|
348
|
+
}
|
349
|
+
});
|
350
|
+
}
|
351
|
+
return result;
|
352
|
+
};
|
353
|
+
|
354
|
+
// Shuffle a collection.
|
355
|
+
_.shuffle = function(obj) {
|
356
|
+
return _.sample(obj, Infinity);
|
357
|
+
};
|
358
|
+
|
359
|
+
// Sample **n** random values from a collection using the modern version of the
|
360
|
+
// [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher–Yates_shuffle).
|
361
|
+
// If **n** is not specified, returns a single random element.
|
362
|
+
// The internal `guard` argument allows it to work with `map`.
|
363
|
+
_.sample = function(obj, n, guard) {
|
364
|
+
if (n == null || guard) {
|
365
|
+
if (!isArrayLike(obj)) obj = _.values(obj);
|
366
|
+
return obj[_.random(obj.length - 1)];
|
367
|
+
}
|
368
|
+
var sample = isArrayLike(obj) ? _.clone(obj) : _.values(obj);
|
369
|
+
var length = getLength(sample);
|
370
|
+
n = Math.max(Math.min(n, length), 0);
|
371
|
+
var last = length - 1;
|
372
|
+
for (var index = 0; index < n; index++) {
|
373
|
+
var rand = _.random(index, last);
|
374
|
+
var temp = sample[index];
|
375
|
+
sample[index] = sample[rand];
|
376
|
+
sample[rand] = temp;
|
377
|
+
}
|
378
|
+
return sample.slice(0, n);
|
379
|
+
};
|
380
|
+
|
381
|
+
// Sort the object's values by a criterion produced by an iteratee.
|
382
|
+
_.sortBy = function(obj, iteratee, context) {
|
383
|
+
iteratee = cb(iteratee, context);
|
384
|
+
return _.pluck(_.map(obj, function(value, index, list) {
|
385
|
+
return {
|
386
|
+
value: value,
|
387
|
+
index: index,
|
388
|
+
criteria: iteratee(value, index, list)
|
389
|
+
};
|
390
|
+
}).sort(function(left, right) {
|
391
|
+
var a = left.criteria;
|
392
|
+
var b = right.criteria;
|
393
|
+
if (a !== b) {
|
394
|
+
if (a > b || a === void 0) return 1;
|
395
|
+
if (a < b || b === void 0) return -1;
|
396
|
+
}
|
397
|
+
return left.index - right.index;
|
398
|
+
}), 'value');
|
399
|
+
};
|
400
|
+
|
401
|
+
// An internal function used for aggregate "group by" operations.
|
402
|
+
var group = function(behavior, partition) {
|
403
|
+
return function(obj, iteratee, context) {
|
404
|
+
var result = partition ? [[], []] : {};
|
405
|
+
iteratee = cb(iteratee, context);
|
406
|
+
_.each(obj, function(value, index) {
|
407
|
+
var key = iteratee(value, index, obj);
|
408
|
+
behavior(result, value, key);
|
409
|
+
});
|
410
|
+
return result;
|
411
|
+
};
|
412
|
+
};
|
413
|
+
|
414
|
+
// Groups the object's values by a criterion. Pass either a string attribute
|
415
|
+
// to group by, or a function that returns the criterion.
|
416
|
+
_.groupBy = group(function(result, value, key) {
|
417
|
+
if (_.has(result, key)) result[key].push(value); else result[key] = [value];
|
418
|
+
});
|
419
|
+
|
420
|
+
// Indexes the object's values by a criterion, similar to `groupBy`, but for
|
421
|
+
// when you know that your index values will be unique.
|
422
|
+
_.indexBy = group(function(result, value, key) {
|
423
|
+
result[key] = value;
|
424
|
+
});
|
425
|
+
|
426
|
+
// Counts instances of an object that group by a certain criterion. Pass
|
427
|
+
// either a string attribute to count by, or a function that returns the
|
428
|
+
// criterion.
|
429
|
+
_.countBy = group(function(result, value, key) {
|
430
|
+
if (_.has(result, key)) result[key]++; else result[key] = 1;
|
431
|
+
});
|
432
|
+
|
433
|
+
// Safely create a real, live array from anything iterable.
|
434
|
+
_.toArray = function(obj) {
|
435
|
+
if (!obj) return [];
|
436
|
+
if (_.isArray(obj)) return slice.call(obj);
|
437
|
+
if (isArrayLike(obj)) return _.map(obj, _.identity);
|
438
|
+
return _.values(obj);
|
439
|
+
};
|
440
|
+
|
441
|
+
// Return the number of elements in an object.
|
442
|
+
_.size = function(obj) {
|
443
|
+
if (obj == null) return 0;
|
444
|
+
return isArrayLike(obj) ? obj.length : _.keys(obj).length;
|
445
|
+
};
|
446
|
+
|
447
|
+
// Split a collection into two arrays: one whose elements all satisfy the given
|
448
|
+
// predicate, and one whose elements all do not satisfy the predicate.
|
449
|
+
_.partition = group(function(result, value, pass) {
|
450
|
+
result[pass ? 0 : 1].push(value);
|
451
|
+
}, true);
|
452
|
+
|
453
|
+
// Array Functions
|
454
|
+
// ---------------
|
455
|
+
|
456
|
+
// Get the first element of an array. Passing **n** will return the first N
|
457
|
+
// values in the array. Aliased as `head` and `take`. The **guard** check
|
458
|
+
// allows it to work with `_.map`.
|
459
|
+
_.first = _.head = _.take = function(array, n, guard) {
|
460
|
+
if (array == null) return void 0;
|
461
|
+
if (n == null || guard) return array[0];
|
462
|
+
return _.initial(array, array.length - n);
|
463
|
+
};
|
464
|
+
|
465
|
+
// Returns everything but the last entry of the array. Especially useful on
|
466
|
+
// the arguments object. Passing **n** will return all the values in
|
467
|
+
// the array, excluding the last N.
|
468
|
+
_.initial = function(array, n, guard) {
|
469
|
+
return slice.call(array, 0, Math.max(0, array.length - (n == null || guard ? 1 : n)));
|
470
|
+
};
|
471
|
+
|
472
|
+
// Get the last element of an array. Passing **n** will return the last N
|
473
|
+
// values in the array.
|
474
|
+
_.last = function(array, n, guard) {
|
475
|
+
if (array == null) return void 0;
|
476
|
+
if (n == null || guard) return array[array.length - 1];
|
477
|
+
return _.rest(array, Math.max(0, array.length - n));
|
478
|
+
};
|
479
|
+
|
480
|
+
// Returns everything but the first entry of the array. Aliased as `tail` and `drop`.
|
481
|
+
// Especially useful on the arguments object. Passing an **n** will return
|
482
|
+
// the rest N values in the array.
|
483
|
+
_.rest = _.tail = _.drop = function(array, n, guard) {
|
484
|
+
return slice.call(array, n == null || guard ? 1 : n);
|
485
|
+
};
|
486
|
+
|
487
|
+
// Trim out all falsy values from an array.
|
488
|
+
_.compact = function(array) {
|
489
|
+
return _.filter(array, _.identity);
|
490
|
+
};
|
491
|
+
|
492
|
+
// Internal implementation of a recursive `flatten` function.
|
493
|
+
var flatten = function(input, shallow, strict, output) {
|
494
|
+
output = output || [];
|
495
|
+
var idx = output.length;
|
496
|
+
for (var i = 0, length = getLength(input); i < length; i++) {
|
497
|
+
var value = input[i];
|
498
|
+
if (isArrayLike(value) && (_.isArray(value) || _.isArguments(value))) {
|
499
|
+
//flatten current level of array or arguments object
|
500
|
+
if (shallow) {
|
501
|
+
var j = 0, len = value.length;
|
502
|
+
while (j < len) output[idx++] = value[j++];
|
503
|
+
} else {
|
504
|
+
flatten(value, shallow, strict, output);
|
505
|
+
idx = output.length;
|
506
|
+
}
|
507
|
+
} else if (!strict) {
|
508
|
+
output[idx++] = value;
|
509
|
+
}
|
510
|
+
}
|
511
|
+
return output;
|
512
|
+
};
|
513
|
+
|
514
|
+
// Flatten out an array, either recursively (by default), or just one level.
|
515
|
+
_.flatten = function(array, shallow) {
|
516
|
+
return flatten(array, shallow, false);
|
517
|
+
};
|
518
|
+
|
519
|
+
// Return a version of the array that does not contain the specified value(s).
|
520
|
+
_.without = restArgs(function(array, otherArrays) {
|
521
|
+
return _.difference(array, otherArrays);
|
522
|
+
});
|
523
|
+
|
524
|
+
// Produce a duplicate-free version of the array. If the array has already
|
525
|
+
// been sorted, you have the option of using a faster algorithm.
|
526
|
+
// Aliased as `unique`.
|
527
|
+
_.uniq = _.unique = function(array, isSorted, iteratee, context) {
|
528
|
+
if (!_.isBoolean(isSorted)) {
|
529
|
+
context = iteratee;
|
530
|
+
iteratee = isSorted;
|
531
|
+
isSorted = false;
|
532
|
+
}
|
533
|
+
if (iteratee != null) iteratee = cb(iteratee, context);
|
534
|
+
var result = [];
|
535
|
+
var seen = [];
|
536
|
+
for (var i = 0, length = getLength(array); i < length; i++) {
|
537
|
+
var value = array[i],
|
538
|
+
computed = iteratee ? iteratee(value, i, array) : value;
|
539
|
+
if (isSorted) {
|
540
|
+
if (!i || seen !== computed) result.push(value);
|
541
|
+
seen = computed;
|
542
|
+
} else if (iteratee) {
|
543
|
+
if (!_.contains(seen, computed)) {
|
544
|
+
seen.push(computed);
|
545
|
+
result.push(value);
|
546
|
+
}
|
547
|
+
} else if (!_.contains(result, value)) {
|
548
|
+
result.push(value);
|
549
|
+
}
|
550
|
+
}
|
551
|
+
return result;
|
552
|
+
};
|
553
|
+
|
554
|
+
// Produce an array that contains the union: each distinct element from all of
|
555
|
+
// the passed-in arrays.
|
556
|
+
_.union = restArgs(function(arrays) {
|
557
|
+
return _.uniq(flatten(arrays, true, true));
|
558
|
+
});
|
559
|
+
|
560
|
+
// Produce an array that contains every item shared between all the
|
561
|
+
// passed-in arrays.
|
562
|
+
_.intersection = function(array) {
|
563
|
+
var result = [];
|
564
|
+
var argsLength = arguments.length;
|
565
|
+
for (var i = 0, length = getLength(array); i < length; i++) {
|
566
|
+
var item = array[i];
|
567
|
+
if (_.contains(result, item)) continue;
|
568
|
+
var j;
|
569
|
+
for (j = 1; j < argsLength; j++) {
|
570
|
+
if (!_.contains(arguments[j], item)) break;
|
571
|
+
}
|
572
|
+
if (j === argsLength) result.push(item);
|
573
|
+
}
|
574
|
+
return result;
|
575
|
+
};
|
576
|
+
|
577
|
+
// Take the difference between one array and a number of other arrays.
|
578
|
+
// Only the elements present in just the first array will remain.
|
579
|
+
_.difference = restArgs(function(array, rest) {
|
580
|
+
rest = flatten(rest, true, true);
|
581
|
+
return _.filter(array, function(value){
|
582
|
+
return !_.contains(rest, value);
|
583
|
+
});
|
584
|
+
});
|
585
|
+
|
586
|
+
// Complement of _.zip. Unzip accepts an array of arrays and groups
|
587
|
+
// each array's elements on shared indices
|
588
|
+
_.unzip = function(array) {
|
589
|
+
var length = array && _.max(array, getLength).length || 0;
|
590
|
+
var result = Array(length);
|
591
|
+
|
592
|
+
for (var index = 0; index < length; index++) {
|
593
|
+
result[index] = _.pluck(array, index);
|
594
|
+
}
|
595
|
+
return result;
|
596
|
+
};
|
597
|
+
|
598
|
+
// Zip together multiple lists into a single array -- elements that share
|
599
|
+
// an index go together.
|
600
|
+
_.zip = restArgs(_.unzip);
|
601
|
+
|
602
|
+
// Converts lists into objects. Pass either a single array of `[key, value]`
|
603
|
+
// pairs, or two parallel arrays of the same length -- one of keys, and one of
|
604
|
+
// the corresponding values.
|
605
|
+
_.object = function(list, values) {
|
606
|
+
var result = {};
|
607
|
+
for (var i = 0, length = getLength(list); i < length; i++) {
|
608
|
+
if (values) {
|
609
|
+
result[list[i]] = values[i];
|
610
|
+
} else {
|
611
|
+
result[list[i][0]] = list[i][1];
|
612
|
+
}
|
613
|
+
}
|
614
|
+
return result;
|
615
|
+
};
|
616
|
+
|
617
|
+
// Generator function to create the findIndex and findLastIndex functions
|
618
|
+
var createPredicateIndexFinder = function(dir) {
|
619
|
+
return function(array, predicate, context) {
|
620
|
+
predicate = cb(predicate, context);
|
621
|
+
var length = getLength(array);
|
622
|
+
var index = dir > 0 ? 0 : length - 1;
|
623
|
+
for (; index >= 0 && index < length; index += dir) {
|
624
|
+
if (predicate(array[index], index, array)) return index;
|
625
|
+
}
|
626
|
+
return -1;
|
627
|
+
};
|
628
|
+
};
|
629
|
+
|
630
|
+
// Returns the first index on an array-like that passes a predicate test
|
631
|
+
_.findIndex = createPredicateIndexFinder(1);
|
632
|
+
_.findLastIndex = createPredicateIndexFinder(-1);
|
633
|
+
|
634
|
+
// Use a comparator function to figure out the smallest index at which
|
635
|
+
// an object should be inserted so as to maintain order. Uses binary search.
|
636
|
+
_.sortedIndex = function(array, obj, iteratee, context) {
|
637
|
+
iteratee = cb(iteratee, context, 1);
|
638
|
+
var value = iteratee(obj);
|
639
|
+
var low = 0, high = getLength(array);
|
640
|
+
while (low < high) {
|
641
|
+
var mid = Math.floor((low + high) / 2);
|
642
|
+
if (iteratee(array[mid]) < value) low = mid + 1; else high = mid;
|
643
|
+
}
|
644
|
+
return low;
|
645
|
+
};
|
646
|
+
|
647
|
+
// Generator function to create the indexOf and lastIndexOf functions
|
648
|
+
var createIndexFinder = function(dir, predicateFind, sortedIndex) {
|
649
|
+
return function(array, item, idx) {
|
650
|
+
var i = 0, length = getLength(array);
|
651
|
+
if (typeof idx == 'number') {
|
652
|
+
if (dir > 0) {
|
653
|
+
i = idx >= 0 ? idx : Math.max(idx + length, i);
|
654
|
+
} else {
|
655
|
+
length = idx >= 0 ? Math.min(idx + 1, length) : idx + length + 1;
|
656
|
+
}
|
657
|
+
} else if (sortedIndex && idx && length) {
|
658
|
+
idx = sortedIndex(array, item);
|
659
|
+
return array[idx] === item ? idx : -1;
|
660
|
+
}
|
661
|
+
if (item !== item) {
|
662
|
+
idx = predicateFind(slice.call(array, i, length), _.isNaN);
|
663
|
+
return idx >= 0 ? idx + i : -1;
|
664
|
+
}
|
665
|
+
for (idx = dir > 0 ? i : length - 1; idx >= 0 && idx < length; idx += dir) {
|
666
|
+
if (array[idx] === item) return idx;
|
667
|
+
}
|
668
|
+
return -1;
|
669
|
+
};
|
670
|
+
};
|
671
|
+
|
672
|
+
// Return the position of the first occurrence of an item in an array,
|
673
|
+
// or -1 if the item is not included in the array.
|
674
|
+
// If the array is large and already in sort order, pass `true`
|
675
|
+
// for **isSorted** to use binary search.
|
676
|
+
_.indexOf = createIndexFinder(1, _.findIndex, _.sortedIndex);
|
677
|
+
_.lastIndexOf = createIndexFinder(-1, _.findLastIndex);
|
678
|
+
|
679
|
+
// Generate an integer Array containing an arithmetic progression. A port of
|
680
|
+
// the native Python `range()` function. See
|
681
|
+
// [the Python documentation](http://docs.python.org/library/functions.html#range).
|
682
|
+
_.range = function(start, stop, step) {
|
683
|
+
if (stop == null) {
|
684
|
+
stop = start || 0;
|
685
|
+
start = 0;
|
686
|
+
}
|
687
|
+
step = step || 1;
|
688
|
+
|
689
|
+
var length = Math.max(Math.ceil((stop - start) / step), 0);
|
690
|
+
var range = Array(length);
|
691
|
+
|
692
|
+
for (var idx = 0; idx < length; idx++, start += step) {
|
693
|
+
range[idx] = start;
|
694
|
+
}
|
695
|
+
|
696
|
+
return range;
|
697
|
+
};
|
698
|
+
|
699
|
+
// Function (ahem) Functions
|
700
|
+
// ------------------
|
701
|
+
|
702
|
+
// Determines whether to execute a function as a constructor
|
703
|
+
// or a normal function with the provided arguments
|
704
|
+
var executeBound = function(sourceFunc, boundFunc, context, callingContext, args) {
|
705
|
+
if (!(callingContext instanceof boundFunc)) return sourceFunc.apply(context, args);
|
706
|
+
var self = baseCreate(sourceFunc.prototype);
|
707
|
+
var result = sourceFunc.apply(self, args);
|
708
|
+
if (_.isObject(result)) return result;
|
709
|
+
return self;
|
710
|
+
};
|
711
|
+
|
712
|
+
// Create a function bound to a given object (assigning `this`, and arguments,
|
713
|
+
// optionally). Delegates to **ECMAScript 5**'s native `Function.bind` if
|
714
|
+
// available.
|
715
|
+
_.bind = restArgs(function(func, context, args) {
|
716
|
+
if (!_.isFunction(func)) throw new TypeError('Bind must be called on a function');
|
717
|
+
var bound = restArgs(function(callArgs) {
|
718
|
+
return executeBound(func, bound, context, this, args.concat(callArgs));
|
719
|
+
});
|
720
|
+
return bound;
|
721
|
+
});
|
722
|
+
|
723
|
+
// Partially apply a function by creating a version that has had some of its
|
724
|
+
// arguments pre-filled, without changing its dynamic `this` context. _ acts
|
725
|
+
// as a placeholder by default, allowing any combination of arguments to be
|
726
|
+
// pre-filled. Set `_.partial.placeholder` for a custom placeholder argument.
|
727
|
+
_.partial = restArgs(function(func, boundArgs) {
|
728
|
+
var placeholder = _.partial.placeholder;
|
729
|
+
var bound = function() {
|
730
|
+
var position = 0, length = boundArgs.length;
|
731
|
+
var args = Array(length);
|
732
|
+
for (var i = 0; i < length; i++) {
|
733
|
+
args[i] = boundArgs[i] === placeholder ? arguments[position++] : boundArgs[i];
|
734
|
+
}
|
735
|
+
while (position < arguments.length) args.push(arguments[position++]);
|
736
|
+
return executeBound(func, bound, this, this, args);
|
737
|
+
};
|
738
|
+
return bound;
|
739
|
+
});
|
740
|
+
|
741
|
+
_.partial.placeholder = _;
|
742
|
+
|
743
|
+
// Bind a number of an object's methods to that object. Remaining arguments
|
744
|
+
// are the method names to be bound. Useful for ensuring that all callbacks
|
745
|
+
// defined on an object belong to it.
|
746
|
+
_.bindAll = restArgs(function(obj, keys) {
|
747
|
+
keys = flatten(keys, false, false);
|
748
|
+
var index = keys.length;
|
749
|
+
if (index < 1) throw new Error('bindAll must be passed function names');
|
750
|
+
while (index--) {
|
751
|
+
var key = keys[index];
|
752
|
+
obj[key] = _.bind(obj[key], obj);
|
753
|
+
}
|
754
|
+
});
|
755
|
+
|
756
|
+
// Memoize an expensive function by storing its results.
|
757
|
+
_.memoize = function(func, hasher) {
|
758
|
+
var memoize = function(key) {
|
759
|
+
var cache = memoize.cache;
|
760
|
+
var address = '' + (hasher ? hasher.apply(this, arguments) : key);
|
761
|
+
if (!_.has(cache, address)) cache[address] = func.apply(this, arguments);
|
762
|
+
return cache[address];
|
763
|
+
};
|
764
|
+
memoize.cache = {};
|
765
|
+
return memoize;
|
766
|
+
};
|
767
|
+
|
768
|
+
// Delays a function for the given number of milliseconds, and then calls
|
769
|
+
// it with the arguments supplied.
|
770
|
+
_.delay = restArgs(function(func, wait, args) {
|
771
|
+
return setTimeout(function(){
|
772
|
+
return func.apply(null, args);
|
773
|
+
}, wait);
|
774
|
+
});
|
775
|
+
|
776
|
+
// Defers a function, scheduling it to run after the current call stack has
|
777
|
+
// cleared.
|
778
|
+
_.defer = _.partial(_.delay, _, 1);
|
779
|
+
|
780
|
+
// Returns a function, that, when invoked, will only be triggered at most once
|
781
|
+
// during a given window of time. Normally, the throttled function will run
|
782
|
+
// as much as it can, without ever going more than once per `wait` duration;
|
783
|
+
// but if you'd like to disable the execution on the leading edge, pass
|
784
|
+
// `{leading: false}`. To disable execution on the trailing edge, ditto.
|
785
|
+
_.throttle = function(func, wait, options) {
|
786
|
+
var context, args, result;
|
787
|
+
var timeout = null;
|
788
|
+
var previous = 0;
|
789
|
+
if (!options) options = {};
|
790
|
+
var later = function() {
|
791
|
+
previous = options.leading === false ? 0 : _.now();
|
792
|
+
timeout = null;
|
793
|
+
result = func.apply(context, args);
|
794
|
+
if (!timeout) context = args = null;
|
795
|
+
};
|
796
|
+
return function() {
|
797
|
+
var now = _.now();
|
798
|
+
if (!previous && options.leading === false) previous = now;
|
799
|
+
var remaining = wait - (now - previous);
|
800
|
+
context = this;
|
801
|
+
args = arguments;
|
802
|
+
if (remaining <= 0 || remaining > wait) {
|
803
|
+
if (timeout) {
|
804
|
+
clearTimeout(timeout);
|
805
|
+
timeout = null;
|
806
|
+
}
|
807
|
+
previous = now;
|
808
|
+
result = func.apply(context, args);
|
809
|
+
if (!timeout) context = args = null;
|
810
|
+
} else if (!timeout && options.trailing !== false) {
|
811
|
+
timeout = setTimeout(later, remaining);
|
812
|
+
}
|
813
|
+
return result;
|
814
|
+
};
|
815
|
+
};
|
816
|
+
|
817
|
+
// Returns a function, that, as long as it continues to be invoked, will not
|
818
|
+
// be triggered. The function will be called after it stops being called for
|
819
|
+
// N milliseconds. If `immediate` is passed, trigger the function on the
|
820
|
+
// leading edge, instead of the trailing.
|
821
|
+
_.debounce = function(func, wait, immediate) {
|
822
|
+
var timeout, args, context, timestamp, result;
|
823
|
+
|
824
|
+
var later = function() {
|
825
|
+
var last = _.now() - timestamp;
|
826
|
+
|
827
|
+
if (last < wait && last >= 0) {
|
828
|
+
timeout = setTimeout(later, wait - last);
|
829
|
+
} else {
|
830
|
+
timeout = null;
|
831
|
+
if (!immediate) {
|
832
|
+
result = func.apply(context, args);
|
833
|
+
if (!timeout) context = args = null;
|
834
|
+
}
|
835
|
+
}
|
836
|
+
};
|
837
|
+
|
838
|
+
return function() {
|
839
|
+
context = this;
|
840
|
+
args = arguments;
|
841
|
+
timestamp = _.now();
|
842
|
+
var callNow = immediate && !timeout;
|
843
|
+
if (!timeout) timeout = setTimeout(later, wait);
|
844
|
+
if (callNow) {
|
845
|
+
result = func.apply(context, args);
|
846
|
+
context = args = null;
|
847
|
+
}
|
848
|
+
|
849
|
+
return result;
|
850
|
+
};
|
851
|
+
};
|
852
|
+
|
853
|
+
// Returns the first function passed as an argument to the second,
|
854
|
+
// allowing you to adjust arguments, run code before and after, and
|
855
|
+
// conditionally execute the original function.
|
856
|
+
_.wrap = function(func, wrapper) {
|
857
|
+
return _.partial(wrapper, func);
|
858
|
+
};
|
859
|
+
|
860
|
+
// Returns a negated version of the passed-in predicate.
|
861
|
+
_.negate = function(predicate) {
|
862
|
+
return function() {
|
863
|
+
return !predicate.apply(this, arguments);
|
864
|
+
};
|
865
|
+
};
|
866
|
+
|
867
|
+
// Returns a function that is the composition of a list of functions, each
|
868
|
+
// consuming the return value of the function that follows.
|
869
|
+
_.compose = function() {
|
870
|
+
var args = arguments;
|
871
|
+
var start = args.length - 1;
|
872
|
+
return function() {
|
873
|
+
var i = start;
|
874
|
+
var result = args[start].apply(this, arguments);
|
875
|
+
while (i--) result = args[i].call(this, result);
|
876
|
+
return result;
|
877
|
+
};
|
878
|
+
};
|
879
|
+
|
880
|
+
// Returns a function that will only be executed on and after the Nth call.
|
881
|
+
_.after = function(times, func) {
|
882
|
+
return function() {
|
883
|
+
if (--times < 1) {
|
884
|
+
return func.apply(this, arguments);
|
885
|
+
}
|
886
|
+
};
|
887
|
+
};
|
888
|
+
|
889
|
+
// Returns a function that will only be executed up to (but not including) the Nth call.
|
890
|
+
_.before = function(times, func) {
|
891
|
+
var memo;
|
892
|
+
return function() {
|
893
|
+
if (--times > 0) {
|
894
|
+
memo = func.apply(this, arguments);
|
895
|
+
}
|
896
|
+
if (times <= 1) func = null;
|
897
|
+
return memo;
|
898
|
+
};
|
899
|
+
};
|
900
|
+
|
901
|
+
// Returns a function that will be executed at most one time, no matter how
|
902
|
+
// often you call it. Useful for lazy initialization.
|
903
|
+
_.once = _.partial(_.before, 2);
|
904
|
+
|
905
|
+
_.restArgs = restArgs;
|
906
|
+
|
907
|
+
// Object Functions
|
908
|
+
// ----------------
|
909
|
+
|
910
|
+
// Keys in IE < 9 that won't be iterated by `for key in ...` and thus missed.
|
911
|
+
var hasEnumBug = !{toString: null}.propertyIsEnumerable('toString');
|
912
|
+
var nonEnumerableProps = ['valueOf', 'isPrototypeOf', 'toString',
|
913
|
+
'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString'];
|
914
|
+
|
915
|
+
var collectNonEnumProps = function(obj, keys) {
|
916
|
+
var nonEnumIdx = nonEnumerableProps.length;
|
917
|
+
var constructor = obj.constructor;
|
918
|
+
var proto = _.isFunction(constructor) && constructor.prototype || ObjProto;
|
919
|
+
|
920
|
+
// Constructor is a special case.
|
921
|
+
var prop = 'constructor';
|
922
|
+
if (_.has(obj, prop) && !_.contains(keys, prop)) keys.push(prop);
|
923
|
+
|
924
|
+
while (nonEnumIdx--) {
|
925
|
+
prop = nonEnumerableProps[nonEnumIdx];
|
926
|
+
if (prop in obj && obj[prop] !== proto[prop] && !_.contains(keys, prop)) {
|
927
|
+
keys.push(prop);
|
928
|
+
}
|
929
|
+
}
|
930
|
+
};
|
931
|
+
|
932
|
+
// Retrieve the names of an object's own properties.
|
933
|
+
// Delegates to **ECMAScript 5**'s native `Object.keys`
|
934
|
+
_.keys = function(obj) {
|
935
|
+
if (!_.isObject(obj)) return [];
|
936
|
+
if (nativeKeys) return nativeKeys(obj);
|
937
|
+
var keys = [];
|
938
|
+
for (var key in obj) if (_.has(obj, key)) keys.push(key);
|
939
|
+
// Ahem, IE < 9.
|
940
|
+
if (hasEnumBug) collectNonEnumProps(obj, keys);
|
941
|
+
return keys;
|
942
|
+
};
|
943
|
+
|
944
|
+
// Retrieve all the property names of an object.
|
945
|
+
_.allKeys = function(obj) {
|
946
|
+
if (!_.isObject(obj)) return [];
|
947
|
+
var keys = [];
|
948
|
+
for (var key in obj) keys.push(key);
|
949
|
+
// Ahem, IE < 9.
|
950
|
+
if (hasEnumBug) collectNonEnumProps(obj, keys);
|
951
|
+
return keys;
|
952
|
+
};
|
953
|
+
|
954
|
+
// Retrieve the values of an object's properties.
|
955
|
+
_.values = function(obj) {
|
956
|
+
var keys = _.keys(obj);
|
957
|
+
var length = keys.length;
|
958
|
+
var values = Array(length);
|
959
|
+
for (var i = 0; i < length; i++) {
|
960
|
+
values[i] = obj[keys[i]];
|
961
|
+
}
|
962
|
+
return values;
|
963
|
+
};
|
964
|
+
|
965
|
+
// Returns the results of applying the iteratee to each element of the object
|
966
|
+
// In contrast to _.map it returns an object
|
967
|
+
_.mapObject = function(obj, iteratee, context) {
|
968
|
+
iteratee = cb(iteratee, context);
|
969
|
+
var keys = _.keys(obj),
|
970
|
+
length = keys.length,
|
971
|
+
results = {};
|
972
|
+
for (var index = 0; index < length; index++) {
|
973
|
+
var currentKey = keys[index];
|
974
|
+
results[currentKey] = iteratee(obj[currentKey], currentKey, obj);
|
975
|
+
}
|
976
|
+
return results;
|
977
|
+
};
|
978
|
+
|
979
|
+
// Convert an object into a list of `[key, value]` pairs.
|
980
|
+
_.pairs = function(obj) {
|
981
|
+
var keys = _.keys(obj);
|
982
|
+
var length = keys.length;
|
983
|
+
var pairs = Array(length);
|
984
|
+
for (var i = 0; i < length; i++) {
|
985
|
+
pairs[i] = [keys[i], obj[keys[i]]];
|
986
|
+
}
|
987
|
+
return pairs;
|
988
|
+
};
|
989
|
+
|
990
|
+
// Invert the keys and values of an object. The values must be serializable.
|
991
|
+
_.invert = function(obj) {
|
992
|
+
var result = {};
|
993
|
+
var keys = _.keys(obj);
|
994
|
+
for (var i = 0, length = keys.length; i < length; i++) {
|
995
|
+
result[obj[keys[i]]] = keys[i];
|
996
|
+
}
|
997
|
+
return result;
|
998
|
+
};
|
999
|
+
|
1000
|
+
// Return a sorted list of the function names available on the object.
|
1001
|
+
// Aliased as `methods`
|
1002
|
+
_.functions = _.methods = function(obj) {
|
1003
|
+
var names = [];
|
1004
|
+
for (var key in obj) {
|
1005
|
+
if (_.isFunction(obj[key])) names.push(key);
|
1006
|
+
}
|
1007
|
+
return names.sort();
|
1008
|
+
};
|
1009
|
+
|
1010
|
+
// An internal function for creating assigner functions.
|
1011
|
+
var createAssigner = function(keysFunc, undefinedOnly) {
|
1012
|
+
return function(obj) {
|
1013
|
+
var length = arguments.length;
|
1014
|
+
if (length < 2 || obj == null) return obj;
|
1015
|
+
for (var index = 1; index < length; index++) {
|
1016
|
+
var source = arguments[index],
|
1017
|
+
keys = keysFunc(source),
|
1018
|
+
l = keys.length;
|
1019
|
+
for (var i = 0; i < l; i++) {
|
1020
|
+
var key = keys[i];
|
1021
|
+
if (!undefinedOnly || obj[key] === void 0) obj[key] = source[key];
|
1022
|
+
}
|
1023
|
+
}
|
1024
|
+
return obj;
|
1025
|
+
};
|
1026
|
+
};
|
1027
|
+
|
1028
|
+
// Extend a given object with all the properties in passed-in object(s).
|
1029
|
+
_.extend = createAssigner(_.allKeys);
|
1030
|
+
|
1031
|
+
// Assigns a given object with all the own properties in the passed-in object(s)
|
1032
|
+
// (https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)
|
1033
|
+
_.extendOwn = _.assign = createAssigner(_.keys);
|
1034
|
+
|
1035
|
+
// Returns the first key on an object that passes a predicate test
|
1036
|
+
_.findKey = function(obj, predicate, context) {
|
1037
|
+
predicate = cb(predicate, context);
|
1038
|
+
var keys = _.keys(obj), key;
|
1039
|
+
for (var i = 0, length = keys.length; i < length; i++) {
|
1040
|
+
key = keys[i];
|
1041
|
+
if (predicate(obj[key], key, obj)) return key;
|
1042
|
+
}
|
1043
|
+
};
|
1044
|
+
|
1045
|
+
// Internal pick helper function to determine if `obj` has key `key`.
|
1046
|
+
var keyInObj = function(value, key, obj) {
|
1047
|
+
return key in obj;
|
1048
|
+
};
|
1049
|
+
|
1050
|
+
// Return a copy of the object only containing the whitelisted properties.
|
1051
|
+
_.pick = restArgs(function(obj, keys) {
|
1052
|
+
var result = {}, iteratee = keys[0];
|
1053
|
+
if (obj == null) return result;
|
1054
|
+
if (_.isFunction(iteratee)) {
|
1055
|
+
if (keys.length > 1) iteratee = optimizeCb(iteratee, keys[1]);
|
1056
|
+
keys = _.allKeys(obj);
|
1057
|
+
} else {
|
1058
|
+
iteratee = keyInObj;
|
1059
|
+
keys = flatten(keys, false, false);
|
1060
|
+
obj = Object(obj);
|
1061
|
+
}
|
1062
|
+
for (var i = 0, length = keys.length; i < length; i++) {
|
1063
|
+
var key = keys[i];
|
1064
|
+
var value = obj[key];
|
1065
|
+
if (iteratee(value, key, obj)) result[key] = value;
|
1066
|
+
}
|
1067
|
+
return result;
|
1068
|
+
});
|
1069
|
+
|
1070
|
+
// Return a copy of the object without the blacklisted properties.
|
1071
|
+
_.omit = restArgs(function(obj, keys) {
|
1072
|
+
var iteratee = keys[0], context;
|
1073
|
+
if (_.isFunction(iteratee)) {
|
1074
|
+
iteratee = _.negate(iteratee);
|
1075
|
+
if (keys.length > 1) context = keys[1];
|
1076
|
+
} else {
|
1077
|
+
keys = _.map(flatten(keys, false, false), String);
|
1078
|
+
iteratee = function(value, key) {
|
1079
|
+
return !_.contains(keys, key);
|
1080
|
+
};
|
1081
|
+
}
|
1082
|
+
return _.pick(obj, iteratee, context);
|
1083
|
+
});
|
1084
|
+
|
1085
|
+
// Fill in a given object with default properties.
|
1086
|
+
_.defaults = createAssigner(_.allKeys, true);
|
1087
|
+
|
1088
|
+
// Creates an object that inherits from the given prototype object.
|
1089
|
+
// If additional properties are provided then they will be added to the
|
1090
|
+
// created object.
|
1091
|
+
_.create = function(prototype, props) {
|
1092
|
+
var result = baseCreate(prototype);
|
1093
|
+
if (props) _.extendOwn(result, props);
|
1094
|
+
return result;
|
1095
|
+
};
|
1096
|
+
|
1097
|
+
// Create a (shallow-cloned) duplicate of an object.
|
1098
|
+
_.clone = function(obj) {
|
1099
|
+
if (!_.isObject(obj)) return obj;
|
1100
|
+
return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
|
1101
|
+
};
|
1102
|
+
|
1103
|
+
// Invokes interceptor with the obj, and then returns obj.
|
1104
|
+
// The primary purpose of this method is to "tap into" a method chain, in
|
1105
|
+
// order to perform operations on intermediate results within the chain.
|
1106
|
+
_.tap = function(obj, interceptor) {
|
1107
|
+
interceptor(obj);
|
1108
|
+
return obj;
|
1109
|
+
};
|
1110
|
+
|
1111
|
+
// Returns whether an object has a given set of `key:value` pairs.
|
1112
|
+
_.isMatch = function(object, attrs) {
|
1113
|
+
var keys = _.keys(attrs), length = keys.length;
|
1114
|
+
if (object == null) return !length;
|
1115
|
+
var obj = Object(object);
|
1116
|
+
for (var i = 0; i < length; i++) {
|
1117
|
+
var key = keys[i];
|
1118
|
+
if (attrs[key] !== obj[key] || !(key in obj)) return false;
|
1119
|
+
}
|
1120
|
+
return true;
|
1121
|
+
};
|
1122
|
+
|
1123
|
+
|
1124
|
+
// Internal recursive comparison function for `isEqual`.
|
1125
|
+
var eq, deepEq;
|
1126
|
+
eq = function(a, b, aStack, bStack) {
|
1127
|
+
// Identical objects are equal. `0 === -0`, but they aren't identical.
|
1128
|
+
// See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal).
|
1129
|
+
if (a === b) return a !== 0 || 1 / a === 1 / b;
|
1130
|
+
// A strict comparison is necessary because `null == undefined`.
|
1131
|
+
if (a == null || b == null) return a === b;
|
1132
|
+
// `NaN`s are equivalent, but non-reflexive.
|
1133
|
+
if (a !== a) return b !== b;
|
1134
|
+
// Exhaust primitive checks
|
1135
|
+
var type = typeof a;
|
1136
|
+
if (type !== 'function' && type !== 'object' && typeof b !== 'object') return false;
|
1137
|
+
return deepEq(a, b, aStack, bStack);
|
1138
|
+
};
|
1139
|
+
|
1140
|
+
// Internal recursive comparison function for `isEqual`.
|
1141
|
+
deepEq = function(a, b, aStack, bStack) {
|
1142
|
+
// Unwrap any wrapped objects.
|
1143
|
+
if (a instanceof _) a = a._wrapped;
|
1144
|
+
if (b instanceof _) b = b._wrapped;
|
1145
|
+
// Compare `[[Class]]` names.
|
1146
|
+
var className = toString.call(a);
|
1147
|
+
if (className !== toString.call(b)) return false;
|
1148
|
+
switch (className) {
|
1149
|
+
// Strings, numbers, regular expressions, dates, and booleans are compared by value.
|
1150
|
+
case '[object RegExp]':
|
1151
|
+
// RegExps are coerced to strings for comparison (Note: '' + /a/i === '/a/i')
|
1152
|
+
case '[object String]':
|
1153
|
+
// Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
|
1154
|
+
// equivalent to `new String("5")`.
|
1155
|
+
return '' + a === '' + b;
|
1156
|
+
case '[object Number]':
|
1157
|
+
// `NaN`s are equivalent, but non-reflexive.
|
1158
|
+
// Object(NaN) is equivalent to NaN
|
1159
|
+
if (+a !== +a) return +b !== +b;
|
1160
|
+
// An `egal` comparison is performed for other numeric values.
|
1161
|
+
return +a === 0 ? 1 / +a === 1 / b : +a === +b;
|
1162
|
+
case '[object Date]':
|
1163
|
+
case '[object Boolean]':
|
1164
|
+
// Coerce dates and booleans to numeric primitive values. Dates are compared by their
|
1165
|
+
// millisecond representations. Note that invalid dates with millisecond representations
|
1166
|
+
// of `NaN` are not equivalent.
|
1167
|
+
return +a === +b;
|
1168
|
+
}
|
1169
|
+
|
1170
|
+
var areArrays = className === '[object Array]';
|
1171
|
+
if (!areArrays) {
|
1172
|
+
if (typeof a != 'object' || typeof b != 'object') return false;
|
1173
|
+
|
1174
|
+
// Objects with different constructors are not equivalent, but `Object`s or `Array`s
|
1175
|
+
// from different frames are.
|
1176
|
+
var aCtor = a.constructor, bCtor = b.constructor;
|
1177
|
+
if (aCtor !== bCtor && !(_.isFunction(aCtor) && aCtor instanceof aCtor &&
|
1178
|
+
_.isFunction(bCtor) && bCtor instanceof bCtor)
|
1179
|
+
&& ('constructor' in a && 'constructor' in b)) {
|
1180
|
+
return false;
|
1181
|
+
}
|
1182
|
+
}
|
1183
|
+
// Assume equality for cyclic structures. The algorithm for detecting cyclic
|
1184
|
+
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
|
1185
|
+
|
1186
|
+
// Initializing stack of traversed objects.
|
1187
|
+
// It's done here since we only need them for objects and arrays comparison.
|
1188
|
+
aStack = aStack || [];
|
1189
|
+
bStack = bStack || [];
|
1190
|
+
var length = aStack.length;
|
1191
|
+
while (length--) {
|
1192
|
+
// Linear search. Performance is inversely proportional to the number of
|
1193
|
+
// unique nested structures.
|
1194
|
+
if (aStack[length] === a) return bStack[length] === b;
|
1195
|
+
}
|
1196
|
+
|
1197
|
+
// Add the first object to the stack of traversed objects.
|
1198
|
+
aStack.push(a);
|
1199
|
+
bStack.push(b);
|
1200
|
+
|
1201
|
+
// Recursively compare objects and arrays.
|
1202
|
+
if (areArrays) {
|
1203
|
+
// Compare array lengths to determine if a deep comparison is necessary.
|
1204
|
+
length = a.length;
|
1205
|
+
if (length !== b.length) return false;
|
1206
|
+
// Deep compare the contents, ignoring non-numeric properties.
|
1207
|
+
while (length--) {
|
1208
|
+
if (!eq(a[length], b[length], aStack, bStack)) return false;
|
1209
|
+
}
|
1210
|
+
} else {
|
1211
|
+
// Deep compare objects.
|
1212
|
+
var keys = _.keys(a), key;
|
1213
|
+
length = keys.length;
|
1214
|
+
// Ensure that both objects contain the same number of properties before comparing deep equality.
|
1215
|
+
if (_.keys(b).length !== length) return false;
|
1216
|
+
while (length--) {
|
1217
|
+
// Deep compare each member
|
1218
|
+
key = keys[length];
|
1219
|
+
if (!(_.has(b, key) && eq(a[key], b[key], aStack, bStack))) return false;
|
1220
|
+
}
|
1221
|
+
}
|
1222
|
+
// Remove the first object from the stack of traversed objects.
|
1223
|
+
aStack.pop();
|
1224
|
+
bStack.pop();
|
1225
|
+
return true;
|
1226
|
+
};
|
1227
|
+
|
1228
|
+
// Perform a deep comparison to check if two objects are equal.
|
1229
|
+
_.isEqual = function(a, b) {
|
1230
|
+
return eq(a, b);
|
1231
|
+
};
|
1232
|
+
|
1233
|
+
// Is a given array, string, or object empty?
|
1234
|
+
// An "empty" object has no enumerable own-properties.
|
1235
|
+
_.isEmpty = function(obj) {
|
1236
|
+
if (obj == null) return true;
|
1237
|
+
if (isArrayLike(obj) && (_.isArray(obj) || _.isString(obj) || _.isArguments(obj))) return obj.length === 0;
|
1238
|
+
return _.keys(obj).length === 0;
|
1239
|
+
};
|
1240
|
+
|
1241
|
+
// Is a given value a DOM element?
|
1242
|
+
_.isElement = function(obj) {
|
1243
|
+
return !!(obj && obj.nodeType === 1);
|
1244
|
+
};
|
1245
|
+
|
1246
|
+
// Is a given value an array?
|
1247
|
+
// Delegates to ECMA5's native Array.isArray
|
1248
|
+
_.isArray = nativeIsArray || function(obj) {
|
1249
|
+
return toString.call(obj) === '[object Array]';
|
1250
|
+
};
|
1251
|
+
|
1252
|
+
// Is a given variable an object?
|
1253
|
+
_.isObject = function(obj) {
|
1254
|
+
var type = typeof obj;
|
1255
|
+
return type === 'function' || type === 'object' && !!obj;
|
1256
|
+
};
|
1257
|
+
|
1258
|
+
// Add some isType methods: isArguments, isFunction, isString, isNumber, isDate, isRegExp, isError.
|
1259
|
+
_.each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp', 'Error'], function(name) {
|
1260
|
+
_['is' + name] = function(obj) {
|
1261
|
+
return toString.call(obj) === '[object ' + name + ']';
|
1262
|
+
};
|
1263
|
+
});
|
1264
|
+
|
1265
|
+
// Define a fallback version of the method in browsers (ahem, IE < 9), where
|
1266
|
+
// there isn't any inspectable "Arguments" type.
|
1267
|
+
if (!_.isArguments(arguments)) {
|
1268
|
+
_.isArguments = function(obj) {
|
1269
|
+
return _.has(obj, 'callee');
|
1270
|
+
};
|
1271
|
+
}
|
1272
|
+
|
1273
|
+
// Optimize `isFunction` if appropriate. Work around some typeof bugs in old v8,
|
1274
|
+
// IE 11 (#1621), and in Safari 8 (#1929).
|
1275
|
+
if (typeof /./ != 'function' && typeof Int8Array != 'object') {
|
1276
|
+
_.isFunction = function(obj) {
|
1277
|
+
return typeof obj == 'function' || false;
|
1278
|
+
};
|
1279
|
+
}
|
1280
|
+
|
1281
|
+
// Is a given object a finite number?
|
1282
|
+
_.isFinite = function(obj) {
|
1283
|
+
return isFinite(obj) && !isNaN(parseFloat(obj));
|
1284
|
+
};
|
1285
|
+
|
1286
|
+
// Is the given value `NaN`? (NaN is the only number which does not equal itself).
|
1287
|
+
_.isNaN = function(obj) {
|
1288
|
+
return _.isNumber(obj) && obj !== +obj;
|
1289
|
+
};
|
1290
|
+
|
1291
|
+
// Is a given value a boolean?
|
1292
|
+
_.isBoolean = function(obj) {
|
1293
|
+
return obj === true || obj === false || toString.call(obj) === '[object Boolean]';
|
1294
|
+
};
|
1295
|
+
|
1296
|
+
// Is a given value equal to null?
|
1297
|
+
_.isNull = function(obj) {
|
1298
|
+
return obj === null;
|
1299
|
+
};
|
1300
|
+
|
1301
|
+
// Is a given variable undefined?
|
1302
|
+
_.isUndefined = function(obj) {
|
1303
|
+
return obj === void 0;
|
1304
|
+
};
|
1305
|
+
|
1306
|
+
// Shortcut function for checking if an object has a given property directly
|
1307
|
+
// on itself (in other words, not on a prototype).
|
1308
|
+
_.has = function(obj, key) {
|
1309
|
+
return obj != null && hasOwnProperty.call(obj, key);
|
1310
|
+
};
|
1311
|
+
|
1312
|
+
// Utility Functions
|
1313
|
+
// -----------------
|
1314
|
+
|
1315
|
+
// Run Underscore.js in *noConflict* mode, returning the `_` variable to its
|
1316
|
+
// previous owner. Returns a reference to the Underscore object.
|
1317
|
+
_.noConflict = function() {
|
1318
|
+
root._ = previousUnderscore;
|
1319
|
+
return this;
|
1320
|
+
};
|
1321
|
+
|
1322
|
+
// Keep the identity function around for default iteratees.
|
1323
|
+
_.identity = function(value) {
|
1324
|
+
return value;
|
1325
|
+
};
|
1326
|
+
|
1327
|
+
// Predicate-generating functions. Often useful outside of Underscore.
|
1328
|
+
_.constant = function(value) {
|
1329
|
+
return function() {
|
1330
|
+
return value;
|
1331
|
+
};
|
1332
|
+
};
|
1333
|
+
|
1334
|
+
_.noop = function(){};
|
1335
|
+
|
1336
|
+
_.property = property;
|
1337
|
+
|
1338
|
+
// Generates a function for a given object that returns a given property.
|
1339
|
+
_.propertyOf = function(obj) {
|
1340
|
+
return obj == null ? function(){} : function(key) {
|
1341
|
+
return obj[key];
|
1342
|
+
};
|
1343
|
+
};
|
1344
|
+
|
1345
|
+
// Returns a predicate for checking whether an object has a given set of
|
1346
|
+
// `key:value` pairs.
|
1347
|
+
_.matcher = _.matches = function(attrs) {
|
1348
|
+
attrs = _.extendOwn({}, attrs);
|
1349
|
+
return function(obj) {
|
1350
|
+
return _.isMatch(obj, attrs);
|
1351
|
+
};
|
1352
|
+
};
|
1353
|
+
|
1354
|
+
// Run a function **n** times.
|
1355
|
+
_.times = function(n, iteratee, context) {
|
1356
|
+
var accum = Array(Math.max(0, n));
|
1357
|
+
iteratee = optimizeCb(iteratee, context, 1);
|
1358
|
+
for (var i = 0; i < n; i++) accum[i] = iteratee(i);
|
1359
|
+
return accum;
|
1360
|
+
};
|
1361
|
+
|
1362
|
+
// Return a random integer between min and max (inclusive).
|
1363
|
+
_.random = function(min, max) {
|
1364
|
+
if (max == null) {
|
1365
|
+
max = min;
|
1366
|
+
min = 0;
|
1367
|
+
}
|
1368
|
+
return min + Math.floor(Math.random() * (max - min + 1));
|
1369
|
+
};
|
1370
|
+
|
1371
|
+
// A (possibly faster) way to get the current timestamp as an integer.
|
1372
|
+
_.now = Date.now || function() {
|
1373
|
+
return new Date().getTime();
|
1374
|
+
};
|
1375
|
+
|
1376
|
+
// List of HTML entities for escaping.
|
1377
|
+
var escapeMap = {
|
1378
|
+
'&': '&',
|
1379
|
+
'<': '<',
|
1380
|
+
'>': '>',
|
1381
|
+
'"': '"',
|
1382
|
+
"'": ''',
|
1383
|
+
'`': '`'
|
1384
|
+
};
|
1385
|
+
var unescapeMap = _.invert(escapeMap);
|
1386
|
+
|
1387
|
+
// Functions for escaping and unescaping strings to/from HTML interpolation.
|
1388
|
+
var createEscaper = function(map) {
|
1389
|
+
var escaper = function(match) {
|
1390
|
+
return map[match];
|
1391
|
+
};
|
1392
|
+
// Regexes for identifying a key that needs to be escaped
|
1393
|
+
var source = '(?:' + _.keys(map).join('|') + ')';
|
1394
|
+
var testRegexp = RegExp(source);
|
1395
|
+
var replaceRegexp = RegExp(source, 'g');
|
1396
|
+
return function(string) {
|
1397
|
+
string = string == null ? '' : '' + string;
|
1398
|
+
return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string;
|
1399
|
+
};
|
1400
|
+
};
|
1401
|
+
_.escape = createEscaper(escapeMap);
|
1402
|
+
_.unescape = createEscaper(unescapeMap);
|
1403
|
+
|
1404
|
+
// If the value of the named `property` is a function then invoke it with the
|
1405
|
+
// `object` as context; otherwise, return it.
|
1406
|
+
_.result = function(object, prop, fallback) {
|
1407
|
+
var value = object == null ? void 0 : object[prop];
|
1408
|
+
if (value === void 0) {
|
1409
|
+
value = fallback;
|
1410
|
+
}
|
1411
|
+
return _.isFunction(value) ? value.call(object) : value;
|
1412
|
+
};
|
1413
|
+
|
1414
|
+
// Generate a unique integer id (unique within the entire client session).
|
1415
|
+
// Useful for temporary DOM ids.
|
1416
|
+
var idCounter = 0;
|
1417
|
+
_.uniqueId = function(prefix) {
|
1418
|
+
var id = ++idCounter + '';
|
1419
|
+
return prefix ? prefix + id : id;
|
1420
|
+
};
|
1421
|
+
|
1422
|
+
// By default, Underscore uses ERB-style template delimiters, change the
|
1423
|
+
// following template settings to use alternative delimiters.
|
1424
|
+
_.templateSettings = {
|
1425
|
+
evaluate: /<%([\s\S]+?)%>/g,
|
1426
|
+
interpolate: /<%=([\s\S]+?)%>/g,
|
1427
|
+
escape: /<%-([\s\S]+?)%>/g
|
1428
|
+
};
|
1429
|
+
|
1430
|
+
// When customizing `templateSettings`, if you don't want to define an
|
1431
|
+
// interpolation, evaluation or escaping regex, we need one that is
|
1432
|
+
// guaranteed not to match.
|
1433
|
+
var noMatch = /(.)^/;
|
1434
|
+
|
1435
|
+
// Certain characters need to be escaped so that they can be put into a
|
1436
|
+
// string literal.
|
1437
|
+
var escapes = {
|
1438
|
+
"'": "'",
|
1439
|
+
'\\': '\\',
|
1440
|
+
'\r': 'r',
|
1441
|
+
'\n': 'n',
|
1442
|
+
'\u2028': 'u2028',
|
1443
|
+
'\u2029': 'u2029'
|
1444
|
+
};
|
1445
|
+
|
1446
|
+
var escapeRegExp = /\\|'|\r|\n|\u2028|\u2029/g;
|
1447
|
+
|
1448
|
+
var escapeChar = function(match) {
|
1449
|
+
return '\\' + escapes[match];
|
1450
|
+
};
|
1451
|
+
|
1452
|
+
// JavaScript micro-templating, similar to John Resig's implementation.
|
1453
|
+
// Underscore templating handles arbitrary delimiters, preserves whitespace,
|
1454
|
+
// and correctly escapes quotes within interpolated code.
|
1455
|
+
// NB: `oldSettings` only exists for backwards compatibility.
|
1456
|
+
_.template = function(text, settings, oldSettings) {
|
1457
|
+
if (!settings && oldSettings) settings = oldSettings;
|
1458
|
+
settings = _.defaults({}, settings, _.templateSettings);
|
1459
|
+
|
1460
|
+
// Combine delimiters into one regular expression via alternation.
|
1461
|
+
var matcher = RegExp([
|
1462
|
+
(settings.escape || noMatch).source,
|
1463
|
+
(settings.interpolate || noMatch).source,
|
1464
|
+
(settings.evaluate || noMatch).source
|
1465
|
+
].join('|') + '|$', 'g');
|
1466
|
+
|
1467
|
+
// Compile the template source, escaping string literals appropriately.
|
1468
|
+
var index = 0;
|
1469
|
+
var source = "__p+='";
|
1470
|
+
text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
|
1471
|
+
source += text.slice(index, offset).replace(escapeRegExp, escapeChar);
|
1472
|
+
index = offset + match.length;
|
1473
|
+
|
1474
|
+
if (escape) {
|
1475
|
+
source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'";
|
1476
|
+
} else if (interpolate) {
|
1477
|
+
source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'";
|
1478
|
+
} else if (evaluate) {
|
1479
|
+
source += "';\n" + evaluate + "\n__p+='";
|
1480
|
+
}
|
1481
|
+
|
1482
|
+
// Adobe VMs need the match returned to produce the correct offest.
|
1483
|
+
return match;
|
1484
|
+
});
|
1485
|
+
source += "';\n";
|
1486
|
+
|
1487
|
+
// If a variable is not specified, place data values in local scope.
|
1488
|
+
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
|
1489
|
+
|
1490
|
+
source = "var __t,__p='',__j=Array.prototype.join," +
|
1491
|
+
"print=function(){__p+=__j.call(arguments,'');};\n" +
|
1492
|
+
source + 'return __p;\n';
|
1493
|
+
|
1494
|
+
var render;
|
1495
|
+
try {
|
1496
|
+
render = new Function(settings.variable || 'obj', '_', source);
|
1497
|
+
} catch (e) {
|
1498
|
+
e.source = source;
|
1499
|
+
throw e;
|
1500
|
+
}
|
1501
|
+
|
1502
|
+
var template = function(data) {
|
1503
|
+
return render.call(this, data, _);
|
1504
|
+
};
|
1505
|
+
|
1506
|
+
// Provide the compiled source as a convenience for precompilation.
|
1507
|
+
var argument = settings.variable || 'obj';
|
1508
|
+
template.source = 'function(' + argument + '){\n' + source + '}';
|
1509
|
+
|
1510
|
+
return template;
|
1511
|
+
};
|
1512
|
+
|
1513
|
+
// Add a "chain" function. Start chaining a wrapped Underscore object.
|
1514
|
+
_.chain = function(obj) {
|
1515
|
+
var instance = _(obj);
|
1516
|
+
instance._chain = true;
|
1517
|
+
return instance;
|
1518
|
+
};
|
1519
|
+
|
1520
|
+
// OOP
|
1521
|
+
// ---------------
|
1522
|
+
// If Underscore is called as a function, it returns a wrapped object that
|
1523
|
+
// can be used OO-style. This wrapper holds altered versions of all the
|
1524
|
+
// underscore functions. Wrapped objects may be chained.
|
1525
|
+
|
1526
|
+
// Helper function to continue chaining intermediate results.
|
1527
|
+
var chainResult = function(instance, obj) {
|
1528
|
+
return instance._chain ? _(obj).chain() : obj;
|
1529
|
+
};
|
1530
|
+
|
1531
|
+
// Add your own custom functions to the Underscore object.
|
1532
|
+
_.mixin = function(obj) {
|
1533
|
+
_.each(_.functions(obj), function(name) {
|
1534
|
+
var func = _[name] = obj[name];
|
1535
|
+
_.prototype[name] = function() {
|
1536
|
+
var args = [this._wrapped];
|
1537
|
+
push.apply(args, arguments);
|
1538
|
+
return chainResult(this, func.apply(_, args));
|
1539
|
+
};
|
1540
|
+
});
|
1541
|
+
};
|
1542
|
+
|
1543
|
+
// Add all of the Underscore functions to the wrapper object.
|
1544
|
+
_.mixin(_);
|
1545
|
+
|
1546
|
+
// Add all mutator Array functions to the wrapper.
|
1547
|
+
_.each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
|
1548
|
+
var method = ArrayProto[name];
|
1549
|
+
_.prototype[name] = function() {
|
1550
|
+
var obj = this._wrapped;
|
1551
|
+
method.apply(obj, arguments);
|
1552
|
+
if ((name === 'shift' || name === 'splice') && obj.length === 0) delete obj[0];
|
1553
|
+
return chainResult(this, obj);
|
1554
|
+
};
|
1555
|
+
});
|
1556
|
+
|
1557
|
+
// Add all accessor Array functions to the wrapper.
|
1558
|
+
_.each(['concat', 'join', 'slice'], function(name) {
|
1559
|
+
var method = ArrayProto[name];
|
1560
|
+
_.prototype[name] = function() {
|
1561
|
+
return chainResult(this, method.apply(this._wrapped, arguments));
|
1562
|
+
};
|
1563
|
+
});
|
1564
|
+
|
1565
|
+
// Extracts the result from a wrapped and chained object.
|
1566
|
+
_.prototype.value = function() {
|
1567
|
+
return this._wrapped;
|
1568
|
+
};
|
1569
|
+
|
1570
|
+
// Provide unwrapping proxy for some methods used in engine operations
|
1571
|
+
// such as arithmetic and JSON stringification.
|
1572
|
+
_.prototype.valueOf = _.prototype.toJSON = _.prototype.value;
|
1573
|
+
|
1574
|
+
_.prototype.toString = function() {
|
1575
|
+
return '' + this._wrapped;
|
1576
|
+
};
|
1577
|
+
|
1578
|
+
// AMD registration happens at the end for compatibility with AMD loaders
|
1579
|
+
// that may not enforce next-turn semantics on modules. Even though general
|
1580
|
+
// practice for AMD registration is to be anonymous, underscore registers
|
1581
|
+
// as a named module because, like jQuery, it is a base library that is
|
1582
|
+
// popular enough to be bundled in a third party lib, but not be part of
|
1583
|
+
// an AMD load request. Those cases could generate an error when an
|
1584
|
+
// anonymous define() is called outside of a loader request.
|
1585
|
+
if (typeof define === 'function' && define.amd) {
|
1586
|
+
define('underscore', [], function() {
|
1587
|
+
return _;
|
1588
|
+
});
|
1589
|
+
}
|
1590
|
+
}());
|