@clipboard-health/util-ts 2.2.5 → 2.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/.eslintrc.json +12 -0
  2. package/jest.config.ts +18 -0
  3. package/package.json +2 -4
  4. package/project.json +33 -0
  5. package/src/{index.d.ts → index.ts} +0 -1
  6. package/src/lib/{attributes.d.ts → attributes.ts} +1 -1
  7. package/src/lib/cbh-error.spec.ts +30 -0
  8. package/src/lib/cbh-error.ts +26 -0
  9. package/src/lib/cbh-response.spec.ts +25 -0
  10. package/src/lib/cbh-response.ts +15 -0
  11. package/src/lib/defined-utils.spec.ts +35 -0
  12. package/src/lib/{defined-utils.d.ts → defined-utils.ts} +9 -4
  13. package/src/lib/delay.spec.ts +11 -0
  14. package/src/lib/delay.ts +5 -0
  15. package/src/lib/either.spec.ts +23 -0
  16. package/src/lib/{either.d.ts → either.ts} +22 -9
  17. package/src/lib/force-cast.spec.ts +8 -0
  18. package/src/lib/force-cast.ts +4 -0
  19. package/src/lib/head.spec.ts +21 -0
  20. package/src/lib/head.ts +5 -0
  21. package/src/lib/identity.spec.ts +17 -0
  22. package/src/lib/{identity.d.ts → identity.ts} +3 -2
  23. package/src/lib/is-string.spec.ts +11 -0
  24. package/src/lib/is-string.ts +3 -0
  25. package/src/lib/non-empty-array.ts +7 -0
  26. package/src/lib/null-to-undefined.spec.ts +33 -0
  27. package/src/lib/{null-to-undefined.d.ts → null-to-undefined.ts} +4 -2
  28. package/src/lib/option.spec.ts +24 -0
  29. package/src/lib/{option.d.ts → option.ts} +19 -8
  30. package/src/lib/stringify.spec.ts +11 -0
  31. package/src/lib/stringify.ts +5 -0
  32. package/src/lib/to-error.spec.ts +28 -0
  33. package/src/lib/to-error.ts +20 -0
  34. package/tsconfig.json +19 -0
  35. package/tsconfig.lib.json +8 -0
  36. package/tsconfig.lint.json +7 -0
  37. package/tsconfig.spec.json +13 -0
  38. package/typedoc.json +4 -0
  39. package/src/index.d.ts.map +0 -1
  40. package/src/index.js +0 -19
  41. package/src/index.js.map +0 -1
  42. package/src/lib/attributes.d.ts.map +0 -1
  43. package/src/lib/attributes.js +0 -3
  44. package/src/lib/attributes.js.map +0 -1
  45. package/src/lib/cbh-error.d.ts +0 -15
  46. package/src/lib/cbh-error.d.ts.map +0 -1
  47. package/src/lib/cbh-error.js +0 -19
  48. package/src/lib/cbh-error.js.map +0 -1
  49. package/src/lib/cbh-response.d.ts +0 -18
  50. package/src/lib/cbh-response.d.ts.map +0 -1
  51. package/src/lib/cbh-response.js +0 -12
  52. package/src/lib/cbh-response.js.map +0 -1
  53. package/src/lib/defined-utils.d.ts.map +0 -1
  54. package/src/lib/defined-utils.js +0 -21
  55. package/src/lib/defined-utils.js.map +0 -1
  56. package/src/lib/delay.d.ts +0 -2
  57. package/src/lib/delay.d.ts.map +0 -1
  58. package/src/lib/delay.js +0 -9
  59. package/src/lib/delay.js.map +0 -1
  60. package/src/lib/either.d.ts.map +0 -1
  61. package/src/lib/either.js +0 -31
  62. package/src/lib/either.js.map +0 -1
  63. package/src/lib/force-cast.d.ts +0 -3
  64. package/src/lib/force-cast.d.ts.map +0 -1
  65. package/src/lib/force-cast.js +0 -8
  66. package/src/lib/force-cast.js.map +0 -1
  67. package/src/lib/head.d.ts +0 -3
  68. package/src/lib/head.d.ts.map +0 -1
  69. package/src/lib/head.js +0 -7
  70. package/src/lib/head.js.map +0 -1
  71. package/src/lib/identity.d.ts.map +0 -1
  72. package/src/lib/identity.js +0 -17
  73. package/src/lib/identity.js.map +0 -1
  74. package/src/lib/is-string.d.ts +0 -2
  75. package/src/lib/is-string.d.ts.map +0 -1
  76. package/src/lib/is-string.js +0 -7
  77. package/src/lib/is-string.js.map +0 -1
  78. package/src/lib/non-empty-array.d.ts +0 -4
  79. package/src/lib/non-empty-array.d.ts.map +0 -1
  80. package/src/lib/non-empty-array.js +0 -7
  81. package/src/lib/non-empty-array.js.map +0 -1
  82. package/src/lib/null-to-undefined.d.ts.map +0 -1
  83. package/src/lib/null-to-undefined.js +0 -17
  84. package/src/lib/null-to-undefined.js.map +0 -1
  85. package/src/lib/option.d.ts.map +0 -1
  86. package/src/lib/option.js +0 -29
  87. package/src/lib/option.js.map +0 -1
  88. package/src/lib/stringify.d.ts +0 -2
  89. package/src/lib/stringify.d.ts.map +0 -1
  90. package/src/lib/stringify.js +0 -7
  91. package/src/lib/stringify.js.map +0 -1
  92. package/src/lib/to-error.d.ts +0 -4
  93. package/src/lib/to-error.d.ts.map +0 -1
  94. package/src/lib/to-error.js +0 -24
  95. package/src/lib/to-error.js.map +0 -1
