From cdfb635737a939bfdde495071686f11023b3f52e Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Thu, 11 Jun 2015 19:32:55 +0200 Subject: [PATCH] refactor(facade): refactor type check function - is*() --- .../src/change_detection/pipes/iterable_changes.ts | 3 ++- .../src/change_detection/pipes/promise_pipe.ts | 6 +++--- modules/angular2/src/core/compiler/compiler.ts | 12 +++++++----- modules/angular2/src/di/binding.ts | 12 ++++++++++-- modules/angular2/src/facade/async.dart | 4 ---- modules/angular2/src/facade/async.ts | 1 - modules/angular2/src/facade/collection.dart | 1 - modules/angular2/src/facade/collection.ts | 7 +++---- modules/angular2/src/facade/lang.dart | 7 ++++--- modules/angular2/src/facade/lang.ts | 12 +++++++++--- modules/angular2/src/forms/form_builder.ts | 4 ++-- modules/angular2/src/render/dom/convert.ts | 4 ++-- .../emulated_scoped_shadow_dom_strategy.ts | 4 ++-- .../render/dom/shadow_dom/shadow_dom_compile_step.ts | 4 ++-- .../src/render/dom/shadow_dom/style_inliner.ts | 3 ++- modules/angular2/src/router/route_registry.ts | 4 ++-- .../test/change_detection/parser/unparser.ts | 4 ++-- 17 files changed, 52 insertions(+), 40 deletions(-) diff --git a/modules/angular2/src/change_detection/pipes/iterable_changes.ts b/modules/angular2/src/change_detection/pipes/iterable_changes.ts index fba550cf0c..767d3f0314 100644 --- a/modules/angular2/src/change_detection/pipes/iterable_changes.ts +++ b/modules/angular2/src/change_detection/pipes/iterable_changes.ts @@ -12,6 +12,7 @@ import { stringify, getMapKey, looseIdentical, + isArray } from 'angular2/src/facade/lang'; import {WrappedValue, Pipe, PipeFactory} from './pipe'; @@ -123,7 +124,7 @@ export class IterableChanges extends Pipe { var index: int; var item; - if (ListWrapper.isList(collection)) { + if (isArray(collection)) { var list = collection; this._length = collection.length; diff --git a/modules/angular2/src/change_detection/pipes/promise_pipe.ts b/modules/angular2/src/change_detection/pipes/promise_pipe.ts index 7fb0bbec25..5048f7c7a6 100644 --- a/modules/angular2/src/change_detection/pipes/promise_pipe.ts +++ b/modules/angular2/src/change_detection/pipes/promise_pipe.ts @@ -1,5 +1,5 @@ import {Promise, PromiseWrapper} from 'angular2/src/facade/async'; -import {isBlank, isPresent} from 'angular2/src/facade/lang'; +import {isBlank, isPresent, isPromise} from 'angular2/src/facade/lang'; import {Pipe, WrappedValue} from './pipe'; import {ChangeDetectorRef} from '../change_detector_ref'; @@ -41,7 +41,7 @@ export class PromisePipe extends Pipe { this._latestReturnedValue = null; } - supports(promise): boolean { return PromiseWrapper.isPromise(promise); } + supports(promise): boolean { return isPromise(promise); } onDestroy(): void { if (isPresent(this._sourcePromise)) { @@ -87,7 +87,7 @@ export class PromisePipe extends Pipe { * @exportedAs angular2/pipes */ export class PromisePipeFactory { - supports(promise): boolean { return PromiseWrapper.isPromise(promise); } + supports(promise): boolean { return isPromise(promise); } create(cdRef): Pipe { return new PromisePipe(cdRef); } } diff --git a/modules/angular2/src/core/compiler/compiler.ts b/modules/angular2/src/core/compiler/compiler.ts index bdbb781e29..ba4036d30b 100644 --- a/modules/angular2/src/core/compiler/compiler.ts +++ b/modules/angular2/src/core/compiler/compiler.ts @@ -5,7 +5,9 @@ import { isPresent, BaseException, normalizeBlank, - stringify + stringify, + isArray, + isPromise } from 'angular2/src/facade/lang'; import {Promise, PromiseWrapper} from 'angular2/src/facade/async'; import {List, ListWrapper, Map, MapWrapper} from 'angular2/src/facade/collection'; @@ -103,8 +105,8 @@ export class Compiler { var componentBinding = this._bindDirective(component); Compiler._assertTypeIsComponent(componentBinding); var pvOrPromise = this._compile(componentBinding); - var pvPromise = PromiseWrapper.isPromise(pvOrPromise) ? >pvOrPromise : - PromiseWrapper.resolve(pvOrPromise); + var pvPromise = isPromise(pvOrPromise) ? >pvOrPromise : + PromiseWrapper.resolve(pvOrPromise); return pvPromise.then((appProtoView) => { return new ProtoViewRef(appProtoView); }); } @@ -174,7 +176,7 @@ export class Compiler { var elementBinderDone = (nestedPv: AppProtoView) => { elementBinder.nestedProtoView = nestedPv; }; var nestedCall = this._compile(nestedComponent); - if (PromiseWrapper.isPromise(nestedCall)) { + if (isPromise(nestedCall)) { ListWrapper.push(nestedPVPromises, (>nestedCall).then(elementBinderDone)); } else if (isPresent(nestedCall)) { @@ -239,7 +241,7 @@ export class Compiler { private _flattenList(tree: List, out: List>): void { for (var i = 0; i < tree.length; i++) { var item = resolveForwardRef(tree[i]); - if (ListWrapper.isList(item)) { + if (isArray(item)) { this._flattenList(item, out); } else { ListWrapper.push(out, item); diff --git a/modules/angular2/src/di/binding.ts b/modules/angular2/src/di/binding.ts index 2d5e7db41e..dd9a2e5fd0 100644 --- a/modules/angular2/src/di/binding.ts +++ b/modules/angular2/src/di/binding.ts @@ -1,4 +1,12 @@ -import {Type, isBlank, isPresent, CONST, BaseException, stringify} from 'angular2/src/facade/lang'; +import { + Type, + isBlank, + isPresent, + CONST, + BaseException, + stringify, + isArray +} from 'angular2/src/facade/lang'; import {List, MapWrapper, ListWrapper} from 'angular2/src/facade/collection'; import {reflector} from 'angular2/src/reflection/reflection'; import {Key} from './key'; @@ -464,7 +472,7 @@ function _extractToken(typeOrFunc, annotations /*List | any*/, var lazy = false; var asPromise = false; - if (!ListWrapper.isList(annotations)) { + if (!isArray(annotations)) { return _createDependency(annotations, asPromise, lazy, optional, depProps); } diff --git a/modules/angular2/src/facade/async.dart b/modules/angular2/src/facade/async.dart index ae3421f07d..1f1d79e556 100644 --- a/modules/angular2/src/facade/async.dart +++ b/modules/angular2/src/facade/async.dart @@ -26,10 +26,6 @@ class PromiseWrapper { } static CompleterWrapper completer() => new CompleterWrapper(new Completer()); - - static bool isPromise(maybePromise) { - return maybePromise is Future; - } } class TimerWrapper { diff --git a/modules/angular2/src/facade/async.ts b/modules/angular2/src/facade/async.ts index cc4a195bfb..f3f2e18743 100644 --- a/modules/angular2/src/facade/async.ts +++ b/modules/angular2/src/facade/async.ts @@ -39,7 +39,6 @@ export class PromiseWrapper { return {promise: p, resolve: resolve, reject: reject}; } - static isPromise(maybePromise): boolean { return maybePromise instanceof Promise; } } export class TimerWrapper { diff --git a/modules/angular2/src/facade/collection.dart b/modules/angular2/src/facade/collection.dart index 0db09a192b..81ad231dfb 100644 --- a/modules/angular2/src/facade/collection.dart +++ b/modules/angular2/src/facade/collection.dart @@ -135,7 +135,6 @@ class ListWrapper { ..setRange(0, a.length, a) ..setRange(a.length, a.length + b.length, b); } - static bool isList(l) => l is List; static void insert(List l, int index, value) { l.insert(index, value); } diff --git a/modules/angular2/src/facade/collection.ts b/modules/angular2/src/facade/collection.ts index 08660ddac8..51368fec82 100644 --- a/modules/angular2/src/facade/collection.ts +++ b/modules/angular2/src/facade/collection.ts @@ -1,4 +1,4 @@ -import {isJsObject, global, isPresent} from 'angular2/src/facade/lang'; +import {isJsObject, global, isPresent, isArray} from 'angular2/src/facade/lang'; export var List = global.Array; export var Map = global.Map; @@ -192,7 +192,6 @@ export class ListWrapper { return a.reverse(); } static concat(a, b) { return a.concat(b); } - static isList(list) { return Array.isArray(list); } static insert(list, index: int, value) { list.splice(index, 0, value); } static removeAt(list, index: int) { var res = list[index]; @@ -243,13 +242,13 @@ export class ListWrapper { export function isListLikeIterable(obj): boolean { if (!isJsObject(obj)) return false; - return ListWrapper.isList(obj) || + return isArray(obj) || (!(obj instanceof Map) && // JS Map are iterables but return entries as [k, v] Symbol.iterator in obj); // JS Iterable have a Symbol.iterator prop } export function iterateListLike(obj, fn: Function) { - if (ListWrapper.isList(obj)) { + if (isArray(obj)) { for (var i = 0; i < obj.length; i++) { fn(obj[i]); } diff --git a/modules/angular2/src/facade/lang.dart b/modules/angular2/src/facade/lang.dart index d811f0eeaf..7229b3233d 100644 --- a/modules/angular2/src/facade/lang.dart +++ b/modules/angular2/src/facade/lang.dart @@ -3,6 +3,7 @@ library angular.core.facade.lang; export 'dart:core' show Type, RegExp, print, DateTime; import 'dart:math' as math; import 'dart:convert' as convert; +import 'dart:async' show Future; class Math { static final _random = new math.Random(); @@ -26,7 +27,9 @@ bool isBlank(obj) => obj == null; bool isString(obj) => obj is String; bool isFunction(obj) => obj is Function; bool isType(obj) => obj is Type; -bool isMap(obj) => obj is Map; +bool isStringMap(obj) => obj is Map; +bool isArray(obj) => obj is List; +bool isPromise(obj) => obj is Future; String stringify(obj) => obj.toString(); @@ -88,8 +91,6 @@ class StringWrapper { static bool contains(String s, String substr) { return s.contains(substr); } - - static bool isString(s) => s is String; } class StringJoiner { diff --git a/modules/angular2/src/facade/lang.ts b/modules/angular2/src/facade/lang.ts index e5fad871eb..fade23d14f 100644 --- a/modules/angular2/src/facade/lang.ts +++ b/modules/angular2/src/facade/lang.ts @@ -75,10 +75,18 @@ export function isType(obj): boolean { return isFunction(obj); } -export function isMap(obj): boolean { +export function isStringMap(obj): boolean { return typeof obj === 'object' && obj !== null; } +export function isPromise(obj): boolean { + return obj instanceof (_global).Promise; +} + +export function isArray(obj): boolean { + return Array.isArray(obj); +} + export function stringify(token): string { if (typeof token === 'string') { return token; @@ -132,8 +140,6 @@ export class StringWrapper { } static contains(s: string, substr: string): boolean { return s.indexOf(substr) != -1; } - - static isString(s: any): boolean { return typeof s === 'string' || s instanceof String; } } export class StringJoiner { diff --git a/modules/angular2/src/forms/form_builder.ts b/modules/angular2/src/forms/form_builder.ts index 917267c6a9..3abf78018e 100644 --- a/modules/angular2/src/forms/form_builder.ts +++ b/modules/angular2/src/forms/form_builder.ts @@ -1,5 +1,5 @@ import {StringMapWrapper, ListWrapper, List} from 'angular2/src/facade/collection'; -import {isPresent} from 'angular2/src/facade/lang'; +import {isPresent, isArray} from 'angular2/src/facade/lang'; import * as modelModule from './model'; @@ -113,7 +113,7 @@ export class FormBuilder { modelModule.ControlArray) { return controlConfig; - } else if (ListWrapper.isList(controlConfig)) { + } else if (isArray(controlConfig)) { var value = ListWrapper.get(controlConfig, 0); var validator = controlConfig.length > 1 ? controlConfig[1] : null; return this.control(value, validator); diff --git a/modules/angular2/src/render/dom/convert.ts b/modules/angular2/src/render/dom/convert.ts index 5a8fdb518b..b73b4f3d86 100644 --- a/modules/angular2/src/render/dom/convert.ts +++ b/modules/angular2/src/render/dom/convert.ts @@ -1,5 +1,5 @@ import {ListWrapper, MapWrapper} from 'angular2/src/facade/collection'; -import {isPresent} from 'angular2/src/facade/lang'; +import {isPresent, isArray} from 'angular2/src/facade/lang'; import {DirectiveMetadata} from 'angular2/src/render/api'; /** @@ -63,5 +63,5 @@ export function directiveMetadataFromMap(map: Map): DirectiveMetada */ function _cloneIfPresent(o): any { if (!isPresent(o)) return null; - return ListWrapper.isList(o) ? ListWrapper.clone(o) : MapWrapper.clone(o); + return isArray(o) ? ListWrapper.clone(o) : MapWrapper.clone(o); } diff --git a/modules/angular2/src/render/dom/shadow_dom/emulated_scoped_shadow_dom_strategy.ts b/modules/angular2/src/render/dom/shadow_dom/emulated_scoped_shadow_dom_strategy.ts index 0a3134f65e..c124b68630 100644 --- a/modules/angular2/src/render/dom/shadow_dom/emulated_scoped_shadow_dom_strategy.ts +++ b/modules/angular2/src/render/dom/shadow_dom/emulated_scoped_shadow_dom_strategy.ts @@ -1,4 +1,4 @@ -import {isBlank, isPresent} from 'angular2/src/facade/lang'; +import {isBlank, isPresent, isPromise} from 'angular2/src/facade/lang'; import {PromiseWrapper, Promise} from 'angular2/src/facade/async'; import {DOM} from 'angular2/src/dom/dom_adapter'; @@ -37,7 +37,7 @@ export class EmulatedScopedShadowDomStrategy extends EmulatedUnscopedShadowDomSt cssText = this.styleUrlResolver.resolveUrls(cssText, templateUrl); var inlinedCss = this.styleInliner.inlineImports(cssText, templateUrl); - if (PromiseWrapper.isPromise(inlinedCss)) { + if (isPromise(inlinedCss)) { DOM.setText(styleEl, ''); return (>inlinedCss) .then((css) => { diff --git a/modules/angular2/src/render/dom/shadow_dom/shadow_dom_compile_step.ts b/modules/angular2/src/render/dom/shadow_dom/shadow_dom_compile_step.ts index 171dfa3172..74e7fceaf7 100644 --- a/modules/angular2/src/render/dom/shadow_dom/shadow_dom_compile_step.ts +++ b/modules/angular2/src/render/dom/shadow_dom/shadow_dom_compile_step.ts @@ -1,4 +1,4 @@ -import {isBlank, isPresent, assertionsEnabled} from 'angular2/src/facade/lang'; +import {isBlank, isPresent, assertionsEnabled, isPromise} from 'angular2/src/facade/lang'; import {MapWrapper, List, ListWrapper} from 'angular2/src/facade/collection'; import {Promise, PromiseWrapper} from 'angular2/src/facade/async'; @@ -30,7 +30,7 @@ export class ShadowDomCompileStep implements CompileStep { _processStyleElement(current: CompileElement, control: CompileControl) { var stylePromise = this._shadowDomStrategy.processStyleElement( this._template.componentId, this._template.templateAbsUrl, current.element); - if (isPresent(stylePromise) && PromiseWrapper.isPromise(stylePromise)) { + if (isPresent(stylePromise) && isPromise(stylePromise)) { ListWrapper.push(this._subTaskPromises, stylePromise); } diff --git a/modules/angular2/src/render/dom/shadow_dom/style_inliner.ts b/modules/angular2/src/render/dom/shadow_dom/style_inliner.ts index f8eeb58501..c6a78fb8cc 100644 --- a/modules/angular2/src/render/dom/shadow_dom/style_inliner.ts +++ b/modules/angular2/src/render/dom/shadow_dom/style_inliner.ts @@ -12,6 +12,7 @@ import { RegExpWrapper, StringWrapper, normalizeBlank, + isPromise } from 'angular2/src/facade/lang'; import { Promise, @@ -76,7 +77,7 @@ export class StyleInliner { promise = PromiseWrapper.then(this._xhr.get(url), (rawCss) => { // resolve nested @import rules var inlinedCss = this._inlineImports(rawCss, url, inlinedUrls); - if (PromiseWrapper.isPromise(inlinedCss)) { + if (isPromise(inlinedCss)) { // wait until nested @import are inlined return (>inlinedCss) .then((css) => {return prefix + this._transformImportedCss(css, mediaQuery, url) + diff --git a/modules/angular2/src/router/route_registry.ts b/modules/angular2/src/router/route_registry.ts index 72c48cadc0..6a09ac82b1 100644 --- a/modules/angular2/src/router/route_registry.ts +++ b/modules/angular2/src/router/route_registry.ts @@ -13,7 +13,7 @@ import { isPresent, isBlank, isType, - isMap, + isStringMap, isFunction, StringWrapper, BaseException @@ -180,7 +180,7 @@ var VALID_COMPONENT_TYPES = ['constructor', 'loader']; function normalizeComponentDeclaration(config: any): StringMap { if (isType(config)) { return {'constructor': config, 'type': 'constructor'}; - } else if (isMap(config)) { + } else if (isStringMap(config)) { if (isBlank(config['type'])) { throw new BaseException( `Component declaration when provided as a map should include a 'type' property`); diff --git a/modules/angular2/test/change_detection/parser/unparser.ts b/modules/angular2/test/change_detection/parser/unparser.ts index 02caa2da8e..f7d399d1e5 100644 --- a/modules/angular2/test/change_detection/parser/unparser.ts +++ b/modules/angular2/test/change_detection/parser/unparser.ts @@ -23,7 +23,7 @@ import { } from 'angular2/src/change_detection/parser/ast'; -import {StringWrapper, RegExpWrapper, isPresent} from 'angular2/src/facade/lang'; +import {StringWrapper, RegExpWrapper, isPresent, isString} from 'angular2/src/facade/lang'; var quoteRegExp = RegExpWrapper.create('"'); @@ -150,7 +150,7 @@ export class Unparser implements AstVisitor { } visitLiteralPrimitive(ast: LiteralPrimitive) { - if (StringWrapper.isString(ast.value)) { + if (isString(ast.value)) { this._expression += `"${StringWrapper.replaceAll(ast.value, quoteRegExp, '\"')}"`; } else { this._expression += `${ast.value}`;