Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
rametta committed Jun 5, 2024
1 parent df420d4 commit 03b16dc
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 282 deletions.
63 changes: 20 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,6 @@ Table of Contents
- [collectResult](#collectResult)
- [collectMaybe](#collectMaybe)

### Changes from V1 to V2

If you are migrating from Pratica V1 to V2. Here is a small list of changes made:

- `Maybe()` utility was renamed to `nullable()`
- `.default(() => 'value')` was renamed to `.alt('value')` and does not require a function to be passed in, just a value.

That's it. Enjoy.

### Monads

#### Maybe
Expand Down Expand Up @@ -182,12 +173,10 @@ Inspect is used for seeing a string respresentation of the Maybe. It is used mos
```js
import { nullable } from "pratica"

const { log } = console

log(nullable(86).inspect()) // `Just(86)`
log(nullable("HELLO").inspect()) // `Just('HELLO')`
log(nullable(null).inspect()) // `Nothing`
log(nullable(undefined).inspect()) // `Nothing`
nullable(86).inspect() // `Just(86)`
nullable("HELLO").inspect() // `Just('HELLO')`
nullable(null).inspect() // `Nothing`
nullable(undefined).inspect() // `Nothing`
```

##### Maybe.cata
Expand Down Expand Up @@ -238,10 +227,8 @@ import { Just, Nothing } from "pratica"

const isOver6Feet = (height) => (height > 6 ? Just(height) : Nothing)

const { log } = console

log(isOver6Feet(7).isJust()) // true
log(isOver6Feet(4).isJust()) // false
isOver6Feet(7).isJust() // true
isOver6Feet(4).isJust() // false
```

##### Maybe.isNothing
Expand All @@ -253,10 +240,8 @@ import { Just, Nothing } from "pratica"

const isOver6Feet = (height) => (height > 6 ? Just(height) : Nothing)

const { log } = console

log(isOver6Feet(7).isNothing()) // false
log(isOver6Feet(4).isNothing()) // true
isOver6Feet(7).isNothing() // false
isOver6Feet(4).isNothing() // true
```

##### Maybe.value
Expand All @@ -268,10 +253,8 @@ import { Just, Nothing } from "pratica"

const isOver6Feet = (height) => (height > 6 ? Just(height) : Nothing)

const { log } = console

log(isOver6Feet(7).value()) // 7
log(isOver6Feet(4).value()) // undefined
isOver6Feet(7).value() // 7
isOver6Feet(4).value() // undefined
```

#### Result
Expand Down Expand Up @@ -418,12 +401,10 @@ Ok(null) // no function to apply
```js
import { Ok, Err } from "pratica"

const { log } = console

log(Ok(86).inspect()) // `Ok(86)`
log(Ok("HELLO").inspect()) // `Ok('HELLO')`
log(Err("Something happened").inspect()) // `Err('Something happened')`
log(Err(404).inspect()) // `Err(404)`
Ok(86).inspect() // `Ok(86)`
Ok("HELLO").inspect() // `Ok('HELLO')`
Err("Something happened").inspect() // `Err('Something happened')`
Err(404).inspect() // `Err(404)`
```

##### Result.cata
Expand Down Expand Up @@ -470,10 +451,8 @@ import { Ok, Err } from "pratica"

const isOver6Feet = (height) => (height > 6 ? Ok(height) : Err("Shorty"))

const { log } = console

log(isOver6Feet(7).isOk()) // true
log(isOver6Feet(4).isOk()) // false
isOver6Feet(7).isOk() // true
isOver6Feet(4).isOk() // false
```

##### Result.isErr
Expand All @@ -483,10 +462,8 @@ import { Ok, Err } from "pratica"

const isOver6Feet = (height) => (height > 6 ? Ok(height) : Err("Shorty"))

const { log } = console

log(isOver6Feet(7).isErr()) // false
log(isOver6Feet(4).isErr()) // true
isOver6Feet(7).isErr() // false
isOver6Feet(4).isErr() // true
```

##### Result.value
Expand All @@ -499,8 +476,8 @@ import { Ok, Err } from "pratica"
const six = Ok(6).value()
const error = Err("Something happened").value()

log(six) // 6
log(error) // 'Something happened'
console.log(six) // 6
console.log(error) // 'Something happened'
```

### Utilities
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"test": "vitest",
"build": "bun run rollup -c",
"deploy": "bun run build && bun run publish",
"pretty": "prettier --check 'src/**/*.ts'",
"pretty-fix": "prettier --write 'src/**/*.ts'"
"pretty": "prettier --check '{src,specs}/**/*.ts'",
"pretty-fix": "prettier --write '{src,specs}/**/*.ts'"
}
}
108 changes: 54 additions & 54 deletions specs/maybe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { describe, it, expect } from "vitest";
import { nullable } from "../src/maybe";
import { describe, it, expect } from "vitest"
import { nullable } from "../src/maybe"

