Skip to content

Commit 009651e

Browse files
pkozlowski-opensourcehansl
authored andcommitted
refactor(core): remove toString() method from DefaultKeyValueDiffer
toString() from DefaultKeyValueDiffer is only used in tests and should not be part of the production code. toString() methods from differs add ~ 0.3KB (min+gzip) to the production bundle size.
1 parent f194f18 commit 009651e

File tree

3 files changed

+58
-56
lines changed

3 files changed

+58
-56
lines changed

packages/core/src/change_detection/differs/default_keyvalue_differ.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -257,26 +257,6 @@ export class DefaultKeyValueDiffer<K, V> implements KeyValueDiffer<K, V>, KeyVal
257257
}
258258
}
259259

260-
toString(): string {
261-
const items: string[] = [];
262-
const previous: string[] = [];
263-
const changes: string[] = [];
264-
const additions: string[] = [];
265-
const removals: string[] = [];
266-
267-
this.forEachItem(r => items.push(stringify(r)));
268-
this.forEachPreviousItem(r => previous.push(stringify(r)));
269-
this.forEachChangedItem(r => changes.push(stringify(r)));
270-
this.forEachAddedItem(r => additions.push(stringify(r)));
271-
this.forEachRemovedItem(r => removals.push(stringify(r)));
272-
273-
return 'map: ' + items.join(', ') + '\n' +
274-
'previous: ' + previous.join(', ') + '\n' +
275-
'additions: ' + additions.join(', ') + '\n' +
276-
'changes: ' + changes.join(', ') + '\n' +
277-
'removals: ' + removals.join(', ') + '\n';
278-
}
279-
280260
/** @internal */
281261
private _forEach<K, V>(obj: Map<K, V>|{[k: string]: V}, fn: (v: V, k: any) => void) {
282262
if (obj instanceof Map) {
@@ -309,11 +289,4 @@ class KeyValueChangeRecord_<K, V> implements KeyValueChangeRecord<K, V> {
309289
_nextChanged: KeyValueChangeRecord_<K, V>|null = null;
310290

311291
constructor(public key: K) {}
312-
313-
toString(): string {
314-
return looseIdentical(this.previousValue, this.currentValue) ?
315-
stringify(this.key) :
316-
(stringify(this.key) + '[' + stringify(this.previousValue) + '->' +
317-
stringify(this.currentValue) + ']');
318-
}
319292
}

packages/core/test/change_detection/differs/default_keyvalue_differ_spec.ts

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
*/
88

99
import {DefaultKeyValueDiffer, DefaultKeyValueDifferFactory} from '@angular/core/src/change_detection/differs/default_keyvalue_differ';
10-
import {kvChangesAsString} from '../../change_detection/util';
10+
11+
import {kvChangesAsString, testChangesAsString} from '../../change_detection/util';
12+
1113

1214
// todo(vicb): Update the code & tests for object equality
1315
export function main() {
@@ -28,13 +30,13 @@ export function main() {
2830

2931
m.set('a', 1);
3032
differ.check(m);
31-
expect(differ.toString())
32-
.toEqual(kvChangesAsString({map: ['a[null->1]'], additions: ['a[null->1]']}));
33+
expect(kvChangesAsString(differ))
34+
.toEqual(testChangesAsString({map: ['a[null->1]'], additions: ['a[null->1]']}));
3335

3436
m.set('b', 2);
3537
differ.check(m);
36-
expect(differ.toString())
37-
.toEqual(kvChangesAsString(
38+
expect(kvChangesAsString(differ))
39+
.toEqual(testChangesAsString(
3840
{map: ['a', 'b[null->2]'], previous: ['a'], additions: ['b[null->2]']}));
3941
});
4042

@@ -46,7 +48,7 @@ export function main() {
4648
m.set(2, 10);
4749
m.set(1, 20);
4850
differ.check(m);
49-
expect(differ.toString()).toEqual(kvChangesAsString({
51+
expect(kvChangesAsString(differ)).toEqual(testChangesAsString({
5052
map: ['1[10->20]', '2[20->10]'],
5153
previous: ['1[10->20]', '2[20->10]'],
5254
changes: ['1[10->20]', '2[20->10]']
@@ -72,19 +74,19 @@ export function main() {
7274

7375
m.set('a', 'A');
7476
differ.check(m);
75-
expect(differ.toString())
76-
.toEqual(kvChangesAsString({map: ['a[null->A]'], additions: ['a[null->A]']}));
77+
expect(kvChangesAsString(differ))
78+
.toEqual(testChangesAsString({map: ['a[null->A]'], additions: ['a[null->A]']}));
7779

7880
m.set('b', 'B');
7981
differ.check(m);
80-
expect(differ.toString())
81-
.toEqual(kvChangesAsString(
82+
expect(kvChangesAsString(differ))
83+
.toEqual(testChangesAsString(
8284
{map: ['a', 'b[null->B]'], previous: ['a'], additions: ['b[null->B]']}));
8385

8486
m.set('b', 'BB');
8587
m.set('d', 'D');
8688
differ.check(m);
87-
expect(differ.toString()).toEqual(kvChangesAsString({
89+
expect(kvChangesAsString(differ)).toEqual(testChangesAsString({
8890
map: ['a', 'b[B->BB]', 'd[null->D]'],
8991
previous: ['a', 'b[B->BB]'],
9092
additions: ['d[null->D]'],
@@ -93,13 +95,13 @@ export function main() {
9395

9496
m.delete('b');
9597
differ.check(m);
96-
expect(differ.toString())
97-
.toEqual(kvChangesAsString(
98+
expect(kvChangesAsString(differ))
99+
.toEqual(testChangesAsString(
98100
{map: ['a', 'd'], previous: ['a', 'b[BB->null]', 'd'], removals: ['b[BB->null]']}));
99101

100102
m.clear();
101103
differ.check(m);
102-
expect(differ.toString()).toEqual(kvChangesAsString({
104+
expect(kvChangesAsString(differ)).toEqual(testChangesAsString({
103105
previous: ['a[A->null]', 'd[D->null]'],
104106
removals: ['a[A->null]', 'd[D->null]']
105107
}));
@@ -110,7 +112,8 @@ export function main() {
110112
differ.check(m);
111113

112114
differ.check(m);
113-
expect(differ.toString()).toEqual(kvChangesAsString({map: ['foo'], previous: ['foo']}));
115+
expect(kvChangesAsString(differ))
116+
.toEqual(testChangesAsString({map: ['foo'], previous: ['foo']}));
114117
});
115118

116119
it('should work regardless key order', () => {
@@ -123,7 +126,7 @@ export function main() {
123126
m.set('a', 1);
124127
differ.check(m);
125128

126-
expect(differ.toString()).toEqual(kvChangesAsString({
129+
expect(kvChangesAsString(differ)).toEqual(testChangesAsString({
127130
map: ['b[0->1]', 'a[0->1]'],
128131
previous: ['a[0->1]', 'b[0->1]'],
129132
changes: ['b[0->1]', 'a[0->1]']
@@ -145,19 +148,19 @@ export function main() {
145148

146149
m['a'] = 'A';
147150
differ.check(m);
148-
expect(differ.toString())
149-
.toEqual(kvChangesAsString({map: ['a[null->A]'], additions: ['a[null->A]']}));
151+
expect(kvChangesAsString(differ))
152+
.toEqual(testChangesAsString({map: ['a[null->A]'], additions: ['a[null->A]']}));
150153

151154
m['b'] = 'B';
152155
differ.check(m);
153-
expect(differ.toString())
154-
.toEqual(kvChangesAsString(
156+
expect(kvChangesAsString(differ))
157+
.toEqual(testChangesAsString(
155158
{map: ['a', 'b[null->B]'], previous: ['a'], additions: ['b[null->B]']}));
156159

157160
m['b'] = 'BB';
158161
m['d'] = 'D';
159162
differ.check(m);
160-
expect(differ.toString()).toEqual(kvChangesAsString({
163+
expect(kvChangesAsString(differ)).toEqual(testChangesAsString({
161164
map: ['a', 'b[B->BB]', 'd[null->D]'],
162165
previous: ['a', 'b[B->BB]'],
163166
additions: ['d[null->D]'],
@@ -168,15 +171,15 @@ export function main() {
168171
m['a'] = 'A';
169172
m['d'] = 'D';
170173
differ.check(m);
171-
expect(differ.toString()).toEqual(kvChangesAsString({
174+
expect(kvChangesAsString(differ)).toEqual(testChangesAsString({
172175
map: ['a', 'd'],
173176
previous: ['a', 'b[BB->null]', 'd'],
174177
removals: ['b[BB->null]']
175178
}));
176179

177180
m = {};
178181
differ.check(m);
179-
expect(differ.toString()).toEqual(kvChangesAsString({
182+
expect(kvChangesAsString(differ)).toEqual(testChangesAsString({
180183
previous: ['a[A->null]', 'd[D->null]'],
181184
removals: ['a[A->null]', 'd[D->null]']
182185
}));
@@ -187,7 +190,7 @@ export function main() {
187190
differ.check({a: 0, b: 0});
188191
differ.check({b: 1, a: 1});
189192

190-
expect(differ.toString()).toEqual(kvChangesAsString({
193+
expect(kvChangesAsString(differ)).toEqual(testChangesAsString({
191194
map: ['b[0->1]', 'a[0->1]'],
192195
previous: ['a[0->1]', 'b[0->1]'],
193196
changes: ['b[0->1]', 'a[0->1]']
@@ -200,7 +203,7 @@ export function main() {
200203
differ.check({b: 3, a: 2});
201204
differ.check({a: 1, b: 2});
202205

203-
expect(differ.toString()).toEqual(kvChangesAsString({
206+
expect(kvChangesAsString(differ)).toEqual(testChangesAsString({
204207
map: ['a[2->1]', 'b[3->2]'],
205208
previous: ['b[3->2]', 'a[2->1]'],
206209
changes: ['a[2->1]', 'b[3->2]']
@@ -211,7 +214,7 @@ export function main() {
211214
differ.check({a: 'a', b: 'b'});
212215
differ.check({c: 'c', a: 'a'});
213216

214-
expect(differ.toString()).toEqual(kvChangesAsString({
217+
expect(kvChangesAsString(differ)).toEqual(testChangesAsString({
215218
map: ['c[null->c]', 'a'],
216219
previous: ['a', 'b[b->null]'],
217220
additions: ['c[null->c]'],
@@ -236,8 +239,8 @@ export function main() {
236239
it('should treat null as an empty list', () => {
237240
m.set('a', 'A');
238241
differ.diff(m);
239-
expect(differ.diff(null).toString())
240-
.toEqual(kvChangesAsString({previous: ['a[A->null]'], removals: ['a[A->null]']}));
242+
expect(kvChangesAsString(differ.diff(null)))
243+
.toEqual(testChangesAsString({previous: ['a[A->null]'], removals: ['a[A->null]']}));
241244
});
242245

243246
it('should throw when given an invalid collection', () => {

packages/core/test/change_detection/util.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {KeyValueChangeRecord, KeyValueChanges} from '@angular/core/src/change_detection/differs/keyvalue_differs';
10+
11+
import {looseIdentical, stringify} from '../../src/util';
912

1013

1114
export function iterableChangesAsString(
@@ -19,7 +22,30 @@ export function iterableChangesAsString(
1922
'identityChanges: ' + identityChanges.join(', ') + '\n';
2023
}
2124

22-
export function kvChangesAsString(
25+
function kvcrAsString(kvcr: KeyValueChangeRecord<string, any>) {
26+
return looseIdentical(kvcr.previousValue, kvcr.currentValue) ?
27+
stringify(kvcr.key) :
28+
(stringify(kvcr.key) + '[' + stringify(kvcr.previousValue) + '->' +
29+
stringify(kvcr.currentValue) + ']');
30+
}
31+
32+
export function kvChangesAsString(kvChanges: KeyValueChanges<string, any>) {
33+
const map: string[] = [];
34+
const previous: string[] = [];
35+
const changes: string[] = [];
36+
const additions: string[] = [];
37+
const removals: string[] = [];
38+
39+
kvChanges.forEachItem(r => map.push(kvcrAsString(r)));
40+
kvChanges.forEachPreviousItem(r => previous.push(kvcrAsString(r)));
41+
kvChanges.forEachChangedItem(r => changes.push(kvcrAsString(r)));
42+
kvChanges.forEachAddedItem(r => additions.push(kvcrAsString(r)));
43+
kvChanges.forEachRemovedItem(r => removals.push(kvcrAsString(r)));
44+
45+
return testChangesAsString({map, previous, additions, changes, removals});
46+
}
47+
48+
export function testChangesAsString(
2349
{map, previous, additions, changes, removals}:
2450
{map?: any[], previous?: any[], additions?: any[], changes?: any[], removals?: any[]}):
2551
string {

0 commit comments

Comments
 (0)