Comparing version 0.12.2 to 0.13.0
@@ -9,3 +9,2 @@ import { Maybe } from './Maybe'; | ||
export interface Either<L, R> { | ||
constructor: typeof Either; | ||
/** Internal property and subject to breaking changes, please use some of the available methods on the object if you want to access it */ | ||
@@ -89,4 +88,4 @@ __value: L | R; | ||
export declare const Either: EitherTypeRef; | ||
export declare function Right<R, L = never>(value: R): Either<L, R>; | ||
export declare function Left<L, R = never>(value: L): Either<L, R>; | ||
export {}; | ||
declare const left: <L, R = never>(value: L) => Either<L, R>; | ||
declare const right: <R, L = never>(value: R) => Either<L, R>; | ||
export { left as Left, right as Right }; |
452
Either.js
@@ -6,3 +6,3 @@ "use strict"; | ||
of(value) { | ||
return Right(value); | ||
return right(value); | ||
}, | ||
@@ -17,6 +17,6 @@ lefts(list) { | ||
try { | ||
return Right(throwsF()); | ||
return right(throwsF()); | ||
} | ||
catch (e) { | ||
return Left(e); | ||
return left(e); | ||
} | ||
@@ -28,225 +28,227 @@ }, | ||
}; | ||
function Right(value) { | ||
return { | ||
constructor: exports.Either, | ||
__value: value, | ||
isLeft() { | ||
return false; | ||
}, | ||
isRight() { | ||
return true; | ||
}, | ||
toJSON() { | ||
return value; | ||
}, | ||
inspect() { | ||
return `Right(${value})`; | ||
}, | ||
toString() { | ||
return this.inspect(); | ||
}, | ||
bimap(_, g) { | ||
return Right(g(value)); | ||
}, | ||
map(f) { | ||
return Right(f(value)); | ||
}, | ||
mapLeft(_) { | ||
return this; | ||
}, | ||
ap(other) { | ||
return other.isLeft() ? other : this.map(other.__value); | ||
}, | ||
equals(other) { | ||
return other.isRight() ? value === other.__value : false; | ||
}, | ||
chain(f) { | ||
return f(value); | ||
}, | ||
join() { | ||
return value; | ||
}, | ||
alt(_) { | ||
return this; | ||
}, | ||
reduce(reducer, initialValue) { | ||
return reducer(initialValue, value); | ||
}, | ||
extend(f) { | ||
return Right(f(this)); | ||
}, | ||
unsafeCoerce() { | ||
return value; | ||
}, | ||
caseOf(patterns) { | ||
return '_' in patterns ? patterns._() : patterns.Right(value); | ||
}, | ||
leftOrDefault(defaultValue) { | ||
return defaultValue; | ||
}, | ||
orDefault(_) { | ||
return value; | ||
}, | ||
orDefaultLazy(_) { | ||
return value; | ||
}, | ||
leftOrDefaultLazy(getDefaultValue) { | ||
return getDefaultValue(); | ||
}, | ||
ifLeft(_) { | ||
return this; | ||
}, | ||
ifRight(effect) { | ||
return effect(value), this; | ||
}, | ||
toMaybe() { | ||
return Maybe_1.Just(value); | ||
}, | ||
leftToMaybe() { | ||
return Maybe_1.Nothing; | ||
}, | ||
either(_, ifRight) { | ||
return ifRight(value); | ||
}, | ||
extract() { | ||
return value; | ||
}, | ||
'fantasy-land/bimap'(f, g) { | ||
return this.bimap(f, g); | ||
}, | ||
'fantasy-land/map'(f) { | ||
return this.map(f); | ||
}, | ||
'fantasy-land/ap'(other) { | ||
return this.ap(other); | ||
}, | ||
'fantasy-land/equals'(other) { | ||
return this.equals(other); | ||
}, | ||
'fantasy-land/chain'(f) { | ||
return this.chain(f); | ||
}, | ||
'fantasy-land/alt'(other) { | ||
return this.alt(other); | ||
}, | ||
'fantasy-land/reduce'(reducer, initialValue) { | ||
return this.reduce(reducer, initialValue); | ||
}, | ||
'fantasy-land/extend'(f) { | ||
return this.extend(f); | ||
} | ||
}; | ||
class Right { | ||
constructor(value) { | ||
this.__value = value; | ||
} | ||
isLeft() { | ||
return false; | ||
} | ||
isRight() { | ||
return true; | ||
} | ||
toJSON() { | ||
return this.__value; | ||
} | ||
inspect() { | ||
return `Right(${this.__value})`; | ||
} | ||
toString() { | ||
return this.inspect(); | ||
} | ||
bimap(_, g) { | ||
return right(g(this.__value)); | ||
} | ||
map(f) { | ||
return right(f(this.__value)); | ||
} | ||
mapLeft(_) { | ||
return this; | ||
} | ||
ap(other) { | ||
return other.isLeft() ? other : this.map(other.__value); | ||
} | ||
equals(other) { | ||
return other.isRight() ? this.__value === other.__value : false; | ||
} | ||
chain(f) { | ||
return f(this.__value); | ||
} | ||
join() { | ||
return this.__value; | ||
} | ||
alt(_) { | ||
return this; | ||
} | ||
reduce(reducer, initialValue) { | ||
return reducer(initialValue, this.__value); | ||
} | ||
extend(f) { | ||
return right(f(this)); | ||
} | ||
unsafeCoerce() { | ||
return this.__value; | ||
} | ||
caseOf(patterns) { | ||
return '_' in patterns ? patterns._() : patterns.Right(this.__value); | ||
} | ||
leftOrDefault(defaultValue) { | ||
return defaultValue; | ||
} | ||
orDefault(_) { | ||
return this.__value; | ||
} | ||
orDefaultLazy(_) { | ||
return this.__value; | ||
} | ||
leftOrDefaultLazy(getDefaultValue) { | ||
return getDefaultValue(); | ||
} | ||
ifLeft(_) { | ||
return this; | ||
} | ||
ifRight(effect) { | ||
return effect(this.__value), this; | ||
} | ||
toMaybe() { | ||
return Maybe_1.Just(this.__value); | ||
} | ||
leftToMaybe() { | ||
return Maybe_1.Nothing; | ||
} | ||
either(_, ifRight) { | ||
return ifRight(this.__value); | ||
} | ||
extract() { | ||
return this.__value; | ||
} | ||
'fantasy-land/bimap'(f, g) { | ||
return this.bimap(f, g); | ||
} | ||
'fantasy-land/map'(f) { | ||
return this.map(f); | ||
} | ||
'fantasy-land/ap'(other) { | ||
return this.ap(other); | ||
} | ||
'fantasy-land/equals'(other) { | ||
return this.equals(other); | ||
} | ||
'fantasy-land/chain'(f) { | ||
return this.chain(f); | ||
} | ||
'fantasy-land/alt'(other) { | ||
return this.alt(other); | ||
} | ||
'fantasy-land/reduce'(reducer, initialValue) { | ||
return this.reduce(reducer, initialValue); | ||
} | ||
'fantasy-land/extend'(f) { | ||
return this.extend(f); | ||
} | ||
} | ||
exports.Right = Right; | ||
function Left(value) { | ||
return { | ||
constructor: exports.Either, | ||
__value: value, | ||
isLeft() { | ||
return true; | ||
}, | ||
isRight() { | ||
return false; | ||
}, | ||
toJSON() { | ||
return value; | ||
}, | ||
inspect() { | ||
return `Left(${value})`; | ||
}, | ||
toString() { | ||
return this.inspect(); | ||
}, | ||
bimap(f, _) { | ||
return Left(f(value)); | ||
}, | ||
map(_) { | ||
return this; | ||
}, | ||
mapLeft(f) { | ||
return Left(f(value)); | ||
}, | ||
ap(other) { | ||
return other.isLeft() ? other : this; | ||
}, | ||
equals(other) { | ||
return other.isLeft() ? other.__value === value : false; | ||
}, | ||
chain(_) { | ||
return this; | ||
}, | ||
join() { | ||
return this; | ||
}, | ||
alt(other) { | ||
return other; | ||
}, | ||
reduce(_, initialValue) { | ||
return initialValue; | ||
}, | ||
extend(_) { | ||
return this; | ||
}, | ||
unsafeCoerce() { | ||
throw new Error('Either got coerced to a Left'); | ||
}, | ||
caseOf(patterns) { | ||
return '_' in patterns ? patterns._() : patterns.Left(value); | ||
}, | ||
leftOrDefault(_) { | ||
return value; | ||
}, | ||
orDefault(defaultValue) { | ||
return defaultValue; | ||
}, | ||
orDefaultLazy(getDefaultValue) { | ||
return getDefaultValue(); | ||
}, | ||
leftOrDefaultLazy(_) { | ||
return value; | ||
}, | ||
ifLeft(effect) { | ||
return effect(value), this; | ||
}, | ||
ifRight(_) { | ||
return this; | ||
}, | ||
toMaybe() { | ||
return Maybe_1.Nothing; | ||
}, | ||
leftToMaybe() { | ||
return Maybe_1.Just(value); | ||
}, | ||
either(ifLeft, _) { | ||
return ifLeft(value); | ||
}, | ||
extract() { | ||
return value; | ||
}, | ||
'fantasy-land/bimap'(f, g) { | ||
return this.bimap(f, g); | ||
}, | ||
'fantasy-land/map'(f) { | ||
return this.map(f); | ||
}, | ||
'fantasy-land/ap'(other) { | ||
return this.ap(other); | ||
}, | ||
'fantasy-land/equals'(other) { | ||
return this.equals(other); | ||
}, | ||
'fantasy-land/chain'(f) { | ||
return this.chain(f); | ||
}, | ||
'fantasy-land/alt'(other) { | ||
return this.alt(other); | ||
}, | ||
'fantasy-land/reduce'(reducer, initialValue) { | ||
return this.reduce(reducer, initialValue); | ||
}, | ||
'fantasy-land/extend'(f) { | ||
return this.extend(f); | ||
} | ||
}; | ||
Right.prototype.constructor = exports.Either; | ||
class Left { | ||
constructor(value) { | ||
this.__value = value; | ||
} | ||
isLeft() { | ||
return true; | ||
} | ||
isRight() { | ||
return false; | ||
} | ||
toJSON() { | ||
return this.__value; | ||
} | ||
inspect() { | ||
return `Left(${this.__value})`; | ||
} | ||
toString() { | ||
return this.inspect(); | ||
} | ||
bimap(f, _) { | ||
return left(f(this.__value)); | ||
} | ||
map(_) { | ||
return this; | ||
} | ||
mapLeft(f) { | ||
return left(f(this.__value)); | ||
} | ||
ap(other) { | ||
return other.isLeft() ? other : this; | ||
} | ||
equals(other) { | ||
return other.isLeft() ? other.__value === this.__value : false; | ||
} | ||
chain(_) { | ||
return this; | ||
} | ||
join() { | ||
return this; | ||
} | ||
alt(other) { | ||
return other; | ||
} | ||
reduce(_, initialValue) { | ||
return initialValue; | ||
} | ||
extend(_) { | ||
return this; | ||
} | ||
unsafeCoerce() { | ||
throw new Error('Either got coerced to a Left'); | ||
} | ||
caseOf(patterns) { | ||
return '_' in patterns ? patterns._() : patterns.Left(this.__value); | ||
} | ||
leftOrDefault(_) { | ||
return this.__value; | ||
} | ||
orDefault(defaultValue) { | ||
return defaultValue; | ||
} | ||
orDefaultLazy(getDefaultValue) { | ||
return getDefaultValue(); | ||
} | ||
leftOrDefaultLazy(_) { | ||
return this.__value; | ||
} | ||
ifLeft(effect) { | ||
return effect(this.__value), this; | ||
} | ||
ifRight(_) { | ||
return this; | ||
} | ||
toMaybe() { | ||
return Maybe_1.Nothing; | ||
} | ||
leftToMaybe() { | ||
return Maybe_1.Just(this.__value); | ||
} | ||
either(ifLeft, _) { | ||
return ifLeft(this.__value); | ||
} | ||
extract() { | ||
return this.__value; | ||
} | ||
'fantasy-land/bimap'(f, g) { | ||
return this.bimap(f, g); | ||
} | ||
'fantasy-land/map'(f) { | ||
return this.map(f); | ||
} | ||
'fantasy-land/ap'(other) { | ||
return this.ap(other); | ||
} | ||
'fantasy-land/equals'(other) { | ||
return this.equals(other); | ||
} | ||
'fantasy-land/chain'(f) { | ||
return this.chain(f); | ||
} | ||
'fantasy-land/alt'(other) { | ||
return this.alt(other); | ||
} | ||
'fantasy-land/reduce'(reducer, initialValue) { | ||
return this.reduce(reducer, initialValue); | ||
} | ||
'fantasy-land/extend'(f) { | ||
return this.extend(f); | ||
} | ||
} | ||
exports.Left = Left; | ||
Left.prototype.constructor = exports.Either; | ||
const left = (value) => new Left(value); | ||
exports.Left = left; | ||
const right = (value) => new Right(value); | ||
exports.Right = right; |
import { Either } from './Either'; | ||
import { MaybeAsync } from './MaybeAsync'; | ||
export interface EitherAsync<L, R> { | ||
constructor: typeof EitherAsync; | ||
/** | ||
@@ -6,0 +5,0 @@ * It's important to remember how `run` will behave because in an |
@@ -27,9 +27,10 @@ "use strict"; | ||
}; | ||
/** Constructs a EitherAsync object from a function that takes an object full of helpers that let you lift things into the EitherAsync context and returns a Promise */ | ||
exports.EitherAsync = (runPromise) => ({ | ||
constructor: exports.EitherAsync, | ||
class EitherAsyncImpl { | ||
constructor(runPromise) { | ||
this.runPromise = runPromise; | ||
} | ||
run() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
return Either_1.Right(yield runPromise(helpers)); | ||
return Either_1.Right(yield this.runPromise(helpers)); | ||
} | ||
@@ -40,12 +41,12 @@ catch (e) { | ||
}); | ||
}, | ||
} | ||
map(f) { | ||
return exports.EitherAsync(helpers => runPromise(helpers).then(f)); | ||
}, | ||
return exports.EitherAsync(helpers => this.runPromise(helpers).then(f)); | ||
} | ||
chain(f) { | ||
return exports.EitherAsync((helpers) => __awaiter(this, void 0, void 0, function* () { | ||
const value = yield runPromise(helpers); | ||
const value = yield this.runPromise(helpers); | ||
return yield helpers.fromPromise(f(value).run()); | ||
})); | ||
}, | ||
} | ||
toMaybeAsync() { | ||
@@ -56,9 +57,11 @@ return MaybeAsync_1.MaybeAsync(({ liftMaybe }) => __awaiter(this, void 0, void 0, function* () { | ||
})); | ||
}, | ||
} | ||
'fantasy-land/map'(f) { | ||
return this.map(f); | ||
}, | ||
} | ||
'fantasy-land/chain'(f) { | ||
return this.chain(f); | ||
} | ||
}); | ||
} | ||
/** Constructs a EitherAsync object from a function that takes an object full of helpers that let you lift things into the EitherAsync context and returns a Promise */ | ||
exports.EitherAsync = (runPromise) => new EitherAsyncImpl(runPromise); |
import { Tuple } from './Tuple'; | ||
import { Maybe } from './Maybe'; | ||
import { Order } from './Function'; | ||
/** Returns the first element which satisfies a predicate. A more typesafe version of the already existing List.prototype.find */ | ||
declare function find<T>(f: (x: T, index: number, arr: T[]) => boolean, list: T[]): Maybe<T>; | ||
declare function find<T>(f: (x: T, index: number, arr: T[]) => boolean): (list: T[]) => Maybe<T>; | ||
/** Returns the index of the first element which satisfies a predicate. A more typesafe version of the already existing List.prototype.findIndex */ | ||
declare function findIndex<T>(f: (x: T, index: number, arr: T[]) => boolean, list: T[]): Maybe<number>; | ||
declare function findIndex<T>(f: (x: T, index: number, arr: T[]) => boolean): (list: T[]) => Maybe<number>; | ||
/** Returns the element at a given index of a list */ | ||
declare function at<T>(index: number, list: T[]): Maybe<T>; | ||
declare function at<T>(index: number): (list: T[]) => Maybe<T>; | ||
/** Sorts an array with the given comparison function */ | ||
declare function sort<T>(compare: (a: T, b: T) => Order, list: T[]): T[]; | ||
declare function sort<T>(compare: (a: T, b: T) => Order): (list: T[]) => T[]; | ||
export declare const List: { | ||
@@ -13,3 +23,7 @@ init: <T>(list: T[]) => Maybe<T[]>; | ||
tail: <T>(list: T[]) => Maybe<T[]>; | ||
find: typeof find; | ||
findIndex: typeof findIndex; | ||
sum: (list: number[]) => number; | ||
sort: typeof sort; | ||
}; | ||
export {}; |
48
List.js
@@ -5,2 +5,3 @@ "use strict"; | ||
const Maybe_1 = require("./Maybe"); | ||
const Function_1 = require("./Function"); | ||
/** Returns Just the first element of an array or Nothing if there is none. If you don't want to work with a Maybe but still keep type safety, check out `NonEmptyList` */ | ||
@@ -16,8 +17,47 @@ const head = (list) => list.length > 0 ? Maybe_1.Just(list[0]) : Maybe_1.Nothing; | ||
const uncons = (list) => list.length > 0 ? Maybe_1.Just(Tuple_1.Tuple(list[0], list.slice(1))) : Maybe_1.Nothing; | ||
/* Returns the sum of all numbers inside an array */ | ||
const sum = (list) => list.reduce((acc, x) => acc + x, 0); | ||
function find(f, list) { | ||
switch (arguments.length) { | ||
case 1: | ||
return (list) => find(f, list); | ||
default: | ||
return Maybe_1.Maybe.fromNullable(list.find(f)); | ||
} | ||
} | ||
function findIndex(f, list) { | ||
switch (arguments.length) { | ||
case 1: | ||
return (list) => findIndex(f, list); | ||
default: | ||
return Maybe_1.Maybe.fromPredicate(x => x !== -1, list.findIndex(f)); | ||
} | ||
} | ||
function at(index, list) { | ||
if (list === undefined) { | ||
return (list) => list[index] === undefined ? Maybe_1.Nothing : Maybe_1.Just(list[index]); | ||
switch (arguments.length) { | ||
case 1: | ||
return (list) => at(index, list); | ||
default: | ||
return list[index] === undefined ? Maybe_1.Nothing : Maybe_1.Just(list[index]); | ||
} | ||
return list[index] === undefined ? Maybe_1.Nothing : Maybe_1.Just(list[index]); | ||
} | ||
exports.List = { init, uncons, at, head, last, tail }; | ||
function sort(compare, list) { | ||
switch (arguments.length) { | ||
case 1: | ||
return (list) => sort(compare, list); | ||
default: | ||
return [...list].sort((x, y) => Function_1.orderToNumber(compare(x, y))); | ||
} | ||
} | ||
exports.List = { | ||
init, | ||
uncons, | ||
at, | ||
head, | ||
last, | ||
tail, | ||
find, | ||
findIndex, | ||
sum, | ||
sort | ||
}; |
@@ -12,3 +12,2 @@ import { Either } from './Either'; | ||
export interface Maybe<T> { | ||
constructor: typeof Maybe; | ||
/** Internal property and subject to breaking changes, please use some of the available methods on the object if you want to access it */ | ||
@@ -19,3 +18,3 @@ __value: T; | ||
/** Returns true if `this` is `Nothing`, otherwise it returns false */ | ||
isNothing(): this is typeof Nothing; | ||
isNothing(): this is Nothing; | ||
inspect(): string; | ||
@@ -78,5 +77,5 @@ toString(): string; | ||
/** Returns `Nothing` */ | ||
empty(): typeof Nothing; | ||
empty(): Nothing; | ||
/** Returns `Nothing` */ | ||
zero(): typeof Nothing; | ||
zero(): Nothing; | ||
/** Takes a value and returns `Nothing` if the value is null or undefined, otherwise a `Just` is returned */ | ||
@@ -97,10 +96,83 @@ fromNullable<T>(value: T | undefined | null | void): Maybe<T>; | ||
'fantasy-land/of'<T>(value: T): Maybe<T>; | ||
'fantasy-land/empty'(): typeof Nothing; | ||
'fantasy-land/zero'(): typeof Nothing; | ||
'fantasy-land/empty'(): Nothing; | ||
'fantasy-land/zero'(): Nothing; | ||
} | ||
export declare const Maybe: MaybeTypeRef; | ||
declare class Just<T> implements Maybe<T> { | ||
__value: T; | ||
constructor(value: T); | ||
isJust(): boolean; | ||
isNothing(): boolean; | ||
inspect(): string; | ||
toString(): string; | ||
toJSON(): T; | ||
equals(other: Maybe<T>): boolean; | ||
map<U>(f: (value: T) => U): Maybe<U>; | ||
ap<U>(maybeF: Maybe<(value: T) => U>): Maybe<U>; | ||
alt(_: Maybe<T>): Maybe<T>; | ||
chain<U>(f: (value: T) => Maybe<U>): Maybe<U>; | ||
chainNullable<U>(f: (value: T) => U | undefined | null | void): Maybe<U>; | ||
join<U>(this: Maybe<Maybe<U>>): Maybe<U>; | ||
reduce<U>(reducer: (accumulator: U, value: T) => U, initialValue: U): U; | ||
extend<U>(f: (value: Maybe<T>) => U): Maybe<U>; | ||
unsafeCoerce(): T; | ||
caseOf<U>(patterns: MaybePatterns<T, U>): U; | ||
orDefault(_: T): T; | ||
orDefaultLazy(_: () => T): T; | ||
toList(): T[]; | ||
mapOrDefault<U>(f: (value: T) => U, _: U): U; | ||
extract(): this extends AlwaysJust ? T : T | undefined; | ||
extractNullable(): this extends AlwaysJust ? T : T | null; | ||
toEither<L>(_: L): Either<L, T>; | ||
ifJust(effect: (value: T) => any): this; | ||
ifNothing(_: () => any): this; | ||
filter(pred: (value: T) => boolean): Maybe<T>; | ||
'fantasy-land/equals'(other: Maybe<T>): boolean; | ||
'fantasy-land/map'<U>(f: (value: T) => U): Maybe<U>; | ||
'fantasy-land/ap'<U>(maybeF: Maybe<(value: T) => U>): Maybe<U>; | ||
'fantasy-land/alt'(other: Maybe<T>): Maybe<T>; | ||
'fantasy-land/chain'<U>(f: (value: T) => Maybe<U>): Maybe<U>; | ||
'fantasy-land/reduce'<U>(reducer: (accumulator: U, value: T) => U, initialValue: U): U; | ||
'fantasy-land/extend'<U>(f: (value: Maybe<T>) => U): Maybe<U>; | ||
} | ||
declare class Nothing implements Maybe<never> { | ||
__value: never; | ||
isJust(): boolean; | ||
isNothing(): boolean; | ||
inspect(): string; | ||
toString(): string; | ||
toJSON(): never; | ||
equals<T>(other: Maybe<T>): boolean; | ||
map<U>(_: (value: never) => U): Maybe<U>; | ||
ap<U>(_: Maybe<(value: never) => U>): Maybe<U>; | ||
alt<T>(other: Maybe<T>): Maybe<T>; | ||
chain<U>(_: (value: never) => Maybe<U>): Maybe<U>; | ||
chainNullable<U>(_: (value: never) => U | undefined | null | void): Maybe<U>; | ||
join<U>(this: Maybe<Maybe<U>>): Maybe<U>; | ||
reduce<U>(_: (accumulator: U, value: never) => U, initialValue: U): U; | ||
extend<U>(_: (value: Maybe<never>) => U): Maybe<U>; | ||
unsafeCoerce<T>(): T; | ||
caseOf<U>(patterns: MaybePatterns<never, U>): U; | ||
orDefault<T>(defaultValue: T): T; | ||
orDefaultLazy<T>(getDefaultValue: () => T): T; | ||
toList<T>(): T[]; | ||
mapOrDefault<U>(_: (value: never) => U, defaultValue: U): U; | ||
extract(): this extends AlwaysJust ? never : undefined; | ||
extractNullable(): this extends AlwaysJust ? never : null; | ||
toEither<L, T>(left: L): Either<L, T>; | ||
ifJust(_: (value: never) => any): this; | ||
ifNothing(effect: () => any): this; | ||
filter(_: (value: never) => boolean): Maybe<never>; | ||
'fantasy-land/equals'(other: Maybe<never>): boolean; | ||
'fantasy-land/map'<U>(f: (value: never) => U): Maybe<U>; | ||
'fantasy-land/ap'<U>(maybeF: Maybe<(value: never) => U>): Maybe<U>; | ||
'fantasy-land/alt'(other: Maybe<never>): Maybe<never>; | ||
'fantasy-land/chain'<U>(f: (value: never) => Maybe<U>): Maybe<U>; | ||
'fantasy-land/reduce'<U>(reducer: (accumulator: U, value: never) => U, initialValue: U): U; | ||
'fantasy-land/extend'<U>(f: (value: Maybe<never>) => U): Maybe<U>; | ||
} | ||
/** Constructs a Just. Respents an optional value that exists. */ | ||
export declare function Just<T>(value: T): Maybe<T>; | ||
declare const just: <T>(value: T) => Just<T>; | ||
/** Represents a missing value, you can think of it as a smart 'null'. */ | ||
export declare const Nothing: Maybe<never>; | ||
export {}; | ||
declare const nothing: Nothing; | ||
export { just as Just, nothing as Nothing }; |
322
Maybe.js
@@ -6,15 +6,15 @@ "use strict"; | ||
of(value) { | ||
return Just(value); | ||
return just(value); | ||
}, | ||
empty() { | ||
return exports.Nothing; | ||
return nothing; | ||
}, | ||
zero() { | ||
return exports.Nothing; | ||
return nothing; | ||
}, | ||
fromNullable(value) { | ||
return value == null ? exports.Nothing : Just(value); | ||
return value == null ? nothing : just(value); | ||
}, | ||
fromFalsy(value) { | ||
return value ? Just(value) : exports.Nothing; | ||
return value ? just(value) : nothing; | ||
}, | ||
@@ -26,3 +26,3 @@ fromPredicate(pred, value) { | ||
default: | ||
return pred(value) ? Just(value) : exports.Nothing; | ||
return pred(value) ? just(value) : nothing; | ||
} | ||
@@ -43,6 +43,6 @@ }, | ||
try { | ||
return Just(thunk()); | ||
return just(thunk()); | ||
} | ||
catch (_a) { | ||
return exports.Nothing; | ||
return nothing; | ||
} | ||
@@ -60,212 +60,214 @@ }, | ||
}; | ||
/** Constructs a Just. Respents an optional value that exists. */ | ||
function Just(value) { | ||
return { | ||
constructor: exports.Maybe, | ||
__value: value, | ||
isJust: () => { | ||
return true; | ||
}, | ||
isNothing: () => { | ||
return false; | ||
}, | ||
inspect() { | ||
return `Just(${value})`; | ||
}, | ||
toString() { | ||
return this.inspect(); | ||
}, | ||
toJSON() { | ||
return value; | ||
}, | ||
equals(other) { | ||
return value === other.__value; | ||
}, | ||
map(f) { | ||
return Just(f(value)); | ||
}, | ||
ap(maybeF) { | ||
return maybeF.isNothing() ? exports.Nothing : this.map(maybeF.__value); | ||
}, | ||
alt(_) { | ||
return this; | ||
}, | ||
chain(f) { | ||
return f(value); | ||
}, | ||
chainNullable(f) { | ||
return exports.Maybe.fromNullable(f(value)); | ||
}, | ||
join() { | ||
return this.__value; | ||
}, | ||
reduce(reducer, initialValue) { | ||
return reducer(initialValue, value); | ||
}, | ||
extend(f) { | ||
return Just(f(this)); | ||
}, | ||
unsafeCoerce() { | ||
return value; | ||
}, | ||
caseOf(patterns) { | ||
return '_' in patterns ? patterns._() : patterns.Just(value); | ||
}, | ||
orDefault(_) { | ||
return value; | ||
}, | ||
orDefaultLazy(_) { | ||
return value; | ||
}, | ||
toList() { | ||
return [value]; | ||
}, | ||
mapOrDefault(f, _) { | ||
return f(value); | ||
}, | ||
extract() { | ||
return value; | ||
}, | ||
extractNullable() { | ||
return value; | ||
}, | ||
toEither(_) { | ||
return Either_1.Right(value); | ||
}, | ||
ifJust(effect) { | ||
return effect(value), this; | ||
}, | ||
ifNothing(_) { | ||
return this; | ||
}, | ||
filter(pred) { | ||
return pred(value) ? Just(value) : exports.Nothing; | ||
}, | ||
'fantasy-land/equals'(other) { | ||
return this.equals(other); | ||
}, | ||
'fantasy-land/map'(f) { | ||
return this.map(f); | ||
}, | ||
'fantasy-land/ap'(maybeF) { | ||
return this.ap(maybeF); | ||
}, | ||
'fantasy-land/alt'(other) { | ||
return this.alt(other); | ||
}, | ||
'fantasy-land/chain'(f) { | ||
return this.chain(f); | ||
}, | ||
'fantasy-land/reduce'(reducer, initialValue) { | ||
return this.reduce(reducer, initialValue); | ||
}, | ||
'fantasy-land/extend'(f) { | ||
return this.extend(f); | ||
} | ||
}; | ||
class Just { | ||
constructor(value) { | ||
this.__value = value; | ||
} | ||
isJust() { | ||
return true; | ||
} | ||
isNothing() { | ||
return false; | ||
} | ||
inspect() { | ||
return `Just(${this.__value})`; | ||
} | ||
toString() { | ||
return this.inspect(); | ||
} | ||
toJSON() { | ||
return this.__value; | ||
} | ||
equals(other) { | ||
return this.__value === other.__value; | ||
} | ||
map(f) { | ||
return just(f(this.__value)); | ||
} | ||
ap(maybeF) { | ||
return maybeF.isNothing() ? nothing : this.map(maybeF.__value); | ||
} | ||
alt(_) { | ||
return this; | ||
} | ||
chain(f) { | ||
return f(this.__value); | ||
} | ||
chainNullable(f) { | ||
return exports.Maybe.fromNullable(f(this.__value)); | ||
} | ||
join() { | ||
return this.__value; | ||
} | ||
reduce(reducer, initialValue) { | ||
return reducer(initialValue, this.__value); | ||
} | ||
extend(f) { | ||
return just(f(this)); | ||
} | ||
unsafeCoerce() { | ||
return this.__value; | ||
} | ||
caseOf(patterns) { | ||
return '_' in patterns ? patterns._() : patterns.Just(this.__value); | ||
} | ||
orDefault(_) { | ||
return this.__value; | ||
} | ||
orDefaultLazy(_) { | ||
return this.__value; | ||
} | ||
toList() { | ||
return [this.__value]; | ||
} | ||
mapOrDefault(f, _) { | ||
return f(this.__value); | ||
} | ||
extract() { | ||
return this.__value; | ||
} | ||
extractNullable() { | ||
return this.__value; | ||
} | ||
toEither(_) { | ||
return Either_1.Right(this.__value); | ||
} | ||
ifJust(effect) { | ||
return effect(this.__value), this; | ||
} | ||
ifNothing(_) { | ||
return this; | ||
} | ||
filter(pred) { | ||
return pred(this.__value) ? just(this.__value) : nothing; | ||
} | ||
'fantasy-land/equals'(other) { | ||
return this.equals(other); | ||
} | ||
'fantasy-land/map'(f) { | ||
return this.map(f); | ||
} | ||
'fantasy-land/ap'(maybeF) { | ||
return this.ap(maybeF); | ||
} | ||
'fantasy-land/alt'(other) { | ||
return this.alt(other); | ||
} | ||
'fantasy-land/chain'(f) { | ||
return this.chain(f); | ||
} | ||
'fantasy-land/reduce'(reducer, initialValue) { | ||
return this.reduce(reducer, initialValue); | ||
} | ||
'fantasy-land/extend'(f) { | ||
return this.extend(f); | ||
} | ||
} | ||
exports.Just = Just; | ||
/** Represents a missing value, you can think of it as a smart 'null'. */ | ||
exports.Nothing = { | ||
constructor: exports.Maybe, | ||
__value: null, | ||
isJust: () => { | ||
Just.prototype.constructor = exports.Maybe; | ||
class Nothing { | ||
isJust() { | ||
return false; | ||
}, | ||
isNothing: () => { | ||
} | ||
isNothing() { | ||
return true; | ||
}, | ||
} | ||
inspect() { | ||
return 'Nothing'; | ||
}, | ||
} | ||
toString() { | ||
return this.inspect(); | ||
}, | ||
} | ||
toJSON() { | ||
return this.__value; | ||
}, | ||
} | ||
equals(other) { | ||
return this.__value === other.__value; | ||
}, | ||
} | ||
map(_) { | ||
return exports.Nothing; | ||
}, | ||
return nothing; | ||
} | ||
ap(_) { | ||
return exports.Nothing; | ||
}, | ||
return nothing; | ||
} | ||
alt(other) { | ||
return other; | ||
}, | ||
} | ||
chain(_) { | ||
return exports.Nothing; | ||
}, | ||
return nothing; | ||
} | ||
chainNullable(_) { | ||
return exports.Nothing; | ||
}, | ||
return nothing; | ||
} | ||
join() { | ||
return exports.Nothing; | ||
}, | ||
return nothing; | ||
} | ||
reduce(_, initialValue) { | ||
return initialValue; | ||
}, | ||
} | ||
extend(_) { | ||
return exports.Nothing; | ||
}, | ||
return nothing; | ||
} | ||
unsafeCoerce() { | ||
throw new Error('Maybe got coerced to a null'); | ||
}, | ||
} | ||
caseOf(patterns) { | ||
return '_' in patterns ? patterns._() : patterns.Nothing(); | ||
}, | ||
} | ||
orDefault(defaultValue) { | ||
return defaultValue; | ||
}, | ||
} | ||
orDefaultLazy(getDefaultValue) { | ||
return getDefaultValue(); | ||
}, | ||
} | ||
toList() { | ||
return []; | ||
}, | ||
} | ||
mapOrDefault(_, defaultValue) { | ||
return defaultValue; | ||
}, | ||
} | ||
extract() { | ||
return undefined; | ||
}, | ||
} | ||
extractNullable() { | ||
return null; | ||
}, | ||
} | ||
toEither(left) { | ||
return Either_1.Left(left); | ||
}, | ||
} | ||
ifJust(_) { | ||
return this; | ||
}, | ||
} | ||
ifNothing(effect) { | ||
return effect(), this; | ||
}, | ||
} | ||
filter(_) { | ||
return exports.Nothing; | ||
}, | ||
return nothing; | ||
} | ||
'fantasy-land/equals'(other) { | ||
return this.equals(other); | ||
}, | ||
} | ||
'fantasy-land/map'(f) { | ||
return this.map(f); | ||
}, | ||
} | ||
'fantasy-land/ap'(maybeF) { | ||
return this.ap(maybeF); | ||
}, | ||
} | ||
'fantasy-land/alt'(other) { | ||
return this.alt(other); | ||
}, | ||
} | ||
'fantasy-land/chain'(f) { | ||
return this.chain(f); | ||
}, | ||
} | ||
'fantasy-land/reduce'(reducer, initialValue) { | ||
return this.reduce(reducer, initialValue); | ||
}, | ||
} | ||
'fantasy-land/extend'(f) { | ||
return this.extend(f); | ||
} | ||
}; | ||
} | ||
Nothing.prototype.constructor = exports.Maybe; | ||
/** Constructs a Just. Respents an optional value that exists. */ | ||
const just = (value) => new Just(value); | ||
exports.Just = just; | ||
/** Represents a missing value, you can think of it as a smart 'null'. */ | ||
const nothing = new Nothing(); | ||
exports.Nothing = nothing; |
import { Maybe } from './Maybe'; | ||
import { EitherAsync } from './EitherAsync'; | ||
export interface MaybeAsync<T> { | ||
constructor: typeof MaybeAsync; | ||
/** | ||
@@ -6,0 +5,0 @@ * It's important to remember how `run` will behave because in an |
@@ -24,9 +24,10 @@ "use strict"; | ||
}; | ||
/** Constructs a MaybeAsync object from a function that takes an object full of helpers that let you lift things into the MaybeAsync context and returns a Promise */ | ||
exports.MaybeAsync = (runPromise) => ({ | ||
constructor: exports.MaybeAsync, | ||
class MaybeAsyncImpl { | ||
constructor(runPromise) { | ||
this.runPromise = runPromise; | ||
} | ||
run() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
return Maybe_1.Just(yield runPromise(helpers)); | ||
return Maybe_1.Just(yield this.runPromise(helpers)); | ||
} | ||
@@ -37,12 +38,12 @@ catch (_a) { | ||
}); | ||
}, | ||
} | ||
map(f) { | ||
return exports.MaybeAsync(helpers => runPromise(helpers).then(f)); | ||
}, | ||
return exports.MaybeAsync(helpers => this.runPromise(helpers).then(f)); | ||
} | ||
chain(f) { | ||
return exports.MaybeAsync((helpers) => __awaiter(this, void 0, void 0, function* () { | ||
const value = yield runPromise(helpers); | ||
const value = yield this.runPromise(helpers); | ||
return yield helpers.fromPromise(f(value).run()); | ||
})); | ||
}, | ||
} | ||
toEitherAsync(error) { | ||
@@ -53,9 +54,11 @@ return EitherAsync_1.EitherAsync(({ liftEither }) => __awaiter(this, void 0, void 0, function* () { | ||
})); | ||
}, | ||
} | ||
'fantasy-land/map'(f) { | ||
return this.map(f); | ||
}, | ||
} | ||
'fantasy-land/chain'(f) { | ||
return this.chain(f); | ||
} | ||
}); | ||
} | ||
/** Constructs a MaybeAsync object from a function that takes an object full of helpers that let you lift things into the MaybeAsync context and returns a Promise */ | ||
exports.MaybeAsync = (runPromise) => new MaybeAsyncImpl(runPromise); |
{ | ||
"name": "purify-ts", | ||
"version": "0.12.2", | ||
"version": "0.13.0", | ||
"description": "Functional programming standard library for TypeScript ", | ||
@@ -30,8 +30,8 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@types/jest": "^23.3.1", | ||
"jest": "^23.6.0", | ||
"ts-jest": "^23.10.0", | ||
"typescript": "3.2.4" | ||
"@types/jest": "^24.0.0", | ||
"jest": "^24.1.0", | ||
"ts-jest": "^24.0.0", | ||
"typescript": "3.5.2" | ||
}, | ||
"dependencies": {} | ||
} |
@@ -0,0 +0,0 @@ <h3 align="center"> |
@@ -11,3 +11,2 @@ export interface TupleTypeRef { | ||
export interface Tuple<F, S> extends Iterable<F | S>, ArrayLike<F | S> { | ||
constructor: typeof Tuple; | ||
0: F; | ||
@@ -14,0 +13,0 @@ 1: S; |
80
Tuple.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const TupleConstructor = (fst, snd) => ({ | ||
constructor: exports.Tuple, | ||
0: fst, | ||
1: snd, | ||
length: 2, | ||
class TupleImpl { | ||
constructor(first, second) { | ||
this.first = first; | ||
this.second = second; | ||
this.length = 2; | ||
this[0] = first; | ||
this[1] = second; | ||
} | ||
*[Symbol.iterator]() { | ||
yield fst; | ||
yield snd; | ||
}, | ||
yield this.first; | ||
yield this.second; | ||
} | ||
toJSON() { | ||
return this.toArray(); | ||
}, | ||
} | ||
inspect() { | ||
return `Tuple(${fst}, ${snd})`; | ||
}, | ||
return `Tuple(${this.first}, ${this.second})`; | ||
} | ||
toString() { | ||
return this.inspect(); | ||
}, | ||
} | ||
fst() { | ||
return fst; | ||
}, | ||
return this.first; | ||
} | ||
snd() { | ||
return snd; | ||
}, | ||
return this.second; | ||
} | ||
equals(other) { | ||
return fst === other.fst() && snd === other.snd(); | ||
}, | ||
return this.first === other.fst() && this.second === other.snd(); | ||
} | ||
bimap(f, g) { | ||
return exports.Tuple(f(fst), g(snd)); | ||
}, | ||
return exports.Tuple(f(this.first), g(this.second)); | ||
} | ||
mapFirst(f) { | ||
return exports.Tuple(f(fst), snd); | ||
}, | ||
return exports.Tuple(f(this.first), this.second); | ||
} | ||
map(f) { | ||
return exports.Tuple(fst, f(snd)); | ||
}, | ||
return exports.Tuple(this.first, f(this.second)); | ||
} | ||
reduce(reducer, initialValue) { | ||
return reducer(initialValue, snd); | ||
}, | ||
return reducer(initialValue, this.second); | ||
} | ||
toArray() { | ||
return [fst, snd]; | ||
}, | ||
return [this.first, this.second]; | ||
} | ||
swap() { | ||
return exports.Tuple(snd, fst); | ||
}, | ||
return exports.Tuple(this.second, this.first); | ||
} | ||
ap(f) { | ||
return exports.Tuple(fst, f.snd()(snd)); | ||
}, | ||
return exports.Tuple(this.first, f.snd()(this.second)); | ||
} | ||
'fantasy-land/equals'(other) { | ||
return this.equals(other); | ||
}, | ||
} | ||
'fantasy-land/bimap'(f, g) { | ||
return this.bimap(f, g); | ||
}, | ||
} | ||
'fantasy-land/map'(f) { | ||
return this.map(f); | ||
}, | ||
} | ||
'fantasy-land/reduce'(reducer, initialValue) { | ||
return this.reduce(reducer, initialValue); | ||
}, | ||
} | ||
'fantasy-land/ap'(f) { | ||
return this.ap(f); | ||
} | ||
}); | ||
exports.Tuple = Object.assign(TupleConstructor, { | ||
} | ||
exports.Tuple = Object.assign((fst, snd) => new TupleImpl(fst, snd), { | ||
fromArray: ([fst, snd]) => { | ||
@@ -83,1 +86,2 @@ return exports.Tuple(fst, snd); | ||
}); | ||
TupleImpl.prototype.constructor = exports.Tuple; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
207038
1280