refactor(MapWrapper): drop delete(), get(), forEach() and size

Closes #4618
This commit is contained in:
Victor Berchet 2015-10-08 16:01:18 -07:00
parent aab0c57aee
commit a8c34ae290
22 changed files with 51 additions and 73 deletions

View File

@ -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)}`);

View File

@ -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(); }

View File

@ -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);
(<Map<any, any>>obj).forEach(<any>fn);
} else {
StringMapWrapper.forEach(obj, fn);
}

View File

@ -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);
}
}

View File

@ -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();

View File

@ -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) {

View File

@ -91,10 +91,6 @@ export class MapWrapper {
return r;
}
static createFromPairs(pairs: any[]): Map<any, any> { return createMapFromPairs(pairs); }
static forEach<K, V>(m: Map<K, V>, fn: /*(V, K) => void*/ Function) { m.forEach(<any>fn); }
static get<K, V>(map: Map<K, V>, key: K): V { return map.get(key); }
static size(m: Map<any, any>): number { return m.size; }
static delete<K>(m: Map<K, any>, k: K) { m.delete(k); }
static clearValues(m: Map<any, any>) { _clearValues(m); }
static iterable<T>(m: T): T { return m; }
static keys<K>(m: Map<K, any>): K[] { return _arrayFromMap(m, false); }

View File

@ -336,8 +336,8 @@ export class AppProtoView {
this.variableLocations = variableLocations;
this.protoLocals = new Map<string, any>();
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); });
}
}

View File

@ -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 {

View File

@ -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);

View File

@ -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<string, string[]>;
constructor(headers?: Headers | {[key: string]: any}) {
if (isBlank(headers)) {
this._headersMap = new Map<string, string[]>();
if (headers instanceof Headers) {
this._headersMap = (<Headers>headers)._headersMap;
return;
}
if (headers instanceof Headers) {
this._headersMap = (<Headers>headers)._headersMap;
} else /*if (headers instanceof StringMap)*/ {
this._headersMap = MapWrapper.createFromStringMap<string[]>(<{[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<string, string[]>();
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<string, string[]>) => void): void {
MapWrapper.forEach(this._headersMap, fn);
this._headersMap.forEach(fn);
}
/**

View File

@ -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); }
}

View File

@ -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(

View File

@ -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));
}

View File

@ -169,14 +169,12 @@ export class TestComponentBuilder {
createAsync(rootComponentType: Type): Promise<RootTestComponent> {
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) =>

View File

@ -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];

View File

@ -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 {

View File

@ -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 {

View File

@ -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;

View File

@ -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(

View File

@ -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); };
}
}

View File

@ -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);