Comparing version 0.1.6 to 0.1.8
@@ -1,34 +0,47 @@ | ||
module.exports = Y; | ||
(function (factory) { | ||
if (typeof exports === 'object') { | ||
module.exports = factory(); | ||
} else { | ||
this.Y = factory(); | ||
} | ||
})(function () { | ||
'use strict'; | ||
Y.isPromise = function(obj) { | ||
return !!obj && typeof obj.then === 'function'; | ||
function isFunction(fn) { | ||
return fn instanceof Function; | ||
} | ||
Y.isPromise = function (obj) { | ||
return !!obj && isFunction(obj.then); | ||
}; | ||
Y.isPromiseArray = function(arr) { | ||
return arr instanceof Array && arr.filter(function(elem) { return Y.isPromise(elem); }).length > 0; | ||
Y.isPromiseArray = function (arr) { | ||
return arr instanceof Array && arr.some(Y.isPromise); | ||
}; | ||
Y.isGenerator = function(obj) { | ||
Y.isGenerator = function (obj) { | ||
return Object.prototype.toString.call(obj) === '[object Generator]'; | ||
}; | ||
Y.isGeneratorFn = function(fn) { | ||
return typeof fn === 'function' && fn.constructor.name === 'GeneratorFunction'; | ||
Y.isGeneratorFn = function (fn) { | ||
return isFunction(fn) && fn.constructor.name === 'GeneratorFunction'; | ||
}; | ||
var AP = Array.prototype; | ||
Y.ncall = function(fn) { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
return Y.napply(fn, args); | ||
Y.ncall = function (fn) { | ||
fn = AP.shift.call(arguments); | ||
return Y.napply(fn, arguments); | ||
}; | ||
Y.napply = function(fn, args) { | ||
Y.napply = function (fn, args) { | ||
var resolve, reject; | ||
args.push(function(err, res) { | ||
if (!err) resolve(res); | ||
else reject(err); | ||
AP.push.call(args, function (err, res) { | ||
!err ? resolve(res) : reject(err); | ||
}); | ||
return { | ||
then: function(resolveFn, rejectFn) { | ||
then: function (resolveFn, rejectFn) { | ||
resolve = resolveFn; | ||
@@ -41,25 +54,27 @@ reject = rejectFn; | ||
Y.nwrap = function(fn) { | ||
Y.nwrap = function (fn) { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
return function() { | ||
return Y.napply(fn, args.concat( Array.prototype.slice.call(arguments) )); | ||
return function () { | ||
return Y.napply(fn, args.concat( Array.prototype.slice.call(arguments) )); | ||
}; | ||
}; | ||
Y.all = function(arr) { | ||
if ( !(arr instanceof Array) ) | ||
arr = Array.prototype.slice.call(arguments); | ||
Y.all = function (arr) { | ||
if (!(arr instanceof Array)) { | ||
arr = AP.slice.call(arguments); | ||
} | ||
return { | ||
then: function(resolveFn, rejectFn) { | ||
for (var i = 0; i < arr.length; i++) { | ||
if (Y.isPromise(arr[i])) (function(index) { | ||
arr[index].then(function(res) { | ||
arr[index] = res; | ||
if (!Y.isPromiseArray(arr)) { | ||
resolveFn(arr); | ||
} | ||
}, rejectFn); | ||
})(i); | ||
} | ||
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); | ||
}); | ||
} | ||
@@ -71,3 +86,5 @@ }; | ||
function Y(fn) { | ||
'use strict'; | ||
if (!Y.isGeneratorFn(fn)) { | ||
return fn; | ||
} | ||
@@ -77,5 +94,12 @@ var gen, ctx = this; | ||
function callNext(value) { | ||
value = value || resolve.value; | ||
if (!arguments.length) { | ||
value = resolve.value; | ||
} | ||
var next = gen.next(value); | ||
if (next.done) return next.value; | ||
if (next.done) { | ||
return next.value; | ||
} | ||
if (Y.isPromise(next.value)) { | ||
@@ -91,3 +115,6 @@ return next.value.then(callNext, gen.throw.bind(gen)); | ||
function resolve() { | ||
if (!gen) gen = fn.apply(ctx, arguments); | ||
if (!gen) { | ||
gen = fn.apply(ctx, arguments); | ||
} | ||
return callNext(); | ||
@@ -97,18 +124,24 @@ } | ||
resolve.once = function() { | ||
if (!gen) gen = fn.apply(ctx, arguments); | ||
resolve.value = gen.next(resolve.value).value; | ||
return resolve.value; | ||
if (!gen) { | ||
gen = fn.apply(ctx, arguments); | ||
} | ||
return resolve.value = gen.next(resolve.value).value; | ||
}; | ||
resolve.toArray = function() { | ||
var arrayGen = fn.apply(ctx, arguments); | ||
var values = [], next = arrayGen.next(); | ||
while (!next.done) { | ||
values.push(next.value); | ||
next = arrayGen.next(next.value); | ||
var values = []; | ||
for (var value of fn.apply(ctx, arguments)) { | ||
values.push(value); | ||
} | ||
return values; | ||
}; | ||
return Y.isGeneratorFn(fn) ? resolve : fn; | ||
return resolve; | ||
}; | ||
return Y; | ||
}); |
{ | ||
"name": "yielding", | ||
"version": "0.1.6", | ||
"version": "0.1.8", | ||
"description": "Easy generators", | ||
"main": "index.js", | ||
"main": "./lib/yielding.js", | ||
"scripts": { | ||
"preinstall": "npm install mocha -g", | ||
"test": "mocha --harmony --reporter=spec test/test.js" | ||
@@ -30,4 +31,5 @@ }, | ||
"q": "~0.9.7", | ||
"chai": "~1.8.1" | ||
"chai": "~1.8.1", | ||
"request": "~2.27.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12477
304
3
16
1