-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(isObjectKeysIn()): check if any value is an object with keys in …
…it(or its prototype chain).
- Loading branch information
1 parent
b7092a9
commit 1e48fb1
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
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,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 | ||
); |