logo

Object

This is a collection of functions for working with objects.

Clone

The cloneObject function creates a copy of an object. Optionally the copy can be deep.

import { cloneObject } from '@aracna/core';

let object, clone;

object = { deep: { value: 0 }, shallow: 0 };
clone = cloneObject(object);

clone.shallow++;

// will log 0
console.log(object.shallow);

clone.deep.value++;

// will log 1
console.log(object.deep.value);

clone = cloneObject(object, { deep: true });

clone.deep.value++;

// will log 1
console.log(object.deep.value);

Copy a property

The copyObjectProperty function copies a property from one object to another. The key supports bracket and dot notation.

import { copyObjectProperty } from '@aracna/core';

const source = { deep: { value: 0 }, shallow: 0 };
const target = {};

// will copy the "shallow" property from source to target
copyObjectProperty(source, 'shallow', target);

// will log 0
console.log(object.shallow);

// will copy the "deep" property from source to target
copyObjectProperty(source, 'deep', target);

// will log 0
console.log(getObjectProperty(object, 'deep.value'));

Delete a property

The deleteObjectProperty function deletes a property from an object. The key supports bracket and dot notation.

import { deleteObjectProperty } from '@aracna/core';

const object = { deep: { value: 0 }, shallow: 0 };

// will delete the "shallow" property
deleteObjectProperty(object, 'shallow');

// will log { deep: { value: 0 } }
console.log(object);

// will delete the "deep.value" property
deleteObjectProperty(object, 'deep.value');

// will log { deep: {} }
console.log(object);

Delete properties

The deleteObjectProperties function deletes the properties of an object that either match the predicate or are in the list of keys. The keys support bracket and dot notation. Optionally deletes deep properties as well.

import { deleteObjectProperties } from '@aracna/core';

const object = { deep: { value: 0 }, shallow: 0 };

deleteObjectProperties(object, ['deep.value', 'shallow']);

// will log { deep: {} }
console.log(object);

Flatten

The flattenObject function flattens an object into a single-depth object with dot notation keys. Optionally flattens arrays as well.

import { flattenObject } from '@aracna/core';

const object = { deep: { value: 0 }, shallow: 0 };

// will log { "deep.value": 0, shallow: 0 }
console.log(flattenObject(object));

Get a property

The getObjectProperty function returns a property from an object. The key supports bracket and dot notation.

import { getObjectProperty } from '@aracna/core';

const object = { deep: { value: 0 }, shallow: 0 };

// will log 0
console.log(getObjectProperty(object, 'shallow'));

// will log 0
console.log(getObjectProperty(object, 'deep.value'));

Merge

The mergeObjects function merges two or more objects into a single object.

import { mergeObjects } from '@aracna/core';

const source = { a: 0 };
const target = { b: 1 };

// will log { a: 0, b: 1 }
console.log(mergeObjects(source, target));

Omit properties

The omitObjectProperties function returns a new object without the properties that match the predicate or are in the list of keys. The keys support bracket and dot notation. Optionally omits deep properties as well.

import { omitObjectProperties } from '@aracna/core';

const object = { a: 0, b: 1, c: 2 };

// will log { b: 1 }
console.log(omitObjectProperties(object, ['a', 'c']));

Pick properties

The pickObjectProperties function returns a new object with only the properties that match the predicate or are in the list of keys. The keys support bracket and dot notation. Optionally picks deep properties as well.

import { pickObjectProperties } from '@aracna/core';

const object = { a: 0, b: 1, c: 2 };

// will log { a: 0, c: 2 }
console.log(pickObjectProperties(object, ['a', 'c']));

Set a property

The setObjectProperty function sets a property on an object. The key supports bracket and dot notation.

import { setObjectProperty } from '@aracna/core';

const object = {};

// will set the "shallow" property with value 0
setObjectProperty(object, 'shallow', 0);

// will log 0
console.log(object.shallow);

// will set the "deep.value" property with value 0
setObjectProperty(object, 'deep.value', 0);

// will log { value: 0 }
console.log(object.deep);

// will log { shallow: 0, deep: { value: 0 } }
console.log(object);

// will set the "deep" property with value { value: 1 }
setObjectProperty(object, 'deep', { value: 1 });

// will log { value: 1 }
console.log(object.deep);

Has a property

The hasObjectProperty function checks if an object has a property. The key supports bracket and dot notation.

import { hasObjectProperty } from '@aracna/core';

const object = { deep: { value: 0 }, shallow: 0 };

// will log true
console.log(hasObjectProperty(object, 'shallow'));

// will log true
console.log(hasObjectProperty(object, 'deep.value'));

Check if a value is an object

The isObject function checks if a unknown value is an object. A value is considered an object if it is typeof “object”, not null and not an array.

import { isObject } from '@aracna/core';

// will log true
console.log(isObject({}));

Check if an object is clonable

The isObjectClonable function checks if an object is clonable. An object is considered clonable if it is an array or a plain object.

import { isObjectClonable } from '@aracna/core';

// will log true
console.log(isObjectClonable({}));

// will log true
console.log(isObjectClonable([]));

Check if an object is flattenable

The isObjectFlattenable function checks if an object is flattenable. An object is considered flattenable if it is an array or a plain object.

import { isObjectFlattenable } from '@aracna/core';

// will log true
console.log(isObjectFlattenable({}));

// will log true
console.log(isObjectFlattenable([], { array: true }));

Check if an object has keys

The isObjectKeysPopulated function checks if an object has keys.

import { isObjectKeysPopulated } from '@aracna/core';

// will log true
console.log(isObjectKeysPopulated({ a: 0 }));

Check if an object has values

The isObjectValuesPopulated function checks if an object has values.

import { isObjectValuesPopulated } from '@aracna/core';

// will log true
console.log(isObjectValuesPopulated({ a: 0 }));

Check if a value is a plain object

The isPlainObject function checks if a unknown value is a plain object. A value is considered a plain object if it matches the default object prototype, it is typeof “object” and not null.

import { isPlainObject } from '@aracna/core';

// will log true
console.log(isPlainObject({}));