Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identity monads #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions identity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function isIdentity<T>(value: unknown | Identity<T>): value is Identity<T>;

```typescript
const value: unknown = 2;

if (isIdentity(value)) {
// ... value is Identity<unknown> at this block
}
Expand Down
5 changes: 4 additions & 1 deletion identity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
Container
} from "@sweet-monads/interfaces";


function isWrappedFunction<A, B>(m: Identity<A> | Identity<(a: A) => B>): m is Identity<(a: A) => B> {
return typeof m.value === "function";
}
Expand Down Expand Up @@ -47,13 +48,15 @@ export default class Identity<T> implements AsyncMonad<T>, Container<T> {

apply<A, B>(this: Identity<(a: A) => B>, arg: Identity<A>): Identity<B>;
apply<A, B>(this: Identity<A>, fn: Identity<(a: A) => B>): Identity<B>;

apply<A, B>(this: Identity<A> | Identity<(a: A) => B>, argOrFn: Identity<A> | Identity<(a: A) => B>): Identity<B> {
if (isWrappedFunction(this)) {
return (argOrFn as Identity<A>).map(this.value as (a: A) => B);
}
if (isWrappedFunction(argOrFn)) {
return (argOrFn as Identity<(a: A) => B>).apply(this as Identity<A>);
}

throw new Error("Some of the arguments should be a function");
}

Expand All @@ -63,7 +66,7 @@ export default class Identity<T> implements AsyncMonad<T>, Container<T> {
this: Identity<Promise<A> | A> | Identity<(a: A) => Promise<B>>,
argOrFn: Identity<Promise<A> | A> | Identity<(a: A) => Promise<B>>
): Promise<Identity<B>> {
if (isWrappedAsyncFunction(this)) {
if (isWrappedFunction(this)) {
return (argOrFn as Identity<Promise<A> | A>)
.map(a => Promise.resolve(a))
.asyncMap(pa => pa.then(this.value as (a: A) => Promise<B>));
Expand Down
2 changes: 1 addition & 1 deletion identity/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
1 change: 0 additions & 1 deletion tests/identity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ describe("Identity", () => {
expect(file.unwrap()).toBe("etc/hosts");
});
});