Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ivy): remove duplicated flatten util #29547

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 2 additions & 8 deletions packages/core/src/linker/query_list.ts
Expand Up @@ -9,6 +9,7 @@
import {Observable} from 'rxjs';

import {EventEmitter} from '../event_emitter';
import {flatten} from '../util/array_utils';
import {getSymbolIterator} from '../util/symbol';


Expand Down Expand Up @@ -110,7 +111,7 @@ export class QueryList<T>/* implements Iterable<T> */ {
* @param resultsTree The results tree to store
*/
reset(resultsTree: Array<T|any[]>): void {
this._results = depthFirstFlatten(resultsTree);
this._results = flatten(resultsTree);
(this as{dirty: boolean}).dirty = false;
(this as{length: number}).length = this._results.length;
(this as{last: T}).last = this._results[this.length - 1];
Expand All @@ -131,10 +132,3 @@ export class QueryList<T>/* implements Iterable<T> */ {
(this.changes as EventEmitter<any>).unsubscribe();
}
}

function depthFirstFlatten<T>(list: Array<T|T[]>): T[] {
return list.reduce((flat: any[], item: T | T[]): T[] => {
const flatItem = Array.isArray(item) ? depthFirstFlatten(item) : item;
return (<T[]>flat).concat(flatItem);
}, []);
}
3 changes: 2 additions & 1 deletion packages/core/src/render3/i18n.ts
Expand Up @@ -9,7 +9,9 @@
import {SRCSET_ATTRS, URI_ATTRS, VALID_ATTRS, VALID_ELEMENTS, getTemplateContent} from '../sanitization/html_sanitizer';
import {InertBodyHelper} from '../sanitization/inert_body';
import {_sanitizeUrl, sanitizeSrcset} from '../sanitization/url_sanitizer';
import {addAllToArray} from '../util/array_utils';
import {assertDefined, assertEqual, assertGreaterThan} from '../util/assert';

import {attachPatchData} from './context_discovery';
import {allocExpando, createNodeAtIndex, elementAttribute, load, textBinding} from './instructions/all';
import {LContainer, NATIVE} from './interfaces/container';
Expand All @@ -22,7 +24,6 @@ import {BINDING_INDEX, HEADER_OFFSET, LView, RENDERER, TVIEW, TView, T_HOST} fro
import {appendChild, createTextNode, nativeRemoveNode} from './node_manipulation';
import {getIsParent, getLView, getPreviousOrParentTNode, setIsParent, setPreviousOrParentTNode} from './state';
import {NO_CHANGE} from './tokens';
import {addAllToArray} from './util/array_utils';
import {renderStringify} from './util/misc_utils';
import {getNativeByIndex, getNativeByTNode, getTNode, isLContainer} from './util/view_utils';

Expand Down
17 changes: 0 additions & 17 deletions packages/core/test/render3/util_spec.ts
Expand Up @@ -7,8 +7,6 @@
*/

import {devModeEqual} from '@angular/core/src/change_detection/change_detection_util';

import {flatten} from '../../src/render3/util/array_utils';
import {isDifferent} from '../../src/render3/util/misc_utils';

describe('util', () => {
Expand Down Expand Up @@ -67,19 +65,4 @@ describe('util', () => {

});

describe('flatten', () => {

it('should flatten an empty array', () => { expect(flatten([])).toEqual([]); });

it('should flatten a flat array', () => { expect(flatten([1, 2, 3])).toEqual([1, 2, 3]); });

it('should flatten a nested array', () => {
expect(flatten([1, [2], 3])).toEqual([1, 2, 3]);
expect(flatten([[1], 2, [3]])).toEqual([1, 2, 3]);
expect(flatten([1, [2, [3]], 4])).toEqual([1, 2, 3, 4]);
expect(flatten([1, [2, [3]], [4]])).toEqual([1, 2, 3, 4]);
expect(flatten([1, [2, [3]], [[[4]]]])).toEqual([1, 2, 3, 4]);
expect(flatten([1, [], 2])).toEqual([1, 2]);
});
});
});
25 changes: 25 additions & 0 deletions packages/core/test/util/array_utils_spec.ts
@@ -0,0 +1,25 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {flatten} from '../../src/util/array_utils';

describe('flatten', () => {

it('should flatten an empty array', () => { expect(flatten([])).toEqual([]); });

it('should flatten a flat array', () => { expect(flatten([1, 2, 3])).toEqual([1, 2, 3]); });

it('should flatten a nested array depth-first', () => {
expect(flatten([1, [2], 3])).toEqual([1, 2, 3]);
expect(flatten([[1], 2, [3]])).toEqual([1, 2, 3]);
expect(flatten([1, [2, [3]], 4])).toEqual([1, 2, 3, 4]);
expect(flatten([1, [2, [3]], [4]])).toEqual([1, 2, 3, 4]);
expect(flatten([1, [2, [3]], [[[4]]]])).toEqual([1, 2, 3, 4]);
expect(flatten([1, [], 2])).toEqual([1, 2]);
});
});