package/.eslintrc.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": ["../../.eslintrc.json"],
3
+ "ignorePatterns": ["!**/*"],
4
+ "parserOptions": {
5
+ "project": "tsconfig.lint.json",
6
+ "tsconfigRootDir": "packages/util-ts"
7
+ },
8
+ "rules": {
9
+ // Block node dependencies so this library is usable in the browser.
10
+ "no-restricted-imports": ["error", { "patterns": ["node:*"] }]
11
+ }
12
+ }
package/jest.config.ts ADDED
@@ -0,0 +1,18 @@
1
+ export default {
2
+ coveragePathIgnorePatterns: [],
3
+ coverageThreshold: {
4
+ global: {
5
+ branches: 100,
6
+ functions: 100,
7
+ lines: 100,
8
+ statements: 100,
9
+ },
10
+ },
11
+ displayName: "util-ts",
12
+ moduleFileExtensions: ["ts", "js"],
13
+ preset: "../../jest.preset.js",
14
+ setupFiles: ["../../jest.setup.js"],
15
+ transform: {
16
+ "^.+\\.[tj]s$": ["ts-jest", { tsconfig: "<rootDir>/tsconfig.spec.json" }],
17
+ },
18
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clipboard-health/util-ts",
3
- "version": "2.2.5",
3
+ "version": "2.2.7",
4
4
  "main": "./src/index.js",
5
5
  "publishConfig": {
6
6
  "access": "restricted"
@@ -9,7 +9,5 @@
9
9
  "build": "nx build util-ts",
10
10
  "lint": "nx lint util-ts",
11
11
  "test": "nx test util-ts"
12
- },
13
- "type": "commonjs",
14
- "types": "./src/index.d.ts"
12
+ }
15
13
  }
package/project.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "util-ts",
3
+ "$schema": "../../node_modules/nx/schemas/project-schema.json",
4
+ "projectType": "library",
5
+ "sourceRoot": "packages/util-ts/src",
6
+ "tags": ["scope:no-external-imports"],
7
+ "targets": {
8
+ "build": {
9
+ "executor": "@nx/js:tsc",
10
+ "options": {
11
+ "assets": ["packages/util-ts/*.md"],
12
+ "main": "packages/util-ts/src/index.js",
13
+ "outputPath": "dist/packages/util-ts",
14
+ "tsConfig": "packages/util-ts/tsconfig.lib.json"
15
+ },
16
+ "outputs": ["{options.outputPath}"]
17
+ },
18
+ "lint": {
19
+ "executor": "@nx/eslint:lint",
20
+ "options": {
21
+ "maxWarnings": 0
22
+ }
23
+ },
24
+ "test": {
25
+ "executor": "@nx/jest:jest",
26
+ "options": {
27
+ "jestConfig": "packages/util-ts/jest.config.ts",
28
+ "passWithNoTests": false
29
+ },
30
+ "outputs": ["{projectRoot}/coverage"]
31
+ }
32
+ }
33
+ }
@@ -13,4 +13,3 @@ export * from "./lib/null-to-undefined";
13
13
  export * from "./lib/option";
