Skip to content

Commit fe91a85

Browse files
ystarlongziGcaufy
authored andcommitted
style: 1)调整实现逻辑及代码缩进;2)添加单测
1 parent d86b473 commit fe91a85

3 files changed

Lines changed: 56 additions & 27 deletions

File tree

packages/wepy/src/util.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
/**
22
* Tencent is pleased to support the open source community by making WePY available.
33
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
4-
*
4+
*
55
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
66
* http://opensource.org/licenses/MIT
77
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
88
*/
99

1010
function isImmutable (maybeImmutable) {
11-
// https://github.com/facebook/immutable-js/blob/master/src/Predicates.js
12-
if (!maybeImmutable || typeof maybeImmutable !== 'object') {
13-
return false;
14-
}
11+
// https://github.com/facebook/immutable-js/blob/master/src/Predicates.js
12+
if (!maybeImmutable || typeof maybeImmutable !== 'object') {
13+
return false;
14+
}
1515

1616
const IMMUTABLE_KEYS = [
17-
'@@__IMMUTABLE_ITERABLE__@@',
18-
'@@__IMMUTABLE_KEYED__@@',
19-
'@@__IMMUTABLE_INDEXED__@@',
20-
'@@__IMMUTABLE_ORDERED__@@',
21-
'@@__IMMUTABLE_RECORD__@@'
22-
]
23-
24-
return !!IMMUTABLE_KEYS.filter(key => maybeImmutable[key]).length;
17+
'@@__IMMUTABLE_ITERABLE__@@',
18+
'@@__IMMUTABLE_KEYED__@@',
19+
'@@__IMMUTABLE_INDEXED__@@',
20+
'@@__IMMUTABLE_ORDERED__@@',
21+
'@@__IMMUTABLE_RECORD__@@'
22+
]
23+
24+
return !!IMMUTABLE_KEYS.filter(key => maybeImmutable[key]).length;
2525
}
2626

2727
export default {
@@ -30,7 +30,9 @@ export default {
3030
},
3131

3232
$isEqual (a, b, aStack, bStack) {
33-
if (isImmutable(a) || isImmutable(b)) return a === b
33+
if (isImmutable(a)) return a.equals(b)
34+
if (isImmutable(b)) return b.equals(a)
35+
3436
// Identical objects are equal. `0 === -0`, but they aren't identical.
3537
// See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal).
3638
if (a === b) return a !== 0 || 1 / a === 1 / b;
@@ -45,7 +47,9 @@ export default {
4547
},
4648

4749
$isDeepEqual (a, b, aStack, bStack) {
48-
if (isImmutable(a) || isImmutable(b)) return a === b
50+
if (isImmutable(a)) a = a.toJS()
51+
52+
if (isImmutable(b)) b = b.toJS()
4953

5054
let self = this;
5155
// Compare `[[Class]]` names.
@@ -309,7 +313,7 @@ export default {
309313
return rst;
310314
},
311315

312-
isImmutable,
316+
isImmutable,
313317

314318
/**
315319
* Hyphenate a camelCase string.

packages/wepy/test/index.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
22
* Tencent is pleased to support the open source community by making WePY available.
33
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
4-
*
4+
*
55
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
66
* http://opensource.org/licenses/MIT
77
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
88
*/
99

10-
10+
var Immutable = require('immutable')
1111
var assert = require('assert');
1212
var wepy = require('../lib/wepy.js').default;
1313

@@ -22,8 +22,19 @@ describe('index.js', () => {
2222
it('wepy.$isDeepEqual', () => {
2323
let a = {a:{b:{c:{d:1}}}};
2424
let b = {a:{b:{c:{d:2}}}};
25+
let a$ = Immutable.fromJS(a);
26+
let a1$ = Immutable.fromJS(a);
27+
let a2$ = a$.setIn(['a', 'b', 'c', 'd'], 2222)
28+
2529
assert.strictEqual(wepy.$isDeepEqual(a, b), false);
2630

31+
assert.equal(wepy.$isDeepEqual(a, a$), true, 'compare with a immutable variable that has same values');
32+
33+
assert.equal(wepy.$isDeepEqual(b, a$), false, 'compare with a immutable variable that has different values');
34+
35+
assert.equal(wepy.$isDeepEqual(a$, a1$), true, 'compare two immutable variable that has same values');
36+
37+
assert.equal(wepy.$isDeepEqual(a1$, a2$), false, 'compare two immutable variable that has different values');
2738

2839
let c = {a:{b:[1,2,3,{e:[12,3,{a:'1'}]}]}};
2940
let d = {a:{b:[1,2,3,{e:[12,3,{a:'1'}]}]}};
@@ -60,11 +71,14 @@ describe('index.js', () => {
6071
assert.strictEqual(wepy.$isDeepEqual(function () {return this}, function () {return this}), false, 'compare function');
6172

6273
assert.strictEqual(wepy.$isDeepEqual(new function () {return this}, new function () {return this}), false, 'compare new instance');
63-
6474
});
6575
it('wepy.$isEqual', () => {
6676
let a = {a:{b:[1,2,3,{e:[12,3,{a:'1'}]}]}};
6777
let b = {a:{b:[1,2,3,{e:[12,3,{a:'1'}]}]}};
78+
let a$ = Immutable.fromJS(a);
79+
let a1$ = Immutable.fromJS(a);
80+
let a2$ = a$.setIn(['a', 'b', 0], 2);
81+
6882
assert.equal(wepy.$isEqual(a, b), true, 'wepy.$isEqual({a:{b:[1,2,3,{e:[12,3,{a:\'1\'}]}]}}, {a:{b:[1,2,3,{e:[12,3,{a:\'1\'}]}]}})');
6983

7084

@@ -83,6 +97,12 @@ describe('index.js', () => {
8397

8498
assert.equal(wepy.$isEqual('a', 'a'), true, 'wepy.$isEqual(\'a\', \'a\')');
8599

100+
assert.equal(wepy.$isEqual(a, a$), false, 'compare with a immutable variable that has same values');
101+
102+
assert.equal(wepy.$isEqual(a$, a1$), true, 'compare two immutable variable that has same values');
103+
104+
assert.equal(wepy.$isEqual(a$, a2$), false, 'compare two immutable variable that has different values');
105+
86106
});
87107
it('wepy.$has', () => {
88108
let a = {a: 1};
@@ -175,4 +195,4 @@ describe('index.js', () => {
175195

176196

177197

178-
})
198+
})

packages/wepy/test/util.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
/**
22
* Tencent is pleased to support the open source community by making WePY available.
33
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
4-
*
4+
*
55
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
66
* http://opensource.org/licenses/MIT
77
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
88
*/
9-
10-
9+
var Immutable = require('immutable')
1110
var assert = require('assert');
1211
var util = require('../lib/util.js').default;
1312

@@ -58,9 +57,6 @@ describe('util.js', () => {
5857

5958
});
6059

61-
62-
63-
6460
it('$getParams', () => {
6561

6662
let p;
@@ -80,4 +76,13 @@ describe('util.js', () => {
8076

8177
});
8278

83-
});
79+
it('isImmutable', () => {
80+
let a$ = Immutable.fromJS({ a: 1111 });
81+
let b = 'a string';
82+
83+
assert.strictEqual(util.isImmutable(a$), true, 'is a immutable variable');
84+
85+
assert.strictEqual(util.isImmutable(b), false, 'is not a immutable variable');
86+
})
87+
88+
});

0 commit comments

Comments
 (0)