UNPKG

@serenity-js/core

Version:

The core Serenity/JS framework, providing the Screenplay Pattern interfaces, as well as the test reporting and integration infrastructure

36 lines (35 loc) 1.1 kB
/** * 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. */ export function has<T>(api: Record<keyof T, string>): (candidate: unknown) => candidate is T { return (candidate: unknown): candidate is T => candidate !== null && candidate !== undefined && Object.entries(api).reduce( (result, [key, type]) => result && (typeof candidate[key]) === type, true, ) }