Giter VIP home page Giter VIP logo

rename-keys's Introduction

rename-keys NPM version NPM monthly downloads NPM total downloads Linux Build Status

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

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.

Install

Install with npm:

$ npm install --save rename-keys

Usage

var renameKeys = require('rename-keys');

API

Params

  • object {Object}: The object with keys to rename.
  • fn {Function}: Renaming function to use on each key in the object.
  • returns {Object}: Returns a new object with renamed keys.

Example

var obj = renameKeys({a: 1, b: 2, c: 3}, function(key, val) {
  return '--' + key;
});
console.log(obj);
//=> { '--a': 1, '--b': 2, '--c': 3}

Rename based on value

var obj = renameKeys({a: 1, b: 2, c: 3}, function(key, val) {
  return val > 1 ? ('++' + key) : ('--' + key);
});
console.log(obj);
//=> {'--a': 1, '++b': 2, '++c': 3};

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Contributors

Commits Contributor
15 jonschlinkert
5 doowb
2 stellard
1 kof
1 robinbullocks4rb
1 palanik

Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Running tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test

Author

Jon Schlinkert

License

Copyright © 2017, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on October 13, 2017.

rename-keys's People

Contributors

doowb avatar jonschlinkert avatar kof avatar palanik avatar robinbullocks4rb avatar stellard avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rename-keys's Issues

Error thrown when input object does not inherit from Object.prototype

The rename function in index.js uses obj.hasOwnProperty, which is not defined when the object does not inherit from Object.prototype. In the most basic example, this would fail with TypeError: obj.hasOwnProperty is not a function:

const input = Object.create(null);
input.a = 0;
require('rename-keys')(input, key => key);

A more realistic example would be trying to rename the keys of an object returned from querystring, which in Node v6 stopped inheriting from Object.prototype.

const querystring = require('querystring'),
    rename = require('rename-keys');

rename(querystring.parse('a=1'), key => key);

Live example of the same

Immutable execution

Can there be a way to immutably rename the keys? ie: returns a new object instead of mutating the source?

Falsey values return original key

Hello Jon,

I ran into an issue when trying to return 0 from the rename function since it evaluates to false.

Although falsey values are valid object keys they are incompatible with the current implementation.

I am assuming here that your intention was probably to handle an undefined return value from the rename function by keeping the original key.

Maybe the tradeoff is to explicitly look for undefined when returning the original by default?

I've solved my problem by just starting my keys from 1 but I thought it was worth discussion anyways.

Cheers,

Scott

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.