@blakek/compose
Advanced tools
Comparing version 3.0.1 to 4.0.0
@@ -5,2 +5,9 @@ 'use strict'; | ||
/** | ||
* Loosely checks if a value is a promise (thenable) | ||
*/ | ||
function isPromise(object) { | ||
return object instanceof Promise || Boolean(object) && (typeof object === 'object' || typeof object === 'function') && typeof object.then === 'function'; | ||
} | ||
function compose(...[fn, ...fns]) { | ||
@@ -13,3 +20,11 @@ // All arguments have been exhausted | ||
return async (...args) => fn(await compose(...fns)(...args)); | ||
return (...args) => { | ||
const value = compose(...fns)(...args); | ||
if (isPromise(value)) { | ||
return Promise.resolve(value).then(value => fn(value)); | ||
} | ||
return fn(value); | ||
}; | ||
} | ||
@@ -23,3 +38,11 @@ function pipe(...[fn, ...fns]) { | ||
return async (...args) => pipe(...fns)(await fn(...args)); | ||
return (...args) => { | ||
const value = fn(...args); | ||
if (isPromise(value)) { | ||
return Promise.resolve(value).then(value => pipe(...fns)(value)); | ||
} | ||
return pipe(...fns)(value); | ||
}; | ||
} | ||
@@ -26,0 +49,0 @@ var index = { |
@@ -0,1 +1,8 @@ | ||
/** | ||
* Loosely checks if a value is a promise (thenable) | ||
*/ | ||
function isPromise(object) { | ||
return object instanceof Promise || Boolean(object) && (typeof object === 'object' || typeof object === 'function') && typeof object.then === 'function'; | ||
} | ||
function compose(...[fn, ...fns]) { | ||
@@ -8,3 +15,11 @@ // All arguments have been exhausted | ||
return async (...args) => fn(await compose(...fns)(...args)); | ||
return (...args) => { | ||
const value = compose(...fns)(...args); | ||
if (isPromise(value)) { | ||
return Promise.resolve(value).then(value => fn(value)); | ||
} | ||
return fn(value); | ||
}; | ||
} | ||
@@ -18,3 +33,11 @@ function pipe(...[fn, ...fns]) { | ||
return async (...args) => pipe(...fns)(await fn(...args)); | ||
return (...args) => { | ||
const value = fn(...args); | ||
if (isPromise(value)) { | ||
return Promise.resolve(value).then(value => pipe(...fns)(value)); | ||
} | ||
return pipe(...fns)(value); | ||
}; | ||
} | ||
@@ -21,0 +44,0 @@ var index = { |
@@ -21,6 +21,9 @@ (function (global, factory) { | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
/** | ||
* Loosely checks if a value is a promise (thenable) | ||
*/ | ||
function isPromise(object) { | ||
return object instanceof Promise || Boolean(object) && (typeof object === 'object' || typeof object === 'function') && typeof object.then === 'function'; | ||
} | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
function compose() { | ||
@@ -40,32 +43,22 @@ for (var _len = arguments.length, _ref = new Array(_len), _key = 0; _key < _len; _key++) { | ||
return /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var _args = arguments; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.t0 = fn; | ||
_context.next = 3; | ||
return compose.apply(void 0, fns).apply(void 0, _args); | ||
return function () { | ||
var value = compose.apply(void 0, fns).apply(void 0, arguments); | ||
case 3: | ||
_context.t1 = _context.sent; | ||
return _context.abrupt("return", (0, _context.t0)(_context.t1)); | ||
if (isPromise(value)) { | ||
return Promise.resolve(value).then(function (value) { | ||
return fn(value); | ||
}); | ||
} | ||
case 5: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
return fn(value); | ||
}; | ||
} | ||
function pipe() { | ||
for (var _len2 = arguments.length, _ref3 = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
_ref3[_key2] = arguments[_key2]; | ||
for (var _len2 = arguments.length, _ref2 = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
_ref2[_key2] = arguments[_key2]; | ||
} | ||
var fn = _ref3[0], | ||
fns = _ref3.slice(1); | ||
var fn = _ref2[0], | ||
fns = _ref2.slice(1); | ||
@@ -78,23 +71,13 @@ // All arguments have been exhausted | ||
return /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
var _args2 = arguments; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_context2.t0 = pipe.apply(void 0, fns); | ||
_context2.next = 3; | ||
return fn.apply(void 0, _args2); | ||
return function () { | ||
var value = fn.apply(void 0, arguments); | ||
case 3: | ||
_context2.t1 = _context2.sent; | ||
return _context2.abrupt("return", (0, _context2.t0)(_context2.t1)); | ||
if (isPromise(value)) { | ||
return Promise.resolve(value).then(function (value) { | ||
return pipe.apply(void 0, fns)(value); | ||
}); | ||
} | ||
case 5: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2); | ||
})); | ||
return pipe.apply(void 0, fns)(value); | ||
}; | ||
} | ||
@@ -101,0 +84,0 @@ |
{ | ||
"name": "@blakek/compose", | ||
"version": "3.0.1", | ||
"version": "4.0.0", | ||
"main": "dist/compose.cjs.js", | ||
@@ -44,3 +44,5 @@ "module": "dist/compose.esm.js", | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"@blakek/is-promise": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
@@ -47,0 +49,0 @@ "@babel/core": "^7.11.6", |
Sorry, the diff of this file is not supported yet
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
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
105492
158
1
+ Added@blakek/is-promise@^1.0.0
+ Added@blakek/is-promise@1.0.0(transitive)