rack-noncache 0.0.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.pryrc +6 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +15 -0
  7. data/README.md +36 -52
  8. data/Rakefile +16 -1
  9. data/features/cache_control.feature +22 -0
  10. data/features/step_definitions/steps.rb +46 -0
  11. data/features/support/continous_integration.rb +47 -0
  12. data/features/support/coverage.rb +18 -0
  13. data/features/support/env.rb +9 -0
  14. data/features/support/helpers.rb +13 -0
  15. data/features/support/web_app/application.rb +70 -0
  16. data/features/support/web_app/config_blacklist.ru +13 -0
  17. data/features/support/web_app/config_whitelist.ru +13 -0
  18. data/features/support/web_app/public/app/collections/todos.js +17 -0
  19. data/features/support/web_app/public/app/config.js +18 -0
  20. data/features/support/web_app/public/app/main.js +21 -0
  21. data/features/support/web_app/public/app/models/todo.js +33 -0
  22. data/features/support/web_app/public/app/templates/todo-add.html +3 -0
  23. data/features/support/web_app/public/app/templates/todo-editor.html +1 -0
  24. data/features/support/web_app/public/app/templates/todo-item.html +2 -0
  25. data/features/support/web_app/public/app/templates/todo-list-empty.html +1 -0
  26. data/features/support/web_app/public/app/templates/todo-stats.html +4 -0
  27. data/features/support/web_app/public/app/views/stats.js +26 -0
  28. data/features/support/web_app/public/app/views/todo.js +55 -0
  29. data/features/support/web_app/public/app/views/todoadd.js +48 -0
  30. data/features/support/web_app/public/app/views/todoedit.js +70 -0
  31. data/features/support/web_app/public/app/views/todolist.js +69 -0
  32. data/features/support/web_app/public/assets/css/bootstrap.css +3990 -0
  33. data/features/support/web_app/public/assets/css/style.css +82 -0
  34. data/features/support/web_app/public/assets/js/libs/backbone.js +1428 -0
  35. data/features/support/web_app/public/assets/js/libs/jquery-1.7.2.js +9404 -0
  36. data/features/support/web_app/public/assets/js/libs/require.js +2053 -0
  37. data/features/support/web_app/public/assets/js/libs/underscore.js +1008 -0
  38. data/features/support/web_app/public/assets/js/plugins/text.js +288 -0
  39. data/features/support/web_app/public/test/SpecRunner.html +42 -0
  40. data/features/support/web_app/public/test/lib/jasmine-1.2.0.rc3/MIT.LICENSE +20 -0
  41. data/features/support/web_app/public/test/lib/jasmine-1.2.0.rc3/jasmine-html.js +616 -0
  42. data/features/support/web_app/public/test/lib/jasmine-1.2.0.rc3/jasmine.css +81 -0
  43. data/features/support/web_app/public/test/lib/jasmine-1.2.0.rc3/jasmine.js +2530 -0
  44. data/features/support/web_app/public/test/lib/jasmine-jquery.js +306 -0
  45. data/features/support/web_app/public/test/spec/models/todo.coffee +37 -0
  46. data/features/support/web_app/public/test/spec/models/todo.js +48 -0
  47. data/features/support/web_app/public/test/spec/spec_helper.coffee +20 -0
  48. data/features/support/web_app/public/test/spec/spec_helper.js +17 -0
  49. data/features/support/web_app/public/test/spec/views/stats.coffee +9 -0
  50. data/features/support/web_app/public/test/spec/views/stats.js +13 -0
  51. data/features/support/web_app/public/test/spec/views/todo.coffee +22 -0
  52. data/features/support/web_app/public/test/spec/views/todo.js +26 -0
  53. data/features/support/web_app/public/test/spec/views/todoadd.coffee +22 -0
  54. data/features/support/web_app/public/test/spec/views/todoadd.js +29 -0
  55. data/features/support/web_app/public/test/spec/views/todoedit.coffee +59 -0
  56. data/features/support/web_app/public/test/spec/views/todoedit.js +72 -0
  57. data/features/support/web_app/public/test/spec/views/todolist.coffee +28 -0
  58. data/features/support/web_app/public/test/spec/views/todolist.js +39 -0
  59. data/features/support/web_app/views/details.erb +14 -0
  60. data/features/support/web_app/views/index.erb +43 -0
  61. data/lib/rack/noncache.rb +5 -7
  62. data/lib/rack/noncache/engine.rb +10 -26
  63. data/lib/rack/noncache/filters.rb +75 -0
  64. data/lib/rack/noncache/version.rb +1 -1
  65. data/rack-noncache.gemspec +32 -10
  66. metadata +382 -7