14
14
  export * from "./lib/stringify";
15
15
  export * from "./lib/to-error";
16
- //# sourceMappingURL=index.d.ts.map
@@ -1,3 +1,3 @@
1
1
  export type AttributeValue = boolean | Date | number | string | Uint8Array | undefined;
2
+
2
3
  export type Attributes = Record<string, AttributeValue>;
3
- //# sourceMappingURL=attributes.d.ts.map
@@ -0,0 +1,30 @@
1
+ import { CbhError } from "./cbh-error";
2
+
3
+ describe("CbhError", () => {
4
+ it("returns proper defaults", () => {
5
+ const message = "boom";
6
+ const statusCode = 500;
7
+
8
+ const error = new CbhError({ message, statusCode });
9
+
10
+ expect(error.name).toBe("CbhError");
11
+ expect(error.message).toBe(message);
12
+ expect(error.issues[0]?.statusCode).toBe(statusCode);
13
+ });
14
+
15
+ it("sets the cause", () => {
16
+ const cause = new Error("cause");
17
+
18
+ const error = new CbhError({ message: "boom", cause });
19
+
20
+ expect(error.cause).toBe(cause);
21
+ });
22
+
23
+ it("accpets an array", () => {
24
+ const message = "boom";
25
+
26
+ const error = new CbhError([{ message }]);
27
+
28
+ expect(error.message).toBe(message);
29
+ });
30
+ });
@@ -0,0 +1,26 @@
1
+ import { type OneOrNonEmptyArray, toNonEmptyArray } from "./non-empty-array";
2
+
3
+ const ERROR_STATUS_CODES = [400, 401, 403, 404, 409, 422, 429, 500] as const;
4
+ export type ErrorStatusCode = (typeof ERROR_STATUS_CODES)[number];
5
+
6
+ export interface CbhIssue {
7
+ cause?: Error;
8
+ id?: string;
9
+ message: string;
10
+ statusCode?: ErrorStatusCode;
11
+ }
12
+
13
+ export class CbhError extends Error {
14
+ public readonly issues: CbhIssue[];
15
+
16
+ constructor(issues: OneOrNonEmptyArray<CbhIssue>) {
17
+ const is = toNonEmptyArray(issues);
18
+ const first = is[0];
19
+ super(first?.message, { cause: first?.cause });
20
+ // See https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#example
21
+ Object.setPrototypeOf(this, new.target.prototype);
22
+
23
+ this.issues = is;
24
+ this.name = "CbhError";
25
+ }
26
+ }
@@ -0,0 +1,25 @@
1
+ import { CbhError } from "./cbh-error";
2
+ import { toErrorCbhResponse, toSuccessCbhResponse } from "./cbh-response";
3
+
4
+ describe("toErrorCbhResponse", () => {
5
+ it("returns error", () => {
6
+ const message = "boom";
7
+
8
+ const response = toErrorCbhResponse({ message });
9
+
10
+ expect(response).toEqual({
11
+ success: false,
12
+ error: new CbhError({ message }),
13
+ });
14
+ });
15
+ });
16
+
17
+ describe("toSuccessCbhResponse", () => {
18
+ it("returns success", () => {
19
+ const message = "success";
20
+
21
+ const response = toSuccessCbhResponse({ message });
22
+
23
+ expect(response).toEqual({ success: true, data: { message } });
24
+ });
25
+ });
@@ -0,0 +1,15 @@
1
+ import { CbhError, type CbhIssue } from "./cbh-error";
2
+ import type { OneOrNonEmptyArray } from "./non-empty-array";
3
+
4
+ export type CbhResponse<T> = { success: true; data: T } | { success: false; error: CbhError };
5
+
6
+ export function toErrorCbhResponse(issues: OneOrNonEmptyArray<CbhIssue>): {
7
+ success: false;
8
+ error: CbhError;
9
+ } {
10
+ return { success: false, error: new CbhError(issues) };
11
+ }
12
+
13
+ export function toSuccessCbhResponse<T>(data: T): { success: true; data: T } {
14
+ return { success: true, data };
15
+ }
@@ -0,0 +1,35 @@
1
+ import { isDefined, isNullOrUndefined } from "./defined-utils";
2
+
3
+ const DEFINED_TEST_CASES = ["gandalf", " ", "", 42, 0, -0];
4
+
5
+ describe("isNullOrUndefined", () => {
6
+ it.each(DEFINED_TEST_CASES)("returns false for (%s)", (value) => {
7
+ expect(isNullOrUndefined(value)).toBe(false);
8
+ });
9
+
10
+ it("returns true if null", () => {
11
+ // eslint-disable-next-line unicorn/no-null
12
+ expect(isNullOrUndefined(null)).toBe(true);
13
+ });
14
+
15
+ it("returns true if undefined", () => {
16
+ // eslint-disable-next-line unicorn/no-useless-undefined
17
+ expect(isNullOrUndefined(undefined)).toBe(true);
18
+ });
19
+ });
20
+
21
+ describe("isDefined", () => {
22
+ it.each(DEFINED_TEST_CASES)("returns true for (%s)", (value) => {
23
+ expect(isDefined(value)).toBe(true);
24
+ });
25
+
26
+ it("returns false if undefined", () => {
27
+ // eslint-disable-next-line unicorn/no-null
28
+ expect(isDefined(null)).toBe(false);
29
+ });
30
+
31
+ it("returns false if null", () => {
32
+ // eslint-disable-next-line unicorn/no-useless-undefined
33
+ expect(isDefined(undefined)).toBe(false);
34
+ });
35
+ });
@@ -2,18 +2,23 @@
2
2
  * We specifically want to guard against both `null` and `undefined`
