refactor: remove keys() and values() from MapWrapper

This commit is contained in:
Victor Berchet
2016-11-03 16:58:27 -07:00
committed by vikerman
parent 121e5080aa
commit ec92f4b198
19 changed files with 138 additions and 216 deletions

View File

@ -7,8 +7,7 @@
*/
import {Injector} from '../di';
import {MapWrapper, Predicate} from '../facade/collection';
import {isPresent} from '../facade/lang';
import {Predicate} from '../facade/collection';
import {RenderDebugInfo} from '../render/api';
export class EventListener { constructor(public name: string, public callback: Function){}; }
@ -23,7 +22,7 @@ export class DebugNode {
constructor(nativeNode: any, parent: DebugNode, private _debugInfo: RenderDebugInfo) {
this.nativeNode = nativeNode;
if (isPresent(parent) && parent instanceof DebugElement) {
if (parent && parent instanceof DebugElement) {
parent.addChild(this);
} else {
this.parent = null;
@ -31,23 +30,19 @@ export class DebugNode {
this.listeners = [];
}
get injector(): Injector { return isPresent(this._debugInfo) ? this._debugInfo.injector : null; }
get injector(): Injector { return this._debugInfo ? this._debugInfo.injector : null; }
get componentInstance(): any {
return isPresent(this._debugInfo) ? this._debugInfo.component : null;
}
get componentInstance(): any { return this._debugInfo ? this._debugInfo.component : null; }
get context(): any { return isPresent(this._debugInfo) ? this._debugInfo.context : null; }
get context(): any { return this._debugInfo ? this._debugInfo.context : null; }
get references(): {[key: string]: any} {
return isPresent(this._debugInfo) ? this._debugInfo.references : null;
return this._debugInfo ? this._debugInfo.references : null;
}
get providerTokens(): any[] {
return isPresent(this._debugInfo) ? this._debugInfo.providerTokens : null;
}
get providerTokens(): any[] { return this._debugInfo ? this._debugInfo.providerTokens : null; }
get source(): string { return isPresent(this._debugInfo) ? this._debugInfo.source : null; }
get source(): string { return this._debugInfo ? this._debugInfo.source : null; }
}
/**
@ -73,14 +68,14 @@ export class DebugElement extends DebugNode {
}
addChild(child: DebugNode) {
if (isPresent(child)) {
if (child) {
this.childNodes.push(child);
child.parent = this;
}
}
removeChild(child: DebugNode) {
var childIndex = this.childNodes.indexOf(child);
const childIndex = this.childNodes.indexOf(child);
if (childIndex !== -1) {
child.parent = null;
this.childNodes.splice(childIndex, 1);
@ -88,14 +83,14 @@ export class DebugElement extends DebugNode {
}
insertChildrenAfter(child: DebugNode, newChildren: DebugNode[]) {
var siblingIndex = this.childNodes.indexOf(child);
const siblingIndex = this.childNodes.indexOf(child);
if (siblingIndex !== -1) {
var previousChildren = this.childNodes.slice(0, siblingIndex + 1);
var nextChildren = this.childNodes.slice(siblingIndex + 1);
const previousChildren = this.childNodes.slice(0, siblingIndex + 1);
const nextChildren = this.childNodes.slice(siblingIndex + 1);
this.childNodes = previousChildren.concat(newChildren, nextChildren);
for (var i = 0; i < newChildren.length; ++i) {
var newChild = newChildren[i];
if (isPresent(newChild.parent)) {
for (let i = 0; i < newChildren.length; ++i) {
const newChild = newChildren[i];
if (newChild.parent) {
newChild.parent.removeChild(newChild);
}
newChild.parent = this;
@ -104,30 +99,24 @@ export class DebugElement extends DebugNode {
}
query(predicate: Predicate<DebugElement>): DebugElement {
var results = this.queryAll(predicate);
return results.length > 0 ? results[0] : null;
const results = this.queryAll(predicate);
return results[0] || null;
}
queryAll(predicate: Predicate<DebugElement>): DebugElement[] {
var matches: DebugElement[] = [];
const matches: DebugElement[] = [];
_queryElementChildren(this, predicate, matches);
return matches;
}
queryAllNodes(predicate: Predicate<DebugNode>): DebugNode[] {
var matches: DebugNode[] = [];
const matches: DebugNode[] = [];
_queryNodeChildren(this, predicate, matches);
return matches;
}
get children(): DebugElement[] {
var children: DebugElement[] = [];
this.childNodes.forEach((node) => {
if (node instanceof DebugElement) {
children.push(node);
}
});
return children;
return this.childNodes.filter((node) => node instanceof DebugElement) as DebugElement[];
}
triggerEventHandler(eventName: string, eventObj: any) {
@ -173,7 +162,7 @@ function _queryNodeChildren(
}
// Need to keep the nodes in a global Map so that multiple angular apps are supported.
var _nativeNodeToDebugNode = new Map<any, DebugNode>();
const _nativeNodeToDebugNode = new Map<any, DebugNode>();
/**
* @experimental
@ -183,7 +172,7 @@ export function getDebugNode(nativeNode: any): DebugNode {
}
export function getAllDebugNodes(): DebugNode[] {
return MapWrapper.values(_nativeNodeToDebugNode);
return Array.from(_nativeNodeToDebugNode.values());
}
export function indexDebugNode(node: DebugNode) {

View File

@ -6,8 +6,6 @@
* found in the LICENSE file at https://angular.io/license
*/
import {MapWrapper} from '../facade/collection';
import {isBlank, isPresent} from '../facade/lang';
import {reflector} from '../reflection/reflection';
import {Type} from '../type';
@ -103,16 +101,16 @@ export class ResolvedReflectiveFactory {
* Resolve a single provider.
*/
function resolveReflectiveFactory(provider: NormalizedProvider): ResolvedReflectiveFactory {
var factoryFn: Function;
var resolvedDeps: ReflectiveDependency[];
if (isPresent(provider.useClass)) {
var useClass = resolveForwardRef(provider.useClass);
let factoryFn: Function;
let resolvedDeps: ReflectiveDependency[];
if (provider.useClass) {
const useClass = resolveForwardRef(provider.useClass);
factoryFn = reflector.factory(useClass);
resolvedDeps = _dependenciesFor(useClass);
} else if (isPresent(provider.useExisting)) {
} else if (provider.useExisting) {
factoryFn = (aliasInstance: any) => aliasInstance;
resolvedDeps = [ReflectiveDependency.fromKey(ReflectiveKey.get(provider.useExisting))];
} else if (isPresent(provider.useFactory)) {
} else if (provider.useFactory) {
factoryFn = provider.useFactory;
resolvedDeps = constructDependencies(provider.useFactory, provider.deps);
} else {
@ -137,10 +135,10 @@ function resolveReflectiveProvider(provider: NormalizedProvider): ResolvedReflec
* Resolve a list of Providers.
*/
export function resolveReflectiveProviders(providers: Provider[]): ResolvedReflectiveProvider[] {
var normalized = _normalizeProviders(providers, []);
var resolved = normalized.map(resolveReflectiveProvider);
return MapWrapper.values(
mergeResolvedReflectiveProviders(resolved, new Map<number, ResolvedReflectiveProvider>()));
const normalized = _normalizeProviders(providers, []);
const resolved = normalized.map(resolveReflectiveProvider);
const resolvedProviderMap = mergeResolvedReflectiveProviders(resolved, new Map());
return Array.from(resolvedProviderMap.values());
}
/**
@ -152,22 +150,22 @@ export function mergeResolvedReflectiveProviders(
providers: ResolvedReflectiveProvider[],
normalizedProvidersMap: Map<number, ResolvedReflectiveProvider>):
Map<number, ResolvedReflectiveProvider> {
for (var i = 0; i < providers.length; i++) {
var provider = providers[i];
var existing = normalizedProvidersMap.get(provider.key.id);
if (isPresent(existing)) {
for (let i = 0; i < providers.length; i++) {
const provider = providers[i];
const existing = normalizedProvidersMap.get(provider.key.id);
if (existing) {
if (provider.multiProvider !== existing.multiProvider) {
throw new MixingMultiProvidersWithRegularProvidersError(existing, provider);
}
if (provider.multiProvider) {
for (var j = 0; j < provider.resolvedFactories.length; j++) {
for (let j = 0; j < provider.resolvedFactories.length; j++) {
existing.resolvedFactories.push(provider.resolvedFactories[j]);
}
} else {
normalizedProvidersMap.set(provider.key.id, provider);
}
} else {
var resolvedProvider: ResolvedReflectiveProvider;
let resolvedProvider: ResolvedReflectiveProvider;
if (provider.multiProvider) {
resolvedProvider = new ResolvedReflectiveProvider_(
provider.key, provider.resolvedFactories.slice(), provider.multiProvider);
@ -204,26 +202,26 @@ export function constructDependencies(
if (!dependencies) {
return _dependenciesFor(typeOrFunc);
} else {
var params: any[][] = dependencies.map(t => [t]);
const params: any[][] = dependencies.map(t => [t]);
return dependencies.map(t => _extractToken(typeOrFunc, t, params));
}
}
function _dependenciesFor(typeOrFunc: any): ReflectiveDependency[] {
var params = reflector.parameters(typeOrFunc);
const params = reflector.parameters(typeOrFunc);
if (!params) return [];
if (params.some(isBlank)) {
if (params.some(p => p == null)) {
throw new NoAnnotationError(typeOrFunc, params);
}
return params.map((p: any[]) => _extractToken(typeOrFunc, p, params));
return params.map(p => _extractToken(typeOrFunc, p, params));
}
function _extractToken(
typeOrFunc: any /** TODO #9100 */, metadata: any /** TODO #9100 */ /*any[] | any*/,
params: any[][]): ReflectiveDependency {
var depProps: any[] /** TODO #9100 */ = [];
var token: any /** TODO #9100 */ = null;
var optional = false;
typeOrFunc: any, metadata: any[] | any, params: any[][]): ReflectiveDependency {
const depProps: any[] = [];
let token: any = null;
let optional = false;
if (!Array.isArray(metadata)) {
if (metadata instanceof Inject) {
@ -233,11 +231,11 @@ function _extractToken(
}
}
var lowerBoundVisibility: any /** TODO #9100 */ = null;
var upperBoundVisibility: any /** TODO #9100 */ = null;
let lowerBoundVisibility: any = null;
let upperBoundVisibility: any = null;
for (var i = 0; i < metadata.length; ++i) {
var paramMetadata = metadata[i];
for (let i = 0; i < metadata.length; ++i) {
let paramMetadata = metadata[i];
if (paramMetadata instanceof Type) {
token = paramMetadata;
@ -261,7 +259,7 @@ function _extractToken(
token = resolveForwardRef(token);
if (isPresent(token)) {
if (token != null) {
return _createDependency(token, optional, lowerBoundVisibility, upperBoundVisibility, depProps);
} else {
throw new NoAnnotationError(typeOrFunc, params);
@ -269,9 +267,8 @@ function _extractToken(
}
function _createDependency(
token: any /** TODO #9100 */, optional: any /** TODO #9100 */,
lowerBoundVisibility: any /** TODO #9100 */, upperBoundVisibility: any /** TODO #9100 */,
depProps: any /** TODO #9100 */): ReflectiveDependency {
token: any, optional: boolean, lowerBoundVisibility: any, upperBoundVisibility: any,
depProps: any[]): ReflectiveDependency {
return new ReflectiveDependency(
ReflectiveKey.get(token), optional, lowerBoundVisibility, upperBoundVisibility, depProps);
}

View File

@ -7,7 +7,6 @@
*/
import {Injectable} from '../di';
import {MapWrapper} from '../facade/collection';
import {scheduleMicroTask} from '../facade/lang';
import {NgZone} from '../zone/ng_zone';
@ -139,9 +138,9 @@ export class TestabilityRegistry {
getTestability(elem: any): Testability { return this._applications.get(elem); }
getAllTestabilities(): Testability[] { return MapWrapper.values(this._applications); }
getAllTestabilities(): Testability[] { return Array.from(this._applications.values()); }
getAllRootElements(): any[] { return MapWrapper.keys(this._applications); }
getAllRootElements(): any[] { return Array.from(this._applications.keys()); }
findTestabilityInTree(elem: Node, findInAncestors: boolean = true): Testability {
return _testabilityGetter.findTestabilityInTree(this, elem, findInAncestors);

View File

@ -41,9 +41,11 @@ export function main() {
});
it('should detect equality for same content', () => {
expect(MapWrapper.createFromStringMap({'a': 1})).toEqual(MapWrapper.createFromStringMap({
'a': 1
}));
const m1: Map<string, number> = new Map();
m1.set('a', 1);
const m2: Map<string, number> = new Map();
m2.set('a', 1);
expect(m1).toEqual(m2);
});
it('should detect missing entries', () => {
@ -59,16 +61,19 @@ export function main() {
});
it('should detect additional entries', () => {
expect(MapWrapper.createFromStringMap({
'a': 1
})).not.toEqual(MapWrapper.createFromStringMap({'a': 1, 'b': 1}));
const m1: Map<string, number> = new Map();
m1.set('a', 1);
const m2: Map<string, number> = new Map();
m2.set('a', 1);
m2.set('b', 2);
expect(m1).not.toEqual(m2);
});
});
describe('spy objects', () => {
let spyObj: any;
beforeEach(() => { spyObj = <any>new SpyTestObj(); });
beforeEach(() => { spyObj = new SpyTestObj(); });
it('should return a new spy func with no calls',
() => { expect(spyObj.spy('someFunc')).not.toHaveBeenCalled(); });
@ -98,8 +103,7 @@ export function main() {
});
it('should support stubs', () => {
var s = SpyObject.stub({'a': 1}, {'b': 2});
const s = SpyObject.stub({'a': 1}, {'b': 2});
expect(s.a()).toEqual(1);
expect(s.b()).toEqual(2);
});