Skip to content

Commit

Permalink
feat(js-core): add isPromiseLike()
Browse files Browse the repository at this point in the history
  • Loading branch information
ersimont committed Nov 11, 2020
1 parent 6495ec3 commit 7cfec64
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
5 changes: 5 additions & 0 deletions projects/integration/src/app/api-tests/js-core.spec.ts
Expand Up @@ -7,6 +7,7 @@ import {
elapsedToString,
isDefined,
isEqualAtDepth,
isPromiseLike,
isSetEqual,
isSuperset,
mapAsKeys,
Expand Down Expand Up @@ -70,6 +71,10 @@ describe('js-core', () => {
expect(isEqualAtDepth).toBeDefined();
});

it('has isPromiseLike', () => {
expect(isPromiseLike).toBeDefined();
});

it('has isSetEqual', () => {
expect(isSetEqual).toBeDefined();
});
Expand Down
11 changes: 5 additions & 6 deletions projects/js-core/src/lib/is-defined.spec.ts
@@ -1,12 +1,11 @@
import { isDefined } from './is-defined';

describe('isDefined()', () => {
it('works', () => {
// tslint:disable-next-line:only-arrow-functions
const args = (function (..._: any[]): IArguments {
return arguments;
})([1, 2, 3]);
function getArguments(): IArguments {
return arguments;
}

it('works', () => {
// falsey values
expect(isDefined(undefined)).toBe(false);
expect(isDefined(null)).toBe(true);
Expand All @@ -16,7 +15,7 @@ describe('isDefined()', () => {
expect(isDefined('')).toBe(true);

// all the other things
expect(isDefined(args)).toBe(true);
expect(isDefined(getArguments())).toBe(true);
expect(isDefined([1, 2, 3])).toBe(true);
expect(isDefined(true)).toBe(true);
expect(isDefined(new Date())).toBe(true);
Expand Down
33 changes: 33 additions & 0 deletions projects/js-core/src/lib/is-promise-like.spec.ts
@@ -0,0 +1,33 @@
import { isPromiseLike } from './is-promise-like';

describe('isPromiseLike()', () => {
function getArguments(): IArguments {
return arguments;
}

it('works', () => {
expect(isPromiseLike(Promise.resolve('hi'))).toBe(true);
expect(isPromiseLike(Promise.reject('bye'))).toBe(true);

// falsey values
expect(isPromiseLike(undefined)).toBe(false);
expect(isPromiseLike(null)).toBe(false);
expect(isPromiseLike(false)).toBe(false);
expect(isPromiseLike(0)).toBe(false);
expect(isPromiseLike(NaN)).toBe(false);
expect(isPromiseLike('')).toBe(false);

// all the other things
expect(isPromiseLike(getArguments())).toBe(false);
expect(isPromiseLike([1, 2, 3])).toBe(false);
expect(isPromiseLike(true)).toBe(false);
expect(isPromiseLike(new Date())).toBe(false);
expect(isPromiseLike(new Error())).toBe(false);
expect(isPromiseLike(Array.prototype.slice)).toBe(false);
expect(isPromiseLike({ a: 1 })).toBe(false);
expect(isPromiseLike(1)).toBe(false);
expect(isPromiseLike(/x/)).toBe(false);
expect(isPromiseLike('a')).toBe(false);
expect(isPromiseLike(Symbol('a'))).toBe(false);
});
});
13 changes: 13 additions & 0 deletions projects/js-core/src/lib/is-promise-like.ts
@@ -0,0 +1,13 @@
/**
* Checks if `value` has a `then()` function, indicating that it is probably promise-like.
*
* ```ts
* isPromiseLike(Promise.resolve('hi')); // true
* isPromiseLike(Promise.reject('bye')); // true
* isPromiseLike({}); // false
* isPromiseLike(null); // false
* ```
*/
export function isPromiseLike(value: any): value is PromiseLike<unknown> {
return typeof value?.then === 'function';
}
1 change: 1 addition & 0 deletions projects/js-core/src/public-api.ts
Expand Up @@ -8,6 +8,7 @@ export * from './lib/sets';
export * from './lib/time';
export { assert } from './lib/assert';
export { isDefined } from './lib/is-defined';
export { isPromiseLike } from './lib/is-promise-like';
export { MigrationManager } from './lib/migration-manager';
export { Persistence } from './lib/persistence';
export { roundToMultipleOf } from './lib/round-to-multiple-of';
Expand Down

0 comments on commit 7cfec64

Please sign in to comment.