├── .editorconfig ├── .eslintrc.json ├── .gitignore ├── .travis.yml ├── .verb.md ├── CHANGELOG.md ├── LICENSE ├── README.md ├── bower.json ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org/ 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = lf 7 | indent_size = 2 8 | indent_style = space 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | 12 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 13 | trim_trailing_whitespace = false 14 | insert_final_newline = false 15 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": false, 4 | "es6": true, 5 | "node": true, 6 | "mocha": true 7 | }, 8 | 9 | "globals": { 10 | "document": false, 11 | "navigator": false, 12 | "window": false 13 | }, 14 | 15 | "rules": { 16 | "accessor-pairs": 2, 17 | "arrow-spacing": [2, { "before": true, "after": true }], 18 | "block-spacing": [2, "always"], 19 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 20 | "comma-dangle": [2, "never"], 21 | "comma-spacing": [2, { "before": false, "after": true }], 22 | "comma-style": [2, "last"], 23 | "constructor-super": 2, 24 | "curly": [2, "multi-line"], 25 | "dot-location": [2, "property"], 26 | "eol-last": 2, 27 | "eqeqeq": [2, "allow-null"], 28 | "generator-star-spacing": [2, { "before": true, "after": true }], 29 | "handle-callback-err": [2, "^(err|error)$" ], 30 | "indent": [2, 2, { "SwitchCase": 1 }], 31 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 32 | "keyword-spacing": [2, { "before": true, "after": true }], 33 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 34 | "new-parens": 2, 35 | "no-array-constructor": 2, 36 | "no-caller": 2, 37 | "no-class-assign": 2, 38 | "no-cond-assign": 2, 39 | "no-const-assign": 2, 40 | "no-control-regex": 2, 41 | "no-debugger": 2, 42 | "no-delete-var": 2, 43 | "no-dupe-args": 2, 44 | "no-dupe-class-members": 2, 45 | "no-dupe-keys": 2, 46 | "no-duplicate-case": 2, 47 | "no-empty-character-class": 2, 48 | "no-eval": 2, 49 | "no-ex-assign": 2, 50 | "no-extend-native": 2, 51 | "no-extra-bind": 2, 52 | "no-extra-boolean-cast": 2, 53 | "no-extra-parens": [2, "functions"], 54 | "no-fallthrough": 2, 55 | "no-floating-decimal": 2, 56 | "no-func-assign": 2, 57 | "no-implied-eval": 2, 58 | "no-inner-declarations": [2, "functions"], 59 | "no-invalid-regexp": 2, 60 | "no-irregular-whitespace": 2, 61 | "no-iterator": 2, 62 | "no-label-var": 2, 63 | "no-labels": 2, 64 | "no-lone-blocks": 2, 65 | "no-mixed-spaces-and-tabs": 2, 66 | "no-multi-spaces": 2, 67 | "no-multi-str": 2, 68 | "no-multiple-empty-lines": [2, { "max": 1 }], 69 | "no-native-reassign": 0, 70 | "no-negated-in-lhs": 2, 71 | "no-new": 2, 72 | "no-new-func": 2, 73 | "no-new-object": 2, 74 | "no-new-require": 2, 75 | "no-new-wrappers": 2, 76 | "no-obj-calls": 2, 77 | "no-octal": 2, 78 | "no-octal-escape": 2, 79 | "no-proto": 0, 80 | "no-redeclare": 2, 81 | "no-regex-spaces": 2, 82 | "no-return-assign": 2, 83 | "no-self-compare": 2, 84 | "no-sequences": 2, 85 | "no-shadow-restricted-names": 2, 86 | "no-spaced-func": 2, 87 | "no-sparse-arrays": 2, 88 | "no-this-before-super": 2, 89 | "no-throw-literal": 2, 90 | "no-trailing-spaces": 0, 91 | "no-undef": 2, 92 | "no-undef-init": 2, 93 | "no-unexpected-multiline": 2, 94 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 95 | "no-unreachable": 2, 96 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 97 | "no-useless-call": 0, 98 | "no-with": 2, 99 | "one-var": [0, { "initialized": "never" }], 100 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 101 | "padded-blocks": [0, "never"], 102 | "quotes": [2, "single", "avoid-escape"], 103 | "radix": 2, 104 | "semi": [2, "always"], 105 | "semi-spacing": [2, { "before": false, "after": true }], 106 | "space-before-blocks": [2, "always"], 107 | "space-before-function-paren": [2, "never"], 108 | "space-in-parens": [2, "never"], 109 | "space-infix-ops": 2, 110 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 111 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 112 | "use-isnan": 2, 113 | "valid-typeof": 2, 114 | "wrap-iife": [2, "any"], 115 | "yoda": [2, "never"] 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | .idea 4 | *.sublime-* 5 | 6 | # test related, or directories generated by tests 7 | test/actual 8 | actual 9 | coverage 10 | .nyc* 11 | 12 | # npm 13 | node_modules 14 | npm-debug.log 15 | 16 | # yarn 17 | yarn.lock 18 | yarn-error.log 19 | 20 | # misc 21 | _gh_pages 22 | _draft 23 | _drafts 24 | bower_components 25 | vendor 26 | temp 27 | tmp 28 | TODO.md 29 | package-lock.json -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | language: node_js 6 | node_js: 7 | - node 8 | - '8' 9 | - '7' 10 | - '6' 11 | - '5' 12 | - '4' 13 | - '0.12' 14 | - '0.10' 15 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | ```js 4 | var {%= camelcase(name) %} = require('{%= name %}'); 5 | ``` 6 | 7 | ## API 8 | 9 | {%= apidocs("index.js") %} 10 | 11 | **Rename based on value** 12 | 13 | ```js 14 | var obj = renameKeys({a: 1, b: 2, c: 3}, function(key, val) { 15 | return val > 1 ? ('++' + key) : ('--' + key); 16 | }); 17 | console.log(obj); 18 | //=> {'--a': 1, '++b': 2, '++c': 3}; 19 | ``` 20 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Release history 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 6 | and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 7 | 8 |
9 | Guiding Principles 10 | 11 | - Changelogs are for humans, not machines. 12 | - There should be an entry for every single version. 13 | - The same types of changes should be grouped. 14 | - Versions and sections should be linkable. 15 | - The latest version comes first. 16 | - The release date of each versions is displayed. 17 | - Mention whether you follow Semantic Versioning. 18 | 19 |
20 | 21 |
22 | Types of changes 23 | 24 | Changelog entries are classified using the following labels _(from [keep-a-changelog](http://keepachangelog.com/)_): 25 | 26 | - `Added` for new features. 27 | - `Changed` for changes in existing functionality. 28 | - `Deprecated` for soon-to-be removed features. 29 | - `Removed` for now removed features. 30 | - `Fixed` for any bug fixes. 31 | - `Security` in case of vulnerabilities. 32 | 33 |
34 | 35 | 36 | ## [2.0.1] - 2017-10-13 37 | 38 | ### Fixed 39 | 40 | - Adds `isobject` to dependencies. 41 | 42 | ## [2.0.0] - 2017-10-13 43 | 44 | ### Removed 45 | 46 | - No longer wrapped in UMD code since it should be added by implementations. 47 | 48 | ### Added 49 | 50 | - Now throws a `TypeError` when the first argument is not an object. 51 | 52 | ### Misc 53 | 54 | - Add `.verb.md` template to generate readme documentation 55 | - update bower.json 56 | 57 | ## [1.2.0] - 2017-10-13 58 | 59 | - Merge pull request #9 from stellard/rename_on_value 60 | - Supports renaming a key based on the value 61 | 62 | ## [1.1.3] - 2017-04-03 63 | 64 | - Merge pull request #8 from S4RB/master 65 | - Fixes `hasOwnProperty` error when input does not inherit from Object.create 66 | 67 | ## [1.1.0] - 2015-01-20 68 | 69 | - add check for function, simplify 70 | 71 | ## [1.0.0] - 2015-01-20 72 | 73 | - Merge pull request #5 from palanik/master 74 | 75 | ## [0.1.0] - 2014-02-10 76 | 77 | - first commit 78 | 79 | [1.2.0]: https://github.com/jonschlinkert/rename-keys/compare/v1.1.3...v1.2.0 80 | [1.1.3]: https://github.com/jonschlinkert/rename-keys/compare/v1.1.0...v1.1.3 81 | [1.1.0]: https://github.com/jonschlinkert/rename-keys/compare/v1.0.0...v1.1.0 82 | [1.0.0]: https://github.com/jonschlinkert/rename-keys/compare/v0.1.0...v1.0.0 83 | [keep-a-changelog]: https://github.com/olivierlacan/keep-a-changelog 84 | 85 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2015, 2017, Jon Schlinkert. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rename-keys [![NPM version](https://img.shields.io/npm/v/rename-keys.svg?style=flat)](https://www.npmjs.com/package/rename-keys) [![NPM monthly downloads](https://img.shields.io/npm/dm/rename-keys.svg?style=flat)](https://npmjs.org/package/rename-keys) [![NPM total downloads](https://img.shields.io/npm/dt/rename-keys.svg?style=flat)](https://npmjs.org/package/rename-keys) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/rename-keys.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/rename-keys) 2 | 3 | > Modify the names of the own enumerable properties (keys) of an object. 4 | 5 | Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. 6 | 7 | ## Install 8 | 9 | Install with [npm](https://www.npmjs.com/): 10 | 11 | ```sh 12 | $ npm install --save rename-keys 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```js 18 | var renameKeys = require('rename-keys'); 19 | ``` 20 | 21 | ## API 22 | 23 | **Params** 24 | 25 | * `object` **{Object}**: The object with keys to rename. 26 | * `fn` **{Function}**: Renaming function to use on each key in the object. 27 | * `returns` **{Object}**: Returns a new object with renamed keys. 28 | 29 | **Example** 30 | 31 | ```js 32 | var obj = renameKeys({a: 1, b: 2, c: 3}, function(key, val) { 33 | return '--' + key; 34 | }); 35 | console.log(obj); 36 | //=> { '--a': 1, '--b': 2, '--c': 3} 37 | ``` 38 | 39 | **Rename based on value** 40 | 41 | ```js 42 | var obj = renameKeys({a: 1, b: 2, c: 3}, function(key, val) { 43 | return val > 1 ? ('++' + key) : ('--' + key); 44 | }); 45 | console.log(obj); 46 | //=> {'--a': 1, '++b': 2, '++c': 3}; 47 | ``` 48 | 49 | ## About 50 | 51 | ### Contributing 52 | 53 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 54 | 55 | ### Contributors 56 | 57 | | **Commits** | **Contributor** | 58 | | --- | --- | 59 | | 15 | [jonschlinkert](https://github.com/jonschlinkert) | 60 | | 5 | [doowb](https://github.com/doowb) | 61 | | 2 | [stellard](https://github.com/stellard) | 62 | | 1 | [kof](https://github.com/kof) | 63 | | 1 | [robinbullocks4rb](https://github.com/robinbullocks4rb) | 64 | | 1 | [palanik](https://github.com/palanik) | 65 | 66 | ### Building docs 67 | 68 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 69 | 70 | To generate the readme, run the following command: 71 | 72 | ```sh 73 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 74 | ``` 75 | 76 | ### Running tests 77 | 78 | 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: 79 | 80 | ```sh 81 | $ npm install && npm test 82 | ``` 83 | 84 | ### Author 85 | 86 | **Jon Schlinkert** 87 | 88 | * [github/jonschlinkert](https://github.com/jonschlinkert) 89 | * [twitter/jonschlinkert](https://twitter.com/jonschlinkert) 90 | 91 | ### License 92 | 93 | Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). 94 | Released under the [MIT License](LICENSE). 95 | 96 | *** 97 | 98 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on October 13, 2017._ -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rename-keys", 3 | "description": "Modify the names of the own enumerable properties (keys) of an object.", 4 | "repository": "jonschlinkert/rename-keys", 5 | "license": "MIT", 6 | "homepage": "https://github.com/jonschlinkert/rename-keys", 7 | "authors": [ 8 | "Jon Schlinkert" 9 | ], 10 | "main": [ 11 | "index.js" 12 | ], 13 | "devDependencies": { 14 | "gulp-format-md": "^1.0.0", 15 | "mocha": "^3.2.0" 16 | }, 17 | "keywords": [ 18 | "enumerable", 19 | "keys", 20 | "object", 21 | "properties", 22 | "rename" 23 | ], 24 | "version": "2.0.0", 25 | "bugs": { 26 | "url": "https://github.com/jonschlinkert/rename-keys/issues" 27 | }, 28 | "ignore": [ 29 | "actual", 30 | "bower_components", 31 | "fixtures", 32 | "node_modules", 33 | "temp", 34 | "test", 35 | "test.js", 36 | "tmp" 37 | ], 38 | "files": [ 39 | "index.js" 40 | ], 41 | "contributors": [ 42 | "Brian Woodward (https://twitter.com/doowb)", 43 | "Jon Schlinkert (http://twitter.com/jonschlinkert)", 44 | "Oleg Slobodskoi (https://kof.github.io)", 45 | "Palani Kumanan (https://github.com/palanik)", 46 | "Robin Bullock (https://github.com/robinbullocks4rb)", 47 | "Scott Ellard (http://www.anenta.com)" 48 | ], 49 | "dependencies": { 50 | "isobject": "^3.0.1" 51 | } 52 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var isObject = require('isobject'); 4 | 5 | /** 6 | * Rename the keys on the given `object` using a renaming `fn`. The 7 | * renaming function receives the `key` and `value` of each property 8 | * in the object. If the renaming function returns a _non-empty string_ 9 | * it will be used to rename the key, otherwise the original key is 10 | * retained. 11 | * 12 | * ```js 13 | * var obj = renameKeys({a: 1, b: 2, c: 3}, function(key, val) { 14 | * return '--' + key; 15 | * }); 16 | * console.log(obj); 17 | * //=> { '--a': 1, '--b': 2, '--c': 3} 18 | * ``` 19 | * @param {Object} `object` The object with keys to rename. 20 | * @param {Function} `fn` Renaming function to use on each key in the object. 21 | * @return {Object} Returns a new object with renamed keys. 22 | * @api public 23 | */ 24 | 25 | module.exports = function(obj, fn) { 26 | if (!isObject(obj)) { 27 | throw new TypeError('expected an object'); 28 | } 29 | 30 | if (typeof fn !== 'function') { 31 | return obj; 32 | } 33 | 34 | var keys = Object.keys(obj); 35 | var result = {}; 36 | 37 | for (var i = 0; i < keys.length; i++) { 38 | var key = keys[i]; 39 | var val = obj[key]; 40 | var str = fn(key, val); 41 | if (typeof str === 'string' && str !== '') { 42 | key = str; 43 | } 44 | result[key] = val; 45 | } 46 | return result; 47 | }; 48 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rename-keys", 3 | "description": "Modify the names of the own enumerable properties (keys) of an object.", 4 | "version": "2.0.1", 5 | "homepage": "https://github.com/jonschlinkert/rename-keys", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "contributors": [ 8 | "Brian Woodward (https://twitter.com/doowb)", 9 | "Jon Schlinkert (http://twitter.com/jonschlinkert)", 10 | "Oleg Slobodskoi (https://kof.github.io)", 11 | "Palani Kumanan (https://github.com/palanik)", 12 | "Robin Bullock (https://github.com/robinbullocks4rb)", 13 | "Scott Ellard (http://www.anenta.com)" 14 | ], 15 | "repository": "jonschlinkert/rename-keys", 16 | "bugs": { 17 | "url": "https://github.com/jonschlinkert/rename-keys/issues" 18 | }, 19 | "license": "MIT", 20 | "files": [ 21 | "index.js" 22 | ], 23 | "main": "index.js", 24 | "engines": { 25 | "node": ">= 0.8.0" 26 | }, 27 | "scripts": { 28 | "test": "mocha" 29 | }, 30 | "dependencies": { 31 | "isobject": "^3.0.1" 32 | }, 33 | "devDependencies": { 34 | "gulp-format-md": "^1.0.0", 35 | "mocha": "^3.2.0" 36 | }, 37 | "keywords": [ 38 | "enumerable", 39 | "keys", 40 | "object", 41 | "properties", 42 | "rename" 43 | ], 44 | "verb": { 45 | "toc": false, 46 | "layout": "default", 47 | "tasks": [ 48 | "readme" 49 | ], 50 | "plugins": [ 51 | "gulp-format-md" 52 | ], 53 | "lint": { 54 | "reflinks": true 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | var assert = require('assert'); 5 | var rename = require('./'); 6 | 7 | describe('rename keys', function() { 8 | it('should throw an error if an object is not passed', function() { 9 | assert.throws(function() { 10 | rename('foo'); 11 | }); 12 | }); 13 | 14 | it('should return the original object if no function is passed.', function() { 15 | assert.deepEqual(rename({name: 'rename-keys'}), {name: 'rename-keys'}); 16 | }); 17 | 18 | it('should return original object because nothing is renamed', function() { 19 | assert.deepEqual(rename({a: 1}, function() {}), {a: 1}); 20 | }); 21 | 22 | it('should rename keys.', function() { 23 | var actual = rename({name: 'rename-keys', description: 'foo'}, function(str) { 24 | return '--' + str; 25 | }); 26 | assert.deepEqual(Object.keys(actual)[1], '--description'); 27 | }); 28 | 29 | it('should only rename keys that are updated:', function() { 30 | assert.deepEqual(rename({a: 1, b: 1}, function(key) { 31 | if (key === 'b') return 'c'; 32 | }), {a: 1, c: 1}); 33 | }); 34 | 35 | it('should rename keys without conflicts', function() { 36 | assert.deepEqual(rename({a: 1, b: 2, c: 3, d: 4}, function(key) { 37 | var renameMap = {a: 'd', b: 'c', c: 'b', d: 'a'}; 38 | return renameMap[key]; 39 | }), {a: 4, b: 3, c: 2, d: 1}); 40 | }); 41 | 42 | it('should not throw an error when input object does not inherit from Object.prototype', function() { 43 | var input = Object.create(null); 44 | input.a = 0; 45 | 46 | rename(input, function(key) { 47 | return key; 48 | }); 49 | }); 50 | 51 | it('should rename key based on value', function() { 52 | assert.deepEqual(rename({a: 1, b: 2, c: 3}, function(key, value) { 53 | if (value > 1) return key + 'x'; 54 | }), {a: 1, bx: 2, cx: 3}); 55 | }); 56 | }); 57 | --------------------------------------------------------------------------------