lady_josephine 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +32 -0
- data/app/assets/fonts/lady_josephine/icons/icons.eot +0 -0
- data/app/assets/fonts/lady_josephine/icons/icons.svg +215 -0
- data/app/assets/fonts/lady_josephine/icons/icons.ttf +0 -0
- data/app/assets/fonts/lady_josephine/icons/icons.woff +0 -0
- data/app/assets/javascripts/lady_josephine/blocks/facebook.js +25 -0
- data/app/assets/javascripts/lady_josephine/blocks/instagram.js +22 -0
- data/app/assets/javascripts/lady_josephine/blocks/soundcloud.js +24 -0
- data/app/assets/javascripts/lady_josephine/blocks/twitter.js +25 -0
- data/app/assets/javascripts/lady_josephine/blocks/vimeo.js +55 -0
- data/app/assets/javascripts/lady_josephine/blocks/youtube.js +64 -0
- data/app/assets/javascripts/lady_josephine/editor.js +40 -0
- data/app/assets/javascripts/lady_josephine/lady_josephine.js +4 -0
- data/app/assets/javascripts/lady_josephine/lady_josephine_view.js +6 -0
- data/app/assets/stylesheets/lady_josephine/content.sass +33 -0
- data/app/assets/stylesheets/lady_josephine/icons.sass +175 -0
- data/app/assets/stylesheets/lady_josephine/lady_josephine.sass +6 -0
- data/app/assets/stylesheets/lady_josephine/sir_trevor.sass +619 -0
- data/app/assets/stylesheets/lady_josephine/vars.sass +36 -0
- data/app/controllers/lady_josephine/application_controller.rb +4 -0
- data/app/controllers/lady_josephine/editor_controller.rb +40 -0
- data/app/controllers/lady_josephine/images_controller.rb +26 -0
- data/app/helpers/lady_josephine/helper.rb +25 -0
- data/app/helpers/lady_josephine/sir_trevor_helper.rb +172 -0
- data/app/models/lady_josephine/concerns/model.rb +29 -0
- data/app/models/lady_josephine/concerns/pingable.rb +28 -0
- data/app/models/lady_josephine/concerns/server_uuid.rb +24 -0
- data/app/models/lady_josephine/concerns/sir_trevor_images.rb +38 -0
- data/app/models/lady_josephine/image.rb +41 -0
- data/app/uploader/lady_josephine/base_uploader.rb +47 -0
- data/app/uploader/lady_josephine/image_uploader.rb +149 -0
- data/app/views/lady_josephine/blocks/_audio_block.haml +2 -0
- data/app/views/lady_josephine/blocks/_definition_block.haml +4 -0
- data/app/views/lady_josephine/blocks/_divider_block.haml +2 -0
- data/app/views/lady_josephine/blocks/_extended_image_block.haml +63 -0
- data/app/views/lady_josephine/blocks/_extended_quote_block.haml +5 -0
- data/app/views/lady_josephine/blocks/_extended_tweet_block.haml +2 -0
- data/app/views/lady_josephine/blocks/_facebook_post_block.haml +2 -0
- data/app/views/lady_josephine/blocks/_heading_block.haml +2 -0
- data/app/views/lady_josephine/blocks/_html_block.haml +1 -0
- data/app/views/lady_josephine/blocks/_instagram_block.haml +2 -0
- data/app/views/lady_josephine/blocks/_list_block.haml +3 -0
- data/app/views/lady_josephine/blocks/_quote_block.haml +5 -0
- data/app/views/lady_josephine/blocks/_text_block.html.haml +2 -0
- data/app/views/lady_josephine/blocks/_video_block.haml +9 -0
- data/config/routes.rb +9 -0
- data/db/migrate/20160222151500_lady_josephine_image.rb +18 -0
- data/lib/generators/lady_josephine_migration/USAGE +9 -0
- data/lib/generators/lady_josephine_migration/lady_josephine_migration_generator.rb +17 -0
- data/lib/generators/lady_josephine_migration/templates/migration.rb +9 -0
- data/lib/lady_josephine.rb +18 -0
- data/lib/lady_josephine/engine.rb +18 -0
- data/lib/lady_josephine/redcarpet/custom_markdown_formatter.rb +63 -0
- data/lib/lady_josephine/renderer/sir_trevor_renderer.rb +16 -0
- data/lib/lady_josephine/version.rb +3 -0
- data/lib/lady_josephine/whitelist.rb +26 -0
- data/lib/tasks/lady_josephine_tasks.rake +4 -0
- data/spec/controllers/lady_josephine/editor_controller_spec.rb +58 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +29 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +26 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +41 -0
- data/spec/dummy/config/environments/production.rb +79 -0
- data/spec/dummy/config/environments/test.rb +42 -0
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +4 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/schema.rb +16 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/bla +0 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/spec_helper.rb +17 -0
- data/vendor/assets/components/Eventable/README.md +12 -0
- data/vendor/assets/components/Eventable/bower.json +10 -0
- data/vendor/assets/components/Eventable/component.json +11 -0
- data/vendor/assets/components/Eventable/eventable.js +207 -0
- data/vendor/assets/components/Eventable/package.json +15 -0
- data/vendor/assets/components/jquery-autosize/bower.json +30 -0
- data/vendor/assets/components/jquery-autosize/demo.html +36 -0
- data/vendor/assets/components/jquery-autosize/jquery.autosize.js +273 -0
- data/vendor/assets/components/jquery-autosize/jquery.autosize.min.js +6 -0
- data/vendor/assets/components/jquery-autosize/package.json +32 -0
- data/vendor/assets/components/jquery-autosize/readme.md +216 -0
- data/vendor/assets/components/jquery/AUTHORS.txt +163 -0
- data/vendor/assets/components/jquery/CONTRIBUTING.md +216 -0
- data/vendor/assets/components/jquery/Gruntfile.js +496 -0
- data/vendor/assets/components/jquery/MIT-LICENSE.txt +21 -0
- data/vendor/assets/components/jquery/README.md +415 -0
- data/vendor/assets/components/jquery/build/release-notes.js +59 -0
- data/vendor/assets/components/jquery/build/release.js +246 -0
- data/vendor/assets/components/jquery/component.json +13 -0
- data/vendor/assets/components/jquery/composer.json +35 -0
- data/vendor/assets/components/jquery/jquery-migrate.js +496 -0
- data/vendor/assets/components/jquery/jquery-migrate.min.js +3 -0
- data/vendor/assets/components/jquery/jquery.js +9597 -0
- data/vendor/assets/components/jquery/jquery.min.js +5 -0
- data/vendor/assets/components/jquery/jquery.min.map +1 -0
- data/vendor/assets/components/jquery/package.json +39 -0
- data/vendor/assets/components/jquery/speed/benchmark.js +15 -0
- data/vendor/assets/components/jquery/speed/benchmarker.css +65 -0
- data/vendor/assets/components/jquery/speed/benchmarker.js +181 -0
- data/vendor/assets/components/jquery/speed/closest.html +39 -0
- data/vendor/assets/components/jquery/speed/css.html +82 -0
- data/vendor/assets/components/jquery/speed/event.html +58 -0
- data/vendor/assets/components/jquery/speed/filter.html +183 -0
- data/vendor/assets/components/jquery/speed/find.html +179 -0
- data/vendor/assets/components/jquery/speed/index.html +72 -0
- data/vendor/assets/components/jquery/speed/jquery-basis.js +6238 -0
- data/vendor/assets/components/jquery/speed/slice.vs.concat.html +47 -0
- data/vendor/assets/components/jquery/src/ajax.js +851 -0
- data/vendor/assets/components/jquery/src/ajax/jsonp.js +80 -0
- data/vendor/assets/components/jquery/src/ajax/script.js +86 -0
- data/vendor/assets/components/jquery/src/ajax/xhr.js +207 -0
- data/vendor/assets/components/jquery/src/attributes.js +661 -0
- data/vendor/assets/components/jquery/src/callbacks.js +196 -0
- data/vendor/assets/components/jquery/src/core.js +949 -0
- data/vendor/assets/components/jquery/src/css.js +679 -0
- data/vendor/assets/components/jquery/src/data.js +340 -0
- data/vendor/assets/components/jquery/src/deferred.js +141 -0
- data/vendor/assets/components/jquery/src/deprecated.js +4 -0
- data/vendor/assets/components/jquery/src/dimensions.js +41 -0
- data/vendor/assets/components/jquery/src/effects.js +730 -0
- data/vendor/assets/components/jquery/src/event-alias.js +15 -0
- data/vendor/assets/components/jquery/src/event.js +991 -0
- data/vendor/assets/components/jquery/src/exports.js +18 -0
- data/vendor/assets/components/jquery/src/intro.js +20 -0
- data/vendor/assets/components/jquery/src/manipulation.js +796 -0
- data/vendor/assets/components/jquery/src/offset.js +169 -0
- data/vendor/assets/components/jquery/src/outro.js +2 -0
- data/vendor/assets/components/jquery/src/queue.js +147 -0
- data/vendor/assets/components/jquery/src/serialize.js +99 -0
- data/vendor/assets/components/jquery/src/sizzle-jquery.js +9 -0
- data/vendor/assets/components/jquery/src/support.js +241 -0
- data/vendor/assets/components/jquery/src/traversing.js +275 -0
- data/vendor/assets/components/jquery/test/csp.php +16 -0
- data/vendor/assets/components/jquery/test/data/1x1.jpg +0 -0
- data/vendor/assets/components/jquery/test/data/ajax/unreleasedXHR.html +25 -0
- data/vendor/assets/components/jquery/test/data/atom+xml.php +4 -0
- data/vendor/assets/components/jquery/test/data/badcall.js +1 -0
- data/vendor/assets/components/jquery/test/data/badjson.js +1 -0
- data/vendor/assets/components/jquery/test/data/cleanScript.html +10 -0
- data/vendor/assets/components/jquery/test/data/core/cc_on.html +22 -0
- data/vendor/assets/components/jquery/test/data/dashboard.xml +11 -0
- data/vendor/assets/components/jquery/test/data/dimensions/documentLarge.html +17 -0
- data/vendor/assets/components/jquery/test/data/dimensions/documentSmall.html +21 -0
- data/vendor/assets/components/jquery/test/data/echoData.php +1 -0
- data/vendor/assets/components/jquery/test/data/echoQuery.php +1 -0
- data/vendor/assets/components/jquery/test/data/errorWithText.php +5 -0
- data/vendor/assets/components/jquery/test/data/etag.php +21 -0
- data/vendor/assets/components/jquery/test/data/evalScript.php +1 -0
- data/vendor/assets/components/jquery/test/data/event/longLoadScript.php +4 -0
- data/vendor/assets/components/jquery/test/data/event/promiseReady.html +17 -0
- data/vendor/assets/components/jquery/test/data/event/syncReady.html +23 -0
- data/vendor/assets/components/jquery/test/data/headers.php +18 -0
- data/vendor/assets/components/jquery/test/data/if_modified_since.php +20 -0
- data/vendor/assets/components/jquery/test/data/iframe.html +8 -0
- data/vendor/assets/components/jquery/test/data/jquery-1.8.2.ajax_xhr.min.js +2 -0
- data/vendor/assets/components/jquery/test/data/json.php +13 -0
- data/vendor/assets/components/jquery/test/data/json_obj.js +1 -0
- data/vendor/assets/components/jquery/test/data/jsonp.php +14 -0
- data/vendor/assets/components/jquery/test/data/manipulation/iframe-denied.html +36 -0
- data/vendor/assets/components/jquery/test/data/name.html +1 -0
- data/vendor/assets/components/jquery/test/data/name.php +24 -0
- data/vendor/assets/components/jquery/test/data/nocontent.php +5 -0
- data/vendor/assets/components/jquery/test/data/offset/absolute.html +41 -0
- data/vendor/assets/components/jquery/test/data/offset/body.html +26 -0
- data/vendor/assets/components/jquery/test/data/offset/fixed.html +34 -0
- data/vendor/assets/components/jquery/test/data/offset/relative.html +31 -0
- data/vendor/assets/components/jquery/test/data/offset/scroll.html +39 -0
- data/vendor/assets/components/jquery/test/data/offset/static.html +31 -0
- data/vendor/assets/components/jquery/test/data/offset/table.html +43 -0
- data/vendor/assets/components/jquery/test/data/params_html.php +12 -0
- data/vendor/assets/components/jquery/test/data/readywaitasset.js +1 -0
- data/vendor/assets/components/jquery/test/data/readywaitloader.js +25 -0
- data/vendor/assets/components/jquery/test/data/script.php +11 -0
- data/vendor/assets/components/jquery/test/data/selector/html5_selector.html +114 -0
- data/vendor/assets/components/jquery/test/data/selector/sizzle_cache.html +21 -0
- data/vendor/assets/components/jquery/test/data/statusText.php +5 -0
- data/vendor/assets/components/jquery/test/data/support/bodyBackground.html +28 -0
- data/vendor/assets/components/jquery/test/data/support/shrinkWrapBlocks.html +23 -0
- data/vendor/assets/components/jquery/test/data/support/testElementCrash.html +17 -0
- data/vendor/assets/components/jquery/test/data/test.html +7 -0
- data/vendor/assets/components/jquery/test/data/test.js +3 -0
- data/vendor/assets/components/jquery/test/data/test.php +7 -0
- data/vendor/assets/components/jquery/test/data/test2.html +5 -0
- data/vendor/assets/components/jquery/test/data/test3.html +3 -0
- data/vendor/assets/components/jquery/test/data/testinit.js +269 -0
- data/vendor/assets/components/jquery/test/data/testrunner.js +368 -0
- data/vendor/assets/components/jquery/test/data/testsuite.css +155 -0
- data/vendor/assets/components/jquery/test/data/text.php +12 -0
- data/vendor/assets/components/jquery/test/data/ua.txt +272 -0
- data/vendor/assets/components/jquery/test/data/with_fries.xml +25 -0
- data/vendor/assets/components/jquery/test/data/with_fries_over_jsonp.php +7 -0
- data/vendor/assets/components/jquery/test/delegatetest.html +228 -0
- data/vendor/assets/components/jquery/test/hovertest.html +158 -0
- data/vendor/assets/components/jquery/test/index.html +330 -0
- data/vendor/assets/components/jquery/test/localfile.html +75 -0
- data/vendor/assets/components/jquery/test/networkerror.html +84 -0
- data/vendor/assets/components/jquery/test/polluted.php +110 -0
- data/vendor/assets/components/jquery/test/readywait.html +70 -0
- data/vendor/assets/components/jquery/test/unit/ajax.js +1971 -0
- data/vendor/assets/components/jquery/test/unit/attributes.js +1332 -0
- data/vendor/assets/components/jquery/test/unit/callbacks.js +326 -0
- data/vendor/assets/components/jquery/test/unit/core.js +1332 -0
- data/vendor/assets/components/jquery/test/unit/css.js +1036 -0
- data/vendor/assets/components/jquery/test/unit/data.js +636 -0
- data/vendor/assets/components/jquery/test/unit/deferred.js +440 -0
- data/vendor/assets/components/jquery/test/unit/deprecated.js +1 -0
- data/vendor/assets/components/jquery/test/unit/dimensions.js +461 -0
- data/vendor/assets/components/jquery/test/unit/effects.js +2050 -0
- data/vendor/assets/components/jquery/test/unit/event.js +2690 -0
- data/vendor/assets/components/jquery/test/unit/exports.js +7 -0
- data/vendor/assets/components/jquery/test/unit/manipulation.js +2242 -0
- data/vendor/assets/components/jquery/test/unit/offset.js +549 -0
- data/vendor/assets/components/jquery/test/unit/queue.js +316 -0
- data/vendor/assets/components/jquery/test/unit/selector.js +185 -0
- data/vendor/assets/components/jquery/test/unit/serialize.js +146 -0
- data/vendor/assets/components/jquery/test/unit/support.js +384 -0
- data/vendor/assets/components/jquery/test/unit/traversing.js +667 -0
- data/vendor/assets/components/jquery/test/xhtml.php +5 -0
- data/vendor/assets/components/sir-trevor-js/CHANGELOG.md +44 -0
- data/vendor/assets/components/sir-trevor-js/Gemfile +3 -0
- data/vendor/assets/components/sir-trevor-js/Gemfile.lock +10 -0
- data/vendor/assets/components/sir-trevor-js/Gruntfile.js +109 -0
- data/vendor/assets/components/sir-trevor-js/MIT-LICENCE +20 -0
- data/vendor/assets/components/sir-trevor-js/Readme.md +84 -0
- data/vendor/assets/components/sir-trevor-js/bower.json +21 -0
- data/vendor/assets/components/sir-trevor-js/component.json +42 -0
- data/vendor/assets/components/sir-trevor-js/contributing.md +74 -0
- data/vendor/assets/components/sir-trevor-js/examples-component/component.json +14 -0
- data/vendor/assets/components/sir-trevor-js/examples-component/index.html +33 -0
- data/vendor/assets/components/sir-trevor-js/examples-component/index.js +29 -0
- data/vendor/assets/components/sir-trevor-js/examples-component/javascript/example_block.js +203 -0
- data/vendor/assets/components/sir-trevor-js/examples-component/javascript/limit_chars.js +46 -0
- data/vendor/assets/components/sir-trevor-js/examples-component/sir-trevor.gif +0 -0
- data/vendor/assets/components/sir-trevor-js/locales/cn.js +51 -0
- data/vendor/assets/components/sir-trevor-js/locales/de.js +51 -0
- data/vendor/assets/components/sir-trevor-js/locales/es.js +51 -0
- data/vendor/assets/components/sir-trevor-js/locales/fi.js +51 -0
- data/vendor/assets/components/sir-trevor-js/locales/fr.js +51 -0
- data/vendor/assets/components/sir-trevor-js/locales/pt.js +52 -0
- data/vendor/assets/components/sir-trevor-js/package.json +23 -0
- data/vendor/assets/components/sir-trevor-js/sir-trevor-icons.css +6 -0
- data/vendor/assets/components/sir-trevor-js/sir-trevor.css +393 -0
- data/vendor/assets/components/sir-trevor-js/sir-trevor.css.map +7 -0
- data/vendor/assets/components/sir-trevor-js/sir-trevor.js +5481 -0
- data/vendor/assets/components/sir-trevor-js/sir-trevor.min.js +13 -0
- data/vendor/assets/components/underscore/CNAME +1 -0
- data/vendor/assets/components/underscore/CONTRIBUTING.md +9 -0
- data/vendor/assets/components/underscore/LICENSE +22 -0
- data/vendor/assets/components/underscore/README.md +19 -0
- data/vendor/assets/components/underscore/Rakefile +10 -0
- data/vendor/assets/components/underscore/docs/docco.css +192 -0
- data/vendor/assets/components/underscore/docs/favicon.ico +0 -0
- data/vendor/assets/components/underscore/docs/images/background.png +0 -0
- data/vendor/assets/components/underscore/docs/images/underscore.png +0 -0
- data/vendor/assets/components/underscore/docs/underscore.html +823 -0
- data/vendor/assets/components/underscore/favicon.ico +0 -0
- data/vendor/assets/components/underscore/index.html +2467 -0
- data/vendor/assets/components/underscore/index.js +1 -0
- data/vendor/assets/components/underscore/package.json +16 -0
- data/vendor/assets/components/underscore/test/arrays.js +200 -0
- data/vendor/assets/components/underscore/test/chaining.js +59 -0
- data/vendor/assets/components/underscore/test/collections.js +453 -0
- data/vendor/assets/components/underscore/test/functions.js +265 -0
- data/vendor/assets/components/underscore/test/index.html +44 -0
- data/vendor/assets/components/underscore/test/objects.js +570 -0
- data/vendor/assets/components/underscore/test/speed.js +75 -0
- data/vendor/assets/components/underscore/test/utility.js +266 -0
- data/vendor/assets/components/underscore/test/vendor/jquery.js +9404 -0
- data/vendor/assets/components/underscore/test/vendor/jslitmus.js +670 -0
- data/vendor/assets/components/underscore/test/vendor/qunit.css +235 -0
- data/vendor/assets/components/underscore/test/vendor/qunit.js +1977 -0
- data/vendor/assets/components/underscore/test/vendor/runner.js +98 -0
- data/vendor/assets/components/underscore/underscore-min.js +1 -0
- data/vendor/assets/components/underscore/underscore.js +1226 -0
- metadata +473 -0
@@ -0,0 +1 @@
|
|
1
|
+
module.exports = require('./underscore');
|
@@ -0,0 +1,16 @@
|
|
1
|
+
{
|
2
|
+
"name" : "underscore",
|
3
|
+
"description" : "JavaScript's functional programming helper library.",
|
4
|
+
"homepage" : "http://underscorejs.org",
|
5
|
+
"keywords" : ["util", "functional", "server", "client", "browser"],
|
6
|
+
"author" : "Jeremy Ashkenas <jeremy@documentcloud.org>",
|
7
|
+
"repository" : {"type": "git", "url": "git://github.com/documentcloud/underscore.git"},
|
8
|
+
"main" : "underscore.js",
|
9
|
+
"version" : "1.4.4",
|
10
|
+
"devDependencies": {
|
11
|
+
"phantomjs": "0.2.2"
|
12
|
+
},
|
13
|
+
"scripts": {
|
14
|
+
"test": "phantomjs test/vendor/runner.js test/index.html?noglobals=true"
|
15
|
+
}
|
16
|
+
}
|
@@ -0,0 +1,200 @@
|
|
1
|
+
$(document).ready(function() {
|
2
|
+
|
3
|
+
module("Arrays");
|
4
|
+
|
5
|
+
test("first", function() {
|
6
|
+
equal(_.first([1,2,3]), 1, 'can pull out the first element of an array');
|
7
|
+
equal(_([1, 2, 3]).first(), 1, 'can perform OO-style "first()"');
|
8
|
+
equal(_.first([1,2,3], 0).join(', '), "", 'can pass an index to first');
|
9
|
+
equal(_.first([1,2,3], 2).join(', '), '1, 2', 'can pass an index to first');
|
10
|
+
equal(_.first([1,2,3], 5).join(', '), '1, 2, 3', 'can pass an index to first');
|
11
|
+
var result = (function(){ return _.first(arguments); })(4, 3, 2, 1);
|
12
|
+
equal(result, 4, 'works on an arguments object.');
|
13
|
+
result = _.map([[1,2,3],[1,2,3]], _.first);
|
14
|
+
equal(result.join(','), '1,1', 'works well with _.map');
|
15
|
+
result = (function() { return _.take([1,2,3], 2); })();
|
16
|
+
equal(result.join(','), '1,2', 'aliased as take');
|
17
|
+
|
18
|
+
equal(_.first(null), undefined, 'handles nulls');
|
19
|
+
});
|
20
|
+
|
21
|
+
test("rest", function() {
|
22
|
+
var numbers = [1, 2, 3, 4];
|
23
|
+
equal(_.rest(numbers).join(", "), "2, 3, 4", 'working rest()');
|
24
|
+
equal(_.rest(numbers, 0).join(", "), "1, 2, 3, 4", 'working rest(0)');
|
25
|
+
equal(_.rest(numbers, 2).join(', '), '3, 4', 'rest can take an index');
|
26
|
+
var result = (function(){ return _(arguments).tail(); })(1, 2, 3, 4);
|
27
|
+
equal(result.join(', '), '2, 3, 4', 'aliased as tail and works on arguments object');
|
28
|
+
result = _.map([[1,2,3],[1,2,3]], _.rest);
|
29
|
+
equal(_.flatten(result).join(','), '2,3,2,3', 'works well with _.map');
|
30
|
+
result = (function(){ return _(arguments).drop(); })(1, 2, 3, 4);
|
31
|
+
equal(result.join(', '), '2, 3, 4', 'aliased as drop and works on arguments object');
|
32
|
+
});
|
33
|
+
|
34
|
+
test("initial", function() {
|
35
|
+
equal(_.initial([1,2,3,4,5]).join(", "), "1, 2, 3, 4", 'working initial()');
|
36
|
+
equal(_.initial([1,2,3,4],2).join(", "), "1, 2", 'initial can take an index');
|
37
|
+
var result = (function(){ return _(arguments).initial(); })(1, 2, 3, 4);
|
38
|
+
equal(result.join(", "), "1, 2, 3", 'initial works on arguments object');
|
39
|
+
result = _.map([[1,2,3],[1,2,3]], _.initial);
|
40
|
+
equal(_.flatten(result).join(','), '1,2,1,2', 'initial works with _.map');
|
41
|
+
});
|
42
|
+
|
43
|
+
test("last", function() {
|
44
|
+
equal(_.last([1,2,3]), 3, 'can pull out the last element of an array');
|
45
|
+
equal(_.last([1,2,3], 0).join(', '), "", 'can pass an index to last');
|
46
|
+
equal(_.last([1,2,3], 2).join(', '), '2, 3', 'can pass an index to last');
|
47
|
+
equal(_.last([1,2,3], 5).join(', '), '1, 2, 3', 'can pass an index to last');
|
48
|
+
var result = (function(){ return _(arguments).last(); })(1, 2, 3, 4);
|
49
|
+
equal(result, 4, 'works on an arguments object');
|
50
|
+
result = _.map([[1,2,3],[1,2,3]], _.last);
|
51
|
+
equal(result.join(','), '3,3', 'works well with _.map');
|
52
|
+
|
53
|
+
equal(_.last(null), undefined, 'handles nulls');
|
54
|
+
});
|
55
|
+
|
56
|
+
test("compact", function() {
|
57
|
+
equal(_.compact([0, 1, false, 2, false, 3]).length, 3, 'can trim out all falsy values');
|
58
|
+
var result = (function(){ return _.compact(arguments).length; })(0, 1, false, 2, false, 3);
|
59
|
+
equal(result, 3, 'works on an arguments object');
|
60
|
+
});
|
61
|
+
|
62
|
+
test("flatten", function() {
|
63
|
+
var list = [1, [2], [3, [[[4]]]]];
|
64
|
+
deepEqual(_.flatten(list), [1,2,3,4], 'can flatten nested arrays');
|
65
|
+
deepEqual(_.flatten(list, true), [1,2,3,[[[4]]]], 'can shallowly flatten nested arrays');
|
66
|
+
var result = (function(){ return _.flatten(arguments); })(1, [2], [3, [[[4]]]]);
|
67
|
+
deepEqual(result, [1,2,3,4], 'works on an arguments object');
|
68
|
+
});
|
69
|
+
|
70
|
+
test("without", function() {
|
71
|
+
var list = [1, 2, 1, 0, 3, 1, 4];
|
72
|
+
equal(_.without(list, 0, 1).join(', '), '2, 3, 4', 'can remove all instances of an object');
|
73
|
+
var result = (function(){ return _.without(arguments, 0, 1); })(1, 2, 1, 0, 3, 1, 4);
|
74
|
+
equal(result.join(', '), '2, 3, 4', 'works on an arguments object');
|
75
|
+
|
76
|
+
var list = [{one : 1}, {two : 2}];
|
77
|
+
ok(_.without(list, {one : 1}).length == 2, 'uses real object identity for comparisons.');
|
78
|
+
ok(_.without(list, list[0]).length == 1, 'ditto.');
|
79
|
+
});
|
80
|
+
|
81
|
+
test("uniq", function() {
|
82
|
+
var list = [1, 2, 1, 3, 1, 4];
|
83
|
+
equal(_.uniq(list).join(', '), '1, 2, 3, 4', 'can find the unique values of an unsorted array');
|
84
|
+
|
85
|
+
var list = [1, 1, 1, 2, 2, 3];
|
86
|
+
equal(_.uniq(list, true).join(', '), '1, 2, 3', 'can find the unique values of a sorted array faster');
|
87
|
+
|
88
|
+
var list = [{name:'moe'}, {name:'curly'}, {name:'larry'}, {name:'curly'}];
|
89
|
+
var iterator = function(value) { return value.name; };
|
90
|
+
equal(_.map(_.uniq(list, false, iterator), iterator).join(', '), 'moe, curly, larry', 'can find the unique values of an array using a custom iterator');
|
91
|
+
|
92
|
+
equal(_.map(_.uniq(list, iterator), iterator).join(', '), 'moe, curly, larry', 'can find the unique values of an array using a custom iterator without specifying whether array is sorted');
|
93
|
+
|
94
|
+
var iterator = function(value) { return value +1; };
|
95
|
+
var list = [1, 2, 2, 3, 4, 4];
|
96
|
+
equal(_.uniq(list, true, iterator).join(', '), '1, 2, 3, 4', 'iterator works with sorted array');
|
97
|
+
|
98
|
+
var result = (function(){ return _.uniq(arguments); })(1, 2, 1, 3, 1, 4);
|
99
|
+
equal(result.join(', '), '1, 2, 3, 4', 'works on an arguments object');
|
100
|
+
});
|
101
|
+
|
102
|
+
test("intersection", function() {
|
103
|
+
var stooges = ['moe', 'curly', 'larry'], leaders = ['moe', 'groucho'];
|
104
|
+
equal(_.intersection(stooges, leaders).join(''), 'moe', 'can take the set intersection of two arrays');
|
105
|
+
equal(_(stooges).intersection(leaders).join(''), 'moe', 'can perform an OO-style intersection');
|
106
|
+
var result = (function(){ return _.intersection(arguments, leaders); })('moe', 'curly', 'larry');
|
107
|
+
equal(result.join(''), 'moe', 'works on an arguments object');
|
108
|
+
});
|
109
|
+
|
110
|
+
test("union", function() {
|
111
|
+
var result = _.union([1, 2, 3], [2, 30, 1], [1, 40]);
|
112
|
+
equal(result.join(' '), '1 2 3 30 40', 'takes the union of a list of arrays');
|
113
|
+
|
114
|
+
var result = _.union([1, 2, 3], [2, 30, 1], [1, 40, [1]]);
|
115
|
+
equal(result.join(' '), '1 2 3 30 40 1', 'takes the union of a list of nested arrays');
|
116
|
+
});
|
117
|
+
|
118
|
+
test("difference", function() {
|
119
|
+
var result = _.difference([1, 2, 3], [2, 30, 40]);
|
120
|
+
equal(result.join(' '), '1 3', 'takes the difference of two arrays');
|
121
|
+
|
122
|
+
var result = _.difference([1, 2, 3, 4], [2, 30, 40], [1, 11, 111]);
|
123
|
+
equal(result.join(' '), '3 4', 'takes the difference of three arrays');
|
124
|
+
});
|
125
|
+
|
126
|
+
test('zip', function() {
|
127
|
+
var names = ['moe', 'larry', 'curly'], ages = [30, 40, 50], leaders = [true];
|
128
|
+
var stooges = _.zip(names, ages, leaders);
|
129
|
+
equal(String(stooges), 'moe,30,true,larry,40,,curly,50,', 'zipped together arrays of different lengths');
|
130
|
+
});
|
131
|
+
|
132
|
+
test('object', function() {
|
133
|
+
var result = _.object(['moe', 'larry', 'curly'], [30, 40, 50]);
|
134
|
+
var shouldBe = {moe: 30, larry: 40, curly: 50};
|
135
|
+
ok(_.isEqual(result, shouldBe), 'two arrays zipped together into an object');
|
136
|
+
|
137
|
+
result = _.object([['one', 1], ['two', 2], ['three', 3]]);
|
138
|
+
shouldBe = {one: 1, two: 2, three: 3};
|
139
|
+
ok(_.isEqual(result, shouldBe), 'an array of pairs zipped together into an object');
|
140
|
+
|
141
|
+
var stooges = {moe: 30, larry: 40, curly: 50};
|
142
|
+
ok(_.isEqual(_.object(_.pairs(stooges)), stooges), 'an object converted to pairs and back to an object');
|
143
|
+
|
144
|
+
ok(_.isEqual(_.object(null), {}), 'handles nulls');
|
145
|
+
});
|
146
|
+
|
147
|
+
test("indexOf", function() {
|
148
|
+
var numbers = [1, 2, 3];
|
149
|
+
numbers.indexOf = null;
|
150
|
+
equal(_.indexOf(numbers, 2), 1, 'can compute indexOf, even without the native function');
|
151
|
+
var result = (function(){ return _.indexOf(arguments, 2); })(1, 2, 3);
|
152
|
+
equal(result, 1, 'works on an arguments object');
|
153
|
+
equal(_.indexOf(null, 2), -1, 'handles nulls properly');
|
154
|
+
|
155
|
+
var numbers = [10, 20, 30, 40, 50], num = 35;
|
156
|
+
var index = _.indexOf(numbers, num, true);
|
157
|
+
equal(index, -1, '35 is not in the list');
|
158
|
+
|
159
|
+
numbers = [10, 20, 30, 40, 50]; num = 40;
|
160
|
+
index = _.indexOf(numbers, num, true);
|
161
|
+
equal(index, 3, '40 is in the list');
|
162
|
+
|
163
|
+
numbers = [1, 40, 40, 40, 40, 40, 40, 40, 50, 60, 70]; num = 40;
|
164
|
+
index = _.indexOf(numbers, num, true);
|
165
|
+
equal(index, 1, '40 is in the list');
|
166
|
+
|
167
|
+
numbers = [1, 2, 3, 1, 2, 3, 1, 2, 3];
|
168
|
+
index = _.indexOf(numbers, 2, 5);
|
169
|
+
equal(index, 7, 'supports the fromIndex argument');
|
170
|
+
});
|
171
|
+
|
172
|
+
test("lastIndexOf", function() {
|
173
|
+
var numbers = [1, 0, 1];
|
174
|
+
equal(_.lastIndexOf(numbers, 1), 2);
|
175
|
+
|
176
|
+
numbers = [1, 0, 1, 0, 0, 1, 0, 0, 0];
|
177
|
+
numbers.lastIndexOf = null;
|
178
|
+
equal(_.lastIndexOf(numbers, 1), 5, 'can compute lastIndexOf, even without the native function');
|
179
|
+
equal(_.lastIndexOf(numbers, 0), 8, 'lastIndexOf the other element');
|
180
|
+
var result = (function(){ return _.lastIndexOf(arguments, 1); })(1, 0, 1, 0, 0, 1, 0, 0, 0);
|
181
|
+
equal(result, 5, 'works on an arguments object');
|
182
|
+
equal(_.indexOf(null, 2), -1, 'handles nulls properly');
|
183
|
+
|
184
|
+
numbers = [1, 2, 3, 1, 2, 3, 1, 2, 3];
|
185
|
+
var index = _.lastIndexOf(numbers, 2, 2);
|
186
|
+
equal(index, 1, 'supports the fromIndex argument');
|
187
|
+
});
|
188
|
+
|
189
|
+
test("range", function() {
|
190
|
+
equal(_.range(0).join(''), '', 'range with 0 as a first argument generates an empty array');
|
191
|
+
equal(_.range(4).join(' '), '0 1 2 3', 'range with a single positive argument generates an array of elements 0,1,2,...,n-1');
|
192
|
+
equal(_.range(5, 8).join(' '), '5 6 7', 'range with two arguments a & b, a<b generates an array of elements a,a+1,a+2,...,b-2,b-1');
|
193
|
+
equal(_.range(8, 5).join(''), '', 'range with two arguments a & b, b<a generates an empty array');
|
194
|
+
equal(_.range(3, 10, 3).join(' '), '3 6 9', 'range with three arguments a & b & c, c < b-a, a < b generates an array of elements a,a+c,a+2c,...,b - (multiplier of a) < c');
|
195
|
+
equal(_.range(3, 10, 15).join(''), '3', 'range with three arguments a & b & c, c > b-a, a < b generates an array with a single element, equal to a');
|
196
|
+
equal(_.range(12, 7, -2).join(' '), '12 10 8', 'range with three arguments a & b & c, a > b, c < 0 generates an array of elements a,a-c,a-2c and ends with the number not less than b');
|
197
|
+
equal(_.range(0, -10, -1).join(' '), '0 -1 -2 -3 -4 -5 -6 -7 -8 -9', 'final example in the Python docs');
|
198
|
+
});
|
199
|
+
|
200
|
+
});
|
@@ -0,0 +1,59 @@
|
|
1
|
+
$(document).ready(function() {
|
2
|
+
|
3
|
+
module("Chaining");
|
4
|
+
|
5
|
+
test("map/flatten/reduce", function() {
|
6
|
+
var lyrics = [
|
7
|
+
"I'm a lumberjack and I'm okay",
|
8
|
+
"I sleep all night and I work all day",
|
9
|
+
"He's a lumberjack and he's okay",
|
10
|
+
"He sleeps all night and he works all day"
|
11
|
+
];
|
12
|
+
var counts = _(lyrics).chain()
|
13
|
+
.map(function(line) { return line.split(''); })
|
14
|
+
.flatten()
|
15
|
+
.reduce(function(hash, l) {
|
16
|
+
hash[l] = hash[l] || 0;
|
17
|
+
hash[l]++;
|
18
|
+
return hash;
|
19
|
+
}, {}).value();
|
20
|
+
ok(counts['a'] == 16 && counts['e'] == 10, 'counted all the letters in the song');
|
21
|
+
});
|
22
|
+
|
23
|
+
test("select/reject/sortBy", function() {
|
24
|
+
var numbers = [1,2,3,4,5,6,7,8,9,10];
|
25
|
+
numbers = _(numbers).chain().select(function(n) {
|
26
|
+
return n % 2 == 0;
|
27
|
+
}).reject(function(n) {
|
28
|
+
return n % 4 == 0;
|
29
|
+
}).sortBy(function(n) {
|
30
|
+
return -n;
|
31
|
+
}).value();
|
32
|
+
equal(numbers.join(', '), "10, 6, 2", "filtered and reversed the numbers");
|
33
|
+
});
|
34
|
+
|
35
|
+
test("select/reject/sortBy in functional style", function() {
|
36
|
+
var numbers = [1,2,3,4,5,6,7,8,9,10];
|
37
|
+
numbers = _.chain(numbers).select(function(n) {
|
38
|
+
return n % 2 == 0;
|
39
|
+
}).reject(function(n) {
|
40
|
+
return n % 4 == 0;
|
41
|
+
}).sortBy(function(n) {
|
42
|
+
return -n;
|
43
|
+
}).value();
|
44
|
+
equal(numbers.join(', '), "10, 6, 2", "filtered and reversed the numbers");
|
45
|
+
});
|
46
|
+
|
47
|
+
test("reverse/concat/unshift/pop/map", function() {
|
48
|
+
var numbers = [1,2,3,4,5];
|
49
|
+
numbers = _(numbers).chain()
|
50
|
+
.reverse()
|
51
|
+
.concat([5, 5, 5])
|
52
|
+
.unshift(17)
|
53
|
+
.pop()
|
54
|
+
.map(function(n){ return n * 2; })
|
55
|
+
.value();
|
56
|
+
equal(numbers.join(', '), "34, 10, 8, 6, 4, 2, 10, 10", 'can chain together array functions.');
|
57
|
+
});
|
58
|
+
|
59
|
+
});
|
@@ -0,0 +1,453 @@
|
|
1
|
+
$(document).ready(function() {
|
2
|
+
|
3
|
+
module("Collections");
|
4
|
+
|
5
|
+
test("each", function() {
|
6
|
+
_.each([1, 2, 3], function(num, i) {
|
7
|
+
equal(num, i + 1, 'each iterators provide value and iteration count');
|
8
|
+
});
|
9
|
+
|
10
|
+
var answers = [];
|
11
|
+
_.each([1, 2, 3], function(num){ answers.push(num * this.multiplier);}, {multiplier : 5});
|
12
|
+
equal(answers.join(', '), '5, 10, 15', 'context object property accessed');
|
13
|
+
|
14
|
+
answers = [];
|
15
|
+
_.forEach([1, 2, 3], function(num){ answers.push(num); });
|
16
|
+
equal(answers.join(', '), '1, 2, 3', 'aliased as "forEach"');
|
17
|
+
|
18
|
+
answers = [];
|
19
|
+
var obj = {one : 1, two : 2, three : 3};
|
20
|
+
obj.constructor.prototype.four = 4;
|
21
|
+
_.each(obj, function(value, key){ answers.push(key); });
|
22
|
+
equal(answers.join(", "), 'one, two, three', 'iterating over objects works, and ignores the object prototype.');
|
23
|
+
delete obj.constructor.prototype.four;
|
24
|
+
|
25
|
+
var answer = null;
|
26
|
+
_.each([1, 2, 3], function(num, index, arr){ if (_.include(arr, num)) answer = true; });
|
27
|
+
ok(answer, 'can reference the original collection from inside the iterator');
|
28
|
+
|
29
|
+
answers = 0;
|
30
|
+
_.each(null, function(){ ++answers; });
|
31
|
+
equal(answers, 0, 'handles a null properly');
|
32
|
+
});
|
33
|
+
|
34
|
+
test('map', function() {
|
35
|
+
var doubled = _.map([1, 2, 3], function(num){ return num * 2; });
|
36
|
+
equal(doubled.join(', '), '2, 4, 6', 'doubled numbers');
|
37
|
+
|
38
|
+
doubled = _.collect([1, 2, 3], function(num){ return num * 2; });
|
39
|
+
equal(doubled.join(', '), '2, 4, 6', 'aliased as "collect"');
|
40
|
+
|
41
|
+
var tripled = _.map([1, 2, 3], function(num){ return num * this.multiplier; }, {multiplier : 3});
|
42
|
+
equal(tripled.join(', '), '3, 6, 9', 'tripled numbers with context');
|
43
|
+
|
44
|
+
var doubled = _([1, 2, 3]).map(function(num){ return num * 2; });
|
45
|
+
equal(doubled.join(', '), '2, 4, 6', 'OO-style doubled numbers');
|
46
|
+
|
47
|
+
if (document.querySelectorAll) {
|
48
|
+
var ids = _.map(document.querySelectorAll('#map-test *'), function(n){ return n.id; });
|
49
|
+
deepEqual(ids, ['id1', 'id2'], 'Can use collection methods on NodeLists.');
|
50
|
+
}
|
51
|
+
|
52
|
+
var ids = _.map($('#map-test').children(), function(n){ return n.id; });
|
53
|
+
deepEqual(ids, ['id1', 'id2'], 'Can use collection methods on jQuery Array-likes.');
|
54
|
+
|
55
|
+
var ids = _.map(document.images, function(n){ return n.id; });
|
56
|
+
ok(ids[0] == 'chart_image', 'can use collection methods on HTMLCollections');
|
57
|
+
|
58
|
+
var ifnull = _.map(null, function(){});
|
59
|
+
ok(_.isArray(ifnull) && ifnull.length === 0, 'handles a null properly');
|
60
|
+
});
|
61
|
+
|
62
|
+
test('reduce', function() {
|
63
|
+
var sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num; }, 0);
|
64
|
+
equal(sum, 6, 'can sum up an array');
|
65
|
+
|
66
|
+
var context = {multiplier : 3};
|
67
|
+
sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num * this.multiplier; }, 0, context);
|
68
|
+
equal(sum, 18, 'can reduce with a context object');
|
69
|
+
|
70
|
+
sum = _.inject([1, 2, 3], function(sum, num){ return sum + num; }, 0);
|
71
|
+
equal(sum, 6, 'aliased as "inject"');
|
72
|
+
|
73
|
+
sum = _([1, 2, 3]).reduce(function(sum, num){ return sum + num; }, 0);
|
74
|
+
equal(sum, 6, 'OO-style reduce');
|
75
|
+
|
76
|
+
var sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num; });
|
77
|
+
equal(sum, 6, 'default initial value');
|
78
|
+
|
79
|
+
var ifnull;
|
80
|
+
try {
|
81
|
+
_.reduce(null, function(){});
|
82
|
+
} catch (ex) {
|
83
|
+
ifnull = ex;
|
84
|
+
}
|
85
|
+
ok(ifnull instanceof TypeError, 'handles a null (without inital value) properly');
|
86
|
+
|
87
|
+
ok(_.reduce(null, function(){}, 138) === 138, 'handles a null (with initial value) properly');
|
88
|
+
equal(_.reduce([], function(){}, undefined), undefined, 'undefined can be passed as a special case');
|
89
|
+
raises(function() { _.reduce([], function(){}); }, TypeError, 'throws an error for empty arrays with no initial value');
|
90
|
+
});
|
91
|
+
|
92
|
+
test('reduceRight', function() {
|
93
|
+
var list = _.reduceRight(["foo", "bar", "baz"], function(memo, str){ return memo + str; }, '');
|
94
|
+
equal(list, 'bazbarfoo', 'can perform right folds');
|
95
|
+
|
96
|
+
var list = _.foldr(["foo", "bar", "baz"], function(memo, str){ return memo + str; }, '');
|
97
|
+
equal(list, 'bazbarfoo', 'aliased as "foldr"');
|
98
|
+
|
99
|
+
var list = _.foldr(["foo", "bar", "baz"], function(memo, str){ return memo + str; });
|
100
|
+
equal(list, 'bazbarfoo', 'default initial value');
|
101
|
+
|
102
|
+
var ifnull;
|
103
|
+
try {
|
104
|
+
_.reduceRight(null, function(){});
|
105
|
+
} catch (ex) {
|
106
|
+
ifnull = ex;
|
107
|
+
}
|
108
|
+
ok(ifnull instanceof TypeError, 'handles a null (without inital value) properly');
|
109
|
+
|
110
|
+
var sum = _.reduceRight({a: 1, b: 2, c: 3}, function(sum, num){ return sum + num; });
|
111
|
+
equal(sum, 6, 'default initial value on object');
|
112
|
+
|
113
|
+
ok(_.reduceRight(null, function(){}, 138) === 138, 'handles a null (with initial value) properly');
|
114
|
+
|
115
|
+
equal(_.reduceRight([], function(){}, undefined), undefined, 'undefined can be passed as a special case');
|
116
|
+
raises(function() { _.reduceRight([], function(){}); }, TypeError, 'throws an error for empty arrays with no initial value');
|
117
|
+
|
118
|
+
// Assert that the correct arguments are being passed.
|
119
|
+
|
120
|
+
var args,
|
121
|
+
memo = {},
|
122
|
+
object = {a: 1, b: 2},
|
123
|
+
lastKey = _.keys(object).pop();
|
124
|
+
|
125
|
+
var expected = lastKey == 'a'
|
126
|
+
? [memo, 1, 'a', object]
|
127
|
+
: [memo, 2, 'b', object];
|
128
|
+
|
129
|
+
_.reduceRight(object, function() {
|
130
|
+
args || (args = _.toArray(arguments));
|
131
|
+
}, memo);
|
132
|
+
|
133
|
+
deepEqual(args, expected);
|
134
|
+
|
135
|
+
// And again, with numeric keys.
|
136
|
+
|
137
|
+
object = {'2': 'a', '1': 'b'};
|
138
|
+
lastKey = _.keys(object).pop();
|
139
|
+
args = null;
|
140
|
+
|
141
|
+
expected = lastKey == '2'
|
142
|
+
? [memo, 'a', '2', object]
|
143
|
+
: [memo, 'b', '1', object];
|
144
|
+
|
145
|
+
_.reduceRight(object, function() {
|
146
|
+
args || (args = _.toArray(arguments));
|
147
|
+
}, memo);
|
148
|
+
|
149
|
+
deepEqual(args, expected);
|
150
|
+
});
|
151
|
+
|
152
|
+
test('find', function() {
|
153
|
+
var array = [1, 2, 3, 4];
|
154
|
+
strictEqual(_.find(array, function(n) { return n > 2; }), 3, 'should return first found `value`');
|
155
|
+
strictEqual(_.find(array, function() { return false; }), void 0, 'should return `undefined` if `value` is not found');
|
156
|
+
});
|
157
|
+
|
158
|
+
test('detect', function() {
|
159
|
+
var result = _.detect([1, 2, 3], function(num){ return num * 2 == 4; });
|
160
|
+
equal(result, 2, 'found the first "2" and broke the loop');
|
161
|
+
});
|
162
|
+
|
163
|
+
test('select', function() {
|
164
|
+
var evens = _.select([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
165
|
+
equal(evens.join(', '), '2, 4, 6', 'selected each even number');
|
166
|
+
|
167
|
+
evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
168
|
+
equal(evens.join(', '), '2, 4, 6', 'aliased as "filter"');
|
169
|
+
});
|
170
|
+
|
171
|
+
test('reject', function() {
|
172
|
+
var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
173
|
+
equal(odds.join(', '), '1, 3, 5', 'rejected each even number');
|
174
|
+
|
175
|
+
var context = "obj";
|
176
|
+
|
177
|
+
var evens = _.reject([1, 2, 3, 4, 5, 6], function(num){
|
178
|
+
equal(context, "obj");
|
179
|
+
return num % 2 != 0;
|
180
|
+
}, context);
|
181
|
+
equal(evens.join(', '), '2, 4, 6', 'rejected each odd number');
|
182
|
+
});
|
183
|
+
|
184
|
+
test('all', function() {
|
185
|
+
ok(_.all([], _.identity), 'the empty set');
|
186
|
+
ok(_.all([true, true, true], _.identity), 'all true values');
|
187
|
+
ok(!_.all([true, false, true], _.identity), 'one false value');
|
188
|
+
ok(_.all([0, 10, 28], function(num){ return num % 2 == 0; }), 'even numbers');
|
189
|
+
ok(!_.all([0, 11, 28], function(num){ return num % 2 == 0; }), 'an odd number');
|
190
|
+
ok(_.all([1], _.identity) === true, 'cast to boolean - true');
|
191
|
+
ok(_.all([0], _.identity) === false, 'cast to boolean - false');
|
192
|
+
ok(_.every([true, true, true], _.identity), 'aliased as "every"');
|
193
|
+
ok(!_.all([undefined, undefined, undefined], _.identity), 'works with arrays of undefined');
|
194
|
+
});
|
195
|
+
|
196
|
+
test('any', function() {
|
197
|
+
var nativeSome = Array.prototype.some;
|
198
|
+
Array.prototype.some = null;
|
199
|
+
ok(!_.any([]), 'the empty set');
|
200
|
+
ok(!_.any([false, false, false]), 'all false values');
|
201
|
+
ok(_.any([false, false, true]), 'one true value');
|
202
|
+
ok(_.any([null, 0, 'yes', false]), 'a string');
|
203
|
+
ok(!_.any([null, 0, '', false]), 'falsy values');
|
204
|
+
ok(!_.any([1, 11, 29], function(num){ return num % 2 == 0; }), 'all odd numbers');
|
205
|
+
ok(_.any([1, 10, 29], function(num){ return num % 2 == 0; }), 'an even number');
|
206
|
+
ok(_.any([1], _.identity) === true, 'cast to boolean - true');
|
207
|
+
ok(_.any([0], _.identity) === false, 'cast to boolean - false');
|
208
|
+
ok(_.some([false, false, true]), 'aliased as "some"');
|
209
|
+
Array.prototype.some = nativeSome;
|
210
|
+
});
|
211
|
+
|
212
|
+
test('include', function() {
|
213
|
+
ok(_.include([1,2,3], 2), 'two is in the array');
|
214
|
+
ok(!_.include([1,3,9], 2), 'two is not in the array');
|
215
|
+
ok(_.contains({moe:1, larry:3, curly:9}, 3) === true, '_.include on objects checks their values');
|
216
|
+
ok(_([1,2,3]).include(2), 'OO-style include');
|
217
|
+
});
|
218
|
+
|
219
|
+
test('invoke', function() {
|
220
|
+
var list = [[5, 1, 7], [3, 2, 1]];
|
221
|
+
var result = _.invoke(list, 'sort');
|
222
|
+
equal(result[0].join(', '), '1, 5, 7', 'first array sorted');
|
223
|
+
equal(result[1].join(', '), '1, 2, 3', 'second array sorted');
|
224
|
+
});
|
225
|
+
|
226
|
+
test('invoke w/ function reference', function() {
|
227
|
+
var list = [[5, 1, 7], [3, 2, 1]];
|
228
|
+
var result = _.invoke(list, Array.prototype.sort);
|
229
|
+
equal(result[0].join(', '), '1, 5, 7', 'first array sorted');
|
230
|
+
equal(result[1].join(', '), '1, 2, 3', 'second array sorted');
|
231
|
+
});
|
232
|
+
|
233
|
+
// Relevant when using ClojureScript
|
234
|
+
test('invoke when strings have a call method', function() {
|
235
|
+
String.prototype.call = function() {
|
236
|
+
return 42;
|
237
|
+
};
|
238
|
+
var list = [[5, 1, 7], [3, 2, 1]];
|
239
|
+
var s = "foo";
|
240
|
+
equal(s.call(), 42, "call function exists");
|
241
|
+
var result = _.invoke(list, 'sort');
|
242
|
+
equal(result[0].join(', '), '1, 5, 7', 'first array sorted');
|
243
|
+
equal(result[1].join(', '), '1, 2, 3', 'second array sorted');
|
244
|
+
delete String.prototype.call;
|
245
|
+
equal(s.call, undefined, "call function removed");
|
246
|
+
});
|
247
|
+
|
248
|
+
test('pluck', function() {
|
249
|
+
var people = [{name : 'moe', age : 30}, {name : 'curly', age : 50}];
|
250
|
+
equal(_.pluck(people, 'name').join(', '), 'moe, curly', 'pulls names out of objects');
|
251
|
+
});
|
252
|
+
|
253
|
+
test('where', function() {
|
254
|
+
var list = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}];
|
255
|
+
var result = _.where(list, {a: 1});
|
256
|
+
equal(result.length, 3);
|
257
|
+
equal(result[result.length - 1].b, 4);
|
258
|
+
result = _.where(list, {b: 2});
|
259
|
+
equal(result.length, 2);
|
260
|
+
equal(result[0].a, 1);
|
261
|
+
});
|
262
|
+
|
263
|
+
test('findWhere', function() {
|
264
|
+
var list = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}, {a: 2, b: 4}];
|
265
|
+
var result = _.findWhere(list, {a: 1});
|
266
|
+
deepEqual(result, {a: 1, b: 2});
|
267
|
+
result = _.findWhere(list, {b: 4});
|
268
|
+
deepEqual(result, {a: 1, b: 4});
|
269
|
+
});
|
270
|
+
|
271
|
+
test('max', function() {
|
272
|
+
equal(3, _.max([1, 2, 3]), 'can perform a regular Math.max');
|
273
|
+
|
274
|
+
var neg = _.max([1, 2, 3], function(num){ return -num; });
|
275
|
+
equal(neg, 1, 'can perform a computation-based max');
|
276
|
+
|
277
|
+
equal(-Infinity, _.max({}), 'Maximum value of an empty object');
|
278
|
+
equal(-Infinity, _.max([]), 'Maximum value of an empty array');
|
279
|
+
equal(_.max({'a': 'a'}), -Infinity, 'Maximum value of a non-numeric collection');
|
280
|
+
|
281
|
+
equal(299999, _.max(_.range(1,300000)), "Maximum value of a too-big array");
|
282
|
+
});
|
283
|
+
|
284
|
+
test('min', function() {
|
285
|
+
equal(1, _.min([1, 2, 3]), 'can perform a regular Math.min');
|
286
|
+
|
287
|
+
var neg = _.min([1, 2, 3], function(num){ return -num; });
|
288
|
+
equal(neg, 3, 'can perform a computation-based min');
|
289
|
+
|
290
|
+
equal(Infinity, _.min({}), 'Minimum value of an empty object');
|
291
|
+
equal(Infinity, _.min([]), 'Minimum value of an empty array');
|
292
|
+
equal(_.min({'a': 'a'}), Infinity, 'Minimum value of a non-numeric collection');
|
293
|
+
|
294
|
+
var now = new Date(9999999999);
|
295
|
+
var then = new Date(0);
|
296
|
+
equal(_.min([now, then]), then);
|
297
|
+
|
298
|
+
equal(1, _.min(_.range(1,300000)), "Minimum value of a too-big array");
|
299
|
+
});
|
300
|
+
|
301
|
+
test('sortBy', function() {
|
302
|
+
var people = [{name : 'curly', age : 50}, {name : 'moe', age : 30}];
|
303
|
+
people = _.sortBy(people, function(person){ return person.age; });
|
304
|
+
equal(_.pluck(people, 'name').join(', '), 'moe, curly', 'stooges sorted by age');
|
305
|
+
|
306
|
+
var list = [undefined, 4, 1, undefined, 3, 2];
|
307
|
+
equal(_.sortBy(list, _.identity).join(','), '1,2,3,4,,', 'sortBy with undefined values');
|
308
|
+
|
309
|
+
var list = ["one", "two", "three", "four", "five"];
|
310
|
+
var sorted = _.sortBy(list, 'length');
|
311
|
+
equal(sorted.join(' '), 'one two four five three', 'sorted by length');
|
312
|
+
|
313
|
+
function Pair(x, y) {
|
314
|
+
this.x = x;
|
315
|
+
this.y = y;
|
316
|
+
}
|
317
|
+
|
318
|
+
var collection = [
|
319
|
+
new Pair(1, 1), new Pair(1, 2),
|
320
|
+
new Pair(1, 3), new Pair(1, 4),
|
321
|
+
new Pair(1, 5), new Pair(1, 6),
|
322
|
+
new Pair(2, 1), new Pair(2, 2),
|
323
|
+
new Pair(2, 3), new Pair(2, 4),
|
324
|
+
new Pair(2, 5), new Pair(2, 6),
|
325
|
+
new Pair(undefined, 1), new Pair(undefined, 2),
|
326
|
+
new Pair(undefined, 3), new Pair(undefined, 4),
|
327
|
+
new Pair(undefined, 5), new Pair(undefined, 6)
|
328
|
+
];
|
329
|
+
|
330
|
+
var actual = _.sortBy(collection, function(pair) {
|
331
|
+
return pair.x;
|
332
|
+
});
|
333
|
+
|
334
|
+
deepEqual(actual, collection, 'sortBy should be stable');
|
335
|
+
});
|
336
|
+
|
337
|
+
test('groupBy', function() {
|
338
|
+
var parity = _.groupBy([1, 2, 3, 4, 5, 6], function(num){ return num % 2; });
|
339
|
+
ok('0' in parity && '1' in parity, 'created a group for each value');
|
340
|
+
equal(parity[0].join(', '), '2, 4, 6', 'put each even number in the right group');
|
341
|
+
|
342
|
+
var list = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"];
|
343
|
+
var grouped = _.groupBy(list, 'length');
|
344
|
+
equal(grouped['3'].join(' '), 'one two six ten');
|
345
|
+
equal(grouped['4'].join(' '), 'four five nine');
|
346
|
+
equal(grouped['5'].join(' '), 'three seven eight');
|
347
|
+
|
348
|
+
var context = {};
|
349
|
+
_.groupBy([{}], function(){ ok(this === context); }, context);
|
350
|
+
|
351
|
+
grouped = _.groupBy([4.2, 6.1, 6.4], function(num) {
|
352
|
+
return Math.floor(num) > 4 ? 'hasOwnProperty' : 'constructor';
|
353
|
+
});
|
354
|
+
equal(grouped.constructor.length, 1);
|
355
|
+
equal(grouped.hasOwnProperty.length, 2);
|
356
|
+
|
357
|
+
var array = [{}];
|
358
|
+
_.groupBy(array, function(value, index, obj){ ok(obj === array); });
|
359
|
+
|
360
|
+
var array = [1, 2, 1, 2, 3];
|
361
|
+
var grouped = _.groupBy(array);
|
362
|
+
equal(grouped['1'].length, 2);
|
363
|
+
equal(grouped['3'].length, 1);
|
364
|
+
});
|
365
|
+
|
366
|
+
test('countBy', function() {
|
367
|
+
var parity = _.countBy([1, 2, 3, 4, 5], function(num){ return num % 2 == 0; });
|
368
|
+
equal(parity['true'], 2);
|
369
|
+
equal(parity['false'], 3);
|
370
|
+
|
371
|
+
var list = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"];
|
372
|
+
var grouped = _.countBy(list, 'length');
|
373
|
+
equal(grouped['3'], 4);
|
374
|
+
equal(grouped['4'], 3);
|
375
|
+
equal(grouped['5'], 3);
|
376
|
+
|
377
|
+
var context = {};
|
378
|
+
_.countBy([{}], function(){ ok(this === context); }, context);
|
379
|
+
|
380
|
+
grouped = _.countBy([4.2, 6.1, 6.4], function(num) {
|
381
|
+
return Math.floor(num) > 4 ? 'hasOwnProperty' : 'constructor';
|
382
|
+
});
|
383
|
+
equal(grouped.constructor, 1);
|
384
|
+
equal(grouped.hasOwnProperty, 2);
|
385
|
+
|
386
|
+
var array = [{}];
|
387
|
+
_.countBy(array, function(value, index, obj){ ok(obj === array); });
|
388
|
+
|
389
|
+
var array = [1, 2, 1, 2, 3];
|
390
|
+
var grouped = _.countBy(array);
|
391
|
+
equal(grouped['1'], 2);
|
392
|
+
equal(grouped['3'], 1);
|
393
|
+
});
|
394
|
+
|
395
|
+
test('sortedIndex', function() {
|
396
|
+
var numbers = [10, 20, 30, 40, 50], num = 35;
|
397
|
+
var indexForNum = _.sortedIndex(numbers, num);
|
398
|
+
equal(indexForNum, 3, '35 should be inserted at index 3');
|
399
|
+
|
400
|
+
var indexFor30 = _.sortedIndex(numbers, 30);
|
401
|
+
equal(indexFor30, 2, '30 should be inserted at index 2');
|
402
|
+
|
403
|
+
var objects = [{x: 10}, {x: 20}, {x: 30}, {x: 40}];
|
404
|
+
var iterator = function(obj){ return obj.x; };
|
405
|
+
strictEqual(_.sortedIndex(objects, {x: 25}, iterator), 2);
|
406
|
+
strictEqual(_.sortedIndex(objects, {x: 35}, 'x'), 3);
|
407
|
+
|
408
|
+
var context = {1: 2, 2: 3, 3: 4};
|
409
|
+
iterator = function(obj){ return this[obj]; };
|
410
|
+
strictEqual(_.sortedIndex([1, 3], 2, iterator, context), 1);
|
411
|
+
});
|
412
|
+
|
413
|
+
test('shuffle', function() {
|
414
|
+
var numbers = _.range(10);
|
415
|
+
var shuffled = _.shuffle(numbers).sort();
|
416
|
+
notStrictEqual(numbers, shuffled, 'original object is unmodified');
|
417
|
+
equal(shuffled.join(','), numbers.join(','), 'contains the same members before and after shuffle');
|
418
|
+
});
|
419
|
+
|
420
|
+
test('toArray', function() {
|
421
|
+
ok(!_.isArray(arguments), 'arguments object is not an array');
|
422
|
+
ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array');
|
423
|
+
var a = [1,2,3];
|
424
|
+
ok(_.toArray(a) !== a, 'array is cloned');
|
425
|
+
equal(_.toArray(a).join(', '), '1, 2, 3', 'cloned array contains same elements');
|
426
|
+
|
427
|
+
var numbers = _.toArray({one : 1, two : 2, three : 3});
|
428
|
+
equal(numbers.join(', '), '1, 2, 3', 'object flattened into array');
|
429
|
+
|
430
|
+
// test in IE < 9
|
431
|
+
try {
|
432
|
+
var actual = _.toArray(document.childNodes);
|
433
|
+
} catch(ex) { }
|
434
|
+
|
435
|
+
ok(_.isArray(actual), 'should not throw converting a node list');
|
436
|
+
});
|
437
|
+
|
438
|
+
test('size', function() {
|
439
|
+
equal(_.size({one : 1, two : 2, three : 3}), 3, 'can compute the size of an object');
|
440
|
+
equal(_.size([1, 2, 3]), 3, 'can compute the size of an array');
|
441
|
+
|
442
|
+
var func = function() {
|
443
|
+
return _.size(arguments);
|
444
|
+
};
|
445
|
+
|
446
|
+
equal(func(1, 2, 3, 4), 4, 'can test the size of the arguments object');
|
447
|
+
|
448
|
+
equal(_.size('hello'), 5, 'can compute the size of a string');
|
449
|
+
|
450
|
+
equal(_.size(null), 0, 'handles nulls');
|
451
|
+
});
|
452
|
+
|
453
|
+
});
|