1 objects
ztimson edited this page 2024-09-22 03:11:09 -04:00

@ztimson/utils / objects

objects

Functions

clean()

clean<T>(obj, undefinedOnly): Partial<T>

Removes any null values from an object in-place

Type Parameters

T

Parameters

obj: T

Object reference that will be cleaned

undefinedOnly: boolean = false

Ignore null values

Returns

Partial<T>

Cleaned object

Example

let test = {a: 0, b: false, c: null, d: 'abc'}
console.log(clean(test)); // Output: {a: 0, b: false, d: 'abc'}

Defined in

src/objects.ts:14


deepCopy()

deepCopy<T>(value): T

Create a deep copy of an object (vs. a shallow copy of references)

Should be replaced by structuredClone once released.

Type Parameters

T

Parameters

value: T

Object to copy

Returns

T

Type

Deprecated

Please use structuredClone

Defined in

src/objects.ts:34


deepMerge()

deepMerge<T>(target, ...sources): T

Merge any number of objects into the target

Type Parameters

T

Parameters

target: any

Destination of all properties

• ...sources: any[]

Objects that will copied into target

Returns

T

The des

Defined in

src/objects.ts:45


dotNotation()

dotNotation(obj, prop, set)

dotNotation<T>(obj, prop, set): T

Get/set a property of an object using dot notation

Type Parameters

T

Parameters

obj: any

source object to search

prop: string

property name (Dot notation & indexing allowed)

set: T

Set object property to value, omit to fetch value instead

Returns

T

property value

Example
// Get a value
const name = dotNotation<string>(person, 'firstName');
const familyCarMake = dotNotation(family, 'cars[0].make');
// Set a value
dotNotation(family, 'cars[0].make', 'toyota');
Defined in

src/objects.ts:77

dotNotation(obj, prop)

dotNotation<T>(obj, prop): T | undefined

Type Parameters

T

Parameters

obj: any

prop: string

Returns

T | undefined

Defined in

src/objects.ts:78


encodeQuery()

encodeQuery(data): string

Convert object into URL encoded query string

Parameters

data: any

data to convert

Returns

string

  • Encoded form data

Example

const query = encodeQuery({page: 1, size: 20});
console.log(query); // Output: "page=1&size=20"

Defined in

src/objects.ts:107


flattenObj()

flattenObj(obj, parent?, result?): any

Recursively flatten a nested object, while maintaining key structure

Parameters

obj: any

Object to flatten

parent?: any

Recursively check if key is a parent key or not

result?: any = {}

Result

Returns

any

  • Flattened object

Example

const car = {honda: {model: "Civic"}};
console.log(flattenObj(car)); //Output {honda.model: "Civic"}

Defined in

src/objects.ts:128


formData()

formData(target): FormData

Convert object to FormData

Parameters

target: any

Object to convert

Returns

FormData

  • Form object

Defined in

src/objects.ts:148


includes()

includes(target, values, allowMissing): boolean

Check that an object has the following values

Parameters

target: any

Object to search

values: any

Criteria to check against

allowMissing: boolean = false

Only check the keys that are available on the target

Returns

boolean

Does target include all the values

Example

const test = {a: 2, b: 2};
includes(test, {a: 1}); // true
includes(test, {b: 1, c: 3}); // false

Defined in

src/objects.ts:169


isEqual()

isEqual(a, b): boolean

Deep check if two objects are equal

Parameters

a: any

first item to compare

b: any

second item to compare

Returns

boolean

True if they match

Defined in

src/objects.ts:188


JSONAttemptParse()

JSONAttemptParse<T>(json): T | string

Parse JSON but return the original string if it fails

Type Parameters

T

Parameters

json: string

JSON string to parse

Returns

T | string

Object if successful, original string otherwise

Defined in

src/objects.ts:222


JSONSanitize()

JSONSanitize(obj, space?): string

Convert an object to a JSON string avoiding any circular references.

Parameters

obj: any

Object to convert to JSON

space?: number

Format the JSON with spaces

Returns

string

JSON string

Defined in

src/objects.ts:234


mixin()

mixin(target, constructors): void

Experimental: Combine multiple object prototypes into one

Parameters

target: any

Object that will have prototypes added

constructors: any[]

Additionally prototypes that should be merged into target

Returns

void

Defined in

src/objects.ts:203