Skip to content

Commit df8704a

Browse files
committedMay 31, 2018
Simplify object check and eliminate lodash depdency (#936)
* Simplify object check and eliminate lodash depdency, mirroring reduxjs/redux#2599 . * Add missing files. * Replace isPlainObject with unmodified version from Redux * Also replace the spec with the unmodified version
1 parent cf24f08 commit df8704a

7 files changed

+241
-191
lines changed
 

‎.babelrc

-6
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,8 @@
3434
["transform-es2015-modules-commonjs", { "loose": true }]
3535
]
3636
},
37-
"es": {
38-
"plugins": [
39-
"./build/use-lodash-es"
40-
]
41-
},
4237
"rollup": {
4338
"plugins": [
44-
"./build/use-lodash-es",
4539
"external-helpers"
4640
]
4741
}

‎build/use-lodash-es.js

-10
This file was deleted.

‎package-lock.json

+199-166
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+6-8
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,13 @@
4545
"dependencies": {
4646
"hoist-non-react-statics": "^2.5.0",
4747
"invariant": "^2.2.4",
48-
"lodash": "^4.17.5",
49-
"lodash-es": "^4.17.5",
5048
"loose-envify": "^1.1.0",
5149
"prop-types": "^15.6.1"
5250
},
5351
"devDependencies": {
5452
"babel-cli": "^6.26.0",
5553
"babel-core": "^6.26.0",
56-
"babel-eslint": "^8.2.2",
54+
"babel-eslint": "^8.2.3",
5755
"babel-plugin-check-es2015-constants": "^6.3.13",
5856
"babel-plugin-external-helpers": "^6.22.0",
5957
"babel-plugin-syntax-jsx": "^6.3.13",
@@ -83,17 +81,17 @@
8381
"cross-env": "^5.1.4",
8482
"es3ify": "^0.2.0",
8583
"eslint": "^4.19.1",
86-
"eslint-plugin-import": "^2.10.0",
84+
"eslint-plugin-import": "^2.11.0",
8785
"eslint-plugin-react": "^7.7.0",
8886
"glob": "^7.1.1",
8987
"jest": "^22.4.3",
90-
"react": "^16.3.0",
91-
"react-dom": "^16.3.0",
92-
"react-test-renderer": "^16.3.0",
88+
"react": "^16.3.2",
89+
"react-dom": "^16.3.2",
90+
"react-test-renderer": "^16.3.2",
9391
"redux": "^3.0.0",
9492
"rimraf": "^2.6.2",
9593
"rollup": "^0.57.1",
96-
"rollup-plugin-babel": "^3.0.3",
94+
"rollup-plugin-babel": "^3.0.4",
9795
"rollup-plugin-commonjs": "^9.1.0",
9896
"rollup-plugin-node-resolve": "^3.3.0",
9997
"rollup-plugin-replace": "^2.0.0",

‎src/utils/isPlainObject.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {any} obj The object to inspect.
3+
* @returns {boolean} True if the argument appears to be a plain object.
4+
*/
5+
export default function isPlainObject(obj) {
6+
if (typeof obj !== 'object' || obj === null) return false
7+
8+
let proto = obj
9+
while (Object.getPrototypeOf(proto) !== null) {
10+
proto = Object.getPrototypeOf(proto)
11+
}
12+
13+
return Object.getPrototypeOf(obj) === proto
14+
}

‎src/utils/verifyPlainObject.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import isPlainObject from 'lodash/isPlainObject'
1+
import isPlainObject from './isPlainObject'
22
import warning from './warning'
33

44
export default function verifyPlainObject(value, displayName, methodName) {

‎test/utils/isPlainObject.spec.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import isPlainObject from '../../src/utils/isPlainObject'
2+
import vm from 'vm'
3+
4+
describe('isPlainObject', () => {
5+
it('returns true only if plain object', () => {
6+
function Test() {
7+
this.prop = 1
8+
}
9+
10+
const sandbox = { fromAnotherRealm: false }
11+
vm.runInNewContext('fromAnotherRealm = {}', sandbox)
12+
13+
expect(isPlainObject(sandbox.fromAnotherRealm)).toBe(true)
14+
expect(isPlainObject(new Test())).toBe(false)
15+
expect(isPlainObject(new Date())).toBe(false)
16+
expect(isPlainObject([1, 2, 3])).toBe(false)
17+
expect(isPlainObject(null)).toBe(false)
18+
expect(isPlainObject()).toBe(false)
19+
expect(isPlainObject({ x: 1, y: 2 })).toBe(true)
20+
})
21+
})

0 commit comments

Comments
 (0)
Please sign in to comment.