diff --git a/modules/@angular/common/src/pipes/async_pipe.ts b/modules/@angular/common/src/pipes/async_pipe.ts index bd0db25ccb..17123c0551 100644 --- a/modules/@angular/common/src/pipes/async_pipe.ts +++ b/modules/@angular/common/src/pipes/async_pipe.ts @@ -8,7 +8,8 @@ import {ChangeDetectorRef, OnDestroy, Pipe, WrappedValue} from '@angular/core'; import {EventEmitter, Observable} from '../facade/async'; -import {isBlank, isPresent, isPromise} from '../facade/lang'; +import {isBlank, isPresent} from '../facade/lang'; +import {isPromise} from '../private_import_core'; import {InvalidPipeArgumentError} from './invalid_pipe_argument_error'; interface SubscriptionStrategy { diff --git a/modules/@angular/common/src/private_import_core.ts b/modules/@angular/common/src/private_import_core.ts new file mode 100644 index 0000000000..0292fbf02f --- /dev/null +++ b/modules/@angular/common/src/private_import_core.ts @@ -0,0 +1,11 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {__core_private__ as r} from '@angular/core'; + +export const isPromise: typeof r.isPromise = r.isPromise; diff --git a/modules/@angular/core/src/application_init.ts b/modules/@angular/core/src/application_init.ts index e7c27af6ee..45657ce9db 100644 --- a/modules/@angular/core/src/application_init.ts +++ b/modules/@angular/core/src/application_init.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {isPromise} from '../src/facade/lang'; +import {isPromise} from '../src/util/lang'; import {Inject, Injectable, OpaqueToken, Optional} from './di'; diff --git a/modules/@angular/core/src/application_ref.ts b/modules/@angular/core/src/application_ref.ts index 5fd20a7fb9..c1ac48631d 100644 --- a/modules/@angular/core/src/application_ref.ts +++ b/modules/@angular/core/src/application_ref.ts @@ -9,7 +9,8 @@ import {ErrorHandler} from '../src/error_handler'; import {ListWrapper} from '../src/facade/collection'; import {unimplemented} from '../src/facade/errors'; -import {isBlank, isPresent, isPromise, stringify} from '../src/facade/lang'; +import {isBlank, isPresent, stringify} from '../src/facade/lang'; +import {isPromise} from '../src/util/lang'; import {ApplicationInitStatus} from './application_init'; import {APP_BOOTSTRAP_LISTENER, PLATFORM_INITIALIZER} from './application_tokens'; diff --git a/modules/@angular/core/src/core_private_export.ts b/modules/@angular/core/src/core_private_export.ts index 778b848446..6a356841da 100644 --- a/modules/@angular/core/src/core_private_export.ts +++ b/modules/@angular/core/src/core_private_export.ts @@ -39,6 +39,7 @@ import * as reflector_reader from './reflection/reflector_reader'; import * as reflection_types from './reflection/types'; import * as api from './render/api'; import * as decorators from './util/decorators'; +import {isPromise} from './util/lang'; export var __core_private__: { isDefaultChangeDetectionStrategy: typeof constants.isDefaultChangeDetectionStrategy, @@ -118,9 +119,11 @@ export var __core_private__: { ANY_STATE: typeof ANY_STATE_, DEFAULT_STATE: typeof DEFAULT_STATE_, EMPTY_STATE: typeof EMPTY_STATE_, - FILL_STYLE_FLAG: typeof FILL_STYLE_FLAG_, _ComponentStillLoadingError?: ComponentStillLoadingError - ComponentStillLoadingError: typeof ComponentStillLoadingError - } = { + FILL_STYLE_FLAG: typeof FILL_STYLE_FLAG_, + _ComponentStillLoadingError?: ComponentStillLoadingError, + ComponentStillLoadingError: typeof ComponentStillLoadingError, + isPromise: typeof isPromise +} = { isDefaultChangeDetectionStrategy: constants.isDefaultChangeDetectionStrategy, ChangeDetectorStatus: constants.ChangeDetectorStatus, CHANGE_DETECTION_STRATEGY_VALUES: constants.CHANGE_DETECTION_STRATEGY_VALUES, @@ -185,5 +188,6 @@ export var __core_private__: { DEFAULT_STATE: DEFAULT_STATE_, EMPTY_STATE: EMPTY_STATE_, FILL_STYLE_FLAG: FILL_STYLE_FLAG_, - ComponentStillLoadingError: ComponentStillLoadingError + ComponentStillLoadingError: ComponentStillLoadingError, + isPromise: isPromise }; diff --git a/modules/@angular/core/src/util/lang.ts b/modules/@angular/core/src/util/lang.ts new file mode 100644 index 0000000000..314473971c --- /dev/null +++ b/modules/@angular/core/src/util/lang.ts @@ -0,0 +1,13 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +export function isPromise(obj: any): obj is Promise { + // allow any Promise/A+ compliant thenable. + // It's up to the caller to ensure that obj.then conforms to the spec + return !!obj && typeof obj.then === 'function'; +} diff --git a/modules/@angular/core/test/util/lang_spec.ts b/modules/@angular/core/test/util/lang_spec.ts new file mode 100644 index 0000000000..961a3fd6da --- /dev/null +++ b/modules/@angular/core/test/util/lang_spec.ts @@ -0,0 +1,28 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ +import {isPromise} from '@angular/core/src/util/lang'; + +export function main() { + describe('isPromise', () => { + it('should be true for native Promises', + () => expect(isPromise(Promise.resolve(true))).toEqual(true)); + + it('should be true for thenables', () => expect(isPromise({then: () => {}})).toEqual(true)); + + it('should be false if "then" is not a function', + () => expect(isPromise({then: 0})).toEqual(false)); + + it('should be false if the argument has no "then" function', + () => expect(isPromise({})).toEqual(false)); + + it('should be false if the argument is undefined or null', () => { + expect(isPromise(undefined)).toEqual(false); + expect(isPromise(null)).toEqual(false); + }); + }); +} diff --git a/modules/@angular/core/testing/private_import_core.ts b/modules/@angular/core/testing/private_import_core.ts new file mode 100644 index 0000000000..0292fbf02f --- /dev/null +++ b/modules/@angular/core/testing/private_import_core.ts @@ -0,0 +1,11 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {__core_private__ as r} from '@angular/core'; + +export const isPromise: typeof r.isPromise = r.isPromise; diff --git a/modules/@angular/core/testing/testing_internal.ts b/modules/@angular/core/testing/testing_internal.ts index 2dbcea90c9..27674f4da4 100644 --- a/modules/@angular/core/testing/testing_internal.ts +++ b/modules/@angular/core/testing/testing_internal.ts @@ -8,7 +8,8 @@ import {AsyncTestCompleter} from './async_test_completer'; import {StringMapWrapper} from './facade/collection'; -import {Math, global, isPromise} from './facade/lang'; +import {Math, global} from './facade/lang'; +import {isPromise} from './private_import_core'; import {TestBed, getTestBed, inject} from './test_bed'; export {AsyncTestCompleter} from './async_test_completer'; diff --git a/modules/@angular/facade/src/lang.ts b/modules/@angular/facade/src/lang.ts index 3b6de25bbd..0d99a2a813 100644 --- a/modules/@angular/facade/src/lang.ts +++ b/modules/@angular/facade/src/lang.ts @@ -113,12 +113,6 @@ export function isStrictStringMap(obj: any): boolean { return isStringMap(obj) && Object.getPrototypeOf(obj) === STRING_MAP_PROTO; } -export function isPromise(obj: any): boolean { - // allow any Promise/A+ compliant thenable. - // It's up to the caller to ensure that obj.then conforms to the spec - return isPresent(obj) && isFunction(obj.then); -} - export function isArray(obj: any): boolean { return Array.isArray(obj); } diff --git a/modules/@angular/facade/test/lang_spec.ts b/modules/@angular/facade/test/lang_spec.ts index e80e9400d1..823cc9ed0b 100644 --- a/modules/@angular/facade/test/lang_spec.ts +++ b/modules/@angular/facade/test/lang_spec.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {NumberWrapper, StringWrapper, escapeRegExp, hasConstructor, isPresent, isPromise, resolveEnumToken} from '../src/lang'; +import {NumberWrapper, StringWrapper, escapeRegExp, hasConstructor, isPresent, resolveEnumToken} from '../src/lang'; enum UsefulEnum { MyToken, @@ -153,22 +153,4 @@ export function main() { () => { expect(hasConstructor(new MySubclass(), MySuperclass)).toEqual(false); }); }); }); - describe('isPromise', () => { - it('should be true for native Promises', - () => expect(isPromise(Promise.resolve(true))).toEqual(true)); - - it('should be true for thenables', - () => expect(isPromise({then: function() {}})).toEqual(true)); - - it('should be false if "then" is not a function', - () => expect(isPromise({then: 0})).toEqual(false)); - - it('should be false if the argument has no "then" function', - () => expect(isPromise({})).toEqual(false)); - - it('should be false if the argument is undefined or null', () => { - expect(isPromise(undefined)).toEqual(false); - expect(isPromise(null)).toEqual(false); - }); - }); } diff --git a/modules/@angular/forms/src/model.ts b/modules/@angular/forms/src/model.ts index c8b21b39d9..a79f98faec 100644 --- a/modules/@angular/forms/src/model.ts +++ b/modules/@angular/forms/src/model.ts @@ -12,7 +12,8 @@ import {composeAsyncValidators, composeValidators} from './directives/shared'; import {AsyncValidatorFn, ValidatorFn} from './directives/validators'; import {EventEmitter, Observable} from './facade/async'; import {ListWrapper, StringMapWrapper} from './facade/collection'; -import {isBlank, isPresent, isPromise, isStringMap, normalizeBool} from './facade/lang'; +import {isBlank, isPresent, isStringMap, normalizeBool} from './facade/lang'; +import {isPromise} from './private_import_core'; diff --git a/modules/@angular/forms/src/private_import_core.ts b/modules/@angular/forms/src/private_import_core.ts new file mode 100644 index 0000000000..0292fbf02f --- /dev/null +++ b/modules/@angular/forms/src/private_import_core.ts @@ -0,0 +1,11 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {__core_private__ as r} from '@angular/core'; + +export const isPromise: typeof r.isPromise = r.isPromise; diff --git a/modules/@angular/forms/src/validators.ts b/modules/@angular/forms/src/validators.ts index 29f7cfe039..196a9d4f7f 100644 --- a/modules/@angular/forms/src/validators.ts +++ b/modules/@angular/forms/src/validators.ts @@ -11,8 +11,10 @@ import {toPromise} from 'rxjs/operator/toPromise'; import {AsyncValidatorFn, ValidatorFn} from './directives/validators'; import {StringMapWrapper} from './facade/collection'; -import {isBlank, isPresent, isPromise, isString} from './facade/lang'; +import {isBlank, isPresent, isString} from './facade/lang'; import {AbstractControl} from './model'; +import {isPromise} from './private_import_core'; + /**