Retrieves the value associated with the key as an array.
The key to retrieve the array for.
The array value.
Retrieves the value associated with the key as a bigint
.
The key to retrieve the bigint value for.
The bigint
value.
Retrieves the value associated with the key as a boolean.
The key to retrieve the boolean value for.
The boolean value.
Checks if the given key exists in the object.
The key to check.
true
if the key exists, otherwise false
.
Checks if the value associated with the key is null
.
The key to check.
true
if the value is null
, otherwise false
.
Checks if the value associated with the key is undefined
.
The key to check.
true
if the value is undefined
, otherwise false
.
Retrieves the value associated with the key as a number.
The key to retrieve the number value for.
The number value.
Retrieves the value associated with the key as an ObjectParser
for nested object access.
The key to retrieve the object value for.
A new ObjectParser
instance for the nested object.
Retrieves the value associated with the key as a string.
The key to retrieve the string value for.
The string value.
Retrieves the value associated with the key as a symbol.
The key to retrieve the symbol for.
The symbol value.
Retrieves the type of the value associated with the specified key.
The key to check the type for.
The type of the value as a string (e.g., "string", "number").
The
ObjectParser
is a specialized parser designed to safely access and validate values within a plain JavaScript object. It provides type-safe methods for retrieving values of various types (strings, numbers, arrays, etc.), ensuring that required fields are present and correctly typed.This parser is useful in scenarios where you need to interact with complex objects and ensure the presence and type correctness of specific keys, such as when dealing with parsed JSON or other data structures.
Usage
ObjectParser
wraps a generic object and provides methods to:has
).string
,number
,array
, etc.).ObjectParser
instances.It also throws custom errors (
MissingKeyError
,InvalidTypeError
,InvalidInstanceOfError
) to handle validation issues, making debugging easier.How It Works
string
,number
, andarray
enforce the type correctness of the retrieved values.object
method returns a newObjectParser
instance, allowing safe and structured access to deeply nested data.instanceOf
method ensures that an object is an instance of the specified constructor, which is useful for retrieving specific types likeDate
.Example