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