diff --git a/identity/README.md b/identity/README.md index adaf30e..2329512 100644 --- a/identity/README.md +++ b/identity/README.md @@ -97,6 +97,7 @@ function isIdentity(value: unknown | Identity): value is Identity; ```typescript const value: unknown = 2; + if (isIdentity(value)) { // ... value is Identity at this block } diff --git a/identity/index.ts b/identity/index.ts index 1de7bd6..2a7b3ba 100644 --- a/identity/index.ts +++ b/identity/index.ts @@ -7,6 +7,7 @@ import type { Container } from "@sweet-monads/interfaces"; + function isWrappedFunction(m: Identity | Identity<(a: A) => B>): m is Identity<(a: A) => B> { return typeof m.value === "function"; } @@ -47,6 +48,7 @@ export default class Identity implements AsyncMonad, Container { apply(this: Identity<(a: A) => B>, arg: Identity): Identity; apply(this: Identity, fn: Identity<(a: A) => B>): Identity; + apply(this: Identity | Identity<(a: A) => B>, argOrFn: Identity | Identity<(a: A) => B>): Identity { if (isWrappedFunction(this)) { return (argOrFn as Identity).map(this.value as (a: A) => B); @@ -54,6 +56,7 @@ export default class Identity implements AsyncMonad, Container { if (isWrappedFunction(argOrFn)) { return (argOrFn as Identity<(a: A) => B>).apply(this as Identity); } + throw new Error("Some of the arguments should be a function"); } @@ -63,7 +66,7 @@ export default class Identity implements AsyncMonad, Container { this: Identity | A> | Identity<(a: A) => Promise>, argOrFn: Identity | A> | Identity<(a: A) => Promise> ): Promise> { - if (isWrappedAsyncFunction(this)) { + if (isWrappedFunction(this)) { return (argOrFn as Identity | A>) .map(a => Promise.resolve(a)) .asyncMap(pa => pa.then(this.value as (a: A) => Promise)); diff --git a/identity/package.json b/identity/package.json index 19cd3fb..b3b0bac 100644 --- a/identity/package.json +++ b/identity/package.json @@ -1,7 +1,7 @@ { "name": "@sweet-monads/identity", "version": "3.2.0", - "description": "", + "description": "Identity monad", "main": "./cjs/index.js", "module": "./esm/index.js", "exports": { diff --git a/tests/identity.test.ts b/tests/identity.test.ts index 45a3496..9ff90ea 100644 --- a/tests/identity.test.ts +++ b/tests/identity.test.ts @@ -12,4 +12,3 @@ describe("Identity", () => { expect(file.unwrap()).toBe("etc/hosts"); }); }); -