predictype

predictype v0.13.0


predictype / predicates/objects/objectKey / objectKey

Function: objectKey()

objectKey(source, oper, key): boolean

Defined in: predicates/objects/objectKey.ts:28

Checks if an object has or lacks a specific key (string or symbol) using the specified operation.

Parameters

source

object

The object to check.

oper

ObjectKeyOper

The key operation to perform (e.g. ‘contains_key’, ‘lacks_key’).

key

string | symbol

The key to check.

Returns

boolean

True if the key check is valid according to the operator, otherwise false.

Throws

If the operation is not recognized.

Example

const obj = { foo: 1 };
const obj2 = {};
const sym = Symbol('baz');
Object.defineProperty(obj2, sym, { value: 2 });

objectKey(obj, 'contains_key', 'foo'); // true
objectKey(obj, 'lacks_key', 'bar'); // true
objectKey(obj2, 'contains_key', sym); // true

Remarks

Supported Operators: