-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
105 additions
and
32 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Returns the `then` property of the passed input, bound to the passed input, if said passed input is promise-like. | ||
* Returns a falsy value (specifically `undefined` or `false`) otherwise. | ||
* | ||
* An input is considered promise-like if it has a `then` property which can be retrieved and is a function. | ||
*/ | ||
export default function takeThen(input, /* This is never provided, thus initially undefined → */ then) { | ||
// Return undefined if the input is null-ish. The try-catch below would cause undefined to be returned for those | ||
// inputs anyway, making this if block technically redundant. However, it does speed up this function significantly | ||
// for those inputs. See https://github.com/Pimm/ruply/issues/2. | ||
if (null == input) { | ||
return /* undefined */; | ||
} | ||
// Return undefined if retrieving the then property causes an error to be thrown. Since the promise awareness is | ||
// somewhat of a hidden feature of this library, it should operate as unintrusive as possible. | ||
try { | ||
({ then } = input); | ||
} catch (error) { | ||
return /* undefined */; | ||
} | ||
// return 'function' == typeof then ? then.bind(input) : false; | ||
// ↓ | ||
return 'function' == typeof then && then.bind(input); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
|
||
import { run, runIf, apply } from '..'; | ||
|
||
function createCallback() { | ||
return jest.fn(() => 'result'); | ||
} | ||
|
||
const hostileObject = { | ||
get then() { | ||
throw 'error'; | ||
} | ||
}; | ||
|
||
const hostileProxy = new Proxy({}, { | ||
get(_, name) { | ||
if ('then' == name) { | ||
throw 'error'; | ||
} | ||
} | ||
}); | ||
|
||
test('throwing-inputs', () => { | ||
expect.assertions(12); | ||
// run with hostile object. | ||
run(createCallback(), callback => { | ||
expect( | ||
() => run(hostileObject, callback) | ||
).not.toThrow(); | ||
expect(callback).toHaveBeenCalledWith(hostileObject); | ||
}); | ||
// runIf with hostile object. | ||
run(createCallback(), callback => { | ||
expect( | ||
() => runIf(hostileObject, callback) | ||
).not.toThrow(); | ||
expect(callback).toHaveBeenCalledWith(hostileObject); | ||
}); | ||
// apply with hostile object. | ||
run(createCallback(), callback => { | ||
expect( | ||
() => apply(hostileObject, callback) | ||
).not.toThrow(); | ||
expect(callback).toHaveBeenCalledWith(hostileObject); | ||
}); | ||
// run with hostile proxy. | ||
run(createCallback(), callback => { | ||
expect( | ||
() => run(hostileProxy, callback) | ||
).not.toThrow(); | ||
expect(callback).toHaveBeenCalledWith(hostileProxy); | ||
}); | ||
// runIf with hostile proxy. | ||
run(createCallback(), callback => { | ||
expect( | ||
() => runIf(hostileProxy, callback) | ||
).not.toThrow(); | ||
expect(callback).toHaveBeenCalledWith(hostileProxy); | ||
}); | ||
// apply with hostile proxy. | ||
run(createCallback(), callback => { | ||
expect( | ||
() => apply(hostileProxy, callback) | ||
).not.toThrow(); | ||
expect(callback).toHaveBeenCalledWith(hostileProxy); | ||
}); | ||
}); |