3
3
  * In this one case, we need to define `null` as an expected argument type.
4
4
  */
5
+ // eslint-disable-next-line @typescript-eslint/ban-types
5
6
  type NullOrUndefined = null | undefined;
7
+
6
8
  /**
7
9
  * Checks whether a value is null or undefined. If it is not defined, the return type ensures type safety.
8
10
  * @param value any value or null or undefined
9
11
  * @returns true if `value` is null or undefined, false otherwise.
10
12
  */
11
- export declare function isNullOrUndefined<T>(value: T | NullOrUndefined): value is NullOrUndefined;
13
+ export function isNullOrUndefined<T>(value: T | NullOrUndefined): value is NullOrUndefined {
14
+ return value === null || value === undefined;
15
+ }
16
+
12
17
  /**
13
18
  * Checks whether a value is defined. If it is defined, the return type ensures type safety.
14
19
  * @param value any value or null or undefined
15
20
  * @returns true if `value` is defined (not null and not undefined), false otherwise.
16
21
  */
17
- export declare function isDefined<T>(value: T | NullOrUndefined): value is T;
18
- export {};
19
- //# sourceMappingURL=defined-utils.d.ts.map
22
+ export function isDefined<T>(value: T | NullOrUndefined): value is T {
23
+ return !isNullOrUndefined(value);
24
+ }
@@ -0,0 +1,11 @@
1
+ import { delay } from "./delay";
2
+
3
+ describe("delay", () => {
4
+ it("should wait for the specified time", async () => {
5
+ const start = Date.now();
6
+
7
+ await delay(10);
8
+
9
+ expect(Date.now() - start).toBeGreaterThanOrEqual(10);
10
+ });
11
+ });
@@ -0,0 +1,5 @@
1
+ export async function delay(milliseconds: number): Promise<void> {
2
+ await new Promise((resolve) => {
3
+ setTimeout(resolve, milliseconds);
4
+ });
5
+ }
@@ -0,0 +1,23 @@
1
+ import * as E from "./either";
2
+
3
+ describe("Either", () => {
4
+ it("left works", () => {
5
+ const error = new Error("boom");
6
+ const either = E.left(error) as E.Left<Error>;
7
+
8
+ expect(E.isLeft(either)).toBe(true);
9
+ expect(E.isRight(either)).toBe(false);
10
+ expect(either.isRight).toBe(false);
11
+ expect(either.left).toBe(error);
12
+ });
13
+
14
+ it("right works", () => {
15
+ const value = "my-value";
16
+ const either = E.right(value) as E.Right<string>;
17
+
18
+ expect(E.isRight(either)).toBe(true);
19
+ expect(E.isLeft(either)).toBe(false);
20
+ expect(either.isRight).toBe(true);
21
+ expect(either.right).toBe(value);
22
+ });
23
+ });
@@ -1,11 +1,13 @@
1
1
  export interface Left<E> {
2
- isRight: false;
3
- left: E;
2
+ isRight: false;
3
+ left: E;
4
4
  }
