Comparing version 0.2.2 to 0.3.0
var Y = require('..'); | ||
var fs = require('fs'); | ||
Y.ncall(fs.readFile, './examples/Y.js', 'utf-8').then(function(content) { | ||
Y.ncall(fs.readFile, './examples/Y.js', 'utf8').then(function(content) { | ||
console.log(content.length); | ||
}); |
@@ -7,3 +7,3 @@ var fs = require('fs'); | ||
function* getContent(filename) { | ||
var content = yield read(filename, 'utf-8'); | ||
var content = yield read(filename, 'utf8'); | ||
console.log(content.length); | ||
@@ -10,0 +10,0 @@ }; |
@@ -0,0 +0,0 @@ var Y = require('..'); |
@@ -8,3 +8,3 @@ var fs = require('fs'); | ||
try { | ||
var content = yield Y.ncall(fs.readFile, 'non_exists_file', 'utf-8'); | ||
var content = yield Y.ncall(fs.readFile, 'non_exists_file', 'utf8'); | ||
} catch(e) { | ||
@@ -11,0 +11,0 @@ console.log('ERROR: ' + e.message); |
@@ -0,0 +0,0 @@ var Y = require('..'); |
@@ -8,3 +8,3 @@ var Y = require('..'); | ||
console.log('start read file ' + filename); | ||
fs.readFile(filename, 'utf-8', function(err, res) { | ||
fs.readFile(filename, 'utf8', function(err, res) { | ||
console.log('end read file ' + filename); | ||
@@ -11,0 +11,0 @@ if (!err) d.resolve(res); |
@@ -0,0 +0,0 @@ var Y = require('..'); |
@@ -0,0 +0,0 @@ var q = require('q'); |
@@ -7,3 +7,3 @@ var fs = require('fs'); | ||
function* getContent(filename) { | ||
var content = yield read(filename, 'utf-8'); | ||
var content = yield read(filename, 'utf8'); | ||
console.log(content.length); | ||
@@ -10,0 +10,0 @@ }; |
@@ -0,0 +0,0 @@ var Y = require('..'); |
@@ -1,8 +0,8 @@ | ||
(function (factory) { | ||
(function (factory, global) { | ||
if (typeof exports === 'object') { | ||
module.exports = factory(); | ||
module.exports = factory(require('es6-promise').Promise); | ||
} else { | ||
this.Y = factory(); | ||
this.Y = factory(global.Promise); | ||
} | ||
})(function () { | ||
})(function (Promise) { | ||
@@ -28,19 +28,19 @@ 'use strict'; | ||
Y.isFunction = function (fn) { | ||
Y.isFunction = function(fn) { | ||
return fn instanceof Function; | ||
}; | ||
Y.isPromise = function (obj) { | ||
Y.isPromise = function(obj) { | ||
return !!obj && Y.isFunction(obj.then); | ||
}; | ||
Y.isPromiseArray = function (arr) { | ||
Y.isPromiseArray = function(arr) { | ||
return arr instanceof Array && arr.some(Y.isPromise); | ||
}; | ||
Y.isGenerator = function (obj) { | ||
Y.isGenerator = function(obj) { | ||
return toString.call(obj) === '[object Generator]'; | ||
}; | ||
Y.isGeneratorFn = function (fn) { | ||
Y.isGeneratorFn = function(fn) { | ||
return Y.isFunction(fn) && fn.constructor.name === 'GeneratorFunction'; | ||
@@ -52,3 +52,3 @@ }; | ||
Y.ncall = function (fn) { | ||
Y.ncall = function(fn) { | ||
fn = shift.call(arguments); | ||
@@ -58,22 +58,15 @@ return Y.napply(fn, arguments); | ||
Y.napply = function (fn, args) { | ||
var resolve, reject; | ||
push.call(args, function (err, res) { | ||
!err ? resolve(res) : reject(err); | ||
Y.napply = function(fn, args) { | ||
return new Promise(function(resolve, reject) { | ||
push.call(args, function(err, res) { | ||
!err ? resolve(res) : reject(err); | ||
}); | ||
return fn.apply(this, args); | ||
}); | ||
return { | ||
then: function (resolveFn, rejectFn) { | ||
resolve = resolveFn; | ||
reject = rejectFn; | ||
return fn.apply(this, args); | ||
} | ||
} | ||
}; | ||
Y.nwrap = function (fn) { | ||
Y.nwrap = function(fn) { | ||
var args = slice.call(arguments, 1); | ||
return function () { | ||
return function() { | ||
return Y.napply(fn, args.concat( slice.call(arguments) )); | ||
@@ -85,21 +78,8 @@ }; | ||
Y.all = function (arr) { | ||
Y.all = function(arr) { | ||
if (!(arr instanceof Array)) { | ||
arr = slice.call(arguments); | ||
} | ||
return { | ||
then: function (resolveFn, rejectFn) { | ||
arr.forEach(function (promise, i, arr) { | ||
if (!Y.isPromise(promise)) return; | ||
promise.then(function(res) { | ||
arr[i] = res; | ||
if (!Y.isPromiseArray(arr)) { | ||
resolveFn(arr); | ||
} | ||
}, rejectFn); | ||
}); | ||
} | ||
}; | ||
return Promise.all(arr); | ||
}; | ||
@@ -173,2 +153,2 @@ | ||
}); | ||
}, this); |
{ | ||
"name": "yielding", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "Easy generators", | ||
"main": "./lib/yielding.js", | ||
"scripts": { | ||
"preinstall": "npm install mocha -g", | ||
"test": "mocha --harmony --reporter=spec test/test.js" | ||
@@ -34,6 +33,9 @@ }, | ||
"devDependencies": { | ||
"q": "~0.9.7", | ||
"chai": "~1.8.1", | ||
"request": "~2.27.0" | ||
"chai": "^1.9.1", | ||
"mocha": "^1.19.0", | ||
"request": "^2.35.0" | ||
}, | ||
"dependencies": { | ||
"es6-promise": "^1.0.0" | ||
} | ||
} |
@@ -1,4 +0,3 @@ | ||
yielding | ||
======== | ||
Easy generators. | ||
#yielding [![NPM version][npm-image]][npm-url] [![Build Status](https://travis-ci.org/mikach/yielding.png?branch=master)](https://travis-ci.org/mikach/yielding) | ||
> Easy generators. | ||
@@ -97,1 +96,8 @@ Use Node version 0.11.x with `--harmony-generators` flag to work with es6 generators. | ||
[See more examples](https://github.com/mikach/yielding/tree/master/examples) | ||
[npm-url]: https://npmjs.org/package/yielding | ||
[npm-image]: https://badge.fury.io/js/yielding.png | ||
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/mikach/yielding/trend.png)](https://bitdeli.com/free "Bitdeli Badge") | ||
var Y = require('..'); | ||
var q = require('q'); | ||
var Promise = require('es6-promise').Promise; | ||
var fs = require('fs'); | ||
var expect = require('chai').expect; | ||
var readFile = function (name) { | ||
var d = q.defer(); | ||
fs.readFile(name, 'utf-8', function (err, res) { | ||
d.resolve(res); | ||
var readFile = function(name) { | ||
return new Promise(function(resolve, reject) { | ||
fs.readFile(name, 'utf8', function(err, res) { | ||
err ? reject(err) : resolve(res); | ||
}); | ||
}); | ||
return d.promise; | ||
}; | ||
describe('detect promises and generators', function () { | ||
describe('detect promises and generators', function() { | ||
var gen = function* () { | ||
@@ -19,15 +19,14 @@ yield 1; | ||
it('isPromise', function () { | ||
it('isPromise', function() { | ||
expect( Y.isPromise(readFile('test')) ).to.be.true; | ||
expect( Y.isPromise(q.defer().promise) ).to.be.true; | ||
expect( Y.isPromise() ).to.be.false; | ||
}); | ||
it('isPromiseArray', function () { | ||
it('isPromiseArray', function() { | ||
expect( Y.isPromiseArray([1,2,3]) ).to.be.false; | ||
expect( Y.isPromiseArray('test') ).to.be.false; | ||
expect( Y.isPromiseArray([1,2,q.defer().promise]) ).to.be.true; | ||
expect( Y.isPromiseArray([1,2,readFile('test')]) ).to.be.true; | ||
}); | ||
it('isGenerator', function () { | ||
it('isGenerator', function() { | ||
expect( Y.isGenerator(gen) ).to.be.false; | ||
@@ -37,3 +36,3 @@ expect( Y.isGenerator(gen()) ).to.be.true; | ||
it('isGeneratorFn', function () { | ||
it('isGeneratorFn', function() { | ||
expect( Y.isGeneratorFn(gen) ).to.be.true; | ||
@@ -44,3 +43,3 @@ expect( Y.isGeneratorFn(readFile) ).to.be.false; | ||
describe('Y function', function () { | ||
describe('Y function', function() { | ||
var gen = function* () { | ||
@@ -53,3 +52,3 @@ for (var i = 0; ++i < 10;) { | ||
it('should return function', function () { | ||
it('should return function', function() { | ||
var b = Y(gen); | ||
@@ -59,12 +58,12 @@ expect(b).to.be.a('function'); | ||
it('should return self if function is not a generator', function () { | ||
var emptyFn = function () {}; | ||
it('should return self if function is not a generator', function() { | ||
var emptyFn = function() {}; | ||
expect( Y(emptyFn) ).to.be.equal( emptyFn ); | ||
}); | ||
it('should return value', function () { | ||
it('should return value', function() { | ||
expect( Y(gen)() ).to.be.equal( 10 ); | ||
}); | ||
it('once()', function () { | ||
it('once()', function() { | ||
var b = Y(gen); | ||
@@ -76,3 +75,3 @@ expect( b.once() ).to.be.equal( 1 ); | ||
it('toArray() Sync', function () { | ||
it('toArray() Sync', function() { | ||
var b = Y(function* (limit) { | ||
@@ -124,4 +123,4 @@ for (var i = 0; i < limit; i++) { | ||
describe('nodejs functions wrappers', function () { | ||
describe('nwrap()', function () { | ||
describe('nodejs functions wrappers', function() { | ||
describe('nwrap()', function() { | ||
var filename = 'test/example.txt'; | ||
@@ -131,3 +130,3 @@ var read = Y.nwrap(fs.readFile); | ||
it('return promise', function () { | ||
it('return promise', function() { | ||
expect( Y.isPromise(read()) ).to.be.true; | ||
@@ -137,4 +136,4 @@ expect( Y.isPromise(readWithParams()) ).to.be.true; | ||
it('read a file', function (done) { | ||
read(filename, 'utf-8').then(function (content) { | ||
it('read a file', function(done) { | ||
read(filename, 'utf8').then(function(content) { | ||
expect(content).to.be.equal('Hello'); | ||
@@ -145,4 +144,4 @@ done(); | ||
it('read a file with params', function (done) { | ||
readWithParams().then(function (content) { | ||
it('read a file with params', function(done) { | ||
readWithParams().then(function(content) { | ||
expect(content).to.be.a('string'); | ||
@@ -155,4 +154,4 @@ done(); | ||
describe('async functions w/o wrapping in Y async scope', function () { | ||
it('read a file', function (done) { | ||
describe('async functions w/o wrapping in Y async scope', function() { | ||
it('read a file', function(done) { | ||
Y(function *async() { | ||
@@ -165,6 +164,6 @@ var content = yield fs.readFile('test/example.txt', 'utf8', async.resume); | ||
it('non-explicitly async function', function (done) { | ||
it('non-explicitly async function', function(done) { | ||
Y(function *async() { | ||
// void operator is needed since setTimeout returns !== undefined and Y thinks it's ready-to-use value | ||
var result = yield void setTimeout(function () { | ||
var result = yield void setTimeout(function() { | ||
async.resume(null, 123); | ||
@@ -177,3 +176,3 @@ }, 200); | ||
it('treat node error as exception', function (done) { | ||
it('treat node error as exception', function(done) { | ||
Y(function *async() { | ||
@@ -198,3 +197,3 @@ try { | ||
describe('errors handling', function () { | ||
it('should handle errors in promises', function (done) { | ||
it('should handle errors in promises', function(done) { | ||
var b = Y(function* () { | ||
@@ -205,4 +204,5 @@ var ex; | ||
} catch (e) { | ||
expect(e).to.be.instanceof(Error); | ||
ex = e; | ||
} finally { | ||
expect(ex).to.be.instanceof(Error); | ||
done(); | ||
@@ -212,2 +212,2 @@ } | ||
}); | ||
}); | ||
}); |
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
15869
17
103
0
1
374
+ Addedes6-promise@^1.0.0
+ Addedes6-promise@1.0.0(transitive)