diff --git a/array.ts b/array.ts index 8b4fb66..aa09e4e 100644 --- a/array.ts +++ b/array.ts @@ -13,9 +13,9 @@ import type { Combinable } from "./combinable.ts"; import type { Comparable } from "./comparable.ts"; import type { Either } from "./either.ts"; import type { Filterable } from "./filterable.ts"; -import type { Flatmappable } from "./flatmappable.ts"; +import type { Bind, Flatmappable, Tap } from "./flatmappable.ts"; import type { Initializable } from "./initializable.ts"; -import type { Mappable } from "./mappable.ts"; +import type { BindTo, Mappable } from "./mappable.ts"; import type { Option } from "./option.ts"; import type { Pair } from "./pair.ts"; import type { Foldable } from "./foldable.ts"; @@ -844,6 +844,15 @@ type Sequence[]> = $; +/** + * The return type of sequence for use with type inference. + * + * @since 2.0.0 + */ +export type SequenceArray = []>( + ...uas: US +) => Sequence; + /** * Sequence over an array of type V, inverting the relationship between V and * ReadonlyArray. This function also keeps the indexed types of in each V at @@ -1045,8 +1054,10 @@ export function updateAt( * * @since 2.0.0 */ -export function modify(modifyFn: (a: A) => A) { - return (index: number) => (arr: ReadonlyArray): ReadonlyArray => +export function modify( + modifyFn: (a: A) => A, +): (index: number) => (ua: ReadonlyArray) => ReadonlyArray { + return (index) => (arr) => isOutOfBounds(index, arr) ? arr : _unsafeUpdateAt(index, modifyFn(arr[index]), arr); @@ -1122,9 +1133,10 @@ export function lookup(index: number): (arr: ReadonlyArray) => Option { * * @since 2.0.0 */ -export function deleteAt(index: number) { - return (arr: ReadonlyArray): ReadonlyArray => - isOutOfBounds(index, arr) ? arr : _unsafeDeleteAt(index, arr); +export function deleteAt( + index: number, +): (ua: ReadonlyArray) => ReadonlyArray { + return (arr) => isOutOfBounds(index, arr) ? arr : _unsafeDeleteAt(index, arr); } /** @@ -1515,16 +1527,14 @@ export const WrappableArray: Wrappable = { wrap }; /** * @since 2.0.0 */ -export const tap: ( - fa: (value: A) => void, -) => (ua: ReadonlyArray) => ReadonlyArray = createTap(FlatmappableArray); +export const tap: Tap = createTap(FlatmappableArray); /** * @since 2.0.0 */ -export const bind = createBind(FlatmappableArray); +export const bind: Bind = createBind(FlatmappableArray); /** * @since 2.0.0 */ -export const bindTo = createBindTo(MappableArray); +export const bindTo: BindTo = createBindTo(MappableArray); diff --git a/async.ts b/async.ts index 1f049a0..638bdb1 100644 --- a/async.ts +++ b/async.ts @@ -11,8 +11,8 @@ import type { Kind, Out } from "./kind.ts"; import type { Applicable } from "./applicable.ts"; import type { Combinable } from "./combinable.ts"; import type { Initializable } from "./initializable.ts"; -import type { Flatmappable } from "./flatmappable.ts"; -import type { Mappable } from "./mappable.ts"; +import type { Bind, Flatmappable, Tap } from "./flatmappable.ts"; +import type { BindTo, Mappable } from "./mappable.ts"; import type { Sync } from "./sync.ts"; import type { Wrappable } from "./wrappable.ts"; @@ -167,14 +167,14 @@ export const FlatmappableAsyncSeq: Flatmappable = { /** * @since 2.0.0 */ -export const tap = createTap(FlatmappableAsync); +export const tap: Tap = createTap(FlatmappableAsync); /** * @since 2.0.0 */ -export const bind = createBind(FlatmappableAsync); +export const bind: Bind = createBind(FlatmappableAsync); /** * @since 2.0.0 */ -export const bindTo = createBindTo(MappableAsync); +export const bindTo: BindTo = createBindTo(MappableAsync); diff --git a/async_either.ts b/async_either.ts index bef5465..b31929d 100644 --- a/async_either.ts +++ b/async_either.ts @@ -14,10 +14,10 @@ import type { Async } from "./async.ts"; import type { Bimappable } from "./bimappable.ts"; import type { Combinable } from "./combinable.ts"; import type { Either } from "./either.ts"; -import type { Failable } from "./failable.ts"; -import type { Flatmappable } from "./flatmappable.ts"; +import type { Failable, Tap } from "./failable.ts"; +import type { Bind, Flatmappable } from "./flatmappable.ts"; import type { Initializable } from "./initializable.ts"; -import type { Mappable } from "./mappable.ts"; +import type { BindTo, Mappable } from "./mappable.ts"; import type { Wrappable } from "./wrappable.ts"; import * as E from "./either.ts"; @@ -483,14 +483,18 @@ export const WrappableAsyncEither: Wrappable = { /** * @since 2.0.0 */ -export const tap = createTap(FailableAsyncEitherParallel); +export const tap: Tap = createTap(FailableAsyncEitherParallel); /** * @since 2.0.0 */ -export const bind = createBind(FlatmappableAsyncEitherParallel); +export const bind: Bind = createBind( + FlatmappableAsyncEitherParallel, +); /** * @since 2.0.0 */ -export const bindTo = createBindTo(FlatmappableAsyncEitherParallel); +export const bindTo: BindTo = createBindTo( + FlatmappableAsyncEitherParallel, +); diff --git a/async_iterable.ts b/async_iterable.ts index b42fc3d..0edf7ae 100644 --- a/async_iterable.ts +++ b/async_iterable.ts @@ -8,15 +8,15 @@ import type { Kind, Out } from "./kind.ts"; import type { Applicable } from "./applicable.ts"; -import type { Wrappable } from "./wrappable.ts"; +import type { Either } from "./either.ts"; import type { Filterable } from "./filterable.ts"; -import type { Mappable } from "./mappable.ts"; +import type { Bind, Flatmappable, Tap } from "./flatmappable.ts"; +import type { BindTo, Mappable } from "./mappable.ts"; import type { Option } from "./option.ts"; -import type { Flatmappable } from "./flatmappable.ts"; -import type { Either } from "./either.ts"; import type { Pair } from "./pair.ts"; import type { Predicate } from "./predicate.ts"; import type { Refinement } from "./refinement.ts"; +import type { Wrappable } from "./wrappable.ts"; import { createBind, createTap } from "./flatmappable.ts"; import { createBindTo } from "./mappable.ts"; @@ -429,14 +429,18 @@ export const WrappableAsyncIterable: Wrappable = { wrap }; /** * @since 2.0.0 */ -export const tap = createTap(FlatmappableAsyncIterable); +export const tap: Tap = createTap(FlatmappableAsyncIterable); /** * @since 2.0.0 */ -export const bind = createBind(FlatmappableAsyncIterable); +export const bind: Bind = createBind( + FlatmappableAsyncIterable, +); /** * @since 2.0.0 */ -export const bindTo = createBindTo(MappableAsyncIterable); +export const bindTo: BindTo = createBindTo( + MappableAsyncIterable, +); diff --git a/combinable.ts b/combinable.ts index 7d134f5..427d7b7 100644 --- a/combinable.ts +++ b/combinable.ts @@ -327,8 +327,10 @@ export function min(sortable: Sortable): Combinable { * * @since 2.0.0 */ -export function intercalcate(middle: A) { - return ({ combine }: Combinable): Combinable => +export function intercalcate( + middle: A, +): (C: Combinable) => Combinable { + return ({ combine }) => fromCombine((second) => (first) => combine(second)(combine(middle)(first))); } diff --git a/contrib/free.ts b/contrib/free.ts index 98d6cd7..047838e 100644 --- a/contrib/free.ts +++ b/contrib/free.ts @@ -80,13 +80,13 @@ export function isLink(ua: Free): ua is Link { export function match( onNode: (value: A) => O, onLink: (first: Free, second: Free) => O, -) { - return (ta: Free): O => { - switch (ta.tag) { +): (ua: Free) => O { + return (ua) => { + switch (ua.tag) { case "Node": - return onNode(ta.value); + return onNode(ua.value); case "Link": - return onLink(ta.first, ta.second); + return onLink(ua.first, ua.second); } }; } diff --git a/contrib/generator.ts b/contrib/generator.ts deleted file mode 100644 index efc6b47..0000000 --- a/contrib/generator.ts +++ /dev/null @@ -1,21 +0,0 @@ -import type { In, Kind, Out } from "../kind.ts"; -import type { Flatmappable } from "../flatmappable.ts"; - -import { todo } from "../fn.ts"; - -export interface KindGenerator extends Kind { - readonly kind: Generator, Out, In>; -} - -export const FlatmappableGenerator: Flatmappable = { - wrap: (a) => { - const ret = function* () { - yield a; - return undefined; - }; - return ret; - }, - map: todo(), - apply: todo(), - flatmap: todo(), -}; diff --git a/contrib/most.ts b/contrib/most.ts index 0aafd57..1d00253 100644 --- a/contrib/most.ts +++ b/contrib/most.ts @@ -1,9 +1,9 @@ -import type { Scheduler, Stream } from "npm:@most/types@1.1.0"; import type { $, Kind, Nest, Out } from "../kind.ts"; -import type { Wrappable } from "../wrappable.ts"; -import type { Mappable } from "../mappable.ts"; import type { Applicable } from "../applicable.ts"; -import type { Flatmappable } from "../flatmappable.ts"; +import type { Bind, Flatmappable, Tap } from "../flatmappable.ts"; +import type { BindTo, Mappable } from "../mappable.ts"; +import type { Scheduler, Stream } from "npm:@most/types@1.1.0"; +import type { Wrappable } from "../wrappable.ts"; import * as M from "npm:@most/core@1.6.1"; import { createBind, createTap } from "../flatmappable.ts"; @@ -96,11 +96,11 @@ export const FlatmappableStream: Flatmappable = { flatmap, }; -export const bind = createBind(FlatmappableStream); +export const bind: Bind = createBind(FlatmappableStream); -export const bindTo = createBindTo(FlatmappableStream); +export const bindTo: BindTo = createBindTo(FlatmappableStream); -export const tap = createTap(FlatmappableStream); +export const tap: Tap = createTap(FlatmappableStream); export interface TransformStream extends Kind { readonly kind: Stream>; diff --git a/datum.ts b/datum.ts index f45cad3..1ef6488 100644 --- a/datum.ts +++ b/datum.ts @@ -9,13 +9,12 @@ import type { $, Kind, Out } from "./kind.ts"; import type { Applicable } from "./applicable.ts"; import type { Combinable } from "./combinable.ts"; -import type { Filterable } from "./filterable.ts"; -import type { Mappable } from "./mappable.ts"; -import type { Wrappable } from "./wrappable.ts"; import type { Comparable } from "./comparable.ts"; import type { Either } from "./either.ts"; -import type { Flatmappable } from "./flatmappable.ts"; +import type { Filterable } from "./filterable.ts"; +import type { Bind, Flatmappable, Tap } from "./flatmappable.ts"; import type { Initializable } from "./initializable.ts"; +import type { BindTo, Mappable } from "./mappable.ts"; import type { Option } from "./option.ts"; import type { Pair } from "./pair.ts"; import type { Predicate } from "./predicate.ts"; @@ -23,6 +22,7 @@ import type { Refinement } from "./refinement.ts"; import type { Showable } from "./showable.ts"; import type { Sortable } from "./sortable.ts"; import type { Traversable } from "./traversable.ts"; +import type { Wrappable } from "./wrappable.ts"; import * as O from "./option.ts"; import { createBind, createTap } from "./flatmappable.ts"; @@ -218,17 +218,17 @@ export function match( onPending: () => B, onReplete: (a: A) => B, onRefresh: (a: A) => B, -) { - return (ma: Datum): B => { - switch (ma.tag) { +): (ua: Datum) => B { + return (ua) => { + switch (ua.tag) { case "Initial": return onInitial(); case "Pending": return onPending(); case "Refresh": - return onRefresh(ma.value); + return onRefresh(ua.value); case "Replete": - return onReplete(ma.value); + return onReplete(ua.value); } }; } @@ -236,8 +236,8 @@ export function match( /** * @since 2.0.0 */ -export function getOrElse(onNone: () => A) { - return match(onNone, onNone, identity, identity); +export function getOrElse(onNone: () => A): (ua: Datum) => A { + return match(onNone, onNone, identity, identity); } /** @@ -565,14 +565,14 @@ export const FilterableDatum: Filterable = { /** * @since 2.0.0 */ -export const tap = createTap(FlatmappableDatum); +export const tap: Tap = createTap(FlatmappableDatum); /** * @since 2.0.0 */ -export const bind = createBind(FlatmappableDatum); +export const bind: Bind = createBind(FlatmappableDatum); /** * @since 2.0.0 */ -export const bindTo = createBindTo(MappableDatum); +export const bindTo: BindTo = createBindTo(MappableDatum); diff --git a/decoder.ts b/decoder.ts index dfb9a6b..9a3a3e4 100644 --- a/decoder.ts +++ b/decoder.ts @@ -16,11 +16,11 @@ import type { Applicable } from "./applicable.ts"; import type { Combinable } from "./combinable.ts"; import type { Composable } from "./composable.ts"; import type { Either } from "./either.ts"; -import type { Flatmappable } from "./flatmappable.ts"; +import type { Bind, Flatmappable, Tap } from "./flatmappable.ts"; import type { FnEither } from "./fn_either.ts"; import type { Forest } from "./tree.ts"; import type { Literal, Schemable } from "./schemable.ts"; -import type { Mappable } from "./mappable.ts"; +import type { BindTo, Mappable } from "./mappable.ts"; import type { Predicate } from "./predicate.ts"; import type { Premappable } from "./premappable.ts"; import type { ReadonlyRecord } from "./record.ts"; @@ -1469,7 +1469,7 @@ export function union( * * @since 2.0.0 */ -export const _null = literal(null); +export const _null: Decoder = literal(null); /** * A decoder combinator that modifies an existing decoder to accept null as an @@ -1499,7 +1499,7 @@ export function nullable( * * @since 2.0.0 */ -export const _undefined = literal(undefined); +export const _undefined: Decoder = literal(undefined); /** * A decoder combinator that modifies an existing decoder to accept undefined @@ -1915,14 +1915,14 @@ export const WrappableDecoder: Wrappable = { wrap }; /** * @since 2.0.0 */ -export const tap = createTap(FlatmappableDecoder); +export const tap: Tap = createTap(FlatmappableDecoder); /** * @since 2.0.0 */ -export const bind = createBind(FlatmappableDecoder); +export const bind: Bind = createBind(FlatmappableDecoder); /** * @since 2.0.0 */ -export const bindTo = createBindTo(MappableDecoder); +export const bindTo: BindTo = createBindTo(MappableDecoder); diff --git a/deno.json b/deno.json index bb8ef7b..dbec945 100644 --- a/deno.json +++ b/deno.json @@ -2,59 +2,58 @@ "name": "@baetheus/fun", "version": "2.0.1", "exports": { - "applicable": "./applicable.ts", - "array": "./array.ts", - "async": "./async.ts", - "async_either": "./async_either.ts", - "async_iterable": "./async_iterable.ts", - "bimappable": "./bimappable.ts", - "boolean": "./boolean.ts", - "combinable": "./combinable.ts", - "comparable": "./comparable.ts", - "composable": "./composable.ts", - "datum": "./datum.ts", - "decoder": "./decoder.ts", - "either": "./either.ts", - "failable": "./failable.ts", - "filterable": "./filterable.ts", - "flatmappable": "./flatmappable.ts", - "fn": "./fn.ts", - "fn_either": "./fn_either.ts", - "foldable": "./foldable.ts", - "identity": "./identity.ts", - "initializable": "./initializable.ts", - "iterable": "./iterable.ts", - "json_schema": "./json_schema.ts", - "kind": "./kind.ts", - "map": "./map.ts", - "mappable": "./mappable.ts", - "newtype": "./newtype.ts", - "nil": "./nil.ts", - "number": "./number.ts", - "optic": "./optic.ts", - "option": "./option.ts", - "pair": "./pair.ts", - "predicate": "./predicate.ts", - "premappable": "./premappable.ts", - "promise": "./promise.ts", - "record": "./record.ts", - "refinement": "./refinement.ts", - "schemable": "./schemable.ts", - "set": "./set.ts", - "showable": "./showable.ts", - "sortable": "./sortable.ts", - "state": "./state.ts", - "string": "./string.ts", - "sync": "./sync.ts", - "sync_either": "./sync_either.ts", - "these": "./these.ts", - "traversable": "./traversable.ts", - "tree": "./tree.ts", - "wrappable": "./wrappable.ts", - "contrib/fast-check": "./contrib/fast-check.ts", - "contrib/free": "./contrib/free.ts", - "contrib/generator": "./contrib/generator.ts", - "contrib/most": "./contrib/most.ts" + "./applicable": "./applicable.ts", + "./array": "./array.ts", + "./async": "./async.ts", + "./async_either": "./async_either.ts", + "./async_iterable": "./async_iterable.ts", + "./bimappable": "./bimappable.ts", + "./boolean": "./boolean.ts", + "./combinable": "./combinable.ts", + "./comparable": "./comparable.ts", + "./composable": "./composable.ts", + "./datum": "./datum.ts", + "./decoder": "./decoder.ts", + "./either": "./either.ts", + "./failable": "./failable.ts", + "./filterable": "./filterable.ts", + "./flatmappable": "./flatmappable.ts", + "./fn": "./fn.ts", + "./fn_either": "./fn_either.ts", + "./foldable": "./foldable.ts", + "./identity": "./identity.ts", + "./initializable": "./initializable.ts", + "./iterable": "./iterable.ts", + "./json_schema": "./json_schema.ts", + "./kind": "./kind.ts", + "./map": "./map.ts", + "./mappable": "./mappable.ts", + "./newtype": "./newtype.ts", + "./nil": "./nil.ts", + "./number": "./number.ts", + "./optic": "./optic.ts", + "./option": "./option.ts", + "./pair": "./pair.ts", + "./predicate": "./predicate.ts", + "./premappable": "./premappable.ts", + "./promise": "./promise.ts", + "./record": "./record.ts", + "./refinement": "./refinement.ts", + "./schemable": "./schemable.ts", + "./set": "./set.ts", + "./showable": "./showable.ts", + "./sortable": "./sortable.ts", + "./state": "./state.ts", + "./string": "./string.ts", + "./sync": "./sync.ts", + "./sync_either": "./sync_either.ts", + "./these": "./these.ts", + "./traversable": "./traversable.ts", + "./tree": "./tree.ts", + "./wrappable": "./wrappable.ts", + "./contrib/fast-check": "./contrib/fast-check.ts", + "./contrib/free": "./contrib/free.ts", + "./contrib/most": "./contrib/most.ts" }, "include": [ "LICENSE", diff --git a/either.ts b/either.ts index 9c741a7..a8ef4cc 100644 --- a/either.ts +++ b/either.ts @@ -13,12 +13,12 @@ import type { Applicable } from "./applicable.ts"; import type { Bimappable } from "./bimappable.ts"; import type { Combinable } from "./combinable.ts"; import type { Comparable } from "./comparable.ts"; -import type { Failable } from "./failable.ts"; +import type { Failable, Tap } from "./failable.ts"; import type { Filterable } from "./filterable.ts"; -import type { Flatmappable } from "./flatmappable.ts"; +import type { Bind, Flatmappable } from "./flatmappable.ts"; import type { Foldable } from "./foldable.ts"; import type { Initializable } from "./initializable.ts"; -import type { Mappable } from "./mappable.ts"; +import type { BindTo, Mappable } from "./mappable.ts"; import type { Pair } from "./pair.ts"; import type { Predicate } from "./predicate.ts"; import type { Refinement } from "./refinement.ts"; @@ -96,10 +96,10 @@ export function fail(b: B): Either { /** * @since 2.0.0 */ -export function fromNullable(fe: () => E) { - return ( - a: A, - ): Either> => (isNotNil(a) ? right(a) : left(fe())); +export function fromNullable( + fe: () => E, +): (value: A) => Either> { + return (a) => (isNotNil(a) ? right(a) : left(fe())); } /** @@ -146,8 +146,8 @@ export function match( /** * @since 2.0.0 */ -export function getOrElse(onLeft: (e: E) => A) { - return (ma: Either): A => isLeft(ma) ? onLeft(ma.left) : ma.right; +export function getOrElse(onLeft: (e: E) => A): (ua: Either) => A { + return (ua) => isLeft(ua) ? onLeft(ua.left) : ua.right; } /** @@ -507,14 +507,14 @@ export const WrappableEither: Wrappable = { wrap }; /** * @since 2.0.0 */ -export const tap = createTap(FailableEither); +export const tap: Tap = createTap(FailableEither); /** * @since 2.0.0 */ -export const bind = createBind(FlatmappableEither); +export const bind: Bind = createBind(FlatmappableEither); /** * @since 2.0.0 */ -export const bindTo = createBindTo(MappableEither); +export const bindTo: BindTo = createBindTo(MappableEither); diff --git a/failable.ts b/failable.ts index 2d29d94..c7a2f62 100644 --- a/failable.ts +++ b/failable.ts @@ -34,6 +34,16 @@ export interface Failable extends Flatmappable, Hold { ) => $; } +/** + * The return type of createTryAll for Failable. Useful for reducing type + * inference in the docs. + * + * @since 2.0.0 + */ +export type TryAll = ( + ...uas: NonEmptyArray<$> +) => $; + /** * Create a tryAll function from an instance of Failable. The tryAll function * allows the trying any number of Failable structures in order until a @@ -56,6 +66,19 @@ export function createTryAll( }; } +/** + * The return type of createTap for Failable. Useful for reducing type + * inference in the docs. + * + * @since 2.0.0 + */ +export type Tap = ( + onSuccess: (value: A) => void, + onFailure: (value: B) => void, +) => ( + ua: $, +) => $; + /** * Create a tap function for a structure with instances of Wrappable and * Flatmappable. A tap function allows one to break out of the functional diff --git a/flatmappable.ts b/flatmappable.ts index 616ead4..d7153a6 100644 --- a/flatmappable.ts +++ b/flatmappable.ts @@ -21,6 +21,18 @@ export interface Flatmappable extends Applicable, Hold { ) => $; } +/** + * The return type for the createTap function on Flatmappable. Useful to reduce + * type inference in documentation. + * + * @since 2.0.0 + */ +export type Tap = ( + fn: (value: A) => void, +) => ( + ua: $, +) => $; + /** * Create a tap function for a structure with instances of Wrappable and * Flatmappable. A tap function allows one to break out of the functional @@ -43,6 +55,32 @@ export function createTap( }); } +/** + * The return type for the createBind function on Flatmappable. Useful to reduce + * type inference in documentation. + * + * @since 2.0.0 + */ +export type Bind = < + N extends string, + A, + I, + J = never, + K = never, + L = unknown, + M = unknown, +>( + name: Exclude, + faui: (a: A) => $, +) => ( + ua: $, +) => $< + U, + [{ readonly [K in keyof A | N]: K extends keyof A ? A[K] : I }, B | J, C | K], + [D & L], + [M] +>; + /** * Create a bind function for a structure with instances of Mappable and * Flatmappable. A bind function allows one to flatmap into named fields in a diff --git a/fn.ts b/fn.ts index fd2d97e..87b6f3f 100644 --- a/fn.ts +++ b/fn.ts @@ -10,8 +10,8 @@ import type { In, Kind, Out } from "./kind.ts"; import type { Applicable } from "./applicable.ts"; import type { Composable } from "./composable.ts"; -import type { Flatmappable } from "./flatmappable.ts"; -import type { Mappable } from "./mappable.ts"; +import type { Bind, Flatmappable, Tap } from "./flatmappable.ts"; +import type { BindTo, Mappable } from "./mappable.ts"; import type { Premappable } from "./premappable.ts"; import type { Wrappable } from "./wrappable.ts"; @@ -903,14 +903,14 @@ export const WrappableFn: Wrappable = { wrap }; /** * @since 2.0.0 */ -export const tap = createTap(FlatmappableFn); +export const tap: Tap = createTap(FlatmappableFn); /** * @since 2.0.0 */ -export const bind = createBind(FlatmappableFn); +export const bind: Bind = createBind(FlatmappableFn); /** * @since 2.0.0 */ -export const bindTo = createBindTo(MappableFn); +export const bindTo: BindTo = createBindTo(MappableFn); diff --git a/fn_either.ts b/fn_either.ts index 05b8aa6..e4f063b 100644 --- a/fn_either.ts +++ b/fn_either.ts @@ -13,10 +13,10 @@ import type { Bimappable } from "./bimappable.ts"; import type { Combinable } from "./combinable.ts"; import type { Composable } from "./composable.ts"; import type { Either } from "./either.ts"; -import type { Failable } from "./failable.ts"; -import type { Flatmappable } from "./flatmappable.ts"; +import type { Failable, Tap } from "./failable.ts"; +import type { Bind, Flatmappable } from "./flatmappable.ts"; import type { Fn } from "./fn.ts"; -import type { Mappable } from "./mappable.ts"; +import type { BindTo, Mappable } from "./mappable.ts"; import type { Predicate } from "./predicate.ts"; import type { Premappable } from "./premappable.ts"; import type { Refinement } from "./refinement.ts"; @@ -740,14 +740,14 @@ export const WrappableFnEither: Wrappable = { wrap }; /** * @since 2.0.0 */ -export const tap = createTap(FailableFnEither); +export const tap: Tap = createTap(FailableFnEither); /** * @since 2.0.0 */ -export const bind = createBind(FlatmappableFnEither); +export const bind: Bind = createBind(FlatmappableFnEither); /** * @since 2.0.0 */ -export const bindTo = createBindTo(MappableFnEither); +export const bindTo: BindTo = createBindTo(MappableFnEither); diff --git a/initializable.ts b/initializable.ts index 5bbbb8a..4c0db1b 100644 --- a/initializable.ts +++ b/initializable.ts @@ -142,8 +142,10 @@ export function dual(M: Initializable): Initializable { * * @since 2.0.0 */ -export function intercalcate(middle: A) { - return ({ combine, init }: Initializable): Initializable => ({ +export function intercalcate( + middle: A, +): (I: Initializable) => Initializable { + return ({ combine, init }) => ({ combine: (second) => combine(combine(second)(middle)), init, }); diff --git a/iterable.ts b/iterable.ts index ce658ef..2f54fb3 100644 --- a/iterable.ts +++ b/iterable.ts @@ -23,10 +23,10 @@ import type { Applicable } from "./applicable.ts"; import type { Combinable } from "./combinable.ts"; import type { Either } from "./either.ts"; import type { Filterable } from "./filterable.ts"; -import type { Flatmappable } from "./flatmappable.ts"; +import type { Bind, Flatmappable, Tap } from "./flatmappable.ts"; import type { Foldable } from "./foldable.ts"; import type { Initializable } from "./initializable.ts"; -import type { Mappable } from "./mappable.ts"; +import type { BindTo, Mappable } from "./mappable.ts"; import type { Option } from "./option.ts"; import type { Pair } from "./pair.ts"; import type { Predicate } from "./predicate.ts"; @@ -176,8 +176,8 @@ export function forEach(fa: (a: A) => void): (ta: Iterable) => void { export function fold( foldr: (accumulator: O, value: A) => O, initial: O, -) { - return (ua: Iterable): O => { +): (ua: Iterable) => O { + return (ua) => { let out = initial; for (const a of ua) { out = foldr(out, a); @@ -480,14 +480,14 @@ export const WrappableIterable: Wrappable = { /** * @since 2.0.0 */ -export const tap = createTap(FlatmappableIterable); +export const tap: Tap = createTap(FlatmappableIterable); /** * @since 2.0.0 */ -export const bind = createBind(FlatmappableIterable); +export const bind: Bind = createBind(FlatmappableIterable); /** * @since 2.0.0 */ -export const bindTo = createBindTo(FlatmappableIterable); +export const bindTo: BindTo = createBindTo(FlatmappableIterable); diff --git a/json_schema.ts b/json_schema.ts index 5d3a079..36a2308 100644 --- a/json_schema.ts +++ b/json_schema.ts @@ -11,8 +11,8 @@ import type { Kind, Out } from "./kind.ts"; import type { Literal, Schemable, TupleSchemable } from "./schemable.ts"; import type { Flatmappable } from "./flatmappable.ts"; -import type { NonEmptyArray } from "./array.ts"; -import type { ReadonlyRecord } from "./record.ts"; +import type { NonEmptyArray, SequenceArray } from "./array.ts"; +import type { ReadonlyRecord, SequenceRecord } from "./record.ts"; import type { Combinable } from "./combinable.ts"; import type { State } from "./state.ts"; @@ -217,12 +217,16 @@ export const FlatmappableJsonBuilder = FlatmappableState as Flatmappable< /** * @since 2.0.0 */ -export const sequenceArray = sequenceA(FlatmappableJsonBuilder); +export const sequenceArray: SequenceArray = sequenceA( + FlatmappableJsonBuilder, +); /** * @since 2.0.0 */ -export const sequenceRecord = sequenceR(FlatmappableJsonBuilder); +export const sequenceRecord: SequenceRecord = sequenceR( + FlatmappableJsonBuilder, +); /** * @since 2.0.0 diff --git a/kind.ts b/kind.ts index de44800..e38bd25 100644 --- a/kind.ts +++ b/kind.ts @@ -118,7 +118,8 @@ export interface Fix extends Kind { /** * Create a scoped symbol for use with Hold. */ -declare const HoldSymbol: unique symbol; +const HoldSymbol = Symbol("Hold"); + /** * The Hold interface allows one to trick the typescript compiler into holding diff --git a/map.ts b/map.ts index 635d2f8..d4c6428 100644 --- a/map.ts +++ b/map.ts @@ -230,8 +230,8 @@ export function values(O: Sortable): (ta: ReadonlyMap) => A[] { export function fold( foldr: (accumulator: O, value: A, key: B) => O, initial: O, -) { - return (ua: ReadonlyMap): O => { +): (ua: ReadonlyMap) => O { + return (ua) => { let result = initial; for (const [key, value] of ua.entries()) { result = foldr(result, value, key); @@ -282,11 +282,9 @@ export function deleteAt( */ export function insert( S: Comparable, -) { +): (value: A) => (key: B) => (ua: ReadonlyMap) => ReadonlyMap { const _lookup = lookupWithKey(S); - return (value: A) => - (key: B) => - (map: ReadonlyMap): ReadonlyMap => + return (value) => (key) => (map) => pipe( _lookup(key)(map), O.match( @@ -312,7 +310,7 @@ export function insert( */ export function insertAt( S: Comparable, -) { +): (key: B) => (value: A) => (ua: ReadonlyMap) => ReadonlyMap { const _insert = insert(S); return (key: B) => (value: A) => _insert(value)(key); } diff --git a/mappable.ts b/mappable.ts index 73f9e64..f6b6149 100644 --- a/mappable.ts +++ b/mappable.ts @@ -21,6 +21,18 @@ export interface Mappable extends Hold { ) => $; } +/** + * The return type for the createBindTo function on Mappable. Useful to reduce + * type inference in documentation. + * + * @since 2.0.0 + */ +export type BindTo = ( + name: N, +) => ( + ua: $, +) => $; + /** * Create a bindTo function from a structure with an instance of Mappable. A * bindTo function takes the inner value of the structure and maps it to the diff --git a/newtype.ts b/newtype.ts index 57b24be..53f5d96 100644 --- a/newtype.ts +++ b/newtype.ts @@ -23,8 +23,8 @@ import { fromPredicate } from "./option.ts"; import { iso as _iso, prism as _prism } from "./optic.ts"; import { identity, unsafeCoerce } from "./fn.ts"; -declare const BrandSymbol: unique symbol; -declare const ValueSymbol: unique symbol; +const BrandSymbol = Symbol("Brand"); +const ValueSymbol = Symbol("Value"); /** * Create a branded type from an existing type. The branded diff --git a/nil.ts b/nil.ts index eb3bbc4..6b3f524 100644 --- a/nil.ts +++ b/nil.ts @@ -16,10 +16,10 @@ import type { Combinable } from "./combinable.ts"; import type { Comparable } from "./comparable.ts"; import type { Either } from "./either.ts"; import type { Filterable } from "./filterable.ts"; -import type { Flatmappable } from "./flatmappable.ts"; +import type { Bind, Flatmappable, Tap } from "./flatmappable.ts"; import type { Foldable } from "./foldable.ts"; import type { Initializable } from "./initializable.ts"; -import type { Mappable } from "./mappable.ts"; +import type { BindTo, Mappable } from "./mappable.ts"; import type { Option } from "./option.ts"; import type { Pair } from "./pair.ts"; import type { Predicate } from "./predicate.ts"; @@ -393,14 +393,14 @@ export const WrappableNil: Wrappable = { /** * @since 2.0.0 */ -export const tap = createTap(FlatmappableNil); +export const tap: Tap = createTap(FlatmappableNil); /** * @since 2.0.0 */ -export const bind = createBind(FlatmappableNil); +export const bind: Bind = createBind(FlatmappableNil); /** * @since 2.0.0 */ -export const bindTo = createBindTo(MappableNil); +export const bindTo: BindTo = createBindTo(MappableNil); diff --git a/option.ts b/option.ts index cc50966..dd2f570 100644 --- a/option.ts +++ b/option.ts @@ -13,9 +13,9 @@ import type { Combinable } from "./combinable.ts"; import type { Comparable } from "./comparable.ts"; import type { Either } from "./either.ts"; import type { Filterable } from "./filterable.ts"; -import type { Flatmappable } from "./flatmappable.ts"; +import type { Bind, Flatmappable, Tap } from "./flatmappable.ts"; import type { Initializable } from "./initializable.ts"; -import type { Mappable } from "./mappable.ts"; +import type { BindTo, Mappable } from "./mappable.ts"; import type { Pair } from "./pair.ts"; import type { Predicate } from "./predicate.ts"; import type { Foldable } from "./foldable.ts"; @@ -230,8 +230,11 @@ export function tryCatch( * * @since 2.0.0 */ -export function match(onNone: () => B, onSome: (a: A) => B) { - return (ta: Option): B => (isNone(ta) ? onNone() : onSome(ta.value)); +export function match( + onNone: () => B, + onSome: (a: A) => B, +): (ua: Option) => B { + return (ua) => (isNone(ua) ? onNone() : onSome(ua.value)); } /** @@ -250,8 +253,8 @@ export function match(onNone: () => B, onSome: (a: A) => B) { * * @since 2.0.0 */ -export function getOrElse(onNone: () => B) { - return (ta: Option): B => isNone(ta) ? onNone() : ta.value; +export function getOrElse(onNone: () => B): (ua: Option) => B { + return (ua) => isNone(ua) ? onNone() : ua.value; } /** @@ -864,14 +867,14 @@ export const WrappableOption: Wrappable = { wrap }; /** * @since 2.0.0 */ -export const tap = createTap(FlatmappableOption); +export const tap: Tap = createTap(FlatmappableOption); /** * @since 2.0.0 */ -export const bind = createBind(FlatmappableOption); +export const bind: Bind = createBind(FlatmappableOption); /** * @since 2.0.0 */ -export const bindTo = createBindTo(MappableOption); +export const bindTo: BindTo = createBindTo(MappableOption); diff --git a/pair.ts b/pair.ts index 3dbdd2d..47d7a82 100644 --- a/pair.ts +++ b/pair.ts @@ -382,7 +382,9 @@ export function fold( * * @since 2.0.0 */ -export function traverse(A: Applicable) { +export function traverse(A: Applicable): ( + favi: (a: A) => $, +) => (ua: Pair) => $, J, K], [L], [M]> { return ( favi: (a: A) => $, ): (ua: Pair) => $, J, K], [L], [M]> => diff --git a/predicate.ts b/predicate.ts index 618d50e..150c308 100644 --- a/predicate.ts +++ b/predicate.ts @@ -126,8 +126,10 @@ export function not(predicate: Predicate): Predicate { * * @since 2.0.0 */ -export function or(second: Predicate) { - return (first: Predicate): Predicate => (a) => first(a) || second(a); +export function or( + second: Predicate, +): (first: Predicate) => Predicate { + return (first) => (a) => first(a) || second(a); } /** @@ -154,8 +156,10 @@ export function or(second: Predicate) { * * @since 2.0.0 */ -export function and(second: Predicate) { - return (first: Predicate): Predicate => (a) => first(a) && second(a); +export function and( + second: Predicate, +): (first: Predicate) => Predicate { + return (first) => (a) => first(a) && second(a); } /** diff --git a/promise.ts b/promise.ts index 4836b27..4328dfa 100644 --- a/promise.ts +++ b/promise.ts @@ -11,8 +11,8 @@ import type { Applicable } from "./applicable.ts"; import type { Combinable } from "./combinable.ts"; import type { Either } from "./either.ts"; import type { Initializable } from "./initializable.ts"; -import type { Mappable } from "./mappable.ts"; -import type { Flatmappable } from "./flatmappable.ts"; +import type { BindTo, Mappable } from "./mappable.ts"; +import type { Bind, Flatmappable, Tap } from "./flatmappable.ts"; import type { Wrappable } from "./wrappable.ts"; import * as E from "./either.ts"; @@ -505,14 +505,14 @@ export const WrappablePromise: Wrappable = { wrap }; /** * @since 2.0.0 */ -export const tap = createTap(FlatmappablePromise); +export const tap: Tap = createTap(FlatmappablePromise); /** * @since 2.0.0 */ -export const bind = createBind(FlatmappablePromise); +export const bind: Bind = createBind(FlatmappablePromise); /** * @since 2.0.0 */ -export const bindTo = createBindTo(MappablePromise); +export const bindTo: BindTo = createBindTo(MappablePromise); diff --git a/record.ts b/record.ts index 37108d0..ab2d791 100644 --- a/record.ts +++ b/record.ts @@ -229,11 +229,11 @@ export function map( export function fold( foao: (o: O, a: A, i: string) => O, o: O, -) { - return (rec: ReadonlyRecord): O => { +): (ua: ReadonlyRecord) => O { + return (ua) => { let result = o; - for (const key in rec) { - result = foao(result, rec[key], key); + for (const key in ua) { + result = foao(result, ua[key], key); } return result; }; @@ -321,7 +321,7 @@ export function collapse( * const result2 = swapOption({ one: O.some(1), two: O.none }); // None * ``` * - * TODO: Revisit because mutability is bad here + * TODO: Revisit because mutability needs proof * @since 2.0.0 */ export function traverse( @@ -383,6 +383,17 @@ type Sequence>> = $; +/** + * The return type of sequence for use with type inference. + * + * @since 2.0.0 + */ +export type SequenceRecord = < + US extends ReadonlyRecord>, +>( + values: NonEmptyRecord, +) => Sequence; + /** * Sequence over an ReadonlyRecord of type V, inverting the relationship between V and * ReadonlyRecord. This function also keeps the indexed types of in each V at @@ -437,9 +448,10 @@ export function sequence( * * @since 2.0.0 */ -export function insert(value: A) { - return (key: string) => (rec: ReadonlyRecord): ReadonlyRecord => - rec[key] === value ? rec : { ...rec, [key]: value }; +export function insert( + value: A, +): (key: string) => (ua: ReadonlyRecord) => ReadonlyRecord { + return (key) => (ua) => ua[key] === value ? ua : { ...ua, [key]: value }; } /** @@ -467,9 +479,10 @@ export function insert(value: A) { * * @since 2.0.0 */ -export function insertAt(key: string) { - return (value: A) => (rec: ReadonlyRecord): ReadonlyRecord => - rec[key] === value ? rec : { ...rec, [key]: value }; +export function insertAt( + key: string, +): (value: A) => (ua: ReadonlyRecord) => ReadonlyRecord { + return (value) => (ua) => ua[key] === value ? ua : { ...ua, [key]: value }; } /** @@ -495,13 +508,15 @@ export function insertAt(key: string) { * * @since 2.0.0 */ -export function modify(modifyFn: (a: A) => A) { - return (key: string) => (rec: ReadonlyRecord): ReadonlyRecord => { - if (Object.hasOwn(rec, key)) { - const out = modifyFn(rec[key]); - return out === rec[key] ? rec : { ...rec, [key]: out }; +export function modify( + modifyFn: (a: A) => A, +): (key: string) => (ua: ReadonlyRecord) => ReadonlyRecord { + return (key) => (ua) => { + if (Object.hasOwn(ua, key)) { + const out = modifyFn(ua[key]); + return out === ua[key] ? ua : { ...ua, [key]: out }; } - return rec; + return ua; }; } @@ -530,14 +545,15 @@ export function modify(modifyFn: (a: A) => A) { * * @since 2.0.0 */ -export function modifyAt(key: string) { - return (modifyFn: (a: A) => A) => - (rec: ReadonlyRecord): ReadonlyRecord => { - if (Object.hasOwn(rec, key)) { - const out = modifyFn(rec[key]); - return out === rec[key] ? rec : { ...rec, [key]: out }; +export function modifyAt( + key: string, +): (modifyFn: (a: A) => A) => (ua: ReadonlyRecord) => ReadonlyRecord { + return (modifyFn) => (ua) => { + if (Object.hasOwn(ua, key)) { + const out = modifyFn(ua[key]); + return out === ua[key] ? ua : { ...ua, [key]: out }; } - return rec; + return ua; }; } @@ -565,9 +581,10 @@ export function modifyAt(key: string) { * * @since 2.0.0 */ -export function update(value: A) { - return (key: string) => (rec: ReadonlyRecord): ReadonlyRecord => - Object.hasOwn(rec, key) ? { ...rec, [key]: value } : rec; +export function update( + value: A, +): (key: string) => (ua: ReadonlyRecord) => ReadonlyRecord { + return (key) => (ua) => Object.hasOwn(ua, key) ? { ...ua, [key]: value } : ua; } /** @@ -595,9 +612,11 @@ export function update(value: A) { * * @since 2.0.0 */ -export function updateAt(key: string) { - return (value: A) => (rec: ReadonlyRecord): ReadonlyRecord => - Object.hasOwn(rec, key) ? { ...rec, [key]: value } : rec; +export function updateAt( + key: string, +): (value: A) => (ua: ReadonlyRecord) => ReadonlyRecord { + return (value) => (ua) => + Object.hasOwn(ua, key) ? { ...ua, [key]: value } : ua; } /** @@ -621,9 +640,8 @@ export function updateAt(key: string) { * * @since 2.0.0 */ -export function lookupAt(key: string) { - return (rec: ReadonlyRecord): Option => - Object.hasOwn(rec, key) ? some(rec[key]) : none; +export function lookupAt(key: string): (ua: ReadonlyRecord) => Option { + return (ua) => Object.hasOwn(ua, key) ? some(ua[key]) : none; } /** @@ -648,10 +666,12 @@ export function lookupAt(key: string) { * * @since 2.0.0 */ -export function lookupWithKey(key: string) { - return (record: ReadonlyRecord): Option> => { - if (Object.hasOwn(record, key)) { - return some([key, record[key]]); +export function lookupWithKey( + key: string, +): (ua: ReadonlyRecord) => Option> { + return (ua) => { + if (Object.hasOwn(ua, key)) { + return some([key, ua[key]]); } return none; }; @@ -680,16 +700,16 @@ export function lookupWithKey(key: string) { * * @since 2.0.0 */ -export function deleteAt(key: string) { - return ( - rec: ReadonlyRecord, - ): ReadonlyRecord => { - if (Object.hasOwn(rec, key)) { - const out = { ...rec }; +export function deleteAt( + key: string, +): (ua: ReadonlyRecord) => ReadonlyRecord { + return (ua) => { + if (Object.hasOwn(ua, key)) { + const out = { ...ua }; delete out[key]; return out; } - return rec; + return ua; }; } @@ -716,17 +736,17 @@ export function deleteAt(key: string) { * * @since 2.0.0 */ -export function deleteAtWithValue(key: string) { - return ( - rec: ReadonlyRecord, - ): Pair, Option> => { - if (Object.hasOwn(rec, key)) { - const out = { ...rec }; - const value = rec[key]; +export function deleteAtWithValue( + key: string, +): (ua: ReadonlyRecord) => Pair, Option> { + return (ua) => { + if (Object.hasOwn(ua, key)) { + const out = { ...ua }; + const value = ua[key]; delete out[key]; return [out, some(value)]; } - return [rec, none]; + return [ua, none]; }; } diff --git a/set.ts b/set.ts index bd4838f..49a224c 100644 --- a/set.ts +++ b/set.ts @@ -12,10 +12,10 @@ import type { Combinable } from "./combinable.ts"; import type { Comparable } from "./comparable.ts"; import type { Either } from "./either.ts"; import type { Filterable } from "./filterable.ts"; -import type { Flatmappable } from "./flatmappable.ts"; +import type { Bind, Flatmappable, Tap } from "./flatmappable.ts"; import type { Foldable } from "./foldable.ts"; import type { Initializable } from "./initializable.ts"; -import type { Mappable } from "./mappable.ts"; +import type { BindTo, Mappable } from "./mappable.ts"; import type { Option } from "./option.ts"; import type { Pair } from "./pair.ts"; import type { Predicate } from "./predicate.ts"; @@ -737,7 +737,9 @@ const unsafeAdd = (ua: ReadonlySet) => (a: A): Set => { */ export function traverse( A: Applicable, -) { +): ( + favi: (a: A) => $, +) => (ua: ReadonlySet) => $, J, K], [L], [M]> { return ( favi: (a: A) => $, ): (ua: ReadonlySet) => $, J, K], [L], [M]> => @@ -934,14 +936,14 @@ export const WrappableSet: Wrappable = { wrap }; /** * @since 2.0.0 */ -export const tap = createTap(FlatmappableSet); +export const tap: Tap = createTap(FlatmappableSet); /** * @since 2.0.0 */ -export const bind = createBind(FlatmappableSet); +export const bind: Bind = createBind(FlatmappableSet); /** * @since 2.0.0 */ -export const bindTo = createBindTo(MappableSet); +export const bindTo: BindTo = createBindTo(MappableSet); diff --git a/state.ts b/state.ts index 8033810..653e785 100644 --- a/state.ts +++ b/state.ts @@ -11,9 +11,9 @@ import type { InOut, Kind, Out } from "./kind.ts"; import type { Applicable } from "./applicable.ts"; import type { Combinable } from "./combinable.ts"; -import type { Flatmappable } from "./flatmappable.ts"; +import type { Bind, Flatmappable, Tap } from "./flatmappable.ts"; import type { Initializable } from "./initializable.ts"; -import type { Mappable } from "./mappable.ts"; +import type { BindTo, Mappable } from "./mappable.ts"; import type { Wrappable } from "./wrappable.ts"; import { flow } from "./fn.ts"; @@ -356,14 +356,14 @@ export const WrappableState: Wrappable = { wrap }; /** * @since 2.0.0 */ -export const tap = createTap(FlatmappableState); +export const tap: Tap = createTap(FlatmappableState); /** * @since 2.0.0 */ -export const bind = createBind(FlatmappableState); +export const bind: Bind = createBind(FlatmappableState); /** * @since 2.0.0 */ -export const bindTo = createBindTo(MappableState); +export const bindTo: BindTo = createBindTo(MappableState); diff --git a/string.ts b/string.ts index b0384dd..d1fcb23 100644 --- a/string.ts +++ b/string.ts @@ -168,10 +168,12 @@ export function length(a: string): number { * * @since 2.0.0 */ -export function split(separator: string | RegExp) { - return (s: string): NonEmptyArray => { - const out = s.split(separator); - return isNonEmpty(out) ? out : [s]; +export function split( + separator: string | RegExp, +): (str: string) => NonEmptyArray { + return (str) => { + const out = str.split(separator); + return isNonEmpty(out) ? out : [str]; }; } @@ -192,8 +194,11 @@ export function split(separator: string | RegExp) { * * @since 2.0.0 */ -export function includes(searchString: string, position?: number) { - return (s: string): boolean => s.includes(searchString, position); +export function includes( + searchString: string, + position?: number, +): (str: string) => boolean { + return (str) => str.includes(searchString, position); } /** @@ -216,9 +221,9 @@ export function includes(searchString: string, position?: number) { export function startsWith( searchString: T, position?: number, -) { - return (s: string): s is `${T}${string}` => - s.startsWith(searchString, position); +): (str: string) => str is `${T}${string}` { + return (str: string): str is `${T}${string}` => + str.startsWith(searchString, position); } /** @@ -238,9 +243,12 @@ export function startsWith( * * @since 2.0.0 */ -export function endsWith(searchString: T, position?: number) { - return (s: string): s is `${string}${T}` => - s.endsWith(searchString, position); +export function endsWith( + searchString: T, + position?: number, +): (str: string) => str is `${string}${T}` { + return (str: string): str is `${string}${T}` => + str.endsWith(searchString, position); } /** @@ -290,8 +298,11 @@ export function toLowerCase(a: string): string { * * @since 2.0.0 */ -export function replace(searchValue: string | RegExp, replaceValue: string) { - return (s: string): string => s.replace(searchValue, replaceValue); +export function replace( + searchValue: string | RegExp, + replaceValue: string, +): (str: string) => string { + return (str) => str.replace(searchValue, replaceValue); } /** @@ -394,8 +405,8 @@ export function plural( * * @since 2.0.0 */ -export function slice(start: number, end: number) { - return (a: string): string => a.slice(start, end); +export function slice(start: number, end: number): (str: string) => string { + return (str) => str.slice(start, end); } /** @@ -416,8 +427,10 @@ export function slice(start: number, end: number) { * * @since 2.0.0 */ -export function match(regex: RegExp) { - return (a: string): Option => fromNullable(a.match(regex)); +export function match( + regex: RegExp, +): (str: string) => Option { + return (str) => fromNullable(str.match(regex)); } /** @@ -440,14 +453,14 @@ export function match(regex: RegExp) { * * @since 2.0.0 */ -export function test(r: RegExp) { - return (s: string): boolean => { +export function test(r: RegExp): (str: string) => boolean { + return (str) => { // See https://tinyurl.com/2fdh6458 // RegExp.test keeps state from any runs, // this function resets that state to // avoid unexpected behavior const initialIndex = r.lastIndex; - const result = r.test(s); + const result = r.test(str); r.lastIndex = initialIndex; return result; }; diff --git a/sync.ts b/sync.ts index 3db7439..7b1dd41 100644 --- a/sync.ts +++ b/sync.ts @@ -10,9 +10,9 @@ import type { $, Kind, Out } from "./kind.ts"; import type { Applicable } from "./applicable.ts"; import type { Combinable } from "./combinable.ts"; -import type { Flatmappable } from "./flatmappable.ts"; +import type { Bind, Flatmappable, Tap } from "./flatmappable.ts"; import type { Initializable } from "./initializable.ts"; -import type { Mappable } from "./mappable.ts"; +import type { BindTo, Mappable } from "./mappable.ts"; import type { Foldable } from "./foldable.ts"; import type { Traversable } from "./traversable.ts"; import type { Wrappable } from "./wrappable.ts"; @@ -145,14 +145,14 @@ export const WrappableSync: Wrappable = { wrap }; /** * @since 2.0.0 */ -export const tap = createTap(FlatmappableSync); +export const tap: Tap = createTap(FlatmappableSync); /** * @since 2.0.0 */ -export const bind = createBind(FlatmappableSync); +export const bind: Bind = createBind(FlatmappableSync); /** * @since 2.0.0 */ -export const bindTo = createBindTo(MappableSync); +export const bindTo: BindTo = createBindTo(MappableSync); diff --git a/sync_either.ts b/sync_either.ts index 3542251..6ba697b 100644 --- a/sync_either.ts +++ b/sync_either.ts @@ -12,11 +12,11 @@ import type { Applicable } from "./applicable.ts"; import type { Bimappable } from "./bimappable.ts"; import type { Combinable } from "./combinable.ts"; import type { Either } from "./either.ts"; -import type { Failable } from "./failable.ts"; -import type { Flatmappable } from "./flatmappable.ts"; +import type { Failable, Tap } from "./failable.ts"; +import type { Bind, Flatmappable } from "./flatmappable.ts"; import type { Foldable } from "./foldable.ts"; import type { Initializable } from "./initializable.ts"; -import type { Mappable } from "./mappable.ts"; +import type { BindTo, Mappable } from "./mappable.ts"; import type { Sync } from "./sync.ts"; import type { Wrappable } from "./wrappable.ts"; @@ -274,14 +274,14 @@ export const WrappableSyncEither: Wrappable = { wrap }; /** * @since 2.0.0 */ -export const tap = createTap(FailableSyncEither); +export const tap: Tap = createTap(FailableSyncEither); /** * @since 2.0.0 */ -export const bind = createBind(FlatmappableSyncEither); +export const bind: Bind = createBind(FlatmappableSyncEither); /** * @since 2.0.0 */ -export const bindTo = createBindTo(MappableSyncEither); +export const bindTo: BindTo = createBindTo(MappableSyncEither); diff --git a/testing/mappable.test.ts b/testing/mappable.test.ts new file mode 100644 index 0000000..5671518 --- /dev/null +++ b/testing/mappable.test.ts @@ -0,0 +1,21 @@ +import { assertEquals } from "https://deno.land/std@0.103.0/testing/asserts.ts"; + +import * as M from "../mappable.ts"; +import * as O from "../option.ts"; +import { pipe } from "../fn.ts"; + +Deno.test("Mappable createBindTo", () => { + const bindTo = M.createBindTo(O.MappableOption); + + const resultSome = pipe( + O.some(1), + bindTo("hello"), + ); + const resultNone = pipe( + O.constNone(), + bindTo("hello"), + ); + + assertEquals(resultSome, O.some({ "hello": 1 })); + assertEquals(resultNone, O.none); +}); diff --git a/tree.ts b/tree.ts index 80c71c2..a2402ee 100644 --- a/tree.ts +++ b/tree.ts @@ -9,9 +9,9 @@ import type { $, Kind, Out } from "./kind.ts"; import type { Applicable } from "./applicable.ts"; import type { Comparable, Compare } from "./comparable.ts"; -import type { Flatmappable } from "./flatmappable.ts"; +import type { Bind, Flatmappable, Tap } from "./flatmappable.ts"; import type { Foldable } from "./foldable.ts"; -import type { Mappable } from "./mappable.ts"; +import type { BindTo, Mappable } from "./mappable.ts"; import type { Showable } from "./showable.ts"; import type { Traversable } from "./traversable.ts"; import type { Wrappable } from "./wrappable.ts"; @@ -290,14 +290,14 @@ export const WrappableTree: Wrappable = { wrap }; /** * @since 2.0.0 */ -export const tap = createTap(FlatmappableTree); +export const tap: Tap = createTap(FlatmappableTree); /** * @since 2.0.0 */ -export const bind = createBind(FlatmappableTree); +export const bind: Bind = createBind(FlatmappableTree); /** * @since 2.0.0 */ -export const bindTo = createBindTo(MappableTree); +export const bindTo: BindTo = createBindTo(MappableTree);