Skip to content
This repository has been archived by the owner on Sep 8, 2021. It is now read-only.

explicitly export types #36

Merged
merged 3 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ const enum Design {
GuardianView,
Quiz,
AdvertisementFeature,
Interactive
Interactive,
}

const enum Display {
Standard,
Immersive,
Showcase,
NumberedList,
Column
Column,
}

interface Format {
Expand All @@ -45,15 +45,6 @@ interface Format {
display: Display;
}


// ----- Exports ----- //

export {
Pillar,
Special,
Theme,
Design,
Display,
};

export type { Format };
export type { Pillar, Special, Theme, Design, Display, Format };
sndrs marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 7 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// ----- Types ----- //

export type { Format } from './format';
export { Pillar, Special, Theme, Design, Display } from './format';
export type { Pillar, Special, Theme, Design, Display, Format } from "./format";

export type {
OphanABEvent,
Expand All @@ -12,13 +11,11 @@ export type {
OphanComponentType,
OphanProduct,
TestMeta,
} from './ophan';
} from "./ophan";

export {
Role,
} from './role';
export type { Role } from "./role";

export type { Option } from './option';
export type { Option } from "./option";
export {
OptionKind,
some,
Expand All @@ -28,9 +25,9 @@ export {
map,
map2,
andThen,
} from './option';
} from "./option";

export type { Result } from './result';
export type { Result } from "./result";
export {
ResultKind,
ok,
Expand All @@ -42,4 +39,4 @@ export {
toOption,
map as resultMap,
andThen as resultAndThen,
} from './result';
} from "./result";
2 changes: 1 addition & 1 deletion src/ophan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type TestMeta = {
products?: OphanProduct[];
};

export {
export type {
OphanABEvent,
OphanABPayload,
OphanAction,
Expand Down
28 changes: 13 additions & 15 deletions src/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ const enum OptionKind {
type Some<A> = {
kind: OptionKind.Some;
value: A;
}
};

type None = {
kind: OptionKind.None;
}
};

/**
* Represents a value that may or may not exist; it's either a Some or a None.
*/
type Option<A> = Some<A> | None;


// ----- Constructors ----- //

const some = <A>(a: A): Some<A> => ({ kind: OptionKind.Some, value: a });
Expand All @@ -35,7 +34,6 @@ const none: None = { kind: OptionKind.None };
const fromNullable = <A>(a: A | null | undefined): Option<A> =>
a === null || a === undefined ? none : some(a);


// ----- Functions ----- //

/**
Expand All @@ -50,7 +48,7 @@ const fromNullable = <A>(a: A | null | undefined): Option<A> =>
*
* const bylineTwo = none;
* withDefault('Jane Smith')(bylineTwo); // Returns 'Jane Smith'
*/
*/
const withDefault = <A>(a: A) => (optA: Option<A>): A =>
optA.kind === OptionKind.Some ? optA.value : a;

Expand All @@ -63,10 +61,10 @@ const withDefault = <A>(a: A) => (optA: Option<A>): A =>
* const creditOne = some('Nicéphore Niépce');
* // Returns Some('Photograph: Nicéphore Niépce')
* map(name => `Photograph: ${name}`)(creditOne);
*
*
* const creditTwo = none;
* map(name => `Photograph: ${name}`)(creditTwo); // Returns None
*
*
* // All together
* compose(withDefault(''), map(name => `Photograph: ${name}`))(credit);
*/
Expand All @@ -81,7 +79,9 @@ const map = <A, B>(f: (a: A) => B) => (optA: Option<A>): Option<B> =>
* @param optB The second Option
* @returns {Option<C>} A new `Option`
*/
const map2 = <A, B, C>(f: (a: A, b: B) => C) => (optA: Option<A>) => (optB: Option<B>): Option<C> =>
const map2 = <A, B, C>(f: (a: A, b: B) => C) => (optA: Option<A>) => (
optB: Option<B>
): Option<C> =>
optA.kind === OptionKind.Some && optB.kind === OptionKind.Some
? some(f(optA.value, optB.value))
: none;
Expand All @@ -96,13 +96,13 @@ const map2 = <A, B, C>(f: (a: A, b: B) => C) => (optA: Option<A>) => (optB: Opti
* @example
* type GetUser = number => Option<User>;
* type GetUserName = User => Option<string>;
*
*
* const userId = 1;
* const username: Option<string> = compose(andThen(getUserName), getUser)(userId);
*/
const andThen = <A, B>(f: (a: A) => Option<B>) => (optA: Option<A>): Option<B> =>
optA.kind === OptionKind.Some ? f(optA.value) : none;

const andThen = <A, B>(f: (a: A) => Option<B>) => (
optA: Option<A>
): Option<B> => (optA.kind === OptionKind.Some ? f(optA.value) : none);

// ----- Exports ----- //

Expand All @@ -117,6 +117,4 @@ export {
andThen,
};

export type {
Option,
};
export type { Option };
47 changes: 23 additions & 24 deletions src/result.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// ----- Imports ----- //

import type { Option } from './option';
import { some, none } from './option';

import type { Option } from "./option";
import { some, none } from "./option";

// ----- Types ----- //

Expand All @@ -14,19 +13,18 @@ const enum ResultKind {
type Ok<A> = {
kind: ResultKind.Ok;
value: A;
}
};

type Err<E> = {
kind: ResultKind.Err;
err: E;
}
};

/**
* Represents either a value or an error; it's either an `Ok` or an `Err`.
*/
type Result<E, A> = Err<E> | Ok<A>;


// ----- Constructors ----- //

const ok = <A>(a: A): Ok<A> => ({ kind: ResultKind.Ok, value: a });
Expand All @@ -45,7 +43,6 @@ function fromUnsafe<A, E>(f: () => A, error: E): Result<E, A> {
}
}


// ----- Functions ----- //

/**
Expand All @@ -58,22 +55,25 @@ function fromUnsafe<A, E>(f: () => A, error: E): Result<E, A> {
* @param result The Result
* @example
* const flakyTaskResult: Result<string, number> = flakyTask(options);
*
*
* either(
* data => `We got the data! Here it is: ${data}`,
* error => `Uh oh, an error: ${error}`,
* )(flakyTaskResult)
*/
const either = <E, A, C>(f: (e: E) => C, g: (a: A) => C) => (result: Result<E, A>): C =>
result.kind === ResultKind.Ok ? g(result.value) : f(result.err);
const either = <E, A, C>(f: (e: E) => C, g: (a: A) => C) => (
result: Result<E, A>
): C => (result.kind === ResultKind.Ok ? g(result.value) : f(result.err));

/**
* The companion to `map`.
* Applies a function to the error in `Err`, does nothing to an `Ok`.
* @param f The function to apply if this is an `Err`
* @param result The Result
*/
const mapError = <E, F>(f: (e: E) => F) => <A>(result: Result<E, A>): Result<F, A> =>
const mapError = <E, F>(f: (e: E) => F) => <A>(
result: Result<E, A>
): Result<F, A> =>
result.kind === ResultKind.Err ? err(f(result.err)) : result;

/**
Expand Down Expand Up @@ -101,14 +101,15 @@ const map = <A, B>(f: (a: A) => B) => <E>(result: Result<E, A>): Result<E, B> =>
* @example
* type RequestUser = number => Result<string, User>;
* type GetEmail = User => Result<string, string>;
*
*
* // Request fails: Err('Network failure')
* // Request succeeds, problem accessing email: Err('Email field missing')
* // Both succeed: Ok('email_address')
* andThen(getEmail)(requestUser(id))
*/
const andThen = <E, A, B>(f: (a: A) => Result<E, B>) => (result: Result<E, A>): Result<E, B> =>
result.kind === ResultKind.Ok ? f(result.value) : result;
const andThen = <E, A, B>(f: (a: A) => Result<E, B>) => (
result: Result<E, A>
): Result<E, B> => (result.kind === ResultKind.Ok ? f(result.value) : result);

/**
* The return type of the `partition` function
Expand All @@ -122,15 +123,15 @@ type Partitioned<E, A> = { errs: E[]; oks: A[] };
* and one for the list of `Ok`s
*/
const partition = <E, A>(results: Result<E, A>[]): Partitioned<E, A> =>
results.reduce(({ errs, oks }: Partitioned<E, A>, result) =>
either<E, A, Partitioned<E, A>>(
err => ({ errs: [ ...errs, err ], oks }),
ok => ({ errs, oks: [ ...oks, ok ] }),
)(result),
{ errs: [], oks: [] },
results.reduce(
({ errs, oks }: Partitioned<E, A>, result) =>
either<E, A, Partitioned<E, A>>(
(err) => ({ errs: [...errs, err], oks }),
(ok) => ({ errs, oks: [...oks, ok] })
)(result),
{ errs: [], oks: [] }
sndrs marked this conversation as resolved.
Show resolved Hide resolved
);


// ----- Exports ----- //

export {
Expand All @@ -146,6 +147,4 @@ export {
andThen,
};

export type {
Result,
};
export type { Result };
4 changes: 1 addition & 3 deletions src/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@ const enum Role {

// ----- Exports ----- //

export {
Role,
}
export type { Role };