Comparing version 1.0.0 to 1.0.1
@@ -1,1 +0,1 @@ | ||
'use strict';Object.defineProperty(exports,'__esModule',{value:true});function checkThenable(a){try{var b=a.then;}catch(a){return !1}return "function"==typeof b}var splice=[].splice;function build(a,b){return Object.defineProperty(function a(c,d){if("function"!=typeof d)throw new TypeError(d+" is not a function");var e,f=this,g=arguments;return checkThenable(c)?c.then(function(b){return g[0]=b,a.apply(f,g)}):(e=b.call(f,c,d),2<g.length?(splice.call(g,0,2,e),a.apply(f,g)):e)},"name",{value:a,configurable:!0})}var run=build("run",function(a,b){return b.call(this,a)}),runIf=build("runIf",function(a,b){return null==a?a:b.call(this,a)}),apply=build("apply",function(a,b,c){return checkThenable(c=b.call(this,a))?c.then(function(){return a}):a});exports.apply=apply;exports.run=run;exports.runIf=runIf; | ||
'use strict';function takeThen(a,b){if(null!=a){try{b=a.then;}catch(a){return}return "function"==typeof b&&b.bind(a)}}var splice=[].splice;function build(a,b){return Object.defineProperty(function d(a,c){if("function"!=typeof c)throw new TypeError(c+" is not a function");var e,f=this,g=arguments,h=takeThen(a);return h?h(function(a){return g[0]=a,d.apply(f,g)}):(e=b.call(f,a,c),2<g.length?(splice.call(g,0,2,e),d.apply(f,g)):e)},"name",{value:a,configurable:!0})}var run=build("run",function(a,b){return b.call(this,a)}),runIf=build("runIf",function(a,b){return null==a?a:b.call(this,a)}),apply=build("apply",function(a,b,c){return (c=takeThen(b.call(this,a)))?c(function(){return a}):a});exports.apply=apply;exports.run=run;exports.runIf=runIf; |
@@ -1,1 +0,1 @@ | ||
function checkThenable(a){try{var b=a.then;}catch(a){return !1}return "function"==typeof b}var splice=[].splice;function build(a,b){return Object.defineProperty(function a(c,d){if("function"!=typeof d)throw new TypeError(d+" is not a function");var e,f=this,g=arguments;return checkThenable(c)?c.then(function(b){return g[0]=b,a.apply(f,g)}):(e=b.call(f,c,d),2<g.length?(splice.call(g,0,2,e),a.apply(f,g)):e)},"name",{value:a,configurable:!0})}var run=build("run",function(a,b){return b.call(this,a)}),runIf=build("runIf",function(a,b){return null==a?a:b.call(this,a)}),apply=build("apply",function(a,b,c){return checkThenable(c=b.call(this,a))?c.then(function(){return a}):a});export{apply,run,runIf}; | ||
function takeThen(a,b){if(null!=a){try{b=a.then;}catch(a){return}return "function"==typeof b&&b.bind(a)}}var splice=[].splice;function build(a,b){return Object.defineProperty(function d(a,c){if("function"!=typeof c)throw new TypeError(c+" is not a function");var e,f=this,g=arguments,h=takeThen(a);return h?h(function(a){return g[0]=a,d.apply(f,g)}):(e=b.call(f,a,c),2<g.length?(splice.call(g,0,2,e),d.apply(f,g)):e)},"name",{value:a,configurable:!0})}var run=build("run",function(a,b){return b.call(this,a)}),runIf=build("runIf",function(a,b){return null==a?a:b.call(this,a)}),apply=build("apply",function(a,b,c){return (c=takeThen(b.call(this,a)))?c(function(){return a}):a});export{apply,run,runIf}; |
type Nullish = null | undefined; | ||
/** | ||
* If `T` is an array of functions, a union of the return type of those functions. | ||
*/ | ||
type ReturnTypes<T extends Array<(...a: Array<any>) => any>> = T extends Array<(...a: Array<any>) => infer R> ? R : never; | ||
/** | ||
* If `T` is a `Promise`, the type of the values to which the promise resolves. Otherwise `T` itself. | ||
@@ -7,2 +11,6 @@ */ | ||
/** | ||
* An intersection of the types of which the union `T` consists. | ||
*/ | ||
type Intersect<T> = (T extends any ? (faux: T) => void : never) extends (faux: infer U) => void ? U : never; | ||
/** | ||
* Like `Exclude` except that if `T` is a `Promise`, the exclusion logic is applied to the type of the values to which | ||
@@ -120,12 +128,7 @@ * the promise resolves instead of to `T` directly. | ||
*/ | ||
declare function apply<T, Z, C>(this: C, value: T, ...callbacks: Array<(this: C, value: Resolve<T>) => Z>): | ||
declare function apply<T, Z, C>(this: C, value: T, callback: (this: C, value: Resolve<T>) => Z): | ||
TransferAsynchronicity<Z, T>; | ||
declare function apply<T, Z, Y, C>(this: C, value: T, ...callbacks: [(this: C, value: Resolve<T>) => Z, (this: C, value: Resolve<T>) => Y]): | ||
TransferAsynchronicity<Z & Y, T>; | ||
declare function apply<T, Z, Y, X, C>(this: C, value: T, ...callbacks: [(this: C, value: Resolve<T>) => Z, (this: C, value: Resolve<T>) => Y, (this: C, value: Resolve<T>) => X]): | ||
TransferAsynchronicity<Z & Y & X, T>; | ||
declare function apply<T, Z, Y, X, W, C>(this: C, value: T, ...callbacks: [(this: C, value: Resolve<T>) => Z, (this: C, value: Resolve<T>) => Y, (this: C, value: Resolve<T>) => X, (this: C, value: Resolve<T>) => W]): | ||
TransferAsynchronicity<Z & Y & X & W, T>; | ||
declare function apply<T, Z, Y, X, W, V, C>(this: C, value: T, ...callbacks: [(this: C, value: Resolve<T>) => Z, (this: C, value: Resolve<T>) => Y, (this: C, value: Resolve<T>) => X, (this: C, value: Resolve<T>) => W, (this: C, value: Resolve<T>) => V]): | ||
TransferAsynchronicity<Z & Y & X & W & V, T>; | ||
// ↑ This overload is not strictly necessary. The one below is a generalised form of it. | ||
declare function apply<T, U extends Array<(this: C, value: Resolve<T>) => any>, C>(this: C, value: T, ...callbacks: U): | ||
TransferAsynchronicity<Intersect<ReturnTypes<U>>, T>; | ||
@@ -132,0 +135,0 @@ export { |
{ | ||
"name": "ruply", | ||
"description": "run[If] and apply functions for easy-to-read code", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"main": "compiled/cjs/ruply.min.js", | ||
@@ -16,11 +16,11 @@ "module": "compiled/esm/ruply.min.js", | ||
"devDependencies": { | ||
"@babel/core": "^7.15.0", | ||
"@babel/preset-env": "^7.15.0", | ||
"@rollup/plugin-babel": "^5.3.0", | ||
"babel-jest": "^27.1.0", | ||
"babel-preset-minify": "^0.5.1", | ||
"jest": "^27.1.0", | ||
"rollup": "^2.56.3", | ||
"tsd": "^0.17.0", | ||
"typescript": "^4.4.2" | ||
"@babel/core": "^7.20.5", | ||
"@babel/preset-env": "^7.20.2", | ||
"@rollup/plugin-babel": "^6.0.3", | ||
"babel-jest": "^29.3.1", | ||
"babel-preset-minify": "^0.5.2", | ||
"jest": "^29.3.1", | ||
"rollup": "^3.5.1", | ||
"tsd": "^0.25.0", | ||
"typescript": "^4.9.3" | ||
}, | ||
@@ -27,0 +27,0 @@ "scripts": { |
@@ -0,1 +1,3 @@ | ||
# ruply · [![License (X11/MIT)](https://badgen.net/github/license/pimm/ruply)](https://github.com/Pimm/ruply/blob/master/copying.txt) [![npm version](https://badgen.net/npm/v/ruply)](https://www.npmjs.com/package/ruply) [![Build Status](https://api.travis-ci.com/Pimm/ruply.svg?branch=master)](https://app.travis-ci.com/github/Pimm/ruply) [![Coverage status](https://coveralls.io/repos/github/Pimm/ruply/badge.svg?branch=master)](https://coveralls.io/github/Pimm/ruply?branch=master) | ||
`run[If]` and `apply` are functions that can help you craft easy-to-read code. | ||
@@ -2,0 +4,0 @@ |
@@ -1,2 +0,2 @@ | ||
import checkThenable from './checkThenable'; | ||
import takeThen from './takeThen'; | ||
@@ -23,5 +23,6 @@ // Steal the splice function from this empty array. | ||
var result; | ||
// If the value is thenable, recall this function (recursively) once it resolves. | ||
if (checkThenable(value)) { | ||
return value.then(value => { | ||
// If the value is promise-like, recall this function (recursively) once it resolves. | ||
const then = takeThen(value); | ||
if (then) { | ||
return then(value => { | ||
// const [, ...callbacks] = arguments; | ||
@@ -138,15 +139,10 @@ // return implementation.call(context, value, ...callbacks); | ||
'apply', | ||
function applyLogic(value, callback, result) { | ||
if ( | ||
// Call the callback. If the result is thenable, chain a function to it which will return the value, and return | ||
// that chain. | ||
checkThenable( | ||
result = callback.call(this, value) | ||
) | ||
) { | ||
return result.then(() => value); | ||
} | ||
// If the result is not thenable, return the value. | ||
return value; | ||
function applyLogic(value, callback, /* This is never provided, thus initially undefined → */ then) { | ||
// Call the callback and check whether the result is promise-like. | ||
return (then = takeThen(callback.call(this, value))) | ||
// If the result is promise-like, chain a function to it which will return the value, then return that chain. | ||
? then(() => value) | ||
// If the result is not promise-like, return the value. | ||
: value; | ||
} | ||
); |
26258
302
281