{"id":18905059,"url":"https://github.com/kenberkeley/replace-with","last_synced_at":"2026-03-04T18:30:19.859Z","repository":{"id":57353729,"uuid":"88131015","full_name":"kenberkeley/replace-with","owner":"kenberkeley","description":"Replace an array | object's content with the other one's while keeping the reference","archived":false,"fork":false,"pushed_at":"2017-04-13T06:19:23.000Z","size":2,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-15T03:12:20.939Z","etag":null,"topics":["array","content","keep","object","preserve","reference","replace"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kenberkeley.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-13T06:10:16.000Z","updated_at":"2017-11-15T03:53:08.000Z","dependencies_parsed_at":"2022-08-31T22:01:02.979Z","dependency_job_id":null,"html_url":"https://github.com/kenberkeley/replace-with","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kenberkeley%2Freplace-with","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kenberkeley%2Freplace-with/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kenberkeley%2Freplace-with/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kenberkeley%2Freplace-with/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kenberkeley","download_url":"https://codeload.github.com/kenberkeley/replace-with/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239889026,"owners_count":19713702,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array","content","keep","object","preserve","reference","replace"],"created_at":"2024-11-08T09:10:30.351Z","updated_at":"2026-03-04T18:30:19.816Z","avatar_url":"https://github.com/kenberkeley.png","language":"JavaScript","readme":"# replaceWith(orig, other)\r\n\r\n[![npm version][npm-v-img]][npm-url]\r\n[![npm download][npm-dl-img]][npm-url]\r\n[![build][build-img]][build-url]\r\n\r\n### Installation\r\n\r\n`npm i replace-with -S`  \r\n\r\n### Source ([index.js](./index.js))\r\n\r\n```js\r\n/**\r\n * Replace an array|object's content with the other one's while keeping the reference\r\n * @param  {Array|Object} orig\r\n * @param  {Array|Object} other\r\n * @return {Array|Object} orig\r\n */\r\nvar keys = Object.keys;\r\n\r\nmodule.exports = function replaceWith(orig, other) {\r\n  if (Array.isArray(orig)) {\r\n    // for Array\r\n    orig.splice.apply(orig, [0, orig.length].concat(other));\r\n  } else {\r\n    // for Object\r\n    keys(orig).forEach(function (k) { delete orig[k] });\r\n    keys(other).forEach(function (k) { orig[k] = other[k] });\r\n  }\r\n  return orig;\r\n};\r\n```\r\n\r\n\r\n### Usage\r\n\u003e Let's take a look at the [test examples](./test/index.js)\r\n\r\n```js\r\ntest('replace array', t =\u003e {\r\n  const orig = [1, 2, 3]\r\n  const ref = orig\r\n  replaceWith(orig, [4, 5, 6])\r\n  t.is(ref, orig) // pass!\r\n  t.deepEqual(orig, [4, 5, 6]) // pass!\r\n})\r\n\r\ntest('replace object', t =\u003e {\r\n  let orig = { a: 1, b: 2, c: 3 }\r\n  const ref = orig\r\n  orig = replaceWith(orig, { d: 4, e: 5, f: 6 })\r\n  t.is(ref, orig) // pass!\r\n  t.deepEqual(orig, { d: 4, e: 5, f: 6 }) // pass!\r\n})\r\n```\r\n\r\n### Test\r\n\r\n`npm test`\r\n\r\n[npm-url]: https://www.npmjs.com/package/replace-with\r\n[npm-v-img]: http://img.shields.io/npm/v/replace-with.svg\r\n[npm-dl-img]: http://img.shields.io/npm/dm/replace-with.svg\r\n[build-img]: https://travis-ci.org/kenberkeley/replace-with.svg?branch=master\r\n[build-url]: https://travis-ci.org/kenberkeley/replace-with\r\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkenberkeley%2Freplace-with","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkenberkeley%2Freplace-with","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkenberkeley%2Freplace-with/lists"}