Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kenberkeley/replace-with
Replace an array | object's content with the other one's while keeping the reference
https://github.com/kenberkeley/replace-with
array content keep object preserve reference replace
Last synced: 5 days ago
JSON representation
Replace an array | object's content with the other one's while keeping the reference
- Host: GitHub
- URL: https://github.com/kenberkeley/replace-with
- Owner: kenberkeley
- Created: 2017-04-13T06:10:16.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-04-13T06:19:23.000Z (over 7 years ago)
- Last Synced: 2024-10-12T23:50:55.593Z (about 1 month ago)
- Topics: array, content, keep, object, preserve, reference, replace
- Language: JavaScript
- Size: 1.95 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# replaceWith(orig, other)
[![npm version][npm-v-img]][npm-url]
[![npm download][npm-dl-img]][npm-url]
[![build][build-img]][build-url]### Installation
`npm i replace-with -S`
### Source ([index.js](./index.js))
```js
/**
* Replace an array|object's content with the other one's while keeping the reference
* @param {Array|Object} orig
* @param {Array|Object} other
* @return {Array|Object} orig
*/
var keys = Object.keys;module.exports = function replaceWith(orig, other) {
if (Array.isArray(orig)) {
// for Array
orig.splice.apply(orig, [0, orig.length].concat(other));
} else {
// for Object
keys(orig).forEach(function (k) { delete orig[k] });
keys(other).forEach(function (k) { orig[k] = other[k] });
}
return orig;
};
```### Usage
> Let's take a look at the [test examples](./test/index.js)```js
test('replace array', t => {
const orig = [1, 2, 3]
const ref = orig
replaceWith(orig, [4, 5, 6])
t.is(ref, orig) // pass!
t.deepEqual(orig, [4, 5, 6]) // pass!
})test('replace object', t => {
let orig = { a: 1, b: 2, c: 3 }
const ref = orig
orig = replaceWith(orig, { d: 4, e: 5, f: 6 })
t.is(ref, orig) // pass!
t.deepEqual(orig, { d: 4, e: 5, f: 6 }) // pass!
})
```### Test
`npm test`
[npm-url]: https://www.npmjs.com/package/replace-with
[npm-v-img]: http://img.shields.io/npm/v/replace-with.svg
[npm-dl-img]: http://img.shields.io/npm/dm/replace-with.svg
[build-img]: https://travis-ci.org/kenberkeley/replace-with.svg?branch=master
[build-url]: https://travis-ci.org/kenberkeley/replace-with