Skip to content

Commit

Permalink
feat(js-core): add isTruthy()
Browse files Browse the repository at this point in the history
  • Loading branch information
ersimont committed Dec 9, 2020
1 parent 62bd6da commit ca38c5d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions projects/integration/src/app/api-tests/js-core.spec.ts
Expand Up @@ -13,6 +13,7 @@ import {
isPromiseLike,
isSetEqual,
isSuperset,
isTruthy,
mapAsKeys,
mapToObject,
MigrationManager,
Expand Down Expand Up @@ -97,6 +98,10 @@ describe('js-core', () => {
expect(isSuperset).toBeDefined();
});

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

it('has mapAsKeys', () => {
expect(mapAsKeys).toBeDefined();
});
Expand Down
1 change: 1 addition & 0 deletions projects/js-core/src/lib/predicates/index.ts
Expand Up @@ -2,3 +2,4 @@ export { isDefined } from './is-defined';
export { isEqualAtDepth } from './is-equal-at-depth';
export { isFalsy } from './is-falsy';
export { isPromiseLike } from './is-promise-like';
export { isTruthy } from './is-truthy';
30 changes: 30 additions & 0 deletions projects/js-core/src/lib/predicates/is-truthy.spec.ts
@@ -0,0 +1,30 @@
import { isTruthy } from './is-truthy';

describe('isTruthy', () => {
// tslint:disable-next-line:only-arrow-functions
it('returns `true` for truthy values', function (): void {
expect(isTruthy(arguments)).toBe(true);
expect(isTruthy([1, 2, 3])).toBe(true);
expect(isTruthy(true)).toBe(true);
expect(isTruthy(new Date())).toBe(true);
expect(isTruthy(new Error())).toBe(true);
expect(isTruthy(Array.prototype.slice)).toBe(true);
expect(isTruthy({ a: 1 })).toBe(true);
expect(isTruthy(1)).toBe(true);
expect(isTruthy(/x/)).toBe(true);
expect(isTruthy('a')).toBe(true);
expect(isTruthy(Symbol('a'))).toBe(true);
});

it('returns `false` for falsy values', () => {
expect(isTruthy(false)).toBe(false);
expect(isTruthy(0)).toBe(false);
expect(isTruthy(-0)).toBe(false);
// uncomment when targeting ES2020
// expect(isTruthy(0n)).toBe(false);
expect(isTruthy('')).toBe(false);
expect(isTruthy(null)).toBe(false);
expect(isTruthy(undefined)).toBe(false);
expect(isTruthy(NaN)).toBe(false);
});
});
6 changes: 6 additions & 0 deletions projects/js-core/src/lib/predicates/is-truthy.ts
@@ -0,0 +1,6 @@
/**
* Checks if `value` is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy).
*/
export function isTruthy(value: any): boolean {
return !!value;
}

0 comments on commit ca38c5d

Please sign in to comment.