npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

clone-rename

v1.2.3

Published

deep copy object and rename keys.

Readme

clone-rename

npm version npm downloads

中文文档

Deep clone your data and rename object keys. It is useful when backend, third-party, or mock data uses different field names from your frontend model.

Install

npm install clone-rename

Usage

import cloneRename from 'clone-rename';

const result = cloneRename(data, filter, options);

The legacy API is still the main API:

cloneRename(input, filter, {
  deepCopy: true,
  deepRename: true
});

Rename Rules

Basic key map

Renames every matching key at every nested level by default.

import cloneRename from 'clone-rename';

const res = [
  {
    id: '001',
    name: 'apple'
  },
  {
    id: '002',
    name: 'banana'
  }
];

const result = cloneRename(res, {
  id: 'goodsID',
  name: 'goodsName'
});

/*
[
  {
    goodsID: '001',
    goodsName: 'apple'
  },
  {
    goodsID: '002',
    goodsName: 'banana'
  }
]
*/

Path-based key map

Use dot paths on the left when you only want to rename a specific nested field. The right side is just the new key name.

import cloneRename from 'clone-rename';

const data = {
  id: 'root-001',
  user: {
    id: 'user-001',
    profile: {
      name: 'Ada Lovelace'
    }
  },
  order: {
    id: 'order-001'
  }
};

const result = cloneRename(data, {
  'user.id': 'userId',
  'user.profile.name': 'displayName'
});

/*
{
  id: 'root-001',
  user: {
    userId: 'user-001',
    profile: {
      displayName: 'Ada Lovelace'
    }
  },
  order: {
    id: 'order-001'
  }
}
*/

Right-side rule: only the last segment after the final . is used as the new key name. Renaming happens in place — cloneRename does not move fields across the tree. For example, { 'user.id': 'account.userId' } and { 'user.id': 'userId' } are equivalent; both produce user.userId and neither creates an account container.

Function key map

Pass a function when the new key depends on the current key or its context.

import cloneRename from 'clone-rename';

function camelCase(key) {
  return key.replace(/_([a-z])/g, (_, char) => char.toUpperCase());
}

const result = cloneRename(data, (key, context) => {
  if (context.path === 'user.id') return 'userId';
  if (key.includes('_')) return camelCase(key);
  return key;
});

The context object contains:

{
  key: string;
  path: string;
  parentPath: string;
  depth: number;
  value: any;
}

Options

Default behavior

deepCopy and deepRename are both true by default.

const project = { name: 'JavaScript' };

const obj = {
  name: 'PsChina',
  age: '25',
  like: [project]
};

const result = cloneRename(obj, {
  name: 'babel'
});

/*
{
  babel: 'PsChina',
  age: '25',
  like: [{ babel: 'JavaScript' }]
}
*/

result.like[0] === project; // false

Shallow copy

const result = cloneRename(obj, filter, {
  deepCopy: false
});

Nested values keep their original references.

Shallow rename

const result = cloneRename(obj, filter, {
  deepRename: false
});

Nested values are still copied, but nested keys are not renamed.

Copy Support

cloneRename can clone Date, RegExp, and root Function values.

const time = new Date();
const sameTime = cloneRename(time);

console.log(time === sameTime); // false
function sum(a, b) {
  return a + b;
}

const sameSum = cloneRename(sum);

sameSum(1, 2); // 3
console.log(sum === sameSum); // false
const numberRegObj = { reg: /[0-9]/ };
const newRegObj = cloneRename(numberRegObj);

console.log(numberRegObj.reg === newRegObj.reg); // false

TypeScript

Type declarations are included.

import cloneRename, {
  type RenameContext,
  type RenameFilter,
  type CloneRenameOptions
} from 'clone-rename';

const filter: RenameFilter = {
  'user.id': 'userId'
};

const options: CloneRenameOptions = {
  deepCopy: true,
  deepRename: true
};

const result = cloneRename({ user: { id: 1 } }, filter, options);

cloneRename(
  { user: { id: 1 } },
  (key: string, context: RenameContext) => {
    if (context.path === 'user.id') return 'userId';
    return key;
  }
);