lkt-object-tools
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

LKT Object Tools

Functions

cloneObject

Arg Type Description
obj object Object to be cloned
import {cloneObject} from "lkt-object-tools";

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

sortObjectProperties

Arg Type Description
obj object Object to be sorted
import {sortObjectProperties} from "lkt-object-tools";

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

fetchInObject

Arg Type Default Description
obj object Object to be sorted
key string Key used to iterate object
separator string '.' 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.

Arg Type Description
obj1 object First object to be merged
obj2 object Second 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

Arg Type Description
obj1 object First object to be merged
obj2 object Second 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

Arg Type Description
obj object Object where props will be removed
keys string[] 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'}

Package Sidebar

Install

npm i lkt-object-tools

Weekly Downloads

10

Version

1.0.2

License

MIT

Unpacked Size

6.43 kB

Total Files

4

Last publish

Collaborators

  • lekrat