@@ -0,0 +1,1008 @@
1
+ // Underscore.js 1.3.1
2
+ // (c) 2009-2012 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 returned to break out of a loop iteration.
21
+ var breaker = {};
22
+
23
+ // Save bytes in the minified (but not gzipped) version:
24
+ var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.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
+ nativeBind = FuncProto.bind;
47
+
48
+ // Create a safe reference to the Underscore object for use below.
49
+ var _ = function(obj) { return new wrapper(obj); };
50
+
51
+ // Export the Underscore object for **Node.js** and **"CommonJS"**, with
52
+ // backwards-compatibility for the old `require()` API. If we're not in
53
+ // CommonJS, add `_` to the global object via a string identifier for
54
+ // the Closure Compiler "advanced" mode. Registration as an AMD module
55
+ // via define() happens at the end of this file.
56
+ if (typeof exports !== 'undefined') {
57
+ if (typeof module !== 'undefined' && module.exports) {
58
+ exports = module.exports = _;
59
+ }
60
+ exports._ = _;
61
+ } else {
62
+ root['_'] = _;
63
+ }
64
+
65
+ // Current version.
66
+ _.VERSION = '1.3.1';
67
+
68
+ // Collection Functions
69
+ // --------------------
70
+
71
+ // The cornerstone, an `each` implementation, aka `forEach`.
72
+ // Handles objects with the built-in `forEach`, arrays, and raw objects.
73
+ // Delegates to **ECMAScript 5**'s native `forEach` if available.
74
+ var each = _.each = _.forEach = function(obj, iterator, context) {
75
+ if (obj == null) return;
76
+ if (nativeForEach && obj.forEach === nativeForEach) {
77
+ obj.forEach(iterator, context);
78
+ } else if (obj.length === +obj.length) {
79
+ for (var i = 0, l = obj.length; i < l; i++) {
80
+ if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) return;
81
+ }
82
+ } else {
83
+ for (var key in obj) {
84
+ if (_.has(obj, key)) {
85
+ if (iterator.call(context, obj[key], key, obj) === breaker) return;
86
+ }
87
+ }
88
+ }
89
+ };
90
+
91
+ // Return the results of applying the iterator to each element.
92
+ // Delegates to **ECMAScript 5**'s native `map` if available.
93
+ _.map = _.collect = function(obj, iterator, context) {
94
+ var results = [];
95
+ if (obj == null) return results;
96
+ if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
97
+ each(obj, function(value, index, list) {
98
+ results[results.length] = iterator.call(context, value, index, list);
99
+ });
100
+ if (obj.length === +obj.length) results.length = obj.length;
101
+ return results;
102
+ };
103
+
104
+ // **Reduce** builds up a single result from a list of values, aka `inject`,
105
+ // or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
106
+ _.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
107
+ var initial = arguments.length > 2;
108
+ if (obj == null) obj = [];
109
+ if (nativeReduce && obj.reduce === nativeReduce) {
110
+ if (context) iterator = _.bind(iterator, context);
111
+ return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);
112
+ }
113
+ each(obj, function(value, index, list) {
114
+ if (!initial) {
115
+ memo = value;
116
+ initial = true;
117
+ } else {
118
+ memo = iterator.call(context, memo, value, index, list);
119
+ }
120
+ });
121
+ if (!initial) throw new TypeError('Reduce of empty array with no initial value');
122
+ return memo;
123
+ };
124
+
125
+ // The right-associative version of reduce, also known as `foldr`.
126
+ // Delegates to **ECMAScript 5**'s native `reduceRight` if available.
127
+ _.reduceRight = _.foldr = function(obj, iterator, memo, context) {
128
+ var initial = arguments.length > 2;
129
+ if (obj == null) obj = [];
130
+ if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
131
+ if (context) iterator = _.bind(iterator, context);
132
+ return initial ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
133
+ }
134
+ var reversed = _.toArray(obj).reverse();
135
+ if (context && !initial) iterator = _.bind(iterator, context);
136
+ return initial ? _.reduce(reversed, iterator, memo, context) : _.reduce(reversed, iterator);
137
+ };
138
+
139
+ // Return the first value which passes a truth test. Aliased as `detect`.
140
+ _.find = _.detect = function(obj, iterator, context) {
141
+ var result;
142
+ any(obj, function(value, index, list) {
143
+ if (iterator.call(context, value, index, list)) {
144
+ result = value;
145
+ return true;
146
+ }
147
+ });
148
+ return result;
149
+ };
150
+
151
+ // Return all the elements that pass a truth test.
152
+ // Delegates to **ECMAScript 5**'s native `filter` if available.
153
+ // Aliased as `select`.
154
+ _.filter = _.select = function(obj, iterator, context) {
155
+ var results = [];
156
+ if (obj == null) return results;
157
+ if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
158
+ each(obj, function(value, index, list) {
159
+ if (iterator.call(context, value, index, list)) results[results.length] = value;
160
+ });
161
+ return results;
162
+ };
163
+
164
+ // Return all the elements for which a truth test fails.
165
+ _.reject = function(obj, iterator, context) {
166
+ var results = [];
167
+ if (obj == null) return results;
168
+ each(obj, function(value, index, list) {
169
+ if (!iterator.call(context, value, index, list)) results[results.length] = value;
170
+ });
171
+ return results;
172
+ };
173
+
174
+ // Determine whether all of the elements match a truth test.
175
+ // Delegates to **ECMAScript 5**'s native `every` if available.
176
+ // Aliased as `all`.
177
+ _.every = _.all = function(obj, iterator, context) {
178
+ var result = true;
179
+ if (obj == null) return result;
180
+ if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
181
+ each(obj, function(value, index, list) {
182
+ if (!(result = result && iterator.call(context, value, index, list))) return breaker;
183
+ });
184
+ return result;
185
+ };
186
+
187
+ // Determine if at least one element in the object matches a truth test.
188
+ // Delegates to **ECMAScript 5**'s native `some` if available.
189
+ // Aliased as `any`.
190
+ var any = _.some = _.any = function(obj, iterator, context) {
191
+ iterator || (iterator = _.identity);
192
+ var result = false;
193
+ if (obj == null) return result;
194
+ if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
195
+ each(obj, function(value, index, list) {
196
+ if (result || (result = iterator.call(context, value, index, list))) return breaker;
197
+ });
198
+ return !!result;
199
+ };
200
+
201
+ // Determine if a given value is included in the array or object using `===`.
202
+ // Aliased as `contains`.
203
+ _.include = _.contains = function(obj, target) {
204
+ var found = false;
205
+ if (obj == null) return found;
206
+ if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
207
+ found = any(obj, function(value) {
208
+ return value === target;
209
+ });
210
+ return found;
211
+ };
212
+
213
+ // Invoke a method (with arguments) on every item in a collection.
214
+ _.invoke = function(obj, method) {
215
+ var args = slice.call(arguments, 2);
216
+ return _.map(obj, function(value) {
217
+ return (_.isFunction(method) ? method || value : value[method]).apply(value, args);
218
+ });
219
+ };
220
+
221
+ // Convenience version of a common use case of `map`: fetching a property.
222
+ _.pluck = function(obj, key) {
223
+ return _.map(obj, function(value){ return value[key]; });
224
+ };
225
+
226
+ // Return the maximum element or (element-based computation).
227
+ _.max = function(obj, iterator, context) {
228
+ if (!iterator && _.isArray(obj)) return Math.max.apply(Math, obj);
229
+ if (!iterator && _.isEmpty(obj)) return -Infinity;
230
+ var result = {computed : -Infinity};
231
+ each(obj, function(value, index, list) {
232
+ var computed = iterator ? iterator.call(context, value, index, list) : value;
233
+ computed >= result.computed && (result = {value : value, computed : computed});
234
+ });
235
+ return result.value;
236
+ };
237
+
238
+ // Return the minimum element (or element-based computation).
239
+ _.min = function(obj, iterator, context) {
240
+ if (!iterator && _.isArray(obj)) return Math.min.apply(Math, obj);
241
+ if (!iterator && _.isEmpty(obj)) return Infinity;
242
+ var result = {computed : Infinity};
243
+ each(obj, function(value, index, list) {
244
+ var computed = iterator ? iterator.call(context, value, index, list) : value;
245
+ computed < result.computed && (result = {value : value, computed : computed});
246
+ });
247
+ return result.value;
248
+ };
249
+
250
+ // Shuffle an array.
251
+ _.shuffle = function(obj) {
252
+ var shuffled = [], rand;
253
+ each(obj, function(value, index, list) {
254
+ if (index == 0) {
255
+ shuffled[0] = value;
256
+ } else {
257
+ rand = Math.floor(Math.random() * (index + 1));
258
+ shuffled[index] = shuffled[rand];
259
+ shuffled[rand] = value;
260
+ }
261
+ });
262
+ return shuffled;
263
+ };
264
+
265
+ // Sort the object's values by a criterion produced by an iterator.
266
+ _.sortBy = function(obj, iterator, context) {
267
+ return _.pluck(_.map(obj, function(value, index, list) {
268
+ return {
269
+ value : value,
270
+ criteria : iterator.call(context, value, index, list)
271
+ };
272
+ }).sort(function(left, right) {
273
+ var a = left.criteria, b = right.criteria;
274
+ return a < b ? -1 : a > b ? 1 : 0;
275
+ }), 'value');
276
+ };
277
+
278
+ // Groups the object's values by a criterion. Pass either a string attribute
279
+ // to group by, or a function that returns the criterion.
280
+ _.groupBy = function(obj, val) {
281
+ var result = {};
282
+ var iterator = _.isFunction(val) ? val : function(obj) { return obj[val]; };
283
+ each(obj, function(value, index) {
284
+ var key = iterator(value, index);
285
+ (result[key] || (result[key] = [])).push(value);
286
+ });
287
+ return result;
288
+ };
289
+
290
+ // Use a comparator function to figure out at what index an object should
291
+ // be inserted so as to maintain order. Uses binary search.
292
+ _.sortedIndex = function(array, obj, iterator) {
293
+ iterator || (iterator = _.identity);
294
+ var low = 0, high = array.length;
295
+ while (low < high) {
296
+ var mid = (low + high) >> 1;
297
+ iterator(array[mid]) < iterator(obj) ? low = mid + 1 : high = mid;
298
+ }
299
+ return low;
300
+ };
301
+
302
+ // Safely convert anything iterable into a real, live array.
303
+ _.toArray = function(iterable) {
304
+ if (!iterable) return [];
305
+ if (iterable.toArray) return iterable.toArray();
306
+ if (_.isArray(iterable)) return slice.call(iterable);
307
+ if (_.isArguments(iterable)) return slice.call(iterable);
308
+ return _.values(iterable);
309
+ };
310
+
311
+ // Return the number of elements in an object.
312
+ _.size = function(obj) {
313
+ return _.toArray(obj).length;
314
+ };
315
+
316
+ // Array Functions
317
+ // ---------------
318
+
319
+ // Get the first element of an array. Passing **n** will return the first N
320
+ // values in the array. Aliased as `head`. The **guard** check allows it to work
321
+ // with `_.map`.
322
+ _.first = _.head = function(array, n, guard) {
323
+ return (n != null) && !guard ? slice.call(array, 0, n) : array[0];
324
+ };
325
+
326
+ // Returns everything but the last entry of the array. Especcialy useful on
327
+ // the arguments object. Passing **n** will return all the values in
328
+ // the array, excluding the last N. The **guard** check allows it to work with
329
+ // `_.map`.
330
+ _.initial = function(array, n, guard) {
331
+ return slice.call(array, 0, array.length - ((n == null) || guard ? 1 : n));
332
+ };
333
+
334
+ // Get the last element of an array. Passing **n** will return the last N
335
+ // values in the array. The **guard** check allows it to work with `_.map`.
336
+ _.last = function(array, n, guard) {
337
+ if ((n != null) && !guard) {
338
+ return slice.call(array, Math.max(array.length - n, 0));
339
+ } else {
340
+ return array[array.length - 1];
341
+ }
342
+ };
343
+
344
+ // Returns everything but the first entry of the array. Aliased as `tail`.
345
+ // Especially useful on the arguments object. Passing an **index** will return
346
+ // the rest of the values in the array from that index onward. The **guard**
347
+ // check allows it to work with `_.map`.
348
+ _.rest = _.tail = function(array, index, guard) {
349
+ return slice.call(array, (index == null) || guard ? 1 : index);
350
+ };
351
+
352
+ // Trim out all falsy values from an array.
353
+ _.compact = function(array) {
354
+ return _.filter(array, function(value){ return !!value; });
355
+ };
356
+
357
+ // Return a completely flattened version of an array.
358
+ _.flatten = function(array, shallow) {
359
+ return _.reduce(array, function(memo, value) {
360
+ if (_.isArray(value)) return memo.concat(shallow ? value : _.flatten(value));
361
+ memo[memo.length] = value;
362
+ return memo;
363
+ }, []);
364
+ };
365
+
366
+ // Return a version of the array that does not contain the specified value(s).
367
+ _.without = function(array) {
368
+ return _.difference(array, slice.call(arguments, 1));
369
+ };
370
+
371
+ // Produce a duplicate-free version of the array. If the array has already
372
+ // been sorted, you have the option of using a faster algorithm.
373
+ // Aliased as `unique`.
374
+ _.uniq = _.unique = function(array, isSorted, iterator) {
375
+ var initial = iterator ? _.map(array, iterator) : array;
376
+ var result = [];
377
+ _.reduce(initial, function(memo, el, i) {
378
+ if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) {
379
+ memo[memo.length] = el;
380
+ result[result.length] = array[i];
381
+ }
382
+ return memo;
383
+ }, []);
384
+ return result;
385
+ };
386
+
387
+ // Produce an array that contains the union: each distinct element from all of
388
+ // the passed-in arrays.
389
+ _.union = function() {
390
+ return _.uniq(_.flatten(arguments, true));
391
+ };
392
+
393
+ // Produce an array that contains every item shared between all the
394
+ // passed-in arrays. (Aliased as "intersect" for back-compat.)
395
+ _.intersection = _.intersect = function(array) {
396
+ var rest = slice.call(arguments, 1);
397
+ return _.filter(_.uniq(array), function(item) {
398
+ return _.every(rest, function(other) {
399
+ return _.indexOf(other, item) >= 0;
400
+ });
401
+ });
402
+ };
403
+
404
+ // Take the difference between one array and a number of other arrays.
405
+ // Only the elements present in just the first array will remain.
406
+ _.difference = function(array) {
407
+ var rest = _.flatten(slice.call(arguments, 1));
408
+ return _.filter(array, function(value){ return !_.include(rest, value); });
409
+ };
410
+
411
+ // Zip together multiple lists into a single array -- elements that share
412
+ // an index go together.
413
+ _.zip = function() {
414
+ var args = slice.call(arguments);
415
+ var length = _.max(_.pluck(args, 'length'));
416
+ var results = new Array(length);
417
+ for (var i = 0; i < length; i++) results[i] = _.pluck(args, "" + i);
418
+ return results;
419
+ };
420
+
421
+ // If the browser doesn't supply us with indexOf (I'm looking at you, **MSIE**),
422
+ // we need this function. Return the position of the first occurrence of an
423
+ // item in an array, or -1 if the item is not included in the array.
424
+ // Delegates to **ECMAScript 5**'s native `indexOf` if available.
425
+ // If the array is large and already in sort order, pass `true`
426
+ // for **isSorted** to use binary search.
427
+ _.indexOf = function(array, item, isSorted) {
428
+ if (array == null) return -1;
429
+ var i, l;
430
+ if (isSorted) {
431
+ i = _.sortedIndex(array, item);
432
+ return array[i] === item ? i : -1;
433
+ }
434
+ if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
435
+ for (i = 0, l = array.length; i < l; i++) if (i in array && array[i] === item) return i;
436
+ return -1;
437
+ };
438
+
439
+ // Delegates to **ECMAScript 5**'s native `lastIndexOf` if available.
440
+ _.lastIndexOf = function(array, item) {
441
+ if (array == null) return -1;
442
+ if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) return array.lastIndexOf(item);
443
+ var i = array.length;
444
+ while (i--) if (i in array && array[i] === item) return i;
445
+ return -1;
446
+ };
447
+
448
+ // Generate an integer Array containing an arithmetic progression. A port of
449
+ // the native Python `range()` function. See
450
+ // [the Python documentation](http://docs.python.org/library/functions.html#range).
451
+ _.range = function(start, stop, step) {
452
+ if (arguments.length <= 1) {
453
+ stop = start || 0;
454
+ start = 0;
455
+ }
456
+ step = arguments[2] || 1;
457
+
458
+ var len = Math.max(Math.ceil((stop - start) / step), 0);
459
+ var idx = 0;
460
+ var range = new Array(len);
461
+
462
+ while(idx < len) {
463
+ range[idx++] = start;
464
+ start += step;
465
+ }
466
+
467
+ return range;
468
+ };
469
+
470
+ // Function (ahem) Functions
471
+ // ------------------
472
+
473
+ // Reusable constructor function for prototype setting.
474
+ var ctor = function(){};
475
+
476
+ // Create a function bound to a given object (assigning `this`, and arguments,
477
+ // optionally). Binding with arguments is also known as `curry`.
478
+ // Delegates to **ECMAScript 5**'s native `Function.bind` if available.
479
+ // We check for `func.bind` first, to fail fast when `func` is undefined.
480
+ _.bind = function bind(func, context) {
481
+ var bound, args;
482
+ if (func.bind === nativeBind && nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
483
+ if (!_.isFunction(func)) throw new TypeError;
484
+ args = slice.call(arguments, 2);
485
+ return bound = function() {
486
+ if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments)));
487
+ ctor.prototype = func.prototype;
488
+ var self = new ctor;
489
+ var result = func.apply(self, args.concat(slice.call(arguments)));
490
+ if (Object(result) === result) return result;
491
+ return self;
492
+ };
493
+ };
494
+
495
+ // Bind all of an object's methods to that object. Useful for ensuring that
496
+ // all callbacks defined on an object belong to it.
497
+ _.bindAll = function(obj) {
498
+ var funcs = slice.call(arguments, 1);
499
+ if (funcs.length == 0) funcs = _.functions(obj);
500
+ each(funcs, function(f) { obj[f] = _.bind(obj[f], obj); });
501
+ return obj;
502
+ };
503
+
504
+ // Memoize an expensive function by storing its results.
505
+ _.memoize = function(func, hasher) {
506
+ var memo = {};
507
+ hasher || (hasher = _.identity);
508
+ return function() {
509
+ var key = hasher.apply(this, arguments);
510
+ return _.has(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments));
511
+ };
512
+ };
513
+
514
+ // Delays a function for the given number of milliseconds, and then calls
515
+ // it with the arguments supplied.
516
+ _.delay = function(func, wait) {
517
+ var args = slice.call(arguments, 2);
518
+ return setTimeout(function(){ return func.apply(func, args); }, wait);
519
+ };
520
+
521
+ // Defers a function, scheduling it to run after the current call stack has
522
+ // cleared.
523
+ _.defer = function(func) {
524
+ return _.delay.apply(_, [func, 1].concat(slice.call(arguments, 1)));
525
+ };
526
+
527
+ // Returns a function, that, when invoked, will only be triggered at most once
528
+ // during a given window of time.
529
+ _.throttle = function(func, wait) {
530
+ var context, args, timeout, throttling, more;
531
+ var whenDone = _.debounce(function(){ more = throttling = false; }, wait);
532
+ return function() {
533
+ context = this; args = arguments;
534
+ var later = function() {
535
+ timeout = null;
536
+ if (more) func.apply(context, args);
537
+ whenDone();
538
+ };
539
+ if (!timeout) timeout = setTimeout(later, wait);
540
+ if (throttling) {
541
+ more = true;
542
+ } else {
543
+ func.apply(context, args);
544
+ }
545
+ whenDone();
546
+ throttling = true;
547
+ };
548
+ };
549
+
550
+ // Returns a function, that, as long as it continues to be invoked, will not
551
+ // be triggered. The function will be called after it stops being called for
552
+ // N milliseconds.
553
+ _.debounce = function(func, wait) {
554
+ var timeout;
555
+ return function() {
556
+ var context = this, args = arguments;
557
+ var later = function() {
558
+ timeout = null;
559
+ func.apply(context, args);
560
+ };
561
+ clearTimeout(timeout);
562
+ timeout = setTimeout(later, wait);
563
+ };
564
+ };
565
+
566
+ // Returns a function that will be executed at most one time, no matter how
567
+ // often you call it. Useful for lazy initialization.
568
+ _.once = function(func) {
569
+ var ran = false, memo;
570
+ return function() {
571
+ if (ran) return memo;
572
+ ran = true;
573
+ return memo = func.apply(this, arguments);
574
+ };
575
+ };
576
+
577
+ // Returns the first function passed as an argument to the second,
578
+ // allowing you to adjust arguments, run code before and after, and
579
+ // conditionally execute the original function.
580
+ _.wrap = function(func, wrapper) {
581
+ return function() {
582
+ var args = [func].concat(slice.call(arguments, 0));
583
+ return wrapper.apply(this, args);
584
+ };
585
+ };
586
+
587
+ // Returns a function that is the composition of a list of functions, each
588
+ // consuming the return value of the function that follows.
589
+ _.compose = function() {
590
+ var funcs = arguments;
591
+ return function() {
592
+ var args = arguments;
593
+ for (var i = funcs.length - 1; i >= 0; i--) {
594
+ args = [funcs[i].apply(this, args)];
595
+ }
596
+ return args[0];
597
+ };
598
+ };
599
+
600
+ // Returns a function that will only be executed after being called N times.
601
+ _.after = function(times, func) {
602
+ if (times <= 0) return func();
603
+ return function() {
604
+ if (--times < 1) { return func.apply(this, arguments); }
605
+ };
606
+ };
607
+
608
+ // Object Functions
609
+ // ----------------
610
+
611
+ // Retrieve the names of an object's properties.
612
+ // Delegates to **ECMAScript 5**'s native `Object.keys`
613
+ _.keys = nativeKeys || function(obj) {
614
+ if (obj !== Object(obj)) throw new TypeError('Invalid object');
615
+ var keys = [];
616
+ for (var key in obj) if (_.has(obj, key)) keys[keys.length] = key;
617
+ return keys;
618
+ };
619
+
620
+ // Retrieve the values of an object's properties.
621
+ _.values = function(obj) {
622
+ return _.map(obj, _.identity);
623
+ };
624
+
625
+ // Return a sorted list of the function names available on the object.
626
+ // Aliased as `methods`
627
+ _.functions = _.methods = function(obj) {
628
+ var names = [];
629
+ for (var key in obj) {
630
+ if (_.isFunction(obj[key])) names.push(key);
631
+ }
632
+ return names.sort();
633
+ };
634
+
635
+ // Extend a given object with all the properties in passed-in object(s).
636
+ _.extend = function(obj) {
637
+ each(slice.call(arguments, 1), function(source) {
638
+ for (var prop in source) {
639
+ obj[prop] = source[prop];
640
+ }
641
+ });
642
+ return obj;
643
+ };
644
+
645
+ // Fill in a given object with default properties.
646
+ _.defaults = function(obj) {
647
+ each(slice.call(arguments, 1), function(source) {
648
+ for (var prop in source) {
649
+ if (obj[prop] == null) obj[prop] = source[prop];
650
+ }
651
+ });
652
+ return obj;
653
+ };
654
+
655
+ // Create a (shallow-cloned) duplicate of an object.
656
+ _.clone = function(obj) {
657
+ if (!_.isObject(obj)) return obj;
658
+ return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
659
+ };
660
+
661
+ // Invokes interceptor with the obj, and then returns obj.
662
+ // The primary purpose of this method is to "tap into" a method chain, in
663
+ // order to perform operations on intermediate results within the chain.
664
+ _.tap = function(obj, interceptor) {
665
+ interceptor(obj);
666
+ return obj;
667
+ };
668
+
669
+ // Internal recursive comparison function.
670
+ function eq(a, b, stack) {
671
+ // Identical objects are equal. `0 === -0`, but they aren't identical.
672
+ // See the Harmony `egal` proposal: http://wiki.ecmascript.org/doku.php?id=harmony:egal.
673
+ if (a === b) return a !== 0 || 1 / a == 1 / b;
674
+ // A strict comparison is necessary because `null == undefined`.
675
+ if (a == null || b == null) return a === b;
676
+ // Unwrap any wrapped objects.
677
+ if (a._chain) a = a._wrapped;
678
+ if (b._chain) b = b._wrapped;
679
+ // Invoke a custom `isEqual` method if one is provided.
680
+ if (a.isEqual && _.isFunction(a.isEqual)) return a.isEqual(b);
681
+ if (b.isEqual && _.isFunction(b.isEqual)) return b.isEqual(a);
682
+ // Compare `[[Class]]` names.
683
+ var className = toString.call(a);
684
+ if (className != toString.call(b)) return false;
685
+ switch (className) {
686
+ // Strings, numbers, dates, and booleans are compared by value.
687
+ case '[object String]':
688
+ // Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
689
+ // equivalent to `new String("5")`.
690
+ return a == String(b);
691
+ case '[object Number]':
692
+ // `NaN`s are equivalent, but non-reflexive. An `egal` comparison is performed for
693
+ // other numeric values.
694
+ return a != +a ? b != +b : (a == 0 ? 1 / a == 1 / b : a == +b);
695
+ case '[object Date]':
696
+ case '[object Boolean]':
697
+ // Coerce dates and booleans to numeric primitive values. Dates are compared by their
698
+ // millisecond representations. Note that invalid dates with millisecond representations
699
+ // of `NaN` are not equivalent.
700
+ return +a == +b;
701
+ // RegExps are compared by their source patterns and flags.
702
+ case '[object RegExp]':
703
+ return a.source == b.source &&
704
+ a.global == b.global &&
705
+ a.multiline == b.multiline &&
706
+ a.ignoreCase == b.ignoreCase;
707
+ }
708
+ if (typeof a != 'object' || typeof b != 'object') return false;
709
+ // Assume equality for cyclic structures. The algorithm for detecting cyclic
710
+ // structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
711
+ var length = stack.length;
712
+ while (length--) {
713
+ // Linear search. Performance is inversely proportional to the number of
714
+ // unique nested structures.
715
+ if (stack[length] == a) return true;
716
+ }
717
+ // Add the first object to the stack of traversed objects.
718
+ stack.push(a);
719
+ var size = 0, result = true;
720
+ // Recursively compare objects and arrays.
721
+ if (className == '[object Array]') {
722
+ // Compare array lengths to determine if a deep comparison is necessary.
723
+ size = a.length;
724
+ result = size == b.length;
725
+ if (result) {
726
+ // Deep compare the contents, ignoring non-numeric properties.
727
+ while (size--) {
728
+ // Ensure commutative equality for sparse arrays.
729
+ if (!(result = size in a == size in b && eq(a[size], b[size], stack))) break;
730
+ }
731
+ }
732
+ } else {
733
+ // Objects with different constructors are not equivalent.
734
+ if ('constructor' in a != 'constructor' in b || a.constructor != b.constructor) return false;
735
+ // Deep compare objects.
736
+ for (var key in a) {
737
+ if (_.has(a, key)) {
738
+ // Count the expected number of properties.
739
+ size++;
740
+ // Deep compare each member.
741
+ if (!(result = _.has(b, key) && eq(a[key], b[key], stack))) break;
742
+ }
743
+ }
744
+ // Ensure that both objects contain the same number of properties.
745
+ if (result) {
746
+ for (key in b) {
747
+ if (_.has(b, key) && !(size--)) break;
748
+ }
749
+ result = !size;
750
+ }
751
+ }
752
+ // Remove the first object from the stack of traversed objects.
753
+ stack.pop();
754
+ return result;
755
+ }
756
+
757
+ // Perform a deep comparison to check if two objects are equal.
758
+ _.isEqual = function(a, b) {
759
+ return eq(a, b, []);
760
+ };
761
+
762
+ // Is a given array, string, or object empty?
763
+ // An "empty" object has no enumerable own-properties.
764
+ _.isEmpty = function(obj) {
765
+ if (_.isArray(obj) || _.isString(obj)) return obj.length === 0;
766
+ for (var key in obj) if (_.has(obj, key)) return false;
767
+ return true;
768
+ };
769
+
770
+ // Is a given value a DOM element?
771
+ _.isElement = function(obj) {
772
+ return !!(obj && obj.nodeType == 1);
773
+ };
774
+
775
+ // Is a given value an array?
776
+ // Delegates to ECMA5's native Array.isArray
777
+ _.isArray = nativeIsArray || function(obj) {
778
+ return toString.call(obj) == '[object Array]';
779
+ };
780
+
781
+ // Is a given variable an object?
782
+ _.isObject = function(obj) {
783
+ return obj === Object(obj);
784
+ };
785
+
786
+ // Is a given variable an arguments object?
787
+ _.isArguments = function(obj) {
788
+ return toString.call(obj) == '[object Arguments]';
789
+ };
790
+ if (!_.isArguments(arguments)) {
791
+ _.isArguments = function(obj) {
792
+ return !!(obj && _.has(obj, 'callee'));
793
+ };
794
+ }
795
+
796
+ // Is a given value a function?
797
+ _.isFunction = function(obj) {
798
+ return toString.call(obj) == '[object Function]';
799
+ };
800
+
801
+ // Is a given value a string?
802
+ _.isString = function(obj) {
803
+ return toString.call(obj) == '[object String]';
804
+ };
805
+
806
+ // Is a given value a number?
807
+ _.isNumber = function(obj) {
808
+ return toString.call(obj) == '[object Number]';
809
+ };
810
+
811
+ // Is the given value `NaN`?
812
+ _.isNaN = function(obj) {
813
+ // `NaN` is the only value for which `===` is not reflexive.
814
+ return obj !== obj;
815
+ };
816
+
817
+ // Is a given value a boolean?
818
+ _.isBoolean = function(obj) {
819
+ return obj === true || obj === false || toString.call(obj) == '[object Boolean]';
820
+ };
821
+
822
+ // Is a given value a date?
823
+ _.isDate = function(obj) {
824
+ return toString.call(obj) == '[object Date]';
825
+ };
826
+
827
+ // Is the given value a regular expression?
828
+ _.isRegExp = function(obj) {
829
+ return toString.call(obj) == '[object RegExp]';
830
+ };
831
+
832
+ // Is a given value equal to null?
833
+ _.isNull = function(obj) {
834
+ return obj === null;
835
+ };
836
+
837
+ // Is a given variable undefined?
838
+ _.isUndefined = function(obj) {
839
+ return obj === void 0;
840
+ };
841
+
842
+ // Has own property?
843
+ _.has = function(obj, key) {
844
+ return hasOwnProperty.call(obj, key);
845
+ };
846
+
847
+ // Utility Functions
848
+ // -----------------
849
+
850
+ // Run Underscore.js in *noConflict* mode, returning the `_` variable to its
851
+ // previous owner. Returns a reference to the Underscore object.
852
+ _.noConflict = function() {
853
+ root._ = previousUnderscore;
854
+ return this;
855
+ };
856
+
857
+ // Keep the identity function around for default iterators.
858
+ _.identity = function(value) {
859
+ return value;
860
+ };
861
+
862
+ // Run a function **n** times.
863
+ _.times = function (n, iterator, context) {
864
+ for (var i = 0; i < n; i++) iterator.call(context, i);
865
+ };
866
+
867
+ // Escape a string for HTML interpolation.
868
+ _.escape = function(string) {
869
+ return (''+string).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#x27;').replace(/\//g,'&#x2F;');
870
+ };
871
+
872
+ // Add your own custom functions to the Underscore object, ensuring that
873
+ // they're correctly added to the OOP wrapper as well.
874
+ _.mixin = function(obj) {
875
+ each(_.functions(obj), function(name){
876
+ addToWrapper(name, _[name] = obj[name]);
877
+ });
878
+ };
879
+
880
+ // Generate a unique integer id (unique within the entire client session).
881
+ // Useful for temporary DOM ids.
882
+ var idCounter = 0;
883
+ _.uniqueId = function(prefix) {
884
+ var id = idCounter++;
885
+ return prefix ? prefix + id : id;
886
+ };
887
+
888
+ // By default, Underscore uses ERB-style template delimiters, change the
889
+ // following template settings to use alternative delimiters.
890
+ _.templateSettings = {
891
+ evaluate : /<%([\s\S]+?)%>/g,
892
+ interpolate : /<%=([\s\S]+?)%>/g,
893
+ escape : /<%-([\s\S]+?)%>/g
894
+ };
895
+
896
+ // When customizing `templateSettings`, if you don't want to define an
897
+ // interpolation, evaluation or escaping regex, we need one that is
898
+ // guaranteed not to match.
899
+ var noMatch = /.^/;
900
+
901
+ // Within an interpolation, evaluation, or escaping, remove HTML escaping
902
+ // that had been previously added.
903
+ var unescape = function(code) {
904
+ return code.replace(/\\\\/g, '\\').replace(/\\'/g, "'");
905
+ };
906
+
907
+ // JavaScript micro-templating, similar to John Resig's implementation.
908
+ // Underscore templating handles arbitrary delimiters, preserves whitespace,
909
+ // and correctly escapes quotes within interpolated code.
910
+ _.template = function(str, data) {
911
+ var c = _.templateSettings;
912
+ var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
913
+ 'with(obj||{}){__p.push(\'' +
914
+ str.replace(/\\/g, '\\\\')
915
+ .replace(/'/g, "\\'")
916
+ .replace(c.escape || noMatch, function(match, code) {
917
+ return "',_.escape(" + unescape(code) + "),'";
918
+ })
919
+ .replace(c.interpolate || noMatch, function(match, code) {
920
+ return "'," + unescape(code) + ",'";
921
+ })
922
+ .replace(c.evaluate || noMatch, function(match, code) {
923
+ return "');" + unescape(code).replace(/[\r\n\t]/g, ' ') + ";__p.push('";
924
+ })
925
+ .replace(/\r/g, '\\r')
926
+ .replace(/\n/g, '\\n')
927
+ .replace(/\t/g, '\\t')
928
+ + "');}return __p.join('');";
929
+ var func = new Function('obj', '_', tmpl);
930
+ if (data) return func(data, _);
931
+ return function(data) {
932
+ return func.call(this, data, _);
933
+ };
934
+ };
935
+
936
+ // Add a "chain" function, which will delegate to the wrapper.
937
+ _.chain = function(obj) {
938
+ return _(obj).chain();
939
+ };
940
+
941
+ // The OOP Wrapper
942
+ // ---------------
943
+
944
+ // If Underscore is called as a function, it returns a wrapped object that
945
+ // can be used OO-style. This wrapper holds altered versions of all the
946
+ // underscore functions. Wrapped objects may be chained.
947
+ var wrapper = function(obj) { this._wrapped = obj; };
948
+
949
+ // Expose `wrapper.prototype` as `_.prototype`
950
+ _.prototype = wrapper.prototype;
951
+
952
+ // Helper function to continue chaining intermediate results.
953
+ var result = function(obj, chain) {
954
+ return chain ? _(obj).chain() : obj;
955
+ };
956
+
957
+ // A method to easily add functions to the OOP wrapper.
958
+ var addToWrapper = function(name, func) {
959
+ wrapper.prototype[name] = function() {
960
+ var args = slice.call(arguments);
961
+ unshift.call(args, this._wrapped);
962
+ return result(func.apply(_, args), this._chain);
963
+ };
964
+ };
965
+
966
+ // Add all of the Underscore functions to the wrapper object.
967
+ _.mixin(_);
968
+
969
+ // Add all mutator Array functions to the wrapper.
970
+ each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
971
+ var method = ArrayProto[name];
972
+ wrapper.prototype[name] = function() {
973
+ var wrapped = this._wrapped;
974
+ method.apply(wrapped, arguments);
975
+ var length = wrapped.length;
976
+ if ((name == 'shift' || name == 'splice') && length === 0) delete wrapped[0];
977
+ return result(wrapped, this._chain);
978
+ };
979
+ });
980
+
981
+ // Add all accessor Array functions to the wrapper.
982
+ each(['concat', 'join', 'slice'], function(name) {
983
+ var method = ArrayProto[name];
984
+ wrapper.prototype[name] = function() {
985
+ return result(method.apply(this._wrapped, arguments), this._chain);
986
+ };
987
+ });
988
+
989
+ // Start chaining a wrapped Underscore object.
990
+ wrapper.prototype.chain = function() {
991
+ this._chain = true;
992
+ return this;
993
+ };
994
+
995
+ // Extracts the result from a wrapped and chained object.
996
+ wrapper.prototype.value = function() {
997
+ return this._wrapped;
998
+ };
999
+
1000
+ // AMD define happens at the end for compatibility with AMD loaders
1001
+ // that don't enforce next-turn semantics on modules.
1002
+ if (typeof define === 'function' && define.amd) {
1003
+ define('underscore', function() {
1004
+ return _;
1005
+ });
1006
+ }
1007
+
1008
+ }).call(this);