0.3.0 • Published 7 months ago
replace-object v0.3.0
replace-object
A simple utility for in-place object replacing.
Installation
yarn add replace-object
# or
npm install replace-object
Usage
Default Replacer
import replaceObject from 'replace-object';
let foo = {a: 123, b: 'abc'};
replaceObject(foo, {b: 'def', c: true});
foo; // {b: 'def', c: true}
let bar = {a: 123, b: 'abc'};
replaceObject(bar, {b: 'def', c: true}, {delete: false});
bar; // {a: 123, b: 'def', c: true}
let pia = {a: 123, b: 'abc'};
replaceObject(pia, {b: 'def', c: true}, {add: false});
pia; // {b: 'def'}
Options:
interface ObjectReplacerOptions {
/**
* Whether to do shallow replace without recursion, defaults to `false`.
*/
shallow?: boolean;
/**
* Whether to add new properties, defaults to `true`.
*/
add?: boolean;
/**
* Whether to delete properties that does not exists on the `withObject`,
* defaults to `true`.
*/
delete?: boolean;
}
MobX Observable Replacer
import {observable} from 'mobx';
import replaceObject from 'replace-object/mobx';
let foo = observable({a: 123, b: 'abc'});
replaceObject(foo, {b: 'def', c: true});
foo; // observable {b: 'def', c: true}
Check out the source code to find out how to customize.
License
MIT License.