5
+
5
6
  export interface Right<A> {
6
- isRight: true;
7
- right: A;
7
+ isRight: true;
8
+ right: A;
8
9
  }
10
+
9
11
  /**
10
12
  * A value of either type `Left<E>` or type `Right<A>`; a disjoint union.
11
13
  *
@@ -13,20 +15,31 @@ export interface Right<A> {
13
15
  * information. Convention dictates that `Left<E>` is used for failure and `Right<A>` for success.
14
16
  */
15
17
  export type Either<E, A> = Left<E> | Right<A>;
18
+
16
19
  /**
17
20
  * Constructs an `Either` holding a `Left<E>` value, usually representing a failure.
18
21
  */
19
- export declare function left<E, A = never>(left: E): Either<E, A>;
22
+ export function left<E, A = never>(left: E): Either<E, A> {
23
+ return { isRight: false, left };
24
+ }
25
+
20
26
  /**
21
27
  * Constructs an `Either` holding a `Right<A>` value, usually representing a success.
22
28
  */
23
- export declare function right<A, E = never>(right: A): Either<E, A>;
29
+ export function right<A, E = never>(right: A): Either<E, A> {
30
+ return { isRight: true, right };
31
+ }
32
+
24
33
  /**
25
34
  * Returns `true` if the either is `Left<E>`, `false` otherwise.
26
35
  */
27
- export declare function isLeft<E, A>(either: Either<E, A>): either is Left<E>;
36
+ export function isLeft<E, A>(either: Either<E, A>): either is Left<E> {
37
+ return !either.isRight;
38
+ }
39
+
28
40
  /**
29
41
  * Returns `true` if the either is `Right<A>`, `false` otherwise.
30
42
  */
31
- export declare function isRight<E, A>(either: Either<E, A>): either is Right<A>;
32
- //# sourceMappingURL=either.d.ts.map
43
+ export function isRight<E, A>(either: Either<E, A>): either is Right<A> {
44
+ return either.isRight;
45
+ }
@@ -0,0 +1,8 @@
1
+ import { forceCast } from "./force-cast";
2
+
3
+ describe("forceCast", () => {
4
+ it("doesn't actually change type", () => {
5
+ const a: number = forceCast<number>("a");
6
+ expect(typeof a).toBe("string");
7
+ });
8
+ });
@@ -0,0 +1,4 @@
1
+ /** Force cast to the provided type. */
2
+ export function forceCast<T>(value: unknown): T {
3
+ return value as T;
4
+ }
@@ -0,0 +1,21 @@
1
+ import { head } from "./head";
2
+
3
+ describe("head", () => {
4
+ it("returns head of list", () => {
5
+ expect(head([1, 2])).toBe(1);
6
+ });
7
+
8
+ it("returns undefined if empty list", () => {
9
+ // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
10
+ expect(head([])).toBeUndefined();
11
+ });
12
+
13
+ it("returns undefined if passed", () => {
14
+ expect(head()).toBeUndefined();
15
+ });
16
+
17
+ it("returns value if not an array", () => {
18
+ const value = { hi: "there" };
19
+ expect(head(value)).toEqual(value);
20
+ });
21
+ });
@@ -0,0 +1,5 @@
1
+ export type OneOrArray<T> = T | T[];
2
+
3
+ export function head<T>(value?: OneOrArray<T>): T | undefined {
4
+ return Array.isArray(value) ? value[0] : (value ?? undefined);
5
+ }
@@ -0,0 +1,17 @@
1
+ import { identity } from "./identity";
2
+
3
+ describe("identity", () => {
4
+ it("returns the provided input", () => {
5
+ expect(identity(1)).toBe(1);
6
+ expect(identity("text")).toBe("text");
7
+ expect(identity({})).toStrictEqual({});
8
+ });
9
+
10
+ it("should correctly infer the type", () => {
11
+ const a = 1;
12
+ expect(typeof a).toBe("number");
13
+
14
+ const b = identity(a);
15
+ expect(typeof b).toBe("number");
16
+ });
17
+ });
@@ -8,5 +8,6 @@
8
8
  * @param {T} value - The value to return unchanged.
