Skip to content

Commit

Permalink
make it a major
Browse files Browse the repository at this point in the history
  • Loading branch information
cevr committed Mar 26, 2024
1 parent 60ce813 commit 1e5518b
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 62 deletions.
5 changes: 3 additions & 2 deletions .changeset/soft-bears-invent.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
"ftld": minor
"ftld": major
---

fix types, simplify types
- Simplify Result/Task/Option types
- Remove `Collection`
30 changes: 0 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pnpm install ftld
- `Option`
- `Result`
- `Task`
- `Collection`

> Note: every collection method can take both an array and an object as input. The output type will be inferred based on the input type.
Expand Down Expand Up @@ -883,35 +882,6 @@ function doSomething(): AsyncTask<SomeError | AnotherError, number> {
}
```

## Collection

`Collection` is a set of utilities for working with collections. It allows for doing things like `map`, `filter`, `some`, `every` and `filterMap` on a lists or objects.

- `reduce` - reduces a collection to a single value.
- `map` - maps a collection to a new collection.
- `filter` - filters a collection to a new collection, automatically discards `None` or `Err` values. If the collection is an object, the keys will be preserved, mapped to an `Option` value.
- `filterMap` - maps a collection to a new collection, automatically discards `None` or `Err` values. If the collection is an object, the keys will be preserved, mapped to an `Option` value.
- `some` - returns true if any of the values in the collection are true, automatically discards `None` or `Err` values.
- `every` - returns true if all of the values in the collection are true, automatically discards `None` or `Err` values.

```ts
import { Collection, Option } from "ftld";

const list = [1, 2, 3, 4, 5];
const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 };

const listMap = Collection.map(list, (n) => n + 1);
const objMap = Collection.map(obj, (n) => n + 1);
const listReduce = Collection.reduce(list, (acc, n) => acc + n, 0); // 15
const objReduce = Collection.reduce(obj, (acc, n) => acc + n, 0); // 15

const listFilter = Collection.filter(list, (n) => n % 2 === 0); // [2, 4]
const objFilter = Collection.filter(obj, (n) => n % 2 === 0); // { a: Option.None(), b: Option.Some(2), c: Option.None(), d: Option.Some(4), e: Option.None() }

const results = [Option.Some(1), Option.None(), Option.Some(2), Option.None()];
const listFilterMap = Collection.filterMap(results, (n) => n + 1); // [2, 3]
```

## Recipes

Here's a list of useful utilities, but don't justify an increase in bundle size.
Expand Down
6 changes: 2 additions & 4 deletions lib/src/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,14 @@ export class Option<A> {
* Creates a Some instance of Option.
*/
static Some<A>(value: A): Option<A> {
// @ts-expect-error
return new _Option(SOME, value);
return new Option(SOME, value);
}

/**
* Creates a None instance of Option.
*/
static None<A = never>(): Option<A> {
// @ts-expect-error
return new _Option(NONE);
return new Option(NONE);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Result<E, A> {
static Ok<A>(value: A): Result<never, A>;
static Ok<A>(value?: A): Result<never, A> {
// @ts-expect-error
return new _Result(OK, value);
return new Result(OK, value);
}
/**
* Creates an Err variant of the Result.
Expand All @@ -26,7 +26,7 @@ export class Result<E, A> {
static Err<E>(error: E): Result<E, never>;
static Err<E>(error?: E): Result<E, never> {
// @ts-expect-error
return new _Result(ERR, error);
return new Result(ERR, error);
}
/**
* Creates a Result based on a predicate function.
Expand Down
11 changes: 3 additions & 8 deletions sandbox/src/index.cjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
const { Result, Option, Task, Do, Collection } = require("ftld");
const { Result, Option, Task, Do } = require("ftld");

const obj = {
a: 1,
b: 2,
};
const t = Do(function* ($) {
const a = yield* $(Task.from(() => 1));
const b = yield* $(Result.from(() => 2));
const c = yield* $(Option.from(3));
const d = Collection.reduce(obj, (acc, a) => acc + a, 0);

return a + b + c + d;
return a + b + c;
});

const x = t.run();
const res = x.unwrap();
if (res !== 9) throw new Error("Expected 9, got " + res);
if (res !== 6) throw new Error("Expected 6, got " + res);
11 changes: 3 additions & 8 deletions sandbox/src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { Result, Option, Task, Do, Collection } from "ftld";
import { Result, Option, Task, Do } from "ftld";

const obj = {
a: 1,
b: 2,
};
const t = Do(function* ($) {
const a = yield* $(Task.from(() => 1));
const b = yield* $(Result.from(() => 2));
const c = yield* $(Option.from(3));
const d = Collection.reduce(obj, (acc, a) => acc + a, 0);

return a + b + c + d;
return a + b + c;
});

const x = t.run();
const res = x.unwrap();
if (res !== 9) throw new Error("Expected 9, got " + res);
if (res !== 6) throw new Error("Expected 6, got " + res);
11 changes: 3 additions & 8 deletions sandbox/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { Result, Option, Task, Do, Collection } from "ftld";
import { Result, Option, Task, Do } from "ftld";

const obj = {
a: 1,
b: 2,
};
const t = Do(function* ($) {
const a = yield* $(Task.from(() => 1));
const b = yield* $(Result.from(() => 2));
const c = yield* $(Option.from(3));
const d = Collection.reduce(obj, (acc, a) => acc + a, 0);

return a + b + c + d;
return a + b + c;
});

const x = t.run();
const res = x.unwrap();
if (res !== 9) throw new Error("Expected 9, got " + res);
if (res !== 6) throw new Error("Expected 6, got " + res);

0 comments on commit 1e5518b

Please sign in to comment.