Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vilicvane/replace-object
A simple utility for in-place object replacing.
https://github.com/vilicvane/replace-object
mobx utility
Last synced: 20 days ago
JSON representation
A simple utility for in-place object replacing.
- Host: GitHub
- URL: https://github.com/vilicvane/replace-object
- Owner: vilicvane
- License: mit
- Created: 2018-03-23T11:27:40.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-10-27T17:21:30.000Z (about 1 year ago)
- Last Synced: 2024-11-30T15:42:00.063Z (23 days ago)
- Topics: mobx, utility
- Language: TypeScript
- Homepage:
- Size: 155 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![NPM version](https://img.shields.io/npm/v/replace-object?color=%23cb3837&style=flat-square)](https://www.npmjs.com/package/replace-object)
[![Repository package.json version](https://img.shields.io/github/package-json/v/vilic/replace-object?color=%230969da&label=repo&style=flat-square)](./package.json)
[![MIT License](https://img.shields.io/badge/license-MIT-999999?style=flat-square)](./LICENSE)
[![Discord](https://img.shields.io/badge/chat-discord-5662f6?style=flat-square)](https://discord.gg/vanVrDwSkS)# replace-object
A simple utility for in-place object replacing.
## Installation
```sh
yarn add replace-object
# or
npm install replace-object
```## Usage
### Default Replacer
```ts
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:
```ts
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
```ts
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.