-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #259 from jacob-alford/ja/256/newtypes
[#256] Add schemata-ts Newtypes
- Loading branch information
Showing
45 changed files
with
361 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* A port of `io-ts` branded types to schemata-ts | ||
* | ||
* @since 1.4.0 | ||
*/ | ||
import { Brand as Brand_ } from 'io-ts' | ||
|
||
/** | ||
* Represents a unique identifier to prevent non-branded types from being assigned to branded types. | ||
* | ||
* @since 1.4.0 | ||
* @category Model | ||
*/ | ||
export interface Brand<B> extends Brand_<B> {} | ||
|
||
/** | ||
* A newtype that's assignable to its underlying type. | ||
* | ||
* @since 1.4.0 | ||
* @category Model | ||
*/ | ||
export type Branded<A, B> = A & Brand<B> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/** | ||
* A port of `monocle-ts` experimental `Iso` to schemata-ts | ||
* | ||
* @since 1.4.0 | ||
*/ | ||
import { Category2 } from 'fp-ts/Category' | ||
import { flow, identity } from 'fp-ts/function' | ||
import { Invariant2 } from 'fp-ts/Invariant' | ||
import { Semigroupoid2 } from 'fp-ts/Semigroupoid' | ||
|
||
/** | ||
* Represents an isomorphism between two types. | ||
* | ||
* @since 1.4.0 | ||
* @category Models | ||
*/ | ||
export interface Iso<A, B> { | ||
readonly get: (a: A) => B | ||
readonly reverseGet: (b: B) => A | ||
} | ||
|
||
/** | ||
* @since 1.4.0 | ||
* @category Constructors | ||
*/ | ||
export const make: <A, B>(get: (a: A) => B, reverseGet: (b: B) => A) => Iso<A, B> = ( | ||
get, | ||
reverseGet, | ||
) => ({ | ||
get, | ||
reverseGet, | ||
}) | ||
|
||
// Non-pipeables | ||
const imap_: <S, A, B>(iso: Iso<S, A>, f: (a: A) => B, g: (b: B) => A) => Iso<S, B> = ( | ||
iso, | ||
f, | ||
g, | ||
) => ({ | ||
get: flow(iso.get, f), | ||
reverseGet: flow(g, iso.reverseGet), | ||
}) | ||
const compose_: <A, B, C>(bc: Iso<B, C>, ab: Iso<A, B>) => Iso<A, C> = (bc, ab) => ({ | ||
get: flow(ab.get, bc.get), | ||
reverseGet: flow(bc.reverseGet, ab.reverseGet), | ||
}) | ||
|
||
/** | ||
* @since 1.4.0 | ||
* @category Instance Methods | ||
*/ | ||
export const imap: <A, B>( | ||
f: (a: A) => B, | ||
g: (b: B) => A, | ||
) => <S>(iso: Iso<S, A>) => Iso<S, B> = (f, g) => iso => imap_(iso, f, g) | ||
|
||
/** | ||
* @since 1.4.0 | ||
* @category Instance Methods | ||
*/ | ||
export const compose: <B, C>(ab: Iso<B, C>) => <A>(iso: Iso<A, B>) => Iso<A, C> = | ||
bc => ab => | ||
compose_(bc, ab) | ||
|
||
/** | ||
* @since 1.4.0 | ||
* @category Instance Methods | ||
*/ | ||
export const id: <A>() => Iso<A, A> = () => ({ | ||
get: identity, | ||
reverseGet: identity, | ||
}) | ||
|
||
/** | ||
* @since 1.4.0 | ||
* @category Instances | ||
*/ | ||
export const URI = 'schemata-ts/Iso' | ||
|
||
/** | ||
* @since 1.4.0 | ||
* @category Instances | ||
*/ | ||
export type URI = typeof URI | ||
|
||
declare module 'fp-ts/lib/HKT' { | ||
interface URItoKind2<E, A> { | ||
readonly [URI]: Iso<E, A> | ||
} | ||
} | ||
|
||
/** | ||
* @since 1.4.0 | ||
* @category Instances | ||
*/ | ||
export const Invariant: Invariant2<URI> = { | ||
URI, | ||
imap: imap_, | ||
} | ||
|
||
/** | ||
* @since 1.4.0 | ||
* @category Instances | ||
*/ | ||
export const Semigroupoid: Semigroupoid2<URI> = { | ||
URI, | ||
compose: compose_, | ||
} | ||
|
||
/** | ||
* @since 1.4.0 | ||
* @category Instances | ||
*/ | ||
export const Category: Category2<URI> = { | ||
...Semigroupoid, | ||
id, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* A port of `newtype-ts` to schemata-ts | ||
* | ||
* @since 1.4.0 | ||
*/ | ||
|
||
import { unsafeCoerce } from 'fp-ts/function' | ||
import { Iso, make } from 'schemata-ts/iso' | ||
|
||
/** | ||
* Represents a wrapped type that's not assignable to its underlying type. | ||
* | ||
* @since 1.4.0 | ||
* @category Models | ||
*/ | ||
export interface Newtype<URI, A> { | ||
readonly _URI: URI | ||
readonly _A: A | ||
} | ||
|
||
/** | ||
* @since 1.4.0 | ||
* @category Type Helpers | ||
*/ | ||
export type URIOf<N extends Newtype<any, any>> = N['_URI'] | ||
|
||
/** | ||
* @since 1.4.0 | ||
* @category Type Helpers | ||
*/ | ||
export type CarrierOf<N extends Newtype<any, any>> = N['_A'] | ||
|
||
/** | ||
* @since 1.4.0 | ||
* @category Type Helpers | ||
*/ | ||
export type CombineURIs< | ||
N1 extends Newtype<any, any>, | ||
N2 extends Newtype<any, CarrierOf<N1>>, | ||
> = Newtype<URIOf<N1> & URIOf<N2>, CarrierOf<N1>> | ||
|
||
/** | ||
* @since 1.4.0 | ||
* @category Constructors | ||
*/ | ||
export const iso: <Nt extends Newtype<any, any>>() => Iso<Nt, CarrierOf<Nt>> = () => | ||
make(unsafeCoerce, unsafeCoerce) | ||
|
||
/** | ||
* @since 1.4.0 | ||
* @category Constructors | ||
*/ | ||
export const wrap: <Nt extends Newtype<any, any>>() => (a: CarrierOf<Nt>) => Nt = () => | ||
unsafeCoerce | ||
|
||
/** | ||
* @since 1.4.0 | ||
* @category Destructors | ||
*/ | ||
export const unwrap: <Nt extends Newtype<any, any>>() => (a: Nt) => CarrierOf<Nt> = () => | ||
unsafeCoerce |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.