Skip to content

Commit

Permalink
feat: add truthy / falsy
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrious committed Dec 27, 2024
1 parent 06da827 commit 3113b68
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/is-to-as.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type { IsFalsy } from ".";

import { describe, it, expect } from "vitest";

import {
isDefined,
isTrue,
toTrue,
asTrue,
isTruthy,
isBoolean,
toBoolean,
isNumber,
Expand Down Expand Up @@ -57,6 +60,19 @@ describe("primitive.ts", () => {
expect(asTrue(1)).toBe(false);
});

it("isTruthy", () => {
expect(isTruthy(true)).toBe(true);
expect(isTruthy(false)).toBe(false);
expect(isTruthy(1)).toBe(true);
});

it("isFalsy", () => {
const isFalsy: IsFalsy = (x: unknown): x is any => !x;

Check failure on line 70 in src/is-to-as.test.ts

View workflow job for this annotation

GitHub Actions / build-and-deploy

Unexpected any. Specify a different type
expect(isFalsy(true)).toBe(false);
expect(isFalsy(false)).toBe(true);
expect(isFalsy(1)).toBe(false);
});

it("isBoolean", () => {
expect(isBoolean(true)).toBe(true);
expect(isBoolean(false)).toBe(true);
Expand Down Expand Up @@ -199,7 +215,7 @@ describe("primitive.ts", () => {
expect(toPlainObjectOfTrue({ a: 1, b: 2 })).toBe(undefined);
});

it("print", () => {
it("show", () => {
expect(print("hello")).toBe("hello");
expect(print(null)).toBe("");
expect(print({ a: 1 })).toBe(JSON.stringify({ a: 1 }, null, 2));
Expand Down
13 changes: 13 additions & 0 deletions src/is-to-as.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ export const toTrue = (x: unknown): true | _ => (x === true ? true : _);
/** Returns `true` if `x` is `true`, otherwise returns `false`. */
export const asTrue = (x: unknown): boolean => (x === true ? x : false);

export type Falsy = false | null | undefined | 0 | "";

export interface IsFalsy {
(x: unknown): x is Falsy;
<T>(x: T): x is Extract<T, Falsy>;
}

/** Returns `true` if `Boolean(x)` is `false`. */
export const isFalsy: IsFalsy = (x: unknown): x is Falsy => !x;

/** Returns `true` if `Boolean(x)` is `true`. */
export const isTruthy = <T>(x: T): x is Exclude<T, Falsy> => !!x;

export const isBoolean = (x: unknown): x is boolean => x === true || x === false;

/** Returns `x` if `x` is `true` or `false`, otherwise returns `undefined`. */
Expand Down
29 changes: 29 additions & 0 deletions src/returns.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, it, expect } from "vitest";

import { noop, returnsUndefined, returnsNull, returnsFalse, returnsTrue, returnsEmptyString } from "./returns";

describe("returns functions", () => {
it("noop should return undefined", () => {
expect(noop()).toBeUndefined();
});

it("returnsUndefined should return undefined", () => {
expect(returnsUndefined()).toBeUndefined();
});

it("returnsNull should return null", () => {
expect(returnsNull()).toBeNull();
});

it("returnsFalse should return false", () => {
expect(returnsFalse()).toBe(false);
});

it("returnsTrue should return true", () => {
expect(returnsTrue()).toBe(true);
});

it("returnsEmptyString should return an empty string", () => {
expect(returnsEmptyString()).toBe("");
});
});
11 changes: 11 additions & 0 deletions src/returns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const noop = (): void => {};

export const returnsUndefined = noop as () => undefined;

export const returnsNull = (): null => null;

export const returnsFalse = (): false => false;

export const returnsTrue = (): true => true;

export const returnsEmptyString = (): string => "";

0 comments on commit 3113b68

Please sign in to comment.