refactor(facade): refactor type check function - is*()

This commit is contained in:
Victor Berchet 2015-06-11 19:32:55 +02:00
parent 37fceda7e8
commit cdfb635737
17 changed files with 52 additions and 40 deletions

View File

@ -12,6 +12,7 @@ import {
stringify, stringify,
getMapKey, getMapKey,
looseIdentical, looseIdentical,
isArray
} from 'angular2/src/facade/lang'; } from 'angular2/src/facade/lang';
import {WrappedValue, Pipe, PipeFactory} from './pipe'; import {WrappedValue, Pipe, PipeFactory} from './pipe';
@ -123,7 +124,7 @@ export class IterableChanges extends Pipe {
var index: int; var index: int;
var item; var item;
if (ListWrapper.isList(collection)) { if (isArray(collection)) {
var list = collection; var list = collection;
this._length = collection.length; this._length = collection.length;

View File

@ -1,5 +1,5 @@
import {Promise, PromiseWrapper} from 'angular2/src/facade/async'; 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 {Pipe, WrappedValue} from './pipe';
import {ChangeDetectorRef} from '../change_detector_ref'; import {ChangeDetectorRef} from '../change_detector_ref';
@ -41,7 +41,7 @@ export class PromisePipe extends Pipe {
this._latestReturnedValue = null; this._latestReturnedValue = null;
} }
supports(promise): boolean { return PromiseWrapper.isPromise(promise); } supports(promise): boolean { return isPromise(promise); }
onDestroy(): void { onDestroy(): void {
if (isPresent(this._sourcePromise)) { if (isPresent(this._sourcePromise)) {
@ -87,7 +87,7 @@ export class PromisePipe extends Pipe {
* @exportedAs angular2/pipes * @exportedAs angular2/pipes
*/ */
export class PromisePipeFactory { export class PromisePipeFactory {
supports(promise): boolean { return PromiseWrapper.isPromise(promise); } supports(promise): boolean { return isPromise(promise); }
create(cdRef): Pipe { return new PromisePipe(cdRef); } create(cdRef): Pipe { return new PromisePipe(cdRef); }
} }

View File

@ -5,7 +5,9 @@ import {
isPresent, isPresent,
BaseException, BaseException,
normalizeBlank, normalizeBlank,
stringify stringify,
isArray,
isPromise
} from 'angular2/src/facade/lang'; } from 'angular2/src/facade/lang';
import {Promise, PromiseWrapper} from 'angular2/src/facade/async'; import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
import {List, ListWrapper, Map, MapWrapper} from 'angular2/src/facade/collection'; import {List, ListWrapper, Map, MapWrapper} from 'angular2/src/facade/collection';
@ -103,8 +105,8 @@ export class Compiler {
var componentBinding = this._bindDirective(component); var componentBinding = this._bindDirective(component);
Compiler._assertTypeIsComponent(componentBinding); Compiler._assertTypeIsComponent(componentBinding);
var pvOrPromise = this._compile(componentBinding); var pvOrPromise = this._compile(componentBinding);
var pvPromise = PromiseWrapper.isPromise(pvOrPromise) ? <Promise<AppProtoView>>pvOrPromise : var pvPromise = isPromise(pvOrPromise) ? <Promise<AppProtoView>>pvOrPromise :
PromiseWrapper.resolve(pvOrPromise); PromiseWrapper.resolve(pvOrPromise);
return pvPromise.then((appProtoView) => { return new ProtoViewRef(appProtoView); }); return pvPromise.then((appProtoView) => { return new ProtoViewRef(appProtoView); });
} }
@ -174,7 +176,7 @@ export class Compiler {
var elementBinderDone = var elementBinderDone =
(nestedPv: AppProtoView) => { elementBinder.nestedProtoView = nestedPv; }; (nestedPv: AppProtoView) => { elementBinder.nestedProtoView = nestedPv; };
var nestedCall = this._compile(nestedComponent); var nestedCall = this._compile(nestedComponent);
if (PromiseWrapper.isPromise(nestedCall)) { if (isPromise(nestedCall)) {
ListWrapper.push(nestedPVPromises, ListWrapper.push(nestedPVPromises,
(<Promise<AppProtoView>>nestedCall).then(elementBinderDone)); (<Promise<AppProtoView>>nestedCall).then(elementBinderDone));
} else if (isPresent(nestedCall)) { } else if (isPresent(nestedCall)) {
@ -239,7 +241,7 @@ export class Compiler {
private _flattenList(tree: List<any>, out: List<Type | Binding | List<any>>): void { private _flattenList(tree: List<any>, out: List<Type | Binding | List<any>>): void {
for (var i = 0; i < tree.length; i++) { for (var i = 0; i < tree.length; i++) {
var item = resolveForwardRef(tree[i]); var item = resolveForwardRef(tree[i]);
if (ListWrapper.isList(item)) { if (isArray(item)) {
this._flattenList(item, out); this._flattenList(item, out);
} else { } else {
ListWrapper.push(out, item); ListWrapper.push(out, item);

View File

@ -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 {List, MapWrapper, ListWrapper} from 'angular2/src/facade/collection';
import {reflector} from 'angular2/src/reflection/reflection'; import {reflector} from 'angular2/src/reflection/reflection';
import {Key} from './key'; import {Key} from './key';
@ -464,7 +472,7 @@ function _extractToken(typeOrFunc, annotations /*List<any> | any*/,
var lazy = false; var lazy = false;
var asPromise = false; var asPromise = false;
if (!ListWrapper.isList(annotations)) { if (!isArray(annotations)) {
return _createDependency(annotations, asPromise, lazy, optional, depProps); return _createDependency(annotations, asPromise, lazy, optional, depProps);
} }

View File

@ -26,10 +26,6 @@ class PromiseWrapper {
} }
static CompleterWrapper completer() => new CompleterWrapper(new Completer()); static CompleterWrapper completer() => new CompleterWrapper(new Completer());
static bool isPromise(maybePromise) {
return maybePromise is Future;
}
} }
class TimerWrapper { class TimerWrapper {

View File

@ -39,7 +39,6 @@ export class PromiseWrapper {
return {promise: p, resolve: resolve, reject: reject}; return {promise: p, resolve: resolve, reject: reject};
} }
static isPromise(maybePromise): boolean { return maybePromise instanceof Promise; }
} }
export class TimerWrapper { export class TimerWrapper {

View File

@ -135,7 +135,6 @@ class ListWrapper {
..setRange(0, a.length, a) ..setRange(0, a.length, a)
..setRange(a.length, a.length + b.length, b); ..setRange(a.length, a.length + b.length, b);
} }
static bool isList(l) => l is List;
static void insert(List l, int index, value) { static void insert(List l, int index, value) {
l.insert(index, value); l.insert(index, value);
} }

View File

@ -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 List = global.Array;
export var Map = global.Map; export var Map = global.Map;
@ -192,7 +192,6 @@ export class ListWrapper {
return a.reverse(); return a.reverse();
} }
static concat(a, b) { return a.concat(b); } 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 insert(list, index: int, value) { list.splice(index, 0, value); }
static removeAt(list, index: int) { static removeAt(list, index: int) {
var res = list[index]; var res = list[index];
@ -243,13 +242,13 @@ export class ListWrapper {
export function isListLikeIterable(obj): boolean { export function isListLikeIterable(obj): boolean {
if (!isJsObject(obj)) return false; 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] (!(obj instanceof Map) && // JS Map are iterables but return entries as [k, v]
Symbol.iterator in obj); // JS Iterable have a Symbol.iterator prop Symbol.iterator in obj); // JS Iterable have a Symbol.iterator prop
} }
export function iterateListLike(obj, fn: Function) { export function iterateListLike(obj, fn: Function) {
if (ListWrapper.isList(obj)) { if (isArray(obj)) {
for (var i = 0; i < obj.length; i++) { for (var i = 0; i < obj.length; i++) {
fn(obj[i]); fn(obj[i]);
} }

View File

@ -3,6 +3,7 @@ library angular.core.facade.lang;
export 'dart:core' show Type, RegExp, print, DateTime; export 'dart:core' show Type, RegExp, print, DateTime;
import 'dart:math' as math; import 'dart:math' as math;
import 'dart:convert' as convert; import 'dart:convert' as convert;
import 'dart:async' show Future;
class Math { class Math {
static final _random = new math.Random(); static final _random = new math.Random();
@ -26,7 +27,9 @@ bool isBlank(obj) => obj == null;
bool isString(obj) => obj is String; bool isString(obj) => obj is String;
bool isFunction(obj) => obj is Function; bool isFunction(obj) => obj is Function;
bool isType(obj) => obj is Type; 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(); String stringify(obj) => obj.toString();
@ -88,8 +91,6 @@ class StringWrapper {
static bool contains(String s, String substr) { static bool contains(String s, String substr) {
return s.contains(substr); return s.contains(substr);
} }
static bool isString(s) => s is String;
} }
class StringJoiner { class StringJoiner {

View File

@ -75,10 +75,18 @@ export function isType(obj): boolean {
return isFunction(obj); return isFunction(obj);
} }
export function isMap(obj): boolean { export function isStringMap(obj): boolean {
return typeof obj === 'object' && obj !== null; return typeof obj === 'object' && obj !== null;
} }
export function isPromise(obj): boolean {
return obj instanceof (<any>_global).Promise;
}
export function isArray(obj): boolean {
return Array.isArray(obj);
}
export function stringify(token): string { export function stringify(token): string {
if (typeof token === 'string') { if (typeof token === 'string') {
return token; return token;
@ -132,8 +140,6 @@ export class StringWrapper {
} }
static contains(s: string, substr: string): boolean { return s.indexOf(substr) != -1; } 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 { export class StringJoiner {

View File

@ -1,5 +1,5 @@
import {StringMapWrapper, ListWrapper, List} from 'angular2/src/facade/collection'; 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'; import * as modelModule from './model';
@ -113,7 +113,7 @@ export class FormBuilder {
modelModule.ControlArray) { modelModule.ControlArray) {
return controlConfig; return controlConfig;
} else if (ListWrapper.isList(controlConfig)) { } else if (isArray(controlConfig)) {
var value = ListWrapper.get(controlConfig, 0); var value = ListWrapper.get(controlConfig, 0);
var validator = controlConfig.length > 1 ? controlConfig[1] : null; var validator = controlConfig.length > 1 ? controlConfig[1] : null;
return this.control(value, validator); return this.control(value, validator);

View File

@ -1,5 +1,5 @@
import {ListWrapper, MapWrapper} from 'angular2/src/facade/collection'; 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'; import {DirectiveMetadata} from 'angular2/src/render/api';
/** /**
@ -63,5 +63,5 @@ export function directiveMetadataFromMap(map: Map<string, any>): DirectiveMetada
*/ */
function _cloneIfPresent(o): any { function _cloneIfPresent(o): any {
if (!isPresent(o)) return null; if (!isPresent(o)) return null;
return ListWrapper.isList(o) ? ListWrapper.clone(o) : MapWrapper.clone(o); return isArray(o) ? ListWrapper.clone(o) : MapWrapper.clone(o);
} }

View File

@ -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 {PromiseWrapper, Promise} from 'angular2/src/facade/async';
import {DOM} from 'angular2/src/dom/dom_adapter'; import {DOM} from 'angular2/src/dom/dom_adapter';
@ -37,7 +37,7 @@ export class EmulatedScopedShadowDomStrategy extends EmulatedUnscopedShadowDomSt
cssText = this.styleUrlResolver.resolveUrls(cssText, templateUrl); cssText = this.styleUrlResolver.resolveUrls(cssText, templateUrl);
var inlinedCss = this.styleInliner.inlineImports(cssText, templateUrl); var inlinedCss = this.styleInliner.inlineImports(cssText, templateUrl);
if (PromiseWrapper.isPromise(inlinedCss)) { if (isPromise(inlinedCss)) {
DOM.setText(styleEl, ''); DOM.setText(styleEl, '');
return (<Promise<string>>inlinedCss) return (<Promise<string>>inlinedCss)
.then((css) => { .then((css) => {

View File

@ -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 {MapWrapper, List, ListWrapper} from 'angular2/src/facade/collection';
import {Promise, PromiseWrapper} from 'angular2/src/facade/async'; import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
@ -30,7 +30,7 @@ export class ShadowDomCompileStep implements CompileStep {
_processStyleElement(current: CompileElement, control: CompileControl) { _processStyleElement(current: CompileElement, control: CompileControl) {
var stylePromise = this._shadowDomStrategy.processStyleElement( var stylePromise = this._shadowDomStrategy.processStyleElement(
this._template.componentId, this._template.templateAbsUrl, current.element); this._template.componentId, this._template.templateAbsUrl, current.element);
if (isPresent(stylePromise) && PromiseWrapper.isPromise(stylePromise)) { if (isPresent(stylePromise) && isPromise(stylePromise)) {
ListWrapper.push(this._subTaskPromises, stylePromise); ListWrapper.push(this._subTaskPromises, stylePromise);
} }

View File

@ -12,6 +12,7 @@ import {
RegExpWrapper, RegExpWrapper,
StringWrapper, StringWrapper,
normalizeBlank, normalizeBlank,
isPromise
} from 'angular2/src/facade/lang'; } from 'angular2/src/facade/lang';
import { import {
Promise, Promise,
@ -76,7 +77,7 @@ export class StyleInliner {
promise = PromiseWrapper.then(this._xhr.get(url), (rawCss) => { promise = PromiseWrapper.then(this._xhr.get(url), (rawCss) => {
// resolve nested @import rules // resolve nested @import rules
var inlinedCss = this._inlineImports(rawCss, url, inlinedUrls); var inlinedCss = this._inlineImports(rawCss, url, inlinedUrls);
if (PromiseWrapper.isPromise(inlinedCss)) { if (isPromise(inlinedCss)) {
// wait until nested @import are inlined // wait until nested @import are inlined
return (<Promise<string>>inlinedCss) return (<Promise<string>>inlinedCss)
.then((css) => {return prefix + this._transformImportedCss(css, mediaQuery, url) + .then((css) => {return prefix + this._transformImportedCss(css, mediaQuery, url) +

View File

@ -13,7 +13,7 @@ import {
isPresent, isPresent,
isBlank, isBlank,
isType, isType,
isMap, isStringMap,
isFunction, isFunction,
StringWrapper, StringWrapper,
BaseException BaseException
@ -180,7 +180,7 @@ var VALID_COMPONENT_TYPES = ['constructor', 'loader'];
function normalizeComponentDeclaration(config: any): StringMap<string, any> { function normalizeComponentDeclaration(config: any): StringMap<string, any> {
if (isType(config)) { if (isType(config)) {
return {'constructor': config, 'type': 'constructor'}; return {'constructor': config, 'type': 'constructor'};
} else if (isMap(config)) { } else if (isStringMap(config)) {
if (isBlank(config['type'])) { if (isBlank(config['type'])) {
throw new BaseException( throw new BaseException(
`Component declaration when provided as a map should include a 'type' property`); `Component declaration when provided as a map should include a 'type' property`);

View File

@ -23,7 +23,7 @@ import {
} from 'angular2/src/change_detection/parser/ast'; } 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('"'); var quoteRegExp = RegExpWrapper.create('"');
@ -150,7 +150,7 @@ export class Unparser implements AstVisitor {
} }
visitLiteralPrimitive(ast: LiteralPrimitive) { visitLiteralPrimitive(ast: LiteralPrimitive) {
if (StringWrapper.isString(ast.value)) { if (isString(ast.value)) {
this._expression += `"${StringWrapper.replaceAll(ast.value, quoteRegExp, '\"')}"`; this._expression += `"${StringWrapper.replaceAll(ast.value, quoteRegExp, '\"')}"`;
} else { } else {
this._expression += `${ast.value}`; this._expression += `${ast.value}`;