describe("Maybe", () => {
it("should be a functor and implement map", () => {
nullable("hello")
.map((x) => `${x} world`)
.map((x) => expect(x).toEqual("hello world"));
});
.map((x) => expect(x).toEqual("hello world"))
})

it("should handle nullable data", () => {
expect(nullable(null).isNothing()).toEqual(true);
expect(nullable(null).isJust()).toEqual(false);
expect(nullable("some data").isNothing()).toEqual(false);
expect(nullable("some data").isJust()).toEqual(true);
});
expect(nullable(null).isNothing()).toEqual(true)
expect(nullable(null).isJust()).toEqual(false)
expect(nullable("some data").isNothing()).toEqual(false)
expect(nullable("some data").isJust()).toEqual(true)
})

it("should implement chain", () => {
nullable("hello")
Expand All @@ -22,74 +22,74 @@ describe("Maybe", () => {
.cata({
Just: (x) => expect(x).toBe("hello world"),
Nothing: () => {
throw new Error();
throw new Error()
},
});
})

nullable(null)
.chain((x) => nullable(x))
.map((x) => x + " world")
.cata({
Just: () => {
throw new Error();
throw new Error()
},
Nothing: () => {},
});
})

const x = nullable("hello")
.chain((x) => nullable(null))
.map((x) => x + " world")
.map((x) => expect(x).toEqual("hello world "));
.map((x) => expect(x).toEqual("hello world "))

expect(x.isNothing()).toBe(true);
expect(x.isJust()).toBe(false);
});
expect(x.isNothing()).toBe(true)
expect(x.isJust()).toBe(false)
})

it("should implement cata", () => {
nullable("hello")
.map((x) => x + " world")
.cata({
Just: (x) => expect(x).toBe("hello world"),
Nothing: () => {
throw new Error();
throw new Error()
},
});
})

nullable(null)
.map(() => {
throw new Error();
throw new Error()
})
.chain(() => {
throw new Error();
throw new Error()
})
.cata({
Just: () => {
throw new Error();
throw new Error()
},
Nothing: () => {},
});
});
})
})

it("should return a default value if nothing", () => {
nullable(null)
.alt("some default")
.cata({
Just: (x) => expect(x).toBe("some default"),
Nothing: () => {
throw new Error();
throw new Error()
},
});
})

nullable("some data")
.chain((data) => nullable(null))
.alt("some default")
.cata({
Just: (x) => expect(x).toBe("some default"),
Nothing: () => {
throw new Error();
throw new Error()
},
});
});
})
})

it(`should ignore the default if it's not nothing`, () => {
nullable({ name: "jason" })
Expand All @@ -98,69 +98,69 @@ describe("Maybe", () => {
.cata({
Just: (x) => expect(x).toBe("jason"),
Nothing: () => {
throw new Error();
throw new Error()
},
});
});
})
})

it("should inspect properly", () => {
expect(nullable("hello").inspect()).toBe("Just(hello)");
expect(nullable(null).inspect()).toBe("Nothing");
});
expect(nullable("hello").inspect()).toBe("Just(hello)")
expect(nullable(null).inspect()).toBe("Nothing")
})

it("should apply 2 monads with ap", () => {
const add = (x: number) => (y: number) => x + y;
const one = nullable(1);
const two = nullable(2);
const add = (x: number) => (y: number) => x + y
const one = nullable(1)
const two = nullable(2)

nullable(add)
.ap(one)
.ap(two)
.cata({
Just: (x) => expect(x).toBe(3),
Nothing: () => {
throw new Error();
throw new Error()
},
});
})

nullable(null)
.ap(one)
.ap(two)
.cata({
Just: () => {
throw new Error();
throw new Error()
},
Nothing: () => {},
});
});
})
})

it("should convert Maybe to Result", (ctx) => {
nullable(6)
.toResult()
.cata({
Ok: (x) => expect(x).toBe(6),
Err: () => {
throw new Error();
throw new Error()
},
});
})

nullable()
.toResult()
.cata({
Ok: () => {
throw new Error();
throw new Error()
},
Err: () => {},
});
});
})
})

it("should get inner value", () => {
expect(nullable(6).value()).toBe(6);
expect(nullable(null).value()).toBe(undefined);
expect(nullable(6).value()).toBe(6)
expect(nullable(null).value()).toBe(undefined)
expect(
nullable({ foo: "bar" })
.map((v) => v.foo)
.value()
).toBe("bar");
});
});
.value(),
).toBe("bar")
})
})
Loading

0 comments on commit 03b16dc

Please sign in to comment.