From 1d968d0e80a8600da70ed42eae4e2b0378255804 Mon Sep 17 00:00:00 2001 From: Franky Chung Date: Wed, 17 Jun 2015 23:56:48 +0900 Subject: [PATCH 1/3] mapValues util and unit test --- src/utils/mapValues.js | 6 ++++++ test/utils/mapValues.spec.js | 12 ++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 src/utils/mapValues.js create mode 100644 test/utils/mapValues.spec.js diff --git a/src/utils/mapValues.js b/src/utils/mapValues.js new file mode 100644 index 0000000000..29d203cf61 --- /dev/null +++ b/src/utils/mapValues.js @@ -0,0 +1,6 @@ +export default function mapValues(obj, fn) { + return Object.keys(obj).reduce((result, key) => { + result[key] = fn(obj[key], key); + return result; + }, {}); +} diff --git a/test/utils/mapValues.spec.js b/test/utils/mapValues.spec.js new file mode 100644 index 0000000000..1862f3e7f8 --- /dev/null +++ b/test/utils/mapValues.spec.js @@ -0,0 +1,12 @@ +import expect from 'expect'; +import mapValues from '../../src/utils/mapValues'; + +describe('Utils', () => { + describe('mapValues', () => { + it('should return object with mapped values', () => { + const test = { 'a': 1, 'b': 2 }; + expect(mapValues(test, val => val * 3)).toEqual({ 'a': 3, 'b': 6 }); + }); + }); +}); + From 1fbb49057771d575eae1a13fd4915f85d7330a83 Mon Sep 17 00:00:00 2001 From: Franky Chung Date: Wed, 17 Jun 2015 23:57:14 +0900 Subject: [PATCH 2/3] Replace lodash/object/mapValues with own utils --- src/utils/bindActionCreators.js | 2 +- src/utils/composeStores.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/bindActionCreators.js b/src/utils/bindActionCreators.js index 801665492c..f9a81a5a80 100644 --- a/src/utils/bindActionCreators.js +++ b/src/utils/bindActionCreators.js @@ -1,4 +1,4 @@ -import mapValues from 'lodash/object/mapValues'; +import mapValues from '../utils/mapValues'; export default function bindActionCreators(actionCreators, dispatch) { return mapValues(actionCreators, actionCreator => diff --git a/src/utils/composeStores.js b/src/utils/composeStores.js index 49b76c6d74..4122006ce4 100644 --- a/src/utils/composeStores.js +++ b/src/utils/composeStores.js @@ -1,4 +1,4 @@ -import mapValues from 'lodash/object/mapValues'; +import mapValues from '../utils/mapValues'; import pick from 'lodash/object/pick'; export default function composeStores(stores) { From a6efb92ca5b9cc2f4ca70c9be1f1f967c0bf65e8 Mon Sep 17 00:00:00 2001 From: Franky Chung Date: Thu, 18 Jun 2015 00:04:08 +0900 Subject: [PATCH 3/3] Test key is passed into iteratee for mapValues utils/composeStores uses the key second argument for the iteratee so updated the test to make sure it isn't missing. --- test/utils/mapValues.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/utils/mapValues.spec.js b/test/utils/mapValues.spec.js index 1862f3e7f8..36c3c6b2c9 100644 --- a/test/utils/mapValues.spec.js +++ b/test/utils/mapValues.spec.js @@ -4,8 +4,8 @@ import mapValues from '../../src/utils/mapValues'; describe('Utils', () => { describe('mapValues', () => { it('should return object with mapped values', () => { - const test = { 'a': 1, 'b': 2 }; - expect(mapValues(test, val => val * 3)).toEqual({ 'a': 3, 'b': 6 }); + const test = { 'a': 'c', 'b': 'd' }; + expect(mapValues(test, (val, key) => val + key)).toEqual({ 'a': 'ca', 'b': 'db' }); }); }); });