tolaria 1.0.0

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