Skip to content

Commit fa468b6

Browse files
authored
Merge pull request #24 from dashed/flow-types
Add flow types
2 parents 84a075f + 3a45c26 commit fa468b6

6 files changed

Lines changed: 99 additions & 5 deletions

File tree

.flowconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[ignore]
2+
3+
[include]
4+
5+
[libs]
6+
7+
[lints]
8+
9+
[options]
10+
11+
[strict]

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//
2+
13
module.exports = function shallowEqual(objA, objB, compare, compareContext) {
24
var ret = compare ? compare.call(compareContext, objA, objB) : void 0;
35

index.js.flow

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @flow
2+
3+
declare module.exports: <T, U>(
4+
objA?: ?T,
5+
objB?: ?U,
6+
compare?: ?(objValue: any, otherValue: any, key?: string) => boolean | void,
7+
compareContext?: ?any
8+
) => boolean;

index.original.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// @flow
2+
3+
module.exports = function shallowEqual<T, U>(
4+
objA?: ?T,
5+
objB?: ?U,
6+
compare?: ?(objValue: any, otherValue: any, key?: string) => boolean | void,
7+
compareContext?: ?any
8+
): boolean {
9+
var ret = compare ? compare.call(compareContext, objA, objB) : void 0;
10+
11+
if (ret !== void 0) {
12+
return !!ret;
13+
}
14+
15+
if (objA === objB) {
16+
return true;
17+
}
18+
19+
if (typeof objA !== "object" || !objA || typeof objB !== "object" || !objB) {
20+
return false;
21+
}
22+
23+
var keysA = Object.keys(objA);
24+
var keysB = Object.keys(objB);
25+
26+
if (keysA.length !== keysB.length) {
27+
return false;
28+
}
29+
30+
var bHasOwnProperty = Object.prototype.hasOwnProperty.bind(objB);
31+
32+
// Test for A's keys different from B.
33+
for (var idx = 0; idx < keysA.length; idx++) {
34+
var key = keysA[idx];
35+
36+
if (!bHasOwnProperty(key)) {
37+
return false;
38+
}
39+
40+
var valueA = objA[key];
41+
var valueB = objB[key];
42+
43+
ret = compare ? compare.call(compareContext, valueA, valueB, key) : void 0;
44+
45+
if (ret === false || (ret === void 0 && valueA !== valueB)) {
46+
return false;
47+
}
48+
}
49+
50+
return true;
51+
};

package.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
"scripts": {
77
"lint": "eslint index.js test",
88
"test": "mocha --require babel-register",
9-
"prepublish": "npm run lint && npm run test",
9+
"build:strip-flow":
10+
"flow-remove-types --pretty index.original.js > index.js",
11+
"build:gen-flow": "flow gen-flow-files index.original.js > index.js.flow",
12+
"build": "npm run build:strip-flow && npm run build:gen-flow",
13+
"prepublish":
14+
"npm run build && npm run pretty && npm run lint && npm run test",
1015
"travis": "npm run lint && npm run test",
11-
"pretty": "prettier --write --tab-width 2 'test/**/*.js' '*.js'",
16+
"pretty": "prettier --write --tab-width 2 'test/**/*.js' '*.{js,js.flow}'",
1217
"precommit": "lint-staged"
1318
},
1419
"lint-staged": {
15-
"*.{js,json,css}": ["prettier --write", "git add"]
20+
"*.{js,json,css,js.flow}": ["prettier --write", "git add"]
1621
},
1722
"author": {
1823
"name": "Alberto Leal",
@@ -21,7 +26,7 @@
2126
},
2227
"repository": "dashed/shallowequal",
2328
"license": "MIT",
24-
"files": ["index.js"],
29+
"files": ["index.js", "index.js.flow", "index.original.js"],
2530
"keywords": [
2631
"shallowequal",
2732
"shallow",
@@ -45,6 +50,8 @@
4550
"babel-register": "^6.24.1",
4651
"chai": "^4.0.0",
4752
"eslint": "^4.7.1",
53+
"flow-bin": "^0.72.0",
54+
"flow-remove-types": "^1.2.3",
4855
"husky": "^0.14.3",
4956
"lint-staged": "^6.0.0",
5057
"mocha": "^5.0.0",

yarn.lock

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ babylon@7.0.0-beta.44:
632632
version "7.0.0-beta.44"
633633
resolved "https://registry.yarnpkg.com/babylon/-/babylon-7.0.0-beta.44.tgz#89159e15e6e30c5096e22d738d8c0af8a0e8ca1d"
634634

635-
babylon@^6.18.0:
635+
babylon@^6.15.0, babylon@^6.18.0:
636636
version "6.18.0"
637637
resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.18.0.tgz#af2f3b88fa6f5c1e4c634d1a0f8eac4f55b395e3"
638638

@@ -1046,6 +1046,17 @@ flat-cache@^1.2.1:
10461046
graceful-fs "^4.1.2"
10471047
write "^0.2.1"
10481048

1049+
flow-bin@^0.72.0:
1050+
version "0.72.0"
1051+
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.72.0.tgz#12051180fb2db7ccb728fefe67c77e955e92a44d"
1052+
1053+
flow-remove-types@^1.2.3:
1054+
version "1.2.3"
1055+
resolved "https://registry.yarnpkg.com/flow-remove-types/-/flow-remove-types-1.2.3.tgz#6131aefc7da43364bb8b479758c9dec7735d1a18"
1056+
dependencies:
1057+
babylon "^6.15.0"
1058+
vlq "^0.2.1"
1059+
10491060
fs.realpath@^1.0.0:
10501061
version "1.0.0"
10511062
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
@@ -1976,6 +1987,10 @@ util-deprecate@~1.0.1:
19761987
version "1.0.2"
19771988
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
19781989

1990+
vlq@^0.2.1:
1991+
version "0.2.3"
1992+
resolved "https://registry.yarnpkg.com/vlq/-/vlq-0.2.3.tgz#8f3e4328cf63b1540c0d67e1b2778386f8975b26"
1993+
19791994
which@^1.2.10, which@^1.2.9:
19801995
version "1.3.0"
19811996
resolved "https://registry.yarnpkg.com/which/-/which-1.3.0.tgz#ff04bdfc010ee547d780bec38e1ac1c2777d253a"

0 commit comments

Comments
 (0)