smart_asset 0.5.4 → 0.5.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/Gemfile +3 -0
- data/LICENSE +2 -2
- data/Rakefile +1 -0
- data/bin/smart_asset +1 -1
- data/lib/smart_asset.rb +2 -6
- data/lib/smart_asset/adapters/stasis.rb +43 -0
- data/smart_asset.gemspec +25 -0
- data/spec/fixtures/all_frameworks/assets.yml +20 -0
- data/spec/fixtures/all_frameworks/test.html.erb +3 -0
- data/spec/fixtures/assets.yml +27 -0
- data/spec/fixtures/assets/javascripts/jquery/jquery.js +7179 -0
- data/spec/fixtures/assets/javascripts/underscore.js +722 -0
- data/spec/fixtures/assets/stylesheets/960.css +648 -0
- data/spec/fixtures/assets/stylesheets/blueprint/blueprint.css +265 -0
- data/spec/fixtures/development_output.txt +5 -0
- data/spec/fixtures/frameworks.yml +32 -0
- data/spec/fixtures/production_output.txt +3 -0
- data/spec/fixtures/rails2/application_controller.rb +17 -0
- data/spec/fixtures/rails2/environment.rb +42 -0
- data/spec/fixtures/rails2/init.rb +1 -0
- data/spec/fixtures/rails2/routes.rb +47 -0
- data/spec/fixtures/rails3/Gemfile +34 -0
- data/spec/fixtures/rails3/application.rb +47 -0
- data/spec/fixtures/rails3/application_controller.rb +11 -0
- data/spec/fixtures/rails3/routes.rb +61 -0
- data/spec/fixtures/sinatra/application.rb +19 -0
- data/spec/run +39 -0
- data/spec/smart_asset/adapters/rails2_spec.rb +46 -0
- data/spec/smart_asset/adapters/rails3_spec.rb +46 -0
- data/spec/smart_asset/adapters/sinatra_spec.rb +47 -0
- data/spec/smart_asset_spec.rb +403 -0
- data/spec/spec_helper.rb +27 -0
- metadata +113 -59
- data/lib/smart_asset/gems.rb +0 -45
- data/lib/smart_asset/version.rb +0 -3
@@ -0,0 +1,722 @@
|
|
1
|
+
// Underscore.js 1.1.2
|
2
|
+
// (c) 2010 Jeremy Ashkenas, DocumentCloud Inc.
|
3
|
+
// Underscore is freely distributable under the MIT license.
|
4
|
+
// Portions of Underscore are inspired or borrowed from Prototype,
|
5
|
+
// Oliver Steele's Functional, and John Resig's Micro-Templating.
|
6
|
+
// For all details and documentation:
|
7
|
+
// http://documentcloud.github.com/underscore
|
8
|
+
|
9
|
+
(function() {
|
10
|
+
|
11
|
+
// Baseline setup
|
12
|
+
// --------------
|
13
|
+
|
14
|
+
// Establish the root object, `window` in the browser, or `global` on the server.
|
15
|
+
var root = this;
|
16
|
+
|
17
|
+
// Save the previous value of the `_` variable.
|
18
|
+
var previousUnderscore = root._;
|
19
|
+
|
20
|
+
// Establish the object that gets thrown to break out of a loop iteration.
|
21
|
+
var breaker = typeof StopIteration !== 'undefined' ? StopIteration : '__break__';
|
22
|
+
|
23
|
+
// Save bytes in the minified (but not gzipped) version:
|
24
|
+
var ArrayProto = Array.prototype, ObjProto = Object.prototype;
|
25
|
+
|
26
|
+
// Create quick reference variables for speed access to core prototypes.
|
27
|
+
var slice = ArrayProto.slice,
|
28
|
+
unshift = ArrayProto.unshift,
|
29
|
+
toString = ObjProto.toString,
|
30
|
+
hasOwnProperty = ObjProto.hasOwnProperty;
|
31
|
+
|
32
|
+
// All **ECMAScript 5** native function implementations that we hope to use
|
33
|
+
// are declared here.
|
34
|
+
var
|
35
|
+
nativeForEach = ArrayProto.forEach,
|
36
|
+
nativeMap = ArrayProto.map,
|
37
|
+
nativeReduce = ArrayProto.reduce,
|
38
|
+
nativeReduceRight = ArrayProto.reduceRight,
|
39
|
+
nativeFilter = ArrayProto.filter,
|
40
|
+
nativeEvery = ArrayProto.every,
|
41
|
+
nativeSome = ArrayProto.some,
|
42
|
+
nativeIndexOf = ArrayProto.indexOf,
|
43
|
+
nativeLastIndexOf = ArrayProto.lastIndexOf,
|
44
|
+
nativeIsArray = Array.isArray,
|
45
|
+
nativeKeys = Object.keys;
|
46
|
+
|
47
|
+
// Create a safe reference to the Underscore object for use below.
|
48
|
+
var _ = function(obj) { return new wrapper(obj); };
|
49
|
+
|
50
|
+
// Export the Underscore object for **CommonJS**.
|
51
|
+
if (typeof exports !== 'undefined') exports._ = _;
|
52
|
+
|
53
|
+
// Export Underscore to the global scope.
|
54
|
+
root._ = _;
|
55
|
+
|
56
|
+
// Current version.
|
57
|
+
_.VERSION = '1.1.2';
|
58
|
+
|
59
|
+
// Collection Functions
|
60
|
+
// --------------------
|
61
|
+
|
62
|
+
// The cornerstone, an `each` implementation, aka `forEach`.
|
63
|
+
// Handles objects implementing `forEach`, arrays, and raw objects.
|
64
|
+
// Delegates to **ECMAScript 5**'s native `forEach` if available.
|
65
|
+
var each = _.each = _.forEach = function(obj, iterator, context) {
|
66
|
+
try {
|
67
|
+
if (nativeForEach && obj.forEach === nativeForEach) {
|
68
|
+
obj.forEach(iterator, context);
|
69
|
+
} else if (_.isNumber(obj.length)) {
|
70
|
+
for (var i = 0, l = obj.length; i < l; i++) iterator.call(context, obj[i], i, obj);
|
71
|
+
} else {
|
72
|
+
for (var key in obj) {
|
73
|
+
if (hasOwnProperty.call(obj, key)) iterator.call(context, obj[key], key, obj);
|
74
|
+
}
|
75
|
+
}
|
76
|
+
} catch(e) {
|
77
|
+
if (e != breaker) throw e;
|
78
|
+
}
|
79
|
+
return obj;
|
80
|
+
};
|
81
|
+
|
82
|
+
// Return the results of applying the iterator to each element.
|
83
|
+
// Delegates to **ECMAScript 5**'s native `map` if available.
|
84
|
+
_.map = function(obj, iterator, context) {
|
85
|
+
if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
|
86
|
+
var results = [];
|
87
|
+
each(obj, function(value, index, list) {
|
88
|
+
results[results.length] = iterator.call(context, value, index, list);
|
89
|
+
});
|
90
|
+
return results;
|
91
|
+
};
|
92
|
+
|
93
|
+
// **Reduce** builds up a single result from a list of values, aka `inject`,
|
94
|
+
// or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
|
95
|
+
_.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
|
96
|
+
var initial = memo !== void 0;
|
97
|
+
if (nativeReduce && obj.reduce === nativeReduce) {
|
98
|
+
if (context) iterator = _.bind(iterator, context);
|
99
|
+
return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);
|
100
|
+
}
|
101
|
+
each(obj, function(value, index, list) {
|
102
|
+
if (!initial && index === 0) {
|
103
|
+
memo = value;
|
104
|
+
} else {
|
105
|
+
memo = iterator.call(context, memo, value, index, list);
|
106
|
+
}
|
107
|
+
});
|
108
|
+
return memo;
|
109
|
+
};
|
110
|
+
|
111
|
+
// The right-associative version of reduce, also known as `foldr`.
|
112
|
+
// Delegates to **ECMAScript 5**'s native `reduceRight` if available.
|
113
|
+
_.reduceRight = _.foldr = function(obj, iterator, memo, context) {
|
114
|
+
if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
|
115
|
+
if (context) iterator = _.bind(iterator, context);
|
116
|
+
return memo !== void 0 ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
|
117
|
+
}
|
118
|
+
var reversed = (_.isArray(obj) ? obj.slice() : _.toArray(obj)).reverse();
|
119
|
+
return _.reduce(reversed, iterator, memo, context);
|
120
|
+
};
|
121
|
+
|
122
|
+
// Return the first value which passes a truth test. Aliased as `detect`.
|
123
|
+
_.find = _.detect = function(obj, iterator, context) {
|
124
|
+
var result;
|
125
|
+
each(obj, function(value, index, list) {
|
126
|
+
if (iterator.call(context, value, index, list)) {
|
127
|
+
result = value;
|
128
|
+
_.breakLoop();
|
129
|
+
}
|
130
|
+
});
|
131
|
+
return result;
|
132
|
+
};
|
133
|
+
|
134
|
+
// Return all the elements that pass a truth test.
|
135
|
+
// Delegates to **ECMAScript 5**'s native `filter` if available.
|
136
|
+
// Aliased as `select`.
|
137
|
+
_.filter = _.select = function(obj, iterator, context) {
|
138
|
+
if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
|
139
|
+
var results = [];
|
140
|
+
each(obj, function(value, index, list) {
|
141
|
+
if (iterator.call(context, value, index, list)) results[results.length] = value;
|
142
|
+
});
|
143
|
+
return results;
|
144
|
+
};
|
145
|
+
|
146
|
+
// Return all the elements for which a truth test fails.
|
147
|
+
_.reject = function(obj, iterator, context) {
|
148
|
+
var results = [];
|
149
|
+
each(obj, function(value, index, list) {
|
150
|
+
if (!iterator.call(context, value, index, list)) results[results.length] = value;
|
151
|
+
});
|
152
|
+
return results;
|
153
|
+
};
|
154
|
+
|
155
|
+
// Determine whether all of the elements match a truth test.
|
156
|
+
// Delegates to **ECMAScript 5**'s native `every` if available.
|
157
|
+
// Aliased as `all`.
|
158
|
+
_.every = _.all = function(obj, iterator, context) {
|
159
|
+
iterator = iterator || _.identity;
|
160
|
+
if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
|
161
|
+
var result = true;
|
162
|
+
each(obj, function(value, index, list) {
|
163
|
+
if (!(result = result && iterator.call(context, value, index, list))) _.breakLoop();
|
164
|
+
});
|
165
|
+
return result;
|
166
|
+
};
|
167
|
+
|
168
|
+
// Determine if at least one element in the object matches a truth test.
|
169
|
+
// Delegates to **ECMAScript 5**'s native `some` if available.
|
170
|
+
// Aliased as `any`.
|
171
|
+
_.some = _.any = function(obj, iterator, context) {
|
172
|
+
iterator = iterator || _.identity;
|
173
|
+
if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
|
174
|
+
var result = false;
|
175
|
+
each(obj, function(value, index, list) {
|
176
|
+
if (result = iterator.call(context, value, index, list)) _.breakLoop();
|
177
|
+
});
|
178
|
+
return result;
|
179
|
+
};
|
180
|
+
|
181
|
+
// Determine if a given value is included in the array or object using `===`.
|
182
|
+
// Aliased as `contains`.
|
183
|
+
_.include = _.contains = function(obj, target) {
|
184
|
+
if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
|
185
|
+
var found = false;
|
186
|
+
each(obj, function(value) {
|
187
|
+
if (found = value === target) _.breakLoop();
|
188
|
+
});
|
189
|
+
return found;
|
190
|
+
};
|
191
|
+
|
192
|
+
// Invoke a method (with arguments) on every item in a collection.
|
193
|
+
_.invoke = function(obj, method) {
|
194
|
+
var args = slice.call(arguments, 2);
|
195
|
+
return _.map(obj, function(value) {
|
196
|
+
return (method ? value[method] : value).apply(value, args);
|
197
|
+
});
|
198
|
+
};
|
199
|
+
|
200
|
+
// Convenience version of a common use case of `map`: fetching a property.
|
201
|
+
_.pluck = function(obj, key) {
|
202
|
+
return _.map(obj, function(value){ return value[key]; });
|
203
|
+
};
|
204
|
+
|
205
|
+
// Return the maximum element or (element-based computation).
|
206
|
+
_.max = function(obj, iterator, context) {
|
207
|
+
if (!iterator && _.isArray(obj)) return Math.max.apply(Math, obj);
|
208
|
+
var result = {computed : -Infinity};
|
209
|
+
each(obj, function(value, index, list) {
|
210
|
+
var computed = iterator ? iterator.call(context, value, index, list) : value;
|
211
|
+
computed >= result.computed && (result = {value : value, computed : computed});
|
212
|
+
});
|
213
|
+
return result.value;
|
214
|
+
};
|
215
|
+
|
216
|
+
// Return the minimum element (or element-based computation).
|
217
|
+
_.min = function(obj, iterator, context) {
|
218
|
+
if (!iterator && _.isArray(obj)) return Math.min.apply(Math, obj);
|
219
|
+
var result = {computed : Infinity};
|
220
|
+
each(obj, function(value, index, list) {
|
221
|
+
var computed = iterator ? iterator.call(context, value, index, list) : value;
|
222
|
+
computed < result.computed && (result = {value : value, computed : computed});
|
223
|
+
});
|
224
|
+
return result.value;
|
225
|
+
};
|
226
|
+
|
227
|
+
// Sort the object's values by a criterion produced by an iterator.
|
228
|
+
_.sortBy = function(obj, iterator, context) {
|
229
|
+
return _.pluck(_.map(obj, function(value, index, list) {
|
230
|
+
return {
|
231
|
+
value : value,
|
232
|
+
criteria : iterator.call(context, value, index, list)
|
233
|
+
};
|
234
|
+
}).sort(function(left, right) {
|
235
|
+
var a = left.criteria, b = right.criteria;
|
236
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
237
|
+
}), 'value');
|
238
|
+
};
|
239
|
+
|
240
|
+
// Use a comparator function to figure out at what index an object should
|
241
|
+
// be inserted so as to maintain order. Uses binary search.
|
242
|
+
_.sortedIndex = function(array, obj, iterator) {
|
243
|
+
iterator = iterator || _.identity;
|
244
|
+
var low = 0, high = array.length;
|
245
|
+
while (low < high) {
|
246
|
+
var mid = (low + high) >> 1;
|
247
|
+
iterator(array[mid]) < iterator(obj) ? low = mid + 1 : high = mid;
|
248
|
+
}
|
249
|
+
return low;
|
250
|
+
};
|
251
|
+
|
252
|
+
// Safely convert anything iterable into a real, live array.
|
253
|
+
_.toArray = function(iterable) {
|
254
|
+
if (!iterable) return [];
|
255
|
+
if (iterable.toArray) return iterable.toArray();
|
256
|
+
if (_.isArray(iterable)) return iterable;
|
257
|
+
if (_.isArguments(iterable)) return slice.call(iterable);
|
258
|
+
return _.values(iterable);
|
259
|
+
};
|
260
|
+
|
261
|
+
// Return the number of elements in an object.
|
262
|
+
_.size = function(obj) {
|
263
|
+
return _.toArray(obj).length;
|
264
|
+
};
|
265
|
+
|
266
|
+
// Array Functions
|
267
|
+
// ---------------
|
268
|
+
|
269
|
+
// Get the first element of an array. Passing **n** will return the first N
|
270
|
+
// values in the array. Aliased as `head`. The **guard** check allows it to work
|
271
|
+
// with `_.map`.
|
272
|
+
_.first = _.head = function(array, n, guard) {
|
273
|
+
return n && !guard ? slice.call(array, 0, n) : array[0];
|
274
|
+
};
|
275
|
+
|
276
|
+
// Returns everything but the first entry of the array. Aliased as `tail`.
|
277
|
+
// Especially useful on the arguments object. Passing an **index** will return
|
278
|
+
// the rest of the values in the array from that index onward. The **guard**
|
279
|
+
// check allows it to work with `_.map`.
|
280
|
+
_.rest = _.tail = function(array, index, guard) {
|
281
|
+
return slice.call(array, _.isUndefined(index) || guard ? 1 : index);
|
282
|
+
};
|
283
|
+
|
284
|
+
// Get the last element of an array.
|
285
|
+
_.last = function(array) {
|
286
|
+
return array[array.length - 1];
|
287
|
+
};
|
288
|
+
|
289
|
+
// Trim out all falsy values from an array.
|
290
|
+
_.compact = function(array) {
|
291
|
+
return _.filter(array, function(value){ return !!value; });
|
292
|
+
};
|
293
|
+
|
294
|
+
// Return a completely flattened version of an array.
|
295
|
+
_.flatten = function(array) {
|
296
|
+
return _.reduce(array, function(memo, value) {
|
297
|
+
if (_.isArray(value)) return memo.concat(_.flatten(value));
|
298
|
+
memo[memo.length] = value;
|
299
|
+
return memo;
|
300
|
+
}, []);
|
301
|
+
};
|
302
|
+
|
303
|
+
// Return a version of the array that does not contain the specified value(s).
|
304
|
+
_.without = function(array) {
|
305
|
+
var values = slice.call(arguments, 1);
|
306
|
+
return _.filter(array, function(value){ return !_.include(values, value); });
|
307
|
+
};
|
308
|
+
|
309
|
+
// Produce a duplicate-free version of the array. If the array has already
|
310
|
+
// been sorted, you have the option of using a faster algorithm.
|
311
|
+
// Aliased as `unique`.
|
312
|
+
_.uniq = _.unique = function(array, isSorted) {
|
313
|
+
return _.reduce(array, function(memo, el, i) {
|
314
|
+
if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) memo[memo.length] = el;
|
315
|
+
return memo;
|
316
|
+
}, []);
|
317
|
+
};
|
318
|
+
|
319
|
+
// Produce an array that contains every item shared between all the
|
320
|
+
// passed-in arrays.
|
321
|
+
_.intersect = function(array) {
|
322
|
+
var rest = slice.call(arguments, 1);
|
323
|
+
return _.filter(_.uniq(array), function(item) {
|
324
|
+
return _.every(rest, function(other) {
|
325
|
+
return _.indexOf(other, item) >= 0;
|
326
|
+
});
|
327
|
+
});
|
328
|
+
};
|
329
|
+
|
330
|
+
// Zip together multiple lists into a single array -- elements that share
|
331
|
+
// an index go together.
|
332
|
+
_.zip = function() {
|
333
|
+
var args = slice.call(arguments);
|
334
|
+
var length = _.max(_.pluck(args, 'length'));
|
335
|
+
var results = new Array(length);
|
336
|
+
for (var i = 0; i < length; i++) results[i] = _.pluck(args, "" + i);
|
337
|
+
return results;
|
338
|
+
};
|
339
|
+
|
340
|
+
// If the browser doesn't supply us with indexOf (I'm looking at you, **MSIE**),
|
341
|
+
// we need this function. Return the position of the first occurence of an
|
342
|
+
// item in an array, or -1 if the item is not included in the array.
|
343
|
+
// Delegates to **ECMAScript 5**'s native `indexOf` if available.
|
344
|
+
_.indexOf = function(array, item) {
|
345
|
+
if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
|
346
|
+
for (var i = 0, l = array.length; i < l; i++) if (array[i] === item) return i;
|
347
|
+
return -1;
|
348
|
+
};
|
349
|
+
|
350
|
+
|
351
|
+
// Delegates to **ECMAScript 5**'s native `lastIndexOf` if available.
|
352
|
+
_.lastIndexOf = function(array, item) {
|
353
|
+
if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) return array.lastIndexOf(item);
|
354
|
+
var i = array.length;
|
355
|
+
while (i--) if (array[i] === item) return i;
|
356
|
+
return -1;
|
357
|
+
};
|
358
|
+
|
359
|
+
// Generate an integer Array containing an arithmetic progression. A port of
|
360
|
+
// the native Python `range()` function. See
|
361
|
+
// [the Python documentation](http://docs.python.org/library/functions.html#range).
|
362
|
+
_.range = function(start, stop, step) {
|
363
|
+
var args = slice.call(arguments),
|
364
|
+
solo = args.length <= 1,
|
365
|
+
start = solo ? 0 : args[0],
|
366
|
+
stop = solo ? args[0] : args[1],
|
367
|
+
step = args[2] || 1,
|
368
|
+
len = Math.max(Math.ceil((stop - start) / step), 0),
|
369
|
+
idx = 0,
|
370
|
+
range = new Array(len);
|
371
|
+
while (idx < len) {
|
372
|
+
range[idx++] = start;
|
373
|
+
start += step;
|
374
|
+
}
|
375
|
+
return range;
|
376
|
+
};
|
377
|
+
|
378
|
+
// Function (ahem) Functions
|
379
|
+
// ------------------
|
380
|
+
|
381
|
+
// Create a function bound to a given object (assigning `this`, and arguments,
|
382
|
+
// optionally). Binding with arguments is also known as `curry`.
|
383
|
+
_.bind = function(func, obj) {
|
384
|
+
var args = slice.call(arguments, 2);
|
385
|
+
return function() {
|
386
|
+
return func.apply(obj || {}, args.concat(slice.call(arguments)));
|
387
|
+
};
|
388
|
+
};
|
389
|
+
|
390
|
+
// Bind all of an object's methods to that object. Useful for ensuring that
|
391
|
+
// all callbacks defined on an object belong to it.
|
392
|
+
_.bindAll = function(obj) {
|
393
|
+
var funcs = slice.call(arguments, 1);
|
394
|
+
if (funcs.length == 0) funcs = _.functions(obj);
|
395
|
+
each(funcs, function(f) { obj[f] = _.bind(obj[f], obj); });
|
396
|
+
return obj;
|
397
|
+
};
|
398
|
+
|
399
|
+
// Memoize an expensive function by storing its results.
|
400
|
+
_.memoize = function(func, hasher) {
|
401
|
+
var memo = {};
|
402
|
+
hasher = hasher || _.identity;
|
403
|
+
return function() {
|
404
|
+
var key = hasher.apply(this, arguments);
|
405
|
+
return key in memo ? memo[key] : (memo[key] = func.apply(this, arguments));
|
406
|
+
};
|
407
|
+
};
|
408
|
+
|
409
|
+
// Delays a function for the given number of milliseconds, and then calls
|
410
|
+
// it with the arguments supplied.
|
411
|
+
_.delay = function(func, wait) {
|
412
|
+
var args = slice.call(arguments, 2);
|
413
|
+
return setTimeout(function(){ return func.apply(func, args); }, wait);
|
414
|
+
};
|
415
|
+
|
416
|
+
// Defers a function, scheduling it to run after the current call stack has
|
417
|
+
// cleared.
|
418
|
+
_.defer = function(func) {
|
419
|
+
return _.delay.apply(_, [func, 1].concat(slice.call(arguments, 1)));
|
420
|
+
};
|
421
|
+
|
422
|
+
// Returns the first function passed as an argument to the second,
|
423
|
+
// allowing you to adjust arguments, run code before and after, and
|
424
|
+
// conditionally execute the original function.
|
425
|
+
_.wrap = function(func, wrapper) {
|
426
|
+
return function() {
|
427
|
+
var args = [func].concat(slice.call(arguments));
|
428
|
+
return wrapper.apply(wrapper, args);
|
429
|
+
};
|
430
|
+
};
|
431
|
+
|
432
|
+
// Returns a function that is the composition of a list of functions, each
|
433
|
+
// consuming the return value of the function that follows.
|
434
|
+
_.compose = function() {
|
435
|
+
var funcs = slice.call(arguments);
|
436
|
+
return function() {
|
437
|
+
var args = slice.call(arguments);
|
438
|
+
for (var i=funcs.length-1; i >= 0; i--) {
|
439
|
+
args = [funcs[i].apply(this, args)];
|
440
|
+
}
|
441
|
+
return args[0];
|
442
|
+
};
|
443
|
+
};
|
444
|
+
|
445
|
+
// Object Functions
|
446
|
+
// ----------------
|
447
|
+
|
448
|
+
// Retrieve the names of an object's properties.
|
449
|
+
// Delegates to **ECMAScript 5**'s native `Object.keys`
|
450
|
+
_.keys = nativeKeys || function(obj) {
|
451
|
+
if (_.isArray(obj)) return _.range(0, obj.length);
|
452
|
+
var keys = [];
|
453
|
+
for (var key in obj) if (hasOwnProperty.call(obj, key)) keys[keys.length] = key;
|
454
|
+
return keys;
|
455
|
+
};
|
456
|
+
|
457
|
+
// Retrieve the values of an object's properties.
|
458
|
+
_.values = function(obj) {
|
459
|
+
return _.map(obj, _.identity);
|
460
|
+
};
|
461
|
+
|
462
|
+
// Return a sorted list of the function names available on the object.
|
463
|
+
// Aliased as `methods`
|
464
|
+
_.functions = _.methods = function(obj) {
|
465
|
+
return _.filter(_.keys(obj), function(key){ return _.isFunction(obj[key]); }).sort();
|
466
|
+
};
|
467
|
+
|
468
|
+
// Extend a given object with all the properties in passed-in object(s).
|
469
|
+
_.extend = function(obj) {
|
470
|
+
each(slice.call(arguments, 1), function(source) {
|
471
|
+
for (var prop in source) obj[prop] = source[prop];
|
472
|
+
});
|
473
|
+
return obj;
|
474
|
+
};
|
475
|
+
|
476
|
+
// Create a (shallow-cloned) duplicate of an object.
|
477
|
+
_.clone = function(obj) {
|
478
|
+
return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
|
479
|
+
};
|
480
|
+
|
481
|
+
// Invokes interceptor with the obj, and then returns obj.
|
482
|
+
// The primary purpose of this method is to "tap into" a method chain, in
|
483
|
+
// order to perform operations on intermediate results within the chain.
|
484
|
+
_.tap = function(obj, interceptor) {
|
485
|
+
interceptor(obj);
|
486
|
+
return obj;
|
487
|
+
};
|
488
|
+
|
489
|
+
// Perform a deep comparison to check if two objects are equal.
|
490
|
+
_.isEqual = function(a, b) {
|
491
|
+
// Check object identity.
|
492
|
+
if (a === b) return true;
|
493
|
+
// Different types?
|
494
|
+
var atype = typeof(a), btype = typeof(b);
|
495
|
+
if (atype != btype) return false;
|
496
|
+
// Basic equality test (watch out for coercions).
|
497
|
+
if (a == b) return true;
|
498
|
+
// One is falsy and the other truthy.
|
499
|
+
if ((!a && b) || (a && !b)) return false;
|
500
|
+
// One of them implements an isEqual()?
|
501
|
+
if (a.isEqual) return a.isEqual(b);
|
502
|
+
// Check dates' integer values.
|
503
|
+
if (_.isDate(a) && _.isDate(b)) return a.getTime() === b.getTime();
|
504
|
+
// Both are NaN?
|
505
|
+
if (_.isNaN(a) && _.isNaN(b)) return false;
|
506
|
+
// Compare regular expressions.
|
507
|
+
if (_.isRegExp(a) && _.isRegExp(b))
|
508
|
+
return a.source === b.source &&
|
509
|
+
a.global === b.global &&
|
510
|
+
a.ignoreCase === b.ignoreCase &&
|
511
|
+
a.multiline === b.multiline;
|
512
|
+
// If a is not an object by this point, we can't handle it.
|
513
|
+
if (atype !== 'object') return false;
|
514
|
+
// Check for different array lengths before comparing contents.
|
515
|
+
if (a.length && (a.length !== b.length)) return false;
|
516
|
+
// Nothing else worked, deep compare the contents.
|
517
|
+
var aKeys = _.keys(a), bKeys = _.keys(b);
|
518
|
+
// Different object sizes?
|
519
|
+
if (aKeys.length != bKeys.length) return false;
|
520
|
+
// Recursive comparison of contents.
|
521
|
+
for (var key in a) if (!(key in b) || !_.isEqual(a[key], b[key])) return false;
|
522
|
+
return true;
|
523
|
+
};
|
524
|
+
|
525
|
+
// Is a given array or object empty?
|
526
|
+
_.isEmpty = function(obj) {
|
527
|
+
if (_.isArray(obj) || _.isString(obj)) return obj.length === 0;
|
528
|
+
for (var key in obj) if (hasOwnProperty.call(obj, key)) return false;
|
529
|
+
return true;
|
530
|
+
};
|
531
|
+
|
532
|
+
// Is a given value a DOM element?
|
533
|
+
_.isElement = function(obj) {
|
534
|
+
return !!(obj && obj.nodeType == 1);
|
535
|
+
};
|
536
|
+
|
537
|
+
// Is a given value an array?
|
538
|
+
// Delegates to ECMA5's native Array.isArray
|
539
|
+
_.isArray = nativeIsArray || function(obj) {
|
540
|
+
return !!(obj && obj.concat && obj.unshift && !obj.callee);
|
541
|
+
};
|
542
|
+
|
543
|
+
// Is a given variable an arguments object?
|
544
|
+
_.isArguments = function(obj) {
|
545
|
+
return !!(obj && obj.callee);
|
546
|
+
};
|
547
|
+
|
548
|
+
// Is a given value a function?
|
549
|
+
_.isFunction = function(obj) {
|
550
|
+
return !!(obj && obj.constructor && obj.call && obj.apply);
|
551
|
+
};
|
552
|
+
|
553
|
+
// Is a given value a string?
|
554
|
+
_.isString = function(obj) {
|
555
|
+
return !!(obj === '' || (obj && obj.charCodeAt && obj.substr));
|
556
|
+
};
|
557
|
+
|
558
|
+
// Is a given value a number?
|
559
|
+
_.isNumber = function(obj) {
|
560
|
+
return (obj === +obj) || (toString.call(obj) === '[object Number]');
|
561
|
+
};
|
562
|
+
|
563
|
+
// Is a given value a boolean?
|
564
|
+
_.isBoolean = function(obj) {
|
565
|
+
return obj === true || obj === false;
|
566
|
+
};
|
567
|
+
|
568
|
+
// Is a given value a date?
|
569
|
+
_.isDate = function(obj) {
|
570
|
+
return !!(obj && obj.getTimezoneOffset && obj.setUTCFullYear);
|
571
|
+
};
|
572
|
+
|
573
|
+
// Is the given value a regular expression?
|
574
|
+
_.isRegExp = function(obj) {
|
575
|
+
return !!(obj && obj.test && obj.exec && (obj.ignoreCase || obj.ignoreCase === false));
|
576
|
+
};
|
577
|
+
|
578
|
+
// Is the given value NaN -- this one is interesting. NaN != NaN, and
|
579
|
+
// isNaN(undefined) == true, so we make sure it's a number first.
|
580
|
+
_.isNaN = function(obj) {
|
581
|
+
return _.isNumber(obj) && isNaN(obj);
|
582
|
+
};
|
583
|
+
|
584
|
+
// Is a given value equal to null?
|
585
|
+
_.isNull = function(obj) {
|
586
|
+
return obj === null;
|
587
|
+
};
|
588
|
+
|
589
|
+
// Is a given variable undefined?
|
590
|
+
_.isUndefined = function(obj) {
|
591
|
+
return typeof obj == 'undefined';
|
592
|
+
};
|
593
|
+
|
594
|
+
// Utility Functions
|
595
|
+
// -----------------
|
596
|
+
|
597
|
+
// Run Underscore.js in *noConflict* mode, returning the `_` variable to its
|
598
|
+
// previous owner. Returns a reference to the Underscore object.
|
599
|
+
_.noConflict = function() {
|
600
|
+
root._ = previousUnderscore;
|
601
|
+
return this;
|
602
|
+
};
|
603
|
+
|
604
|
+
// Keep the identity function around for default iterators.
|
605
|
+
_.identity = function(value) {
|
606
|
+
return value;
|
607
|
+
};
|
608
|
+
|
609
|
+
// Run a function **n** times.
|
610
|
+
_.times = function (n, iterator, context) {
|
611
|
+
for (var i = 0; i < n; i++) iterator.call(context, i);
|
612
|
+
};
|
613
|
+
|
614
|
+
// Break out of the middle of an iteration.
|
615
|
+
_.breakLoop = function() {
|
616
|
+
throw breaker;
|
617
|
+
};
|
618
|
+
|
619
|
+
// Add your own custom functions to the Underscore object, ensuring that
|
620
|
+
// they're correctly added to the OOP wrapper as well.
|
621
|
+
_.mixin = function(obj) {
|
622
|
+
each(_.functions(obj), function(name){
|
623
|
+
addToWrapper(name, _[name] = obj[name]);
|
624
|
+
});
|
625
|
+
};
|
626
|
+
|
627
|
+
// Generate a unique integer id (unique within the entire client session).
|
628
|
+
// Useful for temporary DOM ids.
|
629
|
+
var idCounter = 0;
|
630
|
+
_.uniqueId = function(prefix) {
|
631
|
+
var id = idCounter++;
|
632
|
+
return prefix ? prefix + id : id;
|
633
|
+
};
|
634
|
+
|
635
|
+
// By default, Underscore uses ERB-style template delimiters, change the
|
636
|
+
// following template settings to use alternative delimiters.
|
637
|
+
_.templateSettings = {
|
638
|
+
evaluate : /<%([\s\S]+?)%>/g,
|
639
|
+
interpolate : /<%=([\s\S]+?)%>/g
|
640
|
+
};
|
641
|
+
|
642
|
+
// JavaScript micro-templating, similar to John Resig's implementation.
|
643
|
+
// Underscore templating handles arbitrary delimiters, preserves whitespace,
|
644
|
+
// and correctly escapes quotes within interpolated code.
|
645
|
+
_.template = function(str, data) {
|
646
|
+
var c = _.templateSettings;
|
647
|
+
var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
|
648
|
+
'with(obj||{}){__p.push(\'' +
|
649
|
+
str.replace(/\\/g, '\\\\')
|
650
|
+
.replace(/'/g, "\\'")
|
651
|
+
.replace(c.interpolate, function(match, code) {
|
652
|
+
return "'," + code.replace(/\\'/g, "'") + ",'";
|
653
|
+
})
|
654
|
+
.replace(c.evaluate || null, function(match, code) {
|
655
|
+
return "');" + code.replace(/\\'/g, "'")
|
656
|
+
.replace(/[\r\n\t]/g, ' ') + "__p.push('";
|
657
|
+
})
|
658
|
+
.replace(/\r/g, '\\r')
|
659
|
+
.replace(/\n/g, '\\n')
|
660
|
+
.replace(/\t/g, '\\t')
|
661
|
+
+ "');}return __p.join('');";
|
662
|
+
var func = new Function('obj', tmpl);
|
663
|
+
return data ? func(data) : func;
|
664
|
+
};
|
665
|
+
|
666
|
+
// The OOP Wrapper
|
667
|
+
// ---------------
|
668
|
+
|
669
|
+
// If Underscore is called as a function, it returns a wrapped object that
|
670
|
+
// can be used OO-style. This wrapper holds altered versions of all the
|
671
|
+
// underscore functions. Wrapped objects may be chained.
|
672
|
+
var wrapper = function(obj) { this._wrapped = obj; };
|
673
|
+
|
674
|
+
// Expose `wrapper.prototype` as `_.prototype`
|
675
|
+
_.prototype = wrapper.prototype;
|
676
|
+
|
677
|
+
// Helper function to continue chaining intermediate results.
|
678
|
+
var result = function(obj, chain) {
|
679
|
+
return chain ? _(obj).chain() : obj;
|
680
|
+
};
|
681
|
+
|
682
|
+
// A method to easily add functions to the OOP wrapper.
|
683
|
+
var addToWrapper = function(name, func) {
|
684
|
+
wrapper.prototype[name] = function() {
|
685
|
+
var args = slice.call(arguments);
|
686
|
+
unshift.call(args, this._wrapped);
|
687
|
+
return result(func.apply(_, args), this._chain);
|
688
|
+
};
|
689
|
+
};
|
690
|
+
|
691
|
+
// Add all of the Underscore functions to the wrapper object.
|
692
|
+
_.mixin(_);
|
693
|
+
|
694
|
+
// Add all mutator Array functions to the wrapper.
|
695
|
+
each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
|
696
|
+
var method = ArrayProto[name];
|
697
|
+
wrapper.prototype[name] = function() {
|
698
|
+
method.apply(this._wrapped, arguments);
|
699
|
+
return result(this._wrapped, this._chain);
|
700
|
+
};
|
701
|
+
});
|
702
|
+
|
703
|
+
// Add all accessor Array functions to the wrapper.
|
704
|
+
each(['concat', 'join', 'slice'], function(name) {
|
705
|
+
var method = ArrayProto[name];
|
706
|
+
wrapper.prototype[name] = function() {
|
707
|
+
return result(method.apply(this._wrapped, arguments), this._chain);
|
708
|
+
};
|
709
|
+
});
|
710
|
+
|
711
|
+
// Start chaining a wrapped Underscore object.
|
712
|
+
wrapper.prototype.chain = function() {
|
713
|
+
this._chain = true;
|
714
|
+
return this;
|
715
|
+
};
|
716
|
+
|
717
|
+
// Extracts the result from a wrapped and chained object.
|
718
|
+
wrapper.prototype.value = function() {
|
719
|
+
return this._wrapped;
|
720
|
+
};
|
721
|
+
|
722
|
+
})();
|