1.0.2 • Published 3 months ago

lkt-object-tools v1.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

LKT Object Tools

Functions

cloneObject

ArgTypeDescription
objobjectObject to be cloned
import {cloneObject} from "lkt-object-tools";

const obj = {lorem: 'ipsum'};
const obj2 = cloneObject(obj);

sortObjectProperties

ArgTypeDescription
objobjectObject to be sorted
import {sortObjectProperties} from "lkt-object-tools";

const obj = {lorem: 'ipsum'};
const sortedObj = sortObjectProperties(obj);

fetchInObject

ArgTypeDefaultDescription
objobjectObject to be sorted
keystringKey used to iterate object
separatorstring'.'Key separator. Each time this separator is reached, this function will treat as a nested object
import {fetchInObject} from "lkt-object-tools";

const obj = {
    level1: {
        level2: {
            level3: {
                level4: 'Hi!'
            }
        }
    },
};

fetchInObject(obj, 'level1.level2.level3.level4'); // Returns "Hi!"
fetchInObject(obj, 'level1.level2.level3'); // Returns {level4: 'Hi!'}

// Custom separator
fetchInObject(obj, 'level1/level2/level3/level4', '/'); // Returns "Hi!"
fetchInObject(obj, 'level1->level2->level3->level4', '->'); // Returns "Hi!"

mergeObjects

Merge two objects. If there is a property in both, obj2 value will remain.

ArgTypeDescription
obj1objectFirst object to be merged
obj2objectSecond object to be merged
import {mergeObjects} from "lkt-object-tools";

const obj1 = {
    lorem: 'ipsum'
};
const obj2 = {
    dolor: 'sit amet'
};

mergeObjects(obj1, obj2); // Result: {lorem: 'ipsum', dolor: 'sit amet'};

mergeCommonProperties

Same as mergeObjects, but only will merge props if exists in both objects

ArgTypeDescription
obj1objectFirst object to be merged
obj2objectSecond object to be merged
import {mergeCommonProperties} from "lkt-object-tools";

const obj1 = {
    lorem: 'ipsum'
};
const obj2 = {
    lorem: 'sit amet',
    dolor: 'sit amet'
};
const obj3 = {
    lorem: null
};

mergeCommonProperties(obj1, obj2); // Returns: {lorem: 'sit amet'};
mergeCommonProperties(obj1, obj3); // Returns: {lorem: 'ipsum'};

deleteObjectProperties

Same as mergeObjects, but only will merge props if exists in both objects

ArgTypeDescription
objobjectObject where props will be removed
keysstring[]An array with all the properties
import {deleteObjectProperties} from "lkt-object-tools";


const obj = {
    lorem: 'ipsum',
    name: 'test',
    description: 'description',
    align: 'center',
    isActive: true
}
deleteObjectProperties(obj, ['lorem', 'align', 'isActive']); // Returns: {name: 'test', description: 'description'}
1.0.2

3 months ago

1.0.1

4 months ago

1.0.0

2 years ago