From 3aef46935f63175f9e79d84fce343fa8fe1963f9 Mon Sep 17 00:00:00 2001 From: Franky Chung Date: Thu, 18 Jun 2015 09:20:25 +0900 Subject: [PATCH 1/6] Add replacement utils for lodash modules + tests --- src/utils/identity.js | 3 +++ src/utils/isPlainObject.js | 3 +++ src/utils/pick.js | 8 ++++++++ test/utils/identity.spec.js | 11 +++++++++++ test/utils/isPlainObject.spec.js | 17 +++++++++++++++++ test/utils/pick.spec.js | 11 +++++++++++ 6 files changed, 53 insertions(+) create mode 100644 src/utils/identity.js create mode 100644 src/utils/isPlainObject.js create mode 100644 src/utils/pick.js create mode 100644 test/utils/identity.spec.js create mode 100644 test/utils/isPlainObject.spec.js create mode 100644 test/utils/pick.spec.js diff --git a/src/utils/identity.js b/src/utils/identity.js new file mode 100644 index 0000000000..8c690a8859 --- /dev/null +++ b/src/utils/identity.js @@ -0,0 +1,3 @@ +export default function identity(value) { + return value; +} diff --git a/src/utils/isPlainObject.js b/src/utils/isPlainObject.js new file mode 100644 index 0000000000..2913818d60 --- /dev/null +++ b/src/utils/isPlainObject.js @@ -0,0 +1,3 @@ +export default function isPlainObject(obj) { + return typeof obj == 'object' && Object.getPrototypeOf(obj) === Object.prototype; +} diff --git a/src/utils/pick.js b/src/utils/pick.js new file mode 100644 index 0000000000..2c9719c1c0 --- /dev/null +++ b/src/utils/pick.js @@ -0,0 +1,8 @@ +export default function pick(obj, fn) { + return Object.keys(obj).reduce((result, key) => { + if (fn(obj[key])) { + result[key] = obj[key]; + } + return result; + }, {}); +} diff --git a/test/utils/identity.spec.js b/test/utils/identity.spec.js new file mode 100644 index 0000000000..94dc6f7ed7 --- /dev/null +++ b/test/utils/identity.spec.js @@ -0,0 +1,11 @@ +import expect from 'expect'; +import identity from '../../src/utils/identity'; + +describe('Utils', () => { + describe('identity', () => { + it('should return first argument passed to it', () => { + var test = { 'a': 1 }; + expect(identity(test, 'test')).toEqual(test); + }); + }); +}); diff --git a/test/utils/isPlainObject.spec.js b/test/utils/isPlainObject.spec.js new file mode 100644 index 0000000000..3096b0124f --- /dev/null +++ b/test/utils/isPlainObject.spec.js @@ -0,0 +1,17 @@ +import expect from 'expect'; +import isPlainObject from '../../src/utils/isPlainObject'; + +describe('Utils', () => { + describe('isPlainObject', () => { + it('should return true only if plain object', () => { + function Test() { + this.prop = 1; + } + + expect(isPlainObject(new Test())).toBe(false); + expect(isPlainObject(new Date())).toBe(false); + expect(isPlainObject([1, 2, 3])).toBe(false); + expect(isPlainObject({ 'x': 1, 'y': 2 })).toBe(true); + }); + }); +}); diff --git a/test/utils/pick.spec.js b/test/utils/pick.spec.js new file mode 100644 index 0000000000..6cd95f7395 --- /dev/null +++ b/test/utils/pick.spec.js @@ -0,0 +1,11 @@ +import expect from 'expect'; +import pick from '../../src/utils/pick'; + +describe('Utils', () => { + describe('pick', () => { + it('should return object with picked values', () => { + const test = { 'name': 'lily', 'age': 20 }; + expect(pick(test, x => typeof x === 'string')).toEqual({ 'name': 'lily' }); + }); + }); +}); From 9724f2cc2ed3cd02e20bb51f55ba95db087dbe70 Mon Sep 17 00:00:00 2001 From: Franky Chung Date: Thu, 18 Jun 2015 09:21:18 +0900 Subject: [PATCH 2/6] Replace lodash module imports with utils --- src/components/createConnector.js | 4 ++-- src/utils/composeStores.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/createConnector.js b/src/components/createConnector.js index 1a826c1e0d..3dcf48339e 100644 --- a/src/components/createConnector.js +++ b/src/components/createConnector.js @@ -1,6 +1,6 @@ -import identity from 'lodash/utility/identity'; +import identity from '../utils/identity'; import shallowEqual from '../utils/shallowEqual'; -import isPlainObject from 'lodash/lang/isPlainObject'; +import isPlainObject from '../utils/isPlainObject'; import invariant from 'invariant'; export default function createConnector(React) { diff --git a/src/utils/composeStores.js b/src/utils/composeStores.js index 4122006ce4..21ec65c693 100644 --- a/src/utils/composeStores.js +++ b/src/utils/composeStores.js @@ -1,5 +1,5 @@ import mapValues from '../utils/mapValues'; -import pick from 'lodash/object/pick'; +import pick from '../utils/pick'; export default function composeStores(stores) { stores = pick(stores, (val) => typeof val === 'function'); From a5a6bf47c12093e466065c6b8b1926b303c9bb34 Mon Sep 17 00:00:00 2001 From: Franky Chung Date: Thu, 18 Jun 2015 09:21:47 +0900 Subject: [PATCH 3/6] Remove lodash from package.json --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index 4a65d918b8..12dd3c8333 100644 --- a/package.json +++ b/package.json @@ -55,8 +55,7 @@ "dependencies": { "babel-runtime": "^5.5.8", "envify": "^3.4.0", - "invariant": "^2.0.0", - "lodash": "^3.9.3" + "invariant": "^2.0.0" }, "browserify": { "transform": [ From ddbfc5abb21cb18918d779907002c74a0b5f0d85 Mon Sep 17 00:00:00 2001 From: Franky Chung Date: Thu, 18 Jun 2015 09:35:01 +0900 Subject: [PATCH 4/6] Delint isPlainObject util --- src/utils/isPlainObject.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/isPlainObject.js b/src/utils/isPlainObject.js index 2913818d60..f22967b01e 100644 --- a/src/utils/isPlainObject.js +++ b/src/utils/isPlainObject.js @@ -1,3 +1,3 @@ export default function isPlainObject(obj) { - return typeof obj == 'object' && Object.getPrototypeOf(obj) === Object.prototype; + return typeof obj === 'object' && Object.getPrototypeOf(obj) === Object.prototype; } From d76b2166cc2397d6e9ff343ce954961b79e617fb Mon Sep 17 00:00:00 2001 From: Franky Chung Date: Thu, 18 Jun 2015 10:04:47 +0900 Subject: [PATCH 5/6] Improve identity util test to check reference Just returning the argument so can check by reference instead of deep equal --- test/utils/identity.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/utils/identity.spec.js b/test/utils/identity.spec.js index 94dc6f7ed7..4b45007c74 100644 --- a/test/utils/identity.spec.js +++ b/test/utils/identity.spec.js @@ -5,7 +5,7 @@ describe('Utils', () => { describe('identity', () => { it('should return first argument passed to it', () => { var test = { 'a': 1 }; - expect(identity(test, 'test')).toEqual(test); + expect(identity(test, 'test')).toBe(test); }); }); }); From 974b7b8ddf68beee9f73fea5ecb47672e6700888 Mon Sep 17 00:00:00 2001 From: Franky Chung Date: Thu, 18 Jun 2015 16:14:53 +0900 Subject: [PATCH 6/6] Check null/undefined case for isPlainObject --- src/utils/isPlainObject.js | 2 +- test/utils/isPlainObject.spec.js | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils/isPlainObject.js b/src/utils/isPlainObject.js index f22967b01e..a5845486cf 100644 --- a/src/utils/isPlainObject.js +++ b/src/utils/isPlainObject.js @@ -1,3 +1,3 @@ export default function isPlainObject(obj) { - return typeof obj === 'object' && Object.getPrototypeOf(obj) === Object.prototype; + return obj ? typeof obj === 'object' && Object.getPrototypeOf(obj) === Object.prototype : false; } diff --git a/test/utils/isPlainObject.spec.js b/test/utils/isPlainObject.spec.js index 3096b0124f..13cd49a1cf 100644 --- a/test/utils/isPlainObject.spec.js +++ b/test/utils/isPlainObject.spec.js @@ -11,6 +11,8 @@ describe('Utils', () => { expect(isPlainObject(new Test())).toBe(false); expect(isPlainObject(new Date())).toBe(false); expect(isPlainObject([1, 2, 3])).toBe(false); + expect(isPlainObject(null)).toBe(false); + expect(isPlainObject()).toBe(false); expect(isPlainObject({ 'x': 1, 'y': 2 })).toBe(true); }); });