tpitale-rack-oauth2-server 2.2.1

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