Skip to content

Modify/rename the keys of the own enumerable properties of an object.

License

Notifications You must be signed in to change notification settings

michaelkariv/rename-keys

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rename-keys NPM version

Modify the names of the own enumerable properties (keys) of an object.

Bug? Feature request? Please create an issue.

Quickstart

npm i rename-keys --save
bower install rename-keys --save

Usage

var rename = require('rename-keys');
rename( object, function );

Arguments

  • object {Object}: the object to iterate over.
  • function {Function}: the function to use to rename each own enumerable property of object.

Example

The object to iterate over:

var pkg = require('./package.json');

and this renaming function:

var addDashes = function(str) {
  return '--' + str;
};
console.log(rename(pkg, addDashes));

results in:

{ "--name": "rename-keys",
  "--description": "Modify the names of the own enumerable properties (keys) of an object.",
  "--version": "0.1.0",
  "--homepage": "https://github.com/jonschlinkert/rename-keys",
  "--author": {
    "name": "Jon Schlinkert",
    "url": "https://github.com/jonschlinkert"
  } ... }

Extension - deep rename (suggested by Michael Kariv, 2015-01-20)

rename function now optionally receives the third boolean parameter. If true, the function renames nested objects' keys.

var addDashes = function(str) {
  return '--' + str;
};
console.log(rename(pkg, addDashes, true));

results in:

{ "--name": "rename-keys",
  "--description": "Modify the names of the own enumerable properties (keys) of an object.",
  "--version": "0.1.0",
  "--homepage": "https://github.com/jonschlinkert/rename-keys",
  "--author": {
    "--name": "Jon Schlinkert",
    "--url": "https://github.com/jonschlinkert"
  } ... }

Authors

Jon Schlinkert

License

Copyright (c) 2014-2015, Jon Schlinkert. Released under the MIT license

About

Modify/rename the keys of the own enumerable properties of an object.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%