0.3.0 • Published 7 months ago

replace-object v0.3.0

Weekly downloads
5
License
MIT
Repository
github
Last release
7 months ago

NPM version Repository package.json version MIT License Discord

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.

0.3.0

7 months ago

0.2.1

5 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago