diff --git a/lib/rsvp.js b/lib/rsvp.js index 4f297666..011b715c 100644 --- a/lib/rsvp.js +++ b/lib/rsvp.js @@ -1,25 +1,10 @@ import Promise from './rsvp/promise'; import EventTarget from './rsvp/events'; - -import denodeify from './rsvp/promise/node'; -import all from './rsvp/promise/all'; -import allSettled from './rsvp/promise/all-settled'; -import race from './rsvp/promise/race'; -import hash from './rsvp/promise/hash'; -import hashSettled from './rsvp/promise/hash-settled'; -import rethrow from './rsvp/promise/rethrow'; -import defer from './rsvp/promise/defer'; -import map from './rsvp/promise/map'; -import resolve from './rsvp/promise/resolve'; -import reject from './rsvp/promise/reject'; -import filter from './rsvp/promise/filter'; - import asap from './rsvp/asap'; import { config, configure } from './rsvp/config'; - // defaults config.async = asap; config.after = cb => setTimeout(cb, 0); @@ -52,42 +37,18 @@ export default { asap, Promise, EventTarget, - all, - allSettled, - race, - hash, - hashSettled, - rethrow, - defer, - denodeify, configure, on, off, - resolve, - reject, - map, ['async']: async, // babel seems to error if async isn't a computed prop here... - filter }; export { asap, Promise, EventTarget, - all, - allSettled, - race, - hash, - hashSettled, - rethrow, - defer, - denodeify, configure, on, off, - resolve, - reject, - map, async, - filter }; diff --git a/lib/rsvp/promise/filter.js b/lib/rsvp/promise/filter.js index 5540d554..74805efa 100644 --- a/lib/rsvp/promise/filter.js +++ b/lib/rsvp/promise/filter.js @@ -47,7 +47,7 @@ class FilterEnumerator extends Enumerator { } } else { this._remaining--; - if (value !== true) { + if (!value) { this._result[i] = EMPTY_OBJECT; } } diff --git a/package.json b/package.json index 675d0755..b4399ec3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "rsvp", "namespace": "RSVP", - "version": "4.0.0", + "version": "4.0.1", "description": "A lightweight library that provides tools for organizing asynchronous code", "main": "dist/rsvp.js", "module": "dist/rsvp.es.js", diff --git a/test/extension-test.js b/test/extension-test.js index 437ea11b..3c78d377 100644 --- a/test/extension-test.js +++ b/test/extension-test.js @@ -127,8 +127,8 @@ describe("RSVP extensions", function() { describe("self fulfillment", function(){ it("treats self fulfillment as the recursive base case", function(done){ - var aDefer = new RSVP.defer(), - bDefer = new RSVP.defer(), + var aDefer = new Promise.defer(), + bDefer = new Promise.defer(), promiseA = aDefer.promise, promiseB = bDefer.promise; @@ -418,9 +418,9 @@ describe("RSVP extensions", function() { }); }); - describe("RSVP.defer", function() { + describe("Promise.defer", function() { it("It should return a resolver and promise together", function(done) { - var deferred = RSVP.defer(), value = {}; + var deferred = Promise.defer(), value = {}; // resolve first to confirm that the semantics are async deferred.resolve(value); @@ -432,7 +432,7 @@ describe("RSVP extensions", function() { }); it("The provided resolver should support rejection", function(done) { - var deferred = RSVP.defer(), reason = {}; + var deferred = Promise.defer(), reason = {}; // resolve first to confirm that the semantics are async deferred.reject(reason); @@ -444,9 +444,9 @@ describe("RSVP extensions", function() { }); }); - describe("RSVP.denodeify", function() { + describe("Promise.denodeify", function() { it('it should exist', function() { - assert(RSVP.denodeify); + assert(Promise.denodeify); }); it('calls node function with any arguments passed', function(done) { @@ -457,7 +457,7 @@ describe("RSVP extensions", function() { cb(); } - var denodeifiedFunc = RSVP.denodeify(nodeFunc); + var denodeifiedFunc = Promise.denodeify(nodeFunc); denodeifiedFunc(1, 2, 3).then(function() { assert.deepEqual(args, [1, 2, 3]); @@ -473,7 +473,7 @@ describe("RSVP extensions", function() { cb(); } - var denodeifiedFunc = RSVP.denodeify(nodeFunc); + var denodeifiedFunc = Promise.denodeify(nodeFunc); var promise = new Promise(function(resolve) { resolve(1); }); var thenable = { then: function (onFulfilled) { onFulfilled(2); } }; @@ -490,7 +490,7 @@ describe("RSVP extensions", function() { cb(null, 'nodeFuncResult'); } - var denodeifiedFunc = RSVP.denodeify(nodeFunc); + var denodeifiedFunc = Promise.denodeify(nodeFunc); denodeifiedFunc().then(function(value) { assert.equal(value, 'nodeFuncResult'); @@ -498,12 +498,12 @@ describe("RSVP extensions", function() { }); }); - it('fulfilled with array if if user called RSVP.denodeify(nodeFunc, true)', function(done) { + it('fulfilled with array if if user called Promise.denodeify(nodeFunc, true)', function(done) { function nodeFunc(cb) { cb(null, 1, 2, 3); } - var denodeifiedFunc = RSVP.denodeify(nodeFunc, true); + var denodeifiedFunc = Promise.denodeify(nodeFunc, true); denodeifiedFunc().then(function(value) { assert.deepEqual(value, [1, 2, 3]); @@ -511,12 +511,12 @@ describe("RSVP extensions", function() { }); }); - it('fulfilled with hash if if user called RSVP.denodeify with an array as second argument', function(done) { + it('fulfilled with hash if if user called Promise.denodeify with an array as second argument', function(done) { function nodeFunc(cb) { cb(null, 1, 2, 3); } - var denodeifiedFunc = RSVP.denodeify(nodeFunc, ['a', 'b', 'c']); + var denodeifiedFunc = Promise.denodeify(nodeFunc, ['a', 'b', 'c']); denodeifiedFunc().then(function(value) { assert.equal(value.a, '1'); @@ -531,7 +531,7 @@ describe("RSVP extensions", function() { cb(null, 42, 2, 3); } - var denodeifiedFunc = RSVP.denodeify(nodeFunc); + var denodeifiedFunc = Promise.denodeify(nodeFunc); denodeifiedFunc().then(function(value) { assert.equal(value, 42); @@ -544,7 +544,7 @@ describe("RSVP extensions", function() { cb('bad!'); } - var denodeifiedFunc = RSVP.denodeify(nodeFunc); + var denodeifiedFunc = Promise.denodeify(nodeFunc); denodeifiedFunc().then(function() { assert(false); @@ -560,7 +560,7 @@ describe("RSVP extensions", function() { throw 'bad!'; } - var denodeifiedFunc = RSVP.denodeify(nodeFunc); + var denodeifiedFunc = Promise.denodeify(nodeFunc); denodeifiedFunc().then(function() { assert(false); @@ -578,7 +578,7 @@ describe("RSVP extensions", function() { } nodeFunc.something = 'test123'; - var denodeifiedFunc = RSVP.denodeify(nodeFunc); + var denodeifiedFunc = Promise.denodeify(nodeFunc); assert.equal(denodeifiedFunc.something, 'test123'); @@ -606,8 +606,8 @@ describe("RSVP extensions", function() { }, 0); } - var denodeifiedReadFile = RSVP.denodeify(readFile); - var denodeifiedWriteFile = RSVP.denodeify(writeFile); + var denodeifiedReadFile = Promise.denodeify(readFile); + var denodeifiedWriteFile = Promise.denodeify(writeFile); denodeifiedWriteFile('dest.txt', denodeifiedReadFile('src.txt')).then(function () { assert.deepEqual(writtenTo, ['dest.txt', 'contents of src.txt']); @@ -639,7 +639,7 @@ describe("RSVP extensions", function() { SuchSubclass.prototype.constructor = SuchSubclass; SuchSubclass.prototype._super$constructor = Promise; - var denodeifiedWriteFile = RSVP.denodeify(writeFile); + var denodeifiedWriteFile = Promise.denodeify(writeFile); var file = new SuchSubclass(function(resolve) { readFile('src.txt', function(error, content) { @@ -658,9 +658,9 @@ describe("RSVP extensions", function() { }); }); - describe("RSVP.hash", function() { + describe("Promise.hash", function() { it('should exist', function() { - assert(RSVP.hash); + assert(Promise.hash); }); it('fulfilled only after all of the promise values are fulfilled', function(done) { @@ -688,7 +688,7 @@ describe("RSVP extensions", function() { secondResolver(true); }, 0); - RSVP.hash({ + Promise.hash({ first: first, second: second }).then(function(values) { @@ -723,7 +723,7 @@ describe("RSVP extensions", function() { secondCompleted = true; }); - RSVP.hash({ + Promise.hash({ first: first, second: second }).then(function() { @@ -736,22 +736,22 @@ describe("RSVP extensions", function() { }); }); - it('resolves an empty hash passed to RSVP.hash()', function(done) { - RSVP.hash({}).then(function(results) { + it('resolves an empty hash passed to Promise.hash()', function(done) { + Promise.hash({}).then(function(results) { assert.deepEqual(results, {}, 'expected fulfillment'); done(); }).catch(done); }); it('works with null', function(done) { - RSVP.hash({foo: null}).then(function(results) { + Promise.hash({foo: null}).then(function(results) { assert.deepEqual(results.foo, null); done(); }).catch(done); }); it('works with a truthy value', function(done) { - RSVP.hash({foo: 1}).then(function(results) { + Promise.hash({foo: 1}).then(function(results) { assert.deepEqual(results.foo, true); done(); }).catch(done); @@ -778,7 +778,7 @@ describe("RSVP extensions", function() { var nonPromise = 4; - RSVP.hash({ + Promise.hash({ promise: promise, syncThenable: syncThenable, asyncThenable: asyncThenable, @@ -798,7 +798,7 @@ describe("RSVP extensions", function() { it('works with an object that does not inherit from Object.prototype', function(done) { var hash = Object.create(null) hash.someValue = Promise.resolve('hello') - RSVP.hash(hash).then(function(results) { + Promise.hash(hash).then(function(results) { assert.deepEqual(results, { someValue: 'hello' }); done(); }); @@ -806,9 +806,9 @@ describe("RSVP extensions", function() { }); - describe("RSVP.hashSettled", function() { + describe("Promise.hashSettled", function() { it('should exist', function() { - assert(RSVP.hashSettled); + assert(Promise.hashSettled); }); it('fulfilled only after all of the promise values are fulfilled', function(done) { @@ -836,7 +836,7 @@ describe("RSVP extensions", function() { secondResolver(true); }, 0); - RSVP.hashSettled({ + Promise.hashSettled({ first: first, second: second }).then(function(values) { @@ -851,29 +851,29 @@ describe("RSVP extensions", function() { reject(new Error('WHOOPS')); }); var entries = { rejectedPromise: rejectedPromise }; - RSVP.hashSettled(entries).then(function(results) { + Promise.hashSettled(entries).then(function(results) { assert(results.rejectedPromise.state, 'rejected' ); assert(results.rejectedPromise.reason.message, 'WHOOPS' ); done(); }).catch(done); }); - it('resolves an empty hash passed to RSVP.hashSettled()', function(done) { - RSVP.hashSettled({}).then(function(results) { + it('resolves an empty hash passed to Promise.hashSettled()', function(done) { + Promise.hashSettled({}).then(function(results) { assert.deepEqual(results, {}, 'expected fulfillment'); done(); }).catch(done); }); it('works with null', function(done) { - RSVP.hashSettled({foo: null}).then(function(results) { + Promise.hashSettled({foo: null}).then(function(results) { assert.deepEqual(results.foo, {state: 'fulfilled', value: null} ); done(); }).catch(done); }); it('works with a truthy value', function(done) { - RSVP.hashSettled({foo: 1}).then(function(results) { + Promise.hashSettled({foo: 1}).then(function(results) { assert.deepEqual(results.foo, {state: 'fulfilled', value: true} ); done(); }).catch(done); @@ -896,7 +896,7 @@ describe("RSVP extensions", function() { rejectedPromise: rejectedPromise }; - RSVP.hashSettled(entries).then(function(results) { + Promise.hashSettled(entries).then(function(results) { assert.deepEqual(results.promise, {state: 'fulfilled', value: 1} ); assert.deepEqual(results.syncThenable, {state: 'fulfilled', value: 2} ); assert.deepEqual(results.asyncThenable, {state: 'fulfilled', value: 3} ); @@ -1460,7 +1460,7 @@ describe("RSVP extensions", function() { }); it('resolves an empty array to forever pending Promise', function(done) { - var foreverPendingPromise = RSVP.race([]), + var foreverPendingPromise = Promise.race([]), wasSettled = false; foreverPendingPromise.then(function() { @@ -1495,8 +1495,8 @@ describe("RSVP extensions", function() { }); } - describe("RSVP.race", function() { - testRace(RSVP.race); + describe("Promise.race", function() { + testRace(Promise.race); }); describe("Promise.race", function() { @@ -2078,7 +2078,7 @@ describe("RSVP extensions", function() { done(); }); - deferred = RSVP.defer(); + deferred = Promise.defer(); promise = deferred.promise; }); @@ -2099,8 +2099,8 @@ describe("RSVP extensions", function() { lastIndex= parsedGuid.index; }); - RSVP.defer(); - RSVP.defer(); + Promise.defer(); + Promise.defer(); }); }); @@ -2419,7 +2419,7 @@ describe("RSVP extensions", function() { }).catch(done); }); - it("filters falsy values correctly", function(done){ + it("filters falsy values correctly 1", function(done){ var promises = [ Promise.resolve(false), Promise.resolve(undefined), @@ -2434,6 +2434,37 @@ describe("RSVP extensions", function() { }).catch(done); }); + it("filters falsy values correctly 2", function(done){ + var promises = [ + Promise.resolve(false), + Promise.resolve(undefined), + Promise.resolve(null), + Promise.resolve(0), + Promise.resolve('') + ]; + + Promise.filter(promises, function(val){ return val; }).then(function(results){ + assert.equal(results.length, 0); + done(); + }).catch(done); + }); + + it("filters truthy values correctly", function(done){ + var promises = [ + Promise.resolve(true), + Promise.resolve(1), + Promise.resolve(-10), + Promise.resolve('a'), + Promise.resolve({}), + Promise.resolve([]) + ]; + + Promise.filter(promises, function(val){ return val; }).then(function(results){ + assert.deepEqual(results, [true, 1, -10, 'a', {}, []]); + done(); + }).catch(done); + }); + it("catches error thrown from filterFn", function(done){ var throwerFilter = function(val) { if (val === 2) {