diff --git a/.changeset/soft-bears-invent.md b/.changeset/soft-bears-invent.md index be46693..71389b0 100644 --- a/.changeset/soft-bears-invent.md +++ b/.changeset/soft-bears-invent.md @@ -1,5 +1,6 @@ --- -"ftld": minor +"ftld": major --- -fix types, simplify types +- Simplify Result/Task/Option types +- Remove `Collection` diff --git a/README.md b/README.md index 0756aa8..7949355 100644 --- a/README.md +++ b/README.md @@ -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. @@ -883,35 +882,6 @@ function doSomething(): AsyncTask { } ``` -## 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. diff --git a/lib/src/option.ts b/lib/src/option.ts index dc13c68..0347d9f 100644 --- a/lib/src/option.ts +++ b/lib/src/option.ts @@ -48,16 +48,14 @@ export class Option { * Creates a Some instance of Option. */ static Some(value: A): Option { - // @ts-expect-error - return new _Option(SOME, value); + return new Option(SOME, value); } /** * Creates a None instance of Option. */ static None(): Option { - // @ts-expect-error - return new _Option(NONE); + return new Option(NONE); } /** diff --git a/lib/src/result.ts b/lib/src/result.ts index 1f7c5f5..2c4e912 100644 --- a/lib/src/result.ts +++ b/lib/src/result.ts @@ -17,7 +17,7 @@ export class Result { static Ok(value: A): Result; static Ok(value?: A): Result { // @ts-expect-error - return new _Result(OK, value); + return new Result(OK, value); } /** * Creates an Err variant of the Result. @@ -26,7 +26,7 @@ export class Result { static Err(error: E): Result; static Err(error?: E): Result { // @ts-expect-error - return new _Result(ERR, error); + return new Result(ERR, error); } /** * Creates a Result based on a predicate function. diff --git a/sandbox/src/index.cjs b/sandbox/src/index.cjs index 76b2d8d..f6a45ce 100644 --- a/sandbox/src/index.cjs +++ b/sandbox/src/index.cjs @@ -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); diff --git a/sandbox/src/index.mjs b/sandbox/src/index.mjs index d3829bd..e271115 100644 --- a/sandbox/src/index.mjs +++ b/sandbox/src/index.mjs @@ -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); diff --git a/sandbox/src/index.ts b/sandbox/src/index.ts index d3829bd..e271115 100644 --- a/sandbox/src/index.ts +++ b/sandbox/src/index.ts @@ -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);