
This is a collection of functions for working with objects.
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);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'));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);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);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));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'));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));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']));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']));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);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'));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({}));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([]));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 }));The isObjectKeysPopulated function checks if an object has keys.
import { isObjectKeysPopulated } from '@aracna/core';
// will log true
console.log(isObjectKeysPopulated({ a: 0 }));The isObjectValuesPopulated function checks if an object has values.
import { isObjectValuesPopulated } from '@aracna/core';
// will log true
console.log(isObjectValuesPopulated({ a: 0 }));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({}));