9
9
  * @returns {T} The input value, unchanged.
10
10
  */
11
- export declare function identity<T>(value: T): T;
12
- //# sourceMappingURL=identity.d.ts.map
11
+ export function identity<T>(value: T): T {
12
+ return value;
13
+ }
@@ -0,0 +1,11 @@
1
+ import { isString } from "./is-string";
2
+
3
+ describe("isString", () => {
4
+ it("returns true", () => {
5
+ expect(isString("hi")).toBe(true);
6
+ });
7
+
8
+ it("returns false", () => {
9
+ expect(isString({})).toBe(false);
10
+ });
11
+ });
@@ -0,0 +1,3 @@
1
+ export function isString(value: unknown): value is string {
2
+ return typeof value === "string" || value instanceof String;
3
+ }
@@ -0,0 +1,7 @@
1
+ export type NonEmptyArray<T> = [T, ...T[]];
2
+
3
+ export type OneOrNonEmptyArray<T> = T | NonEmptyArray<T>;
4
+
5
+ export function toNonEmptyArray<T>(value: OneOrNonEmptyArray<T>): NonEmptyArray<T> {
6
+ return Array.isArray(value) ? value : [value];
7
+ }
@@ -0,0 +1,33 @@
1
+ import { nullToUndefined } from "./null-to-undefined";
2
+
3
+ describe("nullToUndefined", () => {
4
+ it("returns undefined", async () => {
5
+ // eslint-disable-next-line unicorn/no-null
6
+ expect(await nullToUndefined(Promise.resolve(null))).toBeUndefined();
7
+ });
8
+
9
+ it("returns value", async () => {
10
+ const expected = "hi";
11
+
12
+ expect(await nullToUndefined(Promise.resolve(expected))).toBe(expected);
13
+ });
14
+
15
+ it("supports PromiseLike objects", async () => {
16
+ const expected = "hello";
17
+
18
+ const promiseLike: PromiseLike<string> = {
19
+ // eslint-disable-next-line unicorn/no-thenable
20
+ then: function <TResult1, TResult2>(
21
+ onfulfilled?: ((value: string) => TResult1 | PromiseLike<TResult1>) | undefined,
22
+ ): PromiseLike<TResult1 | TResult2> {
23
+ if (onfulfilled) {
24
+ return Promise.resolve(onfulfilled(expected));
25
+ }
26
+
27
+ return Promise.reject();
28
+ },
29
+ };
30
+
31
+ expect(await nullToUndefined(promiseLike)).toBe(expected);
32
+ });
33
+ });
@@ -7,5 +7,7 @@
7
7
  *
8
8
  * @param value A promise or a promise-like object.
9
9
  */
10
- export declare function nullToUndefined<T>(value: PromiseLike<T | null>): Promise<T | undefined>;
11
- //# sourceMappingURL=null-to-undefined.d.ts.map
10
+ // eslint-disable-next-line @typescript-eslint/ban-types
11
+ export async function nullToUndefined<T>(value: PromiseLike<T | null>): Promise<T | undefined> {
12
+ return (await value) ?? undefined;
13
+ }
@@ -0,0 +1,24 @@
1
+ import * as O from "./option";
2
+
3
+ const { none, some } = O;
4
+
5
+ describe("Option", () => {
6
+ it("none is not some", () => {
7
+ const option = none;
8
+
9
+ expect(O.isNone(option)).toBe(true);
10
+ expect(O.isSome(option)).toBe(false);
11
+ expect(option.isSome).toBe(false);
12
+ });
13
+
14
+ it("some is some", () => {
15
+ const value = "my-value";
16
+
17
+ const option = some(value);
18
+
19
+ expect(O.isNone(option)).toBe(false);
20
+ expect(O.isSome(option)).toBe(true);
21
+ expect(option.isSome).toBe(true);
22
+ expect(option.value).toBe(value);
23
+ });
24
+ });
@@ -1,28 +1,39 @@
1
1
  export interface None {
2
- isSome: false;
2
+ isSome: false;
3
3
  }
4
+
4
5
  export interface Some<A> {
5
- isSome: true;
6
- value: A;
6
+ isSome: true;
7
+ value: A;
7
8
  }
9
+
8
10
  /**
9
11
  * An optional value. If the value exists, it's of type `Some<A>`, otherwise it's of type `None`.
10
12
  */
11
13
  export type Option<A> = None | Some<A>;
14
+
12
15
  /**
13
16
  * Constructs an `Option` of `None`, representing a missing value.
14
17
  */
15
- export declare const none: None;
18
+ export const none: None = { isSome: false };
19
+
16
20
  /**
17
21
  * Constructs an `Option` holding a `Some<A>`, representing an optional value that exists.
18
22
  */
19
- export declare function some<A>(value: A): Some<A>;
23
+ export function some<A>(value: A): Some<A> {
24
+ return { isSome: true, value };
25
+ }
26
+
20
27
  /**
21
28
  * Returns `true` if the option is `None`, `false` otherwise.
22
29
  */
23
- export declare function isNone<A>(option: Option<A>): option is None;
30
+ export function isNone<A>(option: Option<A>): option is None {
31
+ return !option.isSome;
32
+ }
33
+
24
34
  /**
25
35
  * Returns `true` if the option is `Some<A>`, `false` otherwise.
26
36
  */
27
- export declare function isSome<A>(option: Option<A>): option is Some<A>;
28
- //# sourceMappingURL=option.d.ts.map
37
+ export function isSome<A>(option: Option<A>): option is Some<A> {
38
+ return option.isSome;
39
+ }
@@ -0,0 +1,11 @@
1
+ import { stringify } from "./stringify";
2
+
3
+ describe("stringify", () => {
4
+ test.each([
5
+ [1n, '"1"'],
6
+ [{ a: 1 }, '{"a":1}'],
7
+ [{ a: 1n }, '{"a":"1"}'],
8
+ ])("stringify(%i, %i)", (value, expected) => {
9
+ expect(stringify(value)).toBe(expected);
10
+ });
11
+ });
@@ -0,0 +1,5 @@
1
+ export function stringify(value: unknown): string {
2
+ return JSON.stringify(value, (_, value: unknown) =>
3
+ typeof value === "bigint" ? String(value) : value,
4
+ );
5
+ }
@@ -0,0 +1,28 @@
1
+ import { stringify } from "./stringify";
2
+ import { toErrorMessage } from "./to-error";
3
+
4
+ const MESSAGE = "hi";
5
+
6
+ describe("toErrorMessage", () => {
7
+ it("returns message", () => {
8
+ expect(toErrorMessage(new Error(MESSAGE))).toEqual(MESSAGE);
9
+ });
10
+
11
+ it("returns message if non-error object", () => {
12
+ const error = { hello: MESSAGE };
13
+
14
+ expect(toErrorMessage(error)).toEqual(stringify(error));
15
+ });
16
+
17
+ it("returns message if unable to stringify", () => {
18
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
+ const circularReference = { a: { b: "c" }, d: {} } as any;
20
+ circularReference.a.d = circularReference.a;
21
+
22
+ expect(toErrorMessage(circularReference)).toBe("[object Object]");
23
+ });
24
+
25
+ it("returns message if string", () => {
26
+ expect(toErrorMessage(MESSAGE)).toEqual(MESSAGE);
27
+ });
28
+ });
@@ -0,0 +1,20 @@
1
+ import { isString } from "./is-string";
2
+ import { stringify } from "./stringify";
3
+
4
+ export function isError(error: unknown): error is Error {
5
+ return error instanceof Error;
6
+ }
7
+
8
+ export function toError(error: unknown): Error {
9
+ if (isError(error)) return error;
10
+
11
+ try {
12
+ return new Error(isString(error) ? error : stringify(error));
13
+ } catch {
14
+ return new Error(String(error));
15
+ }
16
+ }
17
+
18
+ export function toErrorMessage(error: unknown): string {
19
+ return toError(error).message;
20
+ }