ObjectTuner
is a TypeScript utility class for manipulating objects in a functional way. It provides methods for replacing, filtering, mapping, merging, picking, omitting, and cloning object properties.
To install the package, use npm or yarn:
npm install object-tune
# or
yarn add object-tune
To create an ObjectTuner
instance, use the ObjectTune
function:
import { ObjectTune } from 'object-tune';
const obj = { a: 1, b: 2, c: 3 };
const tuner = ObjectTune(obj);
Replaces values in the object that match the predicate with the replacement value.
const updated = tuner.replace(value => value === 2, 42).value();
console.log(updated); // { a: 1, b: 42, c: 3 }
Filters the object properties based on the predicate.
const filtered = tuner.filter((key, value) => value > 1).value();
console.log(filtered); // { b: 2, c: 3 }
Recursively replaces values in the object that match the predicate with the replacement value.
const deepObj = { a: 1, b: { c: 2, d: 3 } };
const deepTuner = ObjectTune(deepObj);
const deepUpdated = deepTuner.deepReplace(value => value === 2, 42).value();
console.log(deepUpdated); // { a: 1, b: { c: 42, d: 3 } }
Maps the object properties using the provided function.
const mapped = tuner.map(value => value * 2).value();
console.log(mapped); // { a: 2, b: 4, c: 6 }
Merges the object with another object.
const merged = tuner.merge({ d: 4 }).value();
console.log(merged); // { a: 1, b: 2, c: 3, d: 4 }
Picks the specified keys from the object.
const picked = tuner.pick(['a', 'c']).value();
console.log(picked); // { a: 1, c: 3 }
Omits the specified keys from the object.
const omitted = tuner.omit(['b']).value();
console.log(omitted); // { a: 1, c: 3 }
Creates a deep clone of the object.
const cloned = tuner.deepClone().value();
console.log(cloned); // { a: 1, b: 2, c: 3 }
Checks if the object has the specified key.
const hasKey = tuner.hasKey('a');
console.log(hasKey); // true
Gets the value of the specified key, or returns the default value if the key does not exist.
const value = tuner.get('b', 0);
console.log(value); // 2
Deletes the specified properties from the object.
const cleaned = tuner.deleteProperties(['b']).value();
console.log(cleaned); // { a: 1, c: 3 }
Returns the current value of the object.
const value = tuner.value();
console.log(value); // { a: 1, b: 2, c: 3 }
This project is licensed under the MIT License.