From a8c34ae290c3e7327d7287434f4d113617a29420 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Thu, 8 Oct 2015 16:01:18 -0700 Subject: [PATCH] refactor(MapWrapper): drop delete(), get(), forEach() and size Closes #4618 --- .../change_detection/codegen_name_util.ts | 4 +-- .../differs/default_iterable_differ.ts | 4 +-- .../differs/default_keyvalue_differ.ts | 6 ++-- .../core/debug/debug_element_view_listener.ts | 4 +-- modules/angular2/src/core/di/key.ts | 3 +- .../angular2/src/core/facade/collection.dart | 9 ------ .../angular2/src/core/facade/collection.ts | 4 --- modules/angular2/src/core/linker/view.ts | 7 ++--- .../src/core/linker/view_manager_utils.ts | 2 +- .../angular2/src/http/backends/xhr_backend.ts | 2 +- modules/angular2/src/http/headers.ts | 28 +++++++++---------- .../angular2/src/http/url_search_params.ts | 11 ++++---- .../angular2/src/mock/view_resolver_mock.ts | 2 +- modules/angular2/src/router/router.ts | 5 ++-- .../src/test_lib/test_component_builder.ts | 14 ++++------ modules/angular2/src/test_lib/utils.ts | 2 +- .../render_view_with_fragments_store.ts | 6 ++-- .../src/web_workers/shared/serializer.ts | 2 +- .../change_detector_config.ts | 2 +- .../differs/default_keyvalue_differ_spec.ts | 3 +- .../render/dom/events/event_manager_spec.ts | 2 +- .../src/naive_infinite_scroll/common.ts | 2 +- 22 files changed, 51 insertions(+), 73 deletions(-) diff --git a/modules/angular2/src/core/change_detection/codegen_name_util.ts b/modules/angular2/src/core/change_detection/codegen_name_util.ts index a797e973a3..00d4fb5698 100644 --- a/modules/angular2/src/core/change_detection/codegen_name_util.ts +++ b/modules/angular2/src/core/change_detection/codegen_name_util.ts @@ -83,7 +83,7 @@ export class CodegenNameUtil { getLocalName(idx: number): string { return `l_${this._sanitizedNames[idx]}`; } getEventLocalName(eb: EventBinding, idx: number): string { - return `l_${MapWrapper.get(this._sanitizedEventNames, eb)[idx]}`; + return `l_${this._sanitizedEventNames.get(eb)[idx]}`; } getChangeName(idx: number): string { return `c_${this._sanitizedNames[idx]}`; } @@ -118,7 +118,7 @@ export class CodegenNameUtil { */ genInitEventLocals(): string { var res = [`${this.getLocalName(CONTEXT_INDEX)} = ${this.getFieldName(CONTEXT_INDEX)}`]; - MapWrapper.forEach(this._sanitizedEventNames, (names, eb) => { + this._sanitizedEventNames.forEach((names, eb) => { for (var i = 0; i < names.length; ++i) { if (i !== CONTEXT_INDEX) { res.push(`${this.getEventLocalName(eb, i)}`); diff --git a/modules/angular2/src/core/change_detection/differs/default_iterable_differ.ts b/modules/angular2/src/core/change_detection/differs/default_iterable_differ.ts index 51fbfb5aed..ead34c94a6 100644 --- a/modules/angular2/src/core/change_detection/differs/default_iterable_differ.ts +++ b/modules/angular2/src/core/change_detection/differs/default_iterable_differ.ts @@ -608,12 +608,12 @@ class _DuplicateMap { var recordList: _DuplicateItemRecordList = this.map.get(key); // Remove the list of duplicates when it gets empty if (recordList.remove(record)) { - MapWrapper.delete(this.map, key); + this.map.delete(key); } return record; } - get isEmpty(): boolean { return MapWrapper.size(this.map) === 0; } + get isEmpty(): boolean { return this.map.size === 0; } clear() { this.map.clear(); } diff --git a/modules/angular2/src/core/change_detection/differs/default_keyvalue_differ.ts b/modules/angular2/src/core/change_detection/differs/default_keyvalue_differ.ts index 77d85c988a..0c0756ec83 100644 --- a/modules/angular2/src/core/change_detection/differs/default_keyvalue_differ.ts +++ b/modules/angular2/src/core/change_detection/differs/default_keyvalue_differ.ts @@ -1,4 +1,4 @@ -import {ListWrapper, MapWrapper, StringMapWrapper} from 'angular2/src/core/facade/collection'; +import {MapWrapper, StringMapWrapper} from 'angular2/src/core/facade/collection'; import {stringify, looseIdentical, isJsObject, CONST, isBlank} from 'angular2/src/core/facade/lang'; import {BaseException} from 'angular2/src/core/facade/exceptions'; import {ChangeDetectorRef} from '../change_detector_ref'; @@ -197,7 +197,7 @@ export class DefaultKeyValueDiffer implements KeyValueDiffer { for (var rec: KVChangeRecord = this._removalsHead; rec !== null; rec = rec._nextRemoved) { rec.previousValue = rec.currentValue; rec.currentValue = null; - MapWrapper.delete(this._records, rec.key); + this._records.delete(rec.key); } } @@ -317,7 +317,7 @@ export class DefaultKeyValueDiffer implements KeyValueDiffer { _forEach(obj, fn: Function) { if (obj instanceof Map) { - MapWrapper.forEach(obj, fn); + (>obj).forEach(fn); } else { StringMapWrapper.forEach(obj, fn); } diff --git a/modules/angular2/src/core/debug/debug_element_view_listener.ts b/modules/angular2/src/core/debug/debug_element_view_listener.ts index 2d394e9136..13220876de 100644 --- a/modules/angular2/src/core/debug/debug_element_view_listener.ts +++ b/modules/angular2/src/core/debug/debug_element_view_listener.ts @@ -62,8 +62,8 @@ export class DebugElementViewListener implements AppViewListener { viewDestroyed(view: AppView) { var viewId = _allIdsByView.get(view); - MapWrapper.delete(_allIdsByView, view); - MapWrapper.delete(_allViewsById, viewId); + _allIdsByView.delete(view); + _allViewsById.delete(viewId); } } diff --git a/modules/angular2/src/core/di/key.ts b/modules/angular2/src/core/di/key.ts index 6c46876276..0f99d12125 100644 --- a/modules/angular2/src/core/di/key.ts +++ b/modules/angular2/src/core/di/key.ts @@ -1,4 +1,3 @@ -import {MapWrapper} from 'angular2/src/core/facade/collection'; import {stringify, CONST, Type, isBlank} from 'angular2/src/core/facade/lang'; import {BaseException, WrappedException} from 'angular2/src/core/facade/exceptions'; import {TypeLiteral} from './type_literal'; @@ -70,7 +69,7 @@ export class KeyRegistry { return newKey; } - get numberOfKeys(): number { return MapWrapper.size(this._allKeys); } + get numberOfKeys(): number { return this._allKeys.size; } } var _globalKeyRegistry = new KeyRegistry(); diff --git a/modules/angular2/src/core/facade/collection.dart b/modules/angular2/src/core/facade/collection.dart index a097345b6b..359356aa86 100644 --- a/modules/angular2/src/core/facade/collection.dart +++ b/modules/angular2/src/core/facade/collection.dart @@ -45,15 +45,6 @@ class MapWrapper { m[p[0]] = p[1]; return m; }); - static forEach(Map m, fn(v, k)) { - m.forEach((k, v) => fn(v, k)); - } - - static get(Map map, key) => map[key]; - static int size(Map m) => m.length; - static void delete(Map m, k) { - m.remove(k); - } static void clearValues(Map m) { for (var k in m.keys) { diff --git a/modules/angular2/src/core/facade/collection.ts b/modules/angular2/src/core/facade/collection.ts index 8d6d527c34..d14731797a 100644 --- a/modules/angular2/src/core/facade/collection.ts +++ b/modules/angular2/src/core/facade/collection.ts @@ -91,10 +91,6 @@ export class MapWrapper { return r; } static createFromPairs(pairs: any[]): Map { return createMapFromPairs(pairs); } - static forEach(m: Map, fn: /*(V, K) => void*/ Function) { m.forEach(fn); } - static get(map: Map, key: K): V { return map.get(key); } - static size(m: Map): number { return m.size; } - static delete(m: Map, k: K) { m.delete(k); } static clearValues(m: Map) { _clearValues(m); } static iterable(m: T): T { return m; } static keys(m: Map): K[] { return _arrayFromMap(m, false); } diff --git a/modules/angular2/src/core/linker/view.ts b/modules/angular2/src/core/linker/view.ts index eff1f70382..7f9d779309 100644 --- a/modules/angular2/src/core/linker/view.ts +++ b/modules/angular2/src/core/linker/view.ts @@ -336,8 +336,8 @@ export class AppProtoView { this.variableLocations = variableLocations; this.protoLocals = new Map(); if (isPresent(this.templateVariableBindings)) { - MapWrapper.forEach(this.templateVariableBindings, - (templateName, _) => { this.protoLocals.set(templateName, null); }); + this.templateVariableBindings.forEach( + (templateName, _) => { this.protoLocals.set(templateName, null); }); } if (isPresent(variableLocations)) { // The view's locals needs to have a full set of variable names at construction time @@ -345,8 +345,7 @@ export class AppProtoView { // want // to actually create variable bindings for the $implicit bindings, add to the // protoLocals manually. - MapWrapper.forEach(variableLocations, - (_, templateName) => { this.protoLocals.set(templateName, null); }); + variableLocations.forEach((_, templateName) => { this.protoLocals.set(templateName, null); }); } } diff --git a/modules/angular2/src/core/linker/view_manager_utils.ts b/modules/angular2/src/core/linker/view_manager_utils.ts index 0742d79a4b..68cf8b8420 100644 --- a/modules/angular2/src/core/linker/view_manager_utils.ts +++ b/modules/angular2/src/core/linker/view_manager_utils.ts @@ -216,7 +216,7 @@ export class AppViewManagerUtils { _populateViewLocals(view: viewModule.AppView, elementInjector: eli.ElementInjector, boundElementIdx: number): void { if (isPresent(elementInjector.getDirectiveVariableBindings())) { - MapWrapper.forEach(elementInjector.getDirectiveVariableBindings(), (directiveIndex, name) => { + elementInjector.getDirectiveVariableBindings().forEach((directiveIndex, name) => { if (isBlank(directiveIndex)) { view.locals.set(name, view.elementRefs[boundElementIdx].nativeElement); } else { diff --git a/modules/angular2/src/http/backends/xhr_backend.ts b/modules/angular2/src/http/backends/xhr_backend.ts index 3cd05e3d2e..9ec606fca9 100644 --- a/modules/angular2/src/http/backends/xhr_backend.ts +++ b/modules/angular2/src/http/backends/xhr_backend.ts @@ -64,7 +64,7 @@ export class XHRConnection implements Connection { }; if (isPresent(req.headers)) { - req.headers.forEach((values, name) => { _xhr.setRequestHeader(name, values.join(',')); }); + req.headers.forEach((values, name) => _xhr.setRequestHeader(name, values.join(','))); } _xhr.addEventListener('load', onLoad); diff --git a/modules/angular2/src/http/headers.ts b/modules/angular2/src/http/headers.ts index 37ffc71881..42b0a06543 100644 --- a/modules/angular2/src/http/headers.ts +++ b/modules/angular2/src/http/headers.ts @@ -4,6 +4,7 @@ import { isListLikeIterable, Map, MapWrapper, + StringMapWrapper, ListWrapper, } from 'angular2/src/core/facade/collection'; @@ -36,23 +37,20 @@ import { export class Headers { _headersMap: Map; constructor(headers?: Headers | {[key: string]: any}) { - if (isBlank(headers)) { - this._headersMap = new Map(); + if (headers instanceof Headers) { + this._headersMap = (headers)._headersMap; return; } - if (headers instanceof Headers) { - this._headersMap = (headers)._headersMap; - } else /*if (headers instanceof StringMap)*/ { - this._headersMap = MapWrapper.createFromStringMap(<{[key: string]: any}>headers); - MapWrapper.forEach(this._headersMap, (v, k) => { - if (!isListLikeIterable(v)) { - var list = []; - list.push(v); - this._headersMap.set(k, list); - } - }); + this._headersMap = new Map(); + + if (isBlank(headers)) { + return; } + + // headers instanceof StringMap + StringMapWrapper.forEach( + headers, (v, k) => { this._headersMap.set(k, isListLikeIterable(v) ? v : [v]); }); } /** @@ -68,10 +66,10 @@ export class Headers { /** * Deletes all header values for the given name. */ - delete (name: string): void { MapWrapper.delete(this._headersMap, name); } + delete (name: string): void { this._headersMap.delete(name); } forEach(fn: (values: string[], name: string, headers: Map) => void): void { - MapWrapper.forEach(this._headersMap, fn); + this._headersMap.forEach(fn); } /** diff --git a/modules/angular2/src/http/url_search_params.ts b/modules/angular2/src/http/url_search_params.ts index 6fe45116f0..96c23ed461 100644 --- a/modules/angular2/src/http/url_search_params.ts +++ b/modules/angular2/src/http/url_search_params.ts @@ -71,7 +71,7 @@ export class URLSearchParams { // // TODO(@caitp): document this better setAll(searchParams: URLSearchParams) { - MapWrapper.forEach(searchParams.paramsMap, (value, param) => { + searchParams.paramsMap.forEach((value, param) => { var mapParam = this.paramsMap.get(param); var list = isPresent(mapParam) ? mapParam : []; ListWrapper.clear(list); @@ -95,7 +95,7 @@ export class URLSearchParams { // // TODO(@caitp): document this better appendAll(searchParams: URLSearchParams) { - MapWrapper.forEach(searchParams.paramsMap, (value, param) => { + searchParams.paramsMap.forEach((value, param) => { var mapParam = this.paramsMap.get(param); var list = isPresent(mapParam) ? mapParam : []; for (var i = 0; i < value.length; ++i) { @@ -114,7 +114,7 @@ export class URLSearchParams { // // TODO(@caitp): document this better replaceAll(searchParams: URLSearchParams) { - MapWrapper.forEach(searchParams.paramsMap, (value, param) => { + searchParams.paramsMap.forEach((value, param) => { var mapParam = this.paramsMap.get(param); var list = isPresent(mapParam) ? mapParam : []; ListWrapper.clear(list); @@ -127,10 +127,9 @@ export class URLSearchParams { toString(): string { var paramsList = []; - MapWrapper.forEach(this.paramsMap, - (values, k) => { values.forEach(v => paramsList.push(k + '=' + v)); }); + this.paramsMap.forEach((values, k) => { values.forEach(v => paramsList.push(k + '=' + v)); }); return paramsList.join('&'); } - delete (param: string): void { MapWrapper.delete(this.paramsMap, param); } + delete (param: string): void { this.paramsMap.delete(param); } } diff --git a/modules/angular2/src/mock/view_resolver_mock.ts b/modules/angular2/src/mock/view_resolver_mock.ts index 03d59de5a9..3356eb102c 100644 --- a/modules/angular2/src/mock/view_resolver_mock.ts +++ b/modules/angular2/src/mock/view_resolver_mock.ts @@ -80,7 +80,7 @@ export class MockViewResolver extends ViewResolver { if (isPresent(overrides) && isPresent(directives)) { directives = ListWrapper.clone(view.directives); - MapWrapper.forEach(overrides, (to, from) => { + overrides.forEach((to, from) => { var srcIndex = directives.indexOf(from); if (srcIndex == -1) { throw new BaseException( diff --git a/modules/angular2/src/router/router.ts b/modules/angular2/src/router/router.ts index f6d66096ea..79f2e07e2e 100644 --- a/modules/angular2/src/router/router.ts +++ b/modules/angular2/src/router/router.ts @@ -328,9 +328,8 @@ export class Router { } var promises = []; - MapWrapper.forEach(this._auxRouters, (router, name) => { - promises.push(router.commit(instruction.auxInstruction[name])); - }); + this._auxRouters.forEach( + (router, name) => { promises.push(router.commit(instruction.auxInstruction[name])); }); return next.then((_) => PromiseWrapper.all(promises)); } diff --git a/modules/angular2/src/test_lib/test_component_builder.ts b/modules/angular2/src/test_lib/test_component_builder.ts index a67b91a1e4..01f6c2c908 100644 --- a/modules/angular2/src/test_lib/test_component_builder.ts +++ b/modules/angular2/src/test_lib/test_component_builder.ts @@ -169,14 +169,12 @@ export class TestComponentBuilder { createAsync(rootComponentType: Type): Promise { var mockDirectiveResolver = this._injector.get(DirectiveResolver); var mockViewResolver = this._injector.get(ViewResolver); - MapWrapper.forEach(this._viewOverrides, - (view, type) => { mockViewResolver.setView(type, view); }); - MapWrapper.forEach(this._templateOverrides, - (template, type) => { mockViewResolver.setInlineTemplate(type, template); }); - MapWrapper.forEach(this._directiveOverrides, (overrides, component) => { - MapWrapper.forEach(overrides, (to, from) => { - mockViewResolver.overrideViewDirective(component, from, to); - }); + this._viewOverrides.forEach((view, type) => mockViewResolver.setView(type, view)); + this._templateOverrides.forEach((template, type) => + mockViewResolver.setInlineTemplate(type, template)); + this._directiveOverrides.forEach((overrides, component) => { + overrides.forEach( + (to, from) => { mockViewResolver.overrideViewDirective(component, from, to); }); }); this._bindingsOverrides.forEach((bindings, type) => diff --git a/modules/angular2/src/test_lib/utils.ts b/modules/angular2/src/test_lib/utils.ts index 78907b5537..328fe75e7d 100644 --- a/modules/angular2/src/test_lib/utils.ts +++ b/modules/angular2/src/test_lib/utils.ts @@ -105,7 +105,7 @@ export function stringifyElement(el): string { // Attributes in an ordered way var attributeMap = DOM.attributeMap(el); var keys = []; - MapWrapper.forEach(attributeMap, (v, k) => { keys.push(k); }); + attributeMap.forEach((v, k) => keys.push(k)); ListWrapper.sort(keys); for (let i = 0; i < keys.length; i++) { var key = keys[i]; diff --git a/modules/angular2/src/web_workers/shared/render_view_with_fragments_store.ts b/modules/angular2/src/web_workers/shared/render_view_with_fragments_store.ts index 092272b7e4..5acf0ece69 100644 --- a/modules/angular2/src/web_workers/shared/render_view_with_fragments_store.ts +++ b/modules/angular2/src/web_workers/shared/render_view_with_fragments_store.ts @@ -54,13 +54,13 @@ export class RenderViewWithFragmentsStore { this._removeRef(view); var fragments = this._viewFragments.get(view); fragments.forEach((fragment) => { this._removeRef(fragment); }); - MapWrapper.delete(this._viewFragments, view); + this._viewFragments.delete(view); } private _removeRef(ref: RenderViewRef | RenderFragmentRef) { var index = this._lookupByView.get(ref); - MapWrapper.delete(this._lookupByView, ref); - MapWrapper.delete(this._lookupByIndex, index); + this._lookupByView.delete(ref); + this._lookupByIndex.delete(index); } serializeRenderViewRef(viewRef: RenderViewRef): number { diff --git a/modules/angular2/src/web_workers/shared/serializer.ts b/modules/angular2/src/web_workers/shared/serializer.ts index c9118de6bb..ce6f190897 100644 --- a/modules/angular2/src/web_workers/shared/serializer.ts +++ b/modules/angular2/src/web_workers/shared/serializer.ts @@ -104,7 +104,7 @@ export class Serializer { var object = {}; var serialize = isPresent(type); - MapWrapper.forEach(map, (value, key) => { + map.forEach((value, key) => { if (serialize) { object[key] = this.serialize(value, type); } else { diff --git a/modules/angular2/test/core/change_detection/change_detector_config.ts b/modules/angular2/test/core/change_detection/change_detector_config.ts index ee36691657..a69c69dd42 100644 --- a/modules/angular2/test/core/change_detection/change_detector_config.ts +++ b/modules/angular2/test/core/change_detection/change_detector_config.ts @@ -53,7 +53,7 @@ function _convertLocalsToVariableBindings(locals: Locals): any[] { var variableBindings = []; var loc = locals; while (isPresent(loc) && isPresent(loc.current)) { - MapWrapper.forEach(loc.current, (v, k) => variableBindings.push(k)); + loc.current.forEach((v, k) => variableBindings.push(k)); loc = loc.parent; } return variableBindings; diff --git a/modules/angular2/test/core/change_detection/differs/default_keyvalue_differ_spec.ts b/modules/angular2/test/core/change_detection/differs/default_keyvalue_differ_spec.ts index eb9a74bc05..59158c9b41 100644 --- a/modules/angular2/test/core/change_detection/differs/default_keyvalue_differ_spec.ts +++ b/modules/angular2/test/core/change_detection/differs/default_keyvalue_differ_spec.ts @@ -4,7 +4,6 @@ import { DefaultKeyValueDifferFactory } from 'angular2/src/core/change_detection/differs/default_keyvalue_differ'; import {NumberWrapper, isJsObject} from 'angular2/src/core/facade/lang'; -import {MapWrapper} from 'angular2/src/core/facade/collection'; import {kvChangesAsString} from '../../../core/change_detection/util'; // todo(vicb): Update the code & tests for object equality @@ -95,7 +94,7 @@ export function main() { changes: ['b[B->BB]'] })); - MapWrapper.delete(m, 'b'); + m.delete('b'); differ.check(m); expect(differ.toString()) .toEqual(kvChangesAsString( diff --git a/modules/angular2/test/core/render/dom/events/event_manager_spec.ts b/modules/angular2/test/core/render/dom/events/event_manager_spec.ts index 59853be3c3..f4e685fdbd 100644 --- a/modules/angular2/test/core/render/dom/events/event_manager_spec.ts +++ b/modules/angular2/test/core/render/dom/events/event_manager_spec.ts @@ -102,7 +102,7 @@ class FakeEventManagerPlugin extends EventManagerPlugin { addEventListener(element, eventName: string, handler: Function) { this._eventHandler.set(eventName, handler); - return () => { MapWrapper.delete(this._eventHandler, eventName); }; + return () => { this._eventHandler.delete(eventName); }; } } diff --git a/modules/benchmarks/src/naive_infinite_scroll/common.ts b/modules/benchmarks/src/naive_infinite_scroll/common.ts index 80da8c3124..6ad86841c6 100644 --- a/modules/benchmarks/src/naive_infinite_scroll/common.ts +++ b/modules/benchmarks/src/naive_infinite_scroll/common.ts @@ -87,7 +87,7 @@ export class RawEntity { remove(key: string) { if (!StringWrapper.contains(key, '.')) { - return MapWrapper.delete(this._data, key); + return this._data.delete(key); } var pieces = key.split('.'); var last = ListWrapper.last(pieces);