From 337ca98cd6ae31e7a8d21fb3bb85b38dc5544593 Mon Sep 17 00:00:00 2001 From: yazeedb Date: Tue, 10 Apr 2018 13:22:39 -0400 Subject: [PATCH 1/9] add snippet markdown file --- snippets/renameKeys.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 snippets/renameKeys.md diff --git a/snippets/renameKeys.md b/snippets/renameKeys.md new file mode 100644 index 000000000..c7f3386d0 --- /dev/null +++ b/snippets/renameKeys.md @@ -0,0 +1,22 @@ +### renameKeys + +Renames multiple object keys + +Get the object's keys with `Object.keys()` and return an object with the new keys, according to the `keysMap`, using `Array.reduce()`. + +The initial value is an empty object which is used as the accumulator, `acc`, in the callback function. Using the spread operator `(...)`, `acc` is continuously merged with a new object containing the new key and original object's value. If a new key doesn't exist, fallback to original object's key. + +```js +const renameKeys = (keysMap, obj) => Object + .keys(obj) + .reduce((acc, key) => ({ + ...acc, + ...{ [keysMap[key] || key]: obj[key] } + }), {}); +``` + +```js +const obj = { name: 'Bobo', job: 'Front-End Master', shoeSize: 100 }; +renameKeys({ name: 'firstName', job: 'passion' }, obj); +// { firstName: 'Bobo', passion: 'Front-End Master', shoeSize: 100 } +``` From f8fc1272c82da08442517ac4b56e2ddc3c185aec Mon Sep 17 00:00:00 2001 From: yazeedb Date: Tue, 10 Apr 2018 13:25:14 -0400 Subject: [PATCH 2/9] update tag_database --- tag_database | 1 + 1 file changed, 1 insertion(+) diff --git a/tag_database b/tag_database index 24cf93bef..21a260405 100644 --- a/tag_database +++ b/tag_database @@ -216,6 +216,7 @@ reduceSuccessive:array,function reduceWhich:array,function remove:array removeNonASCII:string,regexp +renameKeys:object reverseString:string,array RGBToHex:utility round:math From b196cc0eaa391362336ee5753b050a7ce23f8d8c Mon Sep 17 00:00:00 2001 From: yazeedb Date: Tue, 10 Apr 2018 14:36:37 -0400 Subject: [PATCH 3/9] add unit test --- test/renameKeys/renameKeys.js | 7 +++++++ test/renameKeys/renameKeys.test.js | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 test/renameKeys/renameKeys.js create mode 100644 test/renameKeys/renameKeys.test.js diff --git a/test/renameKeys/renameKeys.js b/test/renameKeys/renameKeys.js new file mode 100644 index 000000000..6af18dae7 --- /dev/null +++ b/test/renameKeys/renameKeys.js @@ -0,0 +1,7 @@ +const renameKeys = (keysMap, obj) => Object +.keys(obj) +.reduce((acc, key) => ({ +...acc, +...{ [keysMap[key] || key]: obj[key] } +}), {}); +module.exports = renameKeys; \ No newline at end of file diff --git a/test/renameKeys/renameKeys.test.js b/test/renameKeys/renameKeys.test.js new file mode 100644 index 000000000..b80b63189 --- /dev/null +++ b/test/renameKeys/renameKeys.test.js @@ -0,0 +1,19 @@ +const test = require('tape'); +const renameKeys = require('./renameKeys.js'); + +test.only('Testing renameKeys', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof renameKeys === 'function', 'renameKeys is a Function'); + + const obj = { name: 'Bobo', job: 'Front-End Master', shoeSize: 100 }; + const renamedObj = renameKeys({ name: 'firstName', job: 'passion' }, obj); + + t.deepEqual(renamedObj, { firstName: 'Bobo', passion: 'Front-End Master', shoeSize: 100 }); + + //t.deepEqual(renameKeys(args..), 'Expected'); + //t.equal(renameKeys(args..), 'Expected'); + //t.false(renameKeys(args..), 'Expected'); + //t.throws(renameKeys(args..), 'Expected'); + t.end(); +}); From 435b3ead37eeeeb6ede88d7999e3dba4dfdbae97 Mon Sep 17 00:00:00 2001 From: yazeedb Date: Tue, 10 Apr 2018 14:37:24 -0400 Subject: [PATCH 4/9] remove test.only --- test/renameKeys/renameKeys.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/renameKeys/renameKeys.test.js b/test/renameKeys/renameKeys.test.js index b80b63189..4487a14a4 100644 --- a/test/renameKeys/renameKeys.test.js +++ b/test/renameKeys/renameKeys.test.js @@ -1,7 +1,7 @@ const test = require('tape'); const renameKeys = require('./renameKeys.js'); -test.only('Testing renameKeys', (t) => { +test('Testing renameKeys', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof renameKeys === 'function', 'renameKeys is a Function'); From e6682837890fd4ee2b59fcbc7696923b502ae639 Mon Sep 17 00:00:00 2001 From: yazeedb Date: Tue, 10 Apr 2018 14:42:45 -0400 Subject: [PATCH 5/9] add newline --- test/renameKeys/renameKeys.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/renameKeys/renameKeys.js b/test/renameKeys/renameKeys.js index 6af18dae7..1a02ed11a 100644 --- a/test/renameKeys/renameKeys.js +++ b/test/renameKeys/renameKeys.js @@ -4,4 +4,4 @@ const renameKeys = (keysMap, obj) => Object ...acc, ...{ [keysMap[key] || key]: obj[key] } }), {}); -module.exports = renameKeys; \ No newline at end of file +module.exports = renameKeys; From 3708afc65e5447c8fa339735baf03c42a7a2d5f1 Mon Sep 17 00:00:00 2001 From: yazeedb Date: Tue, 10 Apr 2018 14:44:14 -0400 Subject: [PATCH 6/9] remove comments --- test/renameKeys/renameKeys.test.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/renameKeys/renameKeys.test.js b/test/renameKeys/renameKeys.test.js index 4487a14a4..328a69e80 100644 --- a/test/renameKeys/renameKeys.test.js +++ b/test/renameKeys/renameKeys.test.js @@ -11,9 +11,5 @@ test('Testing renameKeys', (t) => { t.deepEqual(renamedObj, { firstName: 'Bobo', passion: 'Front-End Master', shoeSize: 100 }); - //t.deepEqual(renameKeys(args..), 'Expected'); - //t.equal(renameKeys(args..), 'Expected'); - //t.false(renameKeys(args..), 'Expected'); - //t.throws(renameKeys(args..), 'Expected'); t.end(); }); From c24f9d9695b26121b036ad45809d470c567d3339 Mon Sep 17 00:00:00 2001 From: yazeedb Date: Tue, 10 Apr 2018 15:46:48 -0400 Subject: [PATCH 7/9] use spaces --- test/renameKeys/renameKeys.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/renameKeys/renameKeys.test.js b/test/renameKeys/renameKeys.test.js index 328a69e80..42900ec17 100644 --- a/test/renameKeys/renameKeys.test.js +++ b/test/renameKeys/renameKeys.test.js @@ -6,10 +6,10 @@ test('Testing renameKeys', (t) => { //Please go to https://github.com/substack/tape t.true(typeof renameKeys === 'function', 'renameKeys is a Function'); - const obj = { name: 'Bobo', job: 'Front-End Master', shoeSize: 100 }; - const renamedObj = renameKeys({ name: 'firstName', job: 'passion' }, obj); + const obj = { name: 'Bobo', job: 'Front-End Master', shoeSize: 100 }; + const renamedObj = renameKeys({ name: 'firstName', job: 'passion' }, obj); - t.deepEqual(renamedObj, { firstName: 'Bobo', passion: 'Front-End Master', shoeSize: 100 }); + t.deepEqual(renamedObj, { firstName: 'Bobo', passion: 'Front-End Master', shoeSize: 100 }); t.end(); }); From f11d74db68663247683c4a20036a4a8b62fa2b00 Mon Sep 17 00:00:00 2001 From: yazeedb Date: Tue, 10 Apr 2018 15:47:06 -0400 Subject: [PATCH 8/9] run linter --- snippets/renameKeys.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/snippets/renameKeys.md b/snippets/renameKeys.md index c7f3386d0..c32133565 100644 --- a/snippets/renameKeys.md +++ b/snippets/renameKeys.md @@ -7,12 +7,14 @@ Get the object's keys with `Object.keys()` and return an object with the new key The initial value is an empty object which is used as the accumulator, `acc`, in the callback function. Using the spread operator `(...)`, `acc` is continuously merged with a new object containing the new key and original object's value. If a new key doesn't exist, fallback to original object's key. ```js -const renameKeys = (keysMap, obj) => Object - .keys(obj) - .reduce((acc, key) => ({ - ...acc, - ...{ [keysMap[key] || key]: obj[key] } - }), {}); +const renameKeys = (keysMap, obj) => + Object.keys(obj).reduce( + (acc, key) => ({ + ...acc, + ...{ [keysMap[key] || key]: obj[key] } + }), + {} + ); ``` ```js From d40f8592880492815b3ecd2962aa18c16c9ecf16 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 10 Apr 2018 23:03:42 +0300 Subject: [PATCH 9/9] Update renameKeys.md --- snippets/renameKeys.md | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/snippets/renameKeys.md b/snippets/renameKeys.md index c32133565..419062323 100644 --- a/snippets/renameKeys.md +++ b/snippets/renameKeys.md @@ -1,24 +1,19 @@ ### renameKeys -Renames multiple object keys +Replaces the names of multiple object keys with the values provided. -Get the object's keys with `Object.keys()` and return an object with the new keys, according to the `keysMap`, using `Array.reduce()`. - -The initial value is an empty object which is used as the accumulator, `acc`, in the callback function. Using the spread operator `(...)`, `acc` is continuously merged with a new object containing the new key and original object's value. If a new key doesn't exist, fallback to original object's key. +Use `Object.keys()` in combination with `Array.reduce()` and the spread operator (`...`) to get the object's keys and rename them according to `keysMap`. ```js -const renameKeys = (keysMap, obj) => - Object.keys(obj).reduce( - (acc, key) => ({ - ...acc, - ...{ [keysMap[key] || key]: obj[key] } - }), - {} - ); +const renameKeys = (keysMap, obj) => Object + .keys(obj) + .reduce((acc, key) => ({ + ...acc, + ...{ [keysMap[key] || key]: obj[key] } + }), {}); ``` ```js const obj = { name: 'Bobo', job: 'Front-End Master', shoeSize: 100 }; -renameKeys({ name: 'firstName', job: 'passion' }, obj); -// { firstName: 'Bobo', passion: 'Front-End Master', shoeSize: 100 } +renameKeys({ name: 'firstName', job: 'passion' }, obj); // { firstName: 'Bobo', passion: 'Front-End Master', shoeSize: 100 } ```