Skip to content

Commit

Permalink
feat(isObjectKeysIn()): check if any value is an object with keys in …
Browse files Browse the repository at this point in the history
…it(or its prototype chain).
  • Loading branch information
sciborrudnicki committed Aug 28, 2021
1 parent b7092a9 commit 1e48fb1
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/is/lib/is-object-keys-in.func.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Function.
import { isArray } from './is-array.func';
import { isObject } from './is-object.func';
import { resultCallback } from '../../lib/result-callback.func';
// Interface.
import { CallbackPayload } from '../../type/callback-payload.type';
// Type.
import { ResultCallback } from '../../type/result-callback.type';
/**
* Checks if any `value` is an `object`(by using the `isObject()`) with keys of the `PropertyKey` in it(or its prototype chain) by
* using the `in` operator.
* @param value The `value` of any type to check against the `object` that contains(or its prototype chain) keys from the given `keys`.
* @param key An array of property keys to check if the given `value` contains(or its prototype chain) all of them.
* @param callback A callback `function` of `ResultCallback` type with parameters, the `value` that has been checked, the `result` of this
* check, and `payload` of the default `CallbackPayload` shape, with the provided `keys` and optional properties from the provided
* `payload`, to handle them before the `result` return. By default, it uses `resultCallback()` function.
* @param payload An optional `object` of a generic type variable `Payload` that is assigned to the `payload` of the provided `callback`
* function.
* @returns The return value is a `boolean` indicating whether the provided `value` is an `object` that contains(or its prototype chain)
* given `keys`.
* @angularpackage
*/
export const isObjectKeysIn = <Obj = object, Payload extends object = object>(
value: any,
keys: PropertyKey[],
callback: ResultCallback<any, typeof payload> = resultCallback,
payload?: CallbackPayload<{ keys: typeof keys } & Payload>
): value is Obj =>
callback(
isObject(value) && isArray(keys) ? keys.every((k) => k in value) : false,
value,
{ name: isObjectKeysIn.name, keys, ...payload } as any
);

0 comments on commit 1e48fb1

Please sign in to comment.