@serenity-js/core
Version:
The core Serenity/JS framework, providing the Screenplay Pattern interfaces, as well as the test reporting and integration infrastructure
34 lines • 1.07 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.has = has;
/**
* Checks if the `candidate` value "quacks like a duck".
* In particular, it checks if the `candidate`:
* - is not `null`
* - is not `undefined`
* - has expected methods or fields (evaluated via [`typeof`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof))
*
* ```ts
* const looksLikeADuck = has({
* name: 'string',
* quack: 'function',
* })
*
* const daisy = {
* name: 'Daisy',
* quack: () => 'quack',
* }
*
* looksLikeADuck(daisy) // true
* ```
*
* @param api
* An object where the keys are names of member fields and methods expected on the `candidate`,
* and values are the names of their types, i.e. `function`, `object`, etc.
*/
function has(api) {
return (candidate) => candidate !== null
&& candidate !== undefined
&& Object.entries(api).reduce((result, [key, type]) => result && (typeof candidate[key]) === type, true);
}
//# sourceMappingURL=has.js.map
;