-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add lensesFromTuple and prismsFromUnion (#5)
- Loading branch information
Showing
7 changed files
with
132 additions
and
6 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
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,50 @@ | ||
import * as t from 'io-ts' | ||
import { Lens } from 'monocle-ts' | ||
|
||
export type LensesFromTuple5<T extends t.TupleType<[t.Any, t.Any, t.Any, t.Any, t.Any], any>> = { | ||
'L0': Lens<t.TypeOf<T>, t.TypeOf<T['types']['0']>> | ||
'L1': Lens<t.TypeOf<T>, t.TypeOf<T['types']['1']>> | ||
'L2': Lens<t.TypeOf<T>, t.TypeOf<T['types']['2']>> | ||
'L3': Lens<t.TypeOf<T>, t.TypeOf<T['types']['3']>> | ||
'L4': Lens<t.TypeOf<T>, t.TypeOf<T['types']['4']>> | ||
} | ||
export type LensesFromTuple4<T extends t.TupleType<[t.Any, t.Any, t.Any, t.Any], any>> = { | ||
'L0': Lens<t.TypeOf<T>, t.TypeOf<T['types']['0']>> | ||
'L1': Lens<t.TypeOf<T>, t.TypeOf<T['types']['1']>> | ||
'L2': Lens<t.TypeOf<T>, t.TypeOf<T['types']['2']>> | ||
'L3': Lens<t.TypeOf<T>, t.TypeOf<T['types']['3']>> | ||
} | ||
export type LensesFromTuple3<T extends t.TupleType<[t.Any, t.Any, t.Any], any>> = { | ||
'L0': Lens<t.TypeOf<T>, t.TypeOf<T['types']['0']>> | ||
'L1': Lens<t.TypeOf<T>, t.TypeOf<T['types']['1']>> | ||
'L2': Lens<t.TypeOf<T>, t.TypeOf<T['types']['2']>> | ||
} | ||
export type LensesFromTuple2<T extends t.TupleType<[t.Any, t.Any], any>> = { | ||
'L0': Lens<t.TypeOf<T>, t.TypeOf<T['types']['0']>> | ||
'L1': Lens<t.TypeOf<T>, t.TypeOf<T['types']['1']>> | ||
} | ||
export type LensesFromTuple1<T extends t.TupleType<[t.Any], any>> = { | ||
'L0': Lens<t.TypeOf<T>, t.TypeOf<T['types']['0']>> | ||
} | ||
|
||
export function lensesFromTuple<T extends t.TupleType<[t.Any, t.Any, t.Any, t.Any, t.Any], any>>( | ||
type: T | ||
): LensesFromTuple5<T> | ||
export function lensesFromTuple<T extends t.TupleType<[t.Any, t.Any, t.Any, t.Any], any>>(type: T): LensesFromTuple4<T> | ||
export function lensesFromTuple<T extends t.TupleType<[t.Any, t.Any, t.Any], any>>(type: T): LensesFromTuple3<T> | ||
export function lensesFromTuple<T extends t.TupleType<[t.Any, t.Any], any>>(type: T): LensesFromTuple2<T> | ||
export function lensesFromTuple<T extends t.TupleType<[t.Any], any>>(type: T): LensesFromTuple1<T> | ||
export function lensesFromTuple<T extends t.TupleType<any, any>>(type: T): { [key: string]: Lens<any, any> } { | ||
const r: { [key: string]: Lens<any, any> } = {} | ||
for (let i = 0; i < type.types.length; i++) { | ||
r['L' + i] = new Lens( | ||
s => s[i], | ||
a => s => { | ||
const s2 = s.slice() | ||
s2[i] = a | ||
return s2 | ||
} | ||
) | ||
} | ||
return r | ||
} |
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,39 @@ | ||
import * as t from 'io-ts' | ||
import { Prism } from 'monocle-ts' | ||
|
||
export type PrismsFromUnion5<T extends t.UnionType<[t.Any, t.Any, t.Any, t.Any, t.Any]>> = { | ||
'P0': Prism<t.TypeOf<T>, t.TypeOf<T['types']['0']>> | ||
'P1': Prism<t.TypeOf<T>, t.TypeOf<T['types']['1']>> | ||
'P2': Prism<t.TypeOf<T>, t.TypeOf<T['types']['2']>> | ||
'P3': Prism<t.TypeOf<T>, t.TypeOf<T['types']['3']>> | ||
'P4': Prism<t.TypeOf<T>, t.TypeOf<T['types']['4']>> | ||
} | ||
export type PrismsFromUnion4<T extends t.UnionType<[t.Any, t.Any, t.Any, t.Any]>> = { | ||
'P0': Prism<t.TypeOf<T>, t.TypeOf<T['types']['0']>> | ||
'P1': Prism<t.TypeOf<T>, t.TypeOf<T['types']['1']>> | ||
'P2': Prism<t.TypeOf<T>, t.TypeOf<T['types']['2']>> | ||
'P3': Prism<t.TypeOf<T>, t.TypeOf<T['types']['3']>> | ||
} | ||
export type PrismsFromUnion3<T extends t.UnionType<[t.Any, t.Any, t.Any]>> = { | ||
'P0': Prism<t.TypeOf<T>, t.TypeOf<T['types']['0']>> | ||
'P1': Prism<t.TypeOf<T>, t.TypeOf<T['types']['1']>> | ||
'P2': Prism<t.TypeOf<T>, t.TypeOf<T['types']['2']>> | ||
} | ||
export type PrismsFromUnion2<T extends t.UnionType<[t.Any, t.Any]>> = { | ||
'P0': Prism<t.TypeOf<T>, t.TypeOf<T['types']['0']>> | ||
'P1': Prism<t.TypeOf<T>, t.TypeOf<T['types']['1']>> | ||
} | ||
|
||
export function prismsFromUnion<T extends t.UnionType<[t.Any, t.Any, t.Any, t.Any, t.Any]>>( | ||
type: T | ||
): PrismsFromUnion5<T> | ||
export function prismsFromUnion<T extends t.UnionType<[t.Any, t.Any, t.Any, t.Any]>>(type: T): PrismsFromUnion4<T> | ||
export function prismsFromUnion<T extends t.UnionType<[t.Any, t.Any, t.Any]>>(type: T): PrismsFromUnion3<T> | ||
export function prismsFromUnion<T extends t.UnionType<[t.Any, t.Any]>>(type: T): PrismsFromUnion2<T> | ||
export function prismsFromUnion<T extends t.UnionType<any>>(type: T): { [key: string]: Prism<any, any> } { | ||
const r: { [key: string]: Prism<any, any> } = {} | ||
for (let i = 0; i < type.types.length; i++) { | ||
r['P' + i] = new Prism(s => t.validate(s, type.types[i]).toOption(), a => a) | ||
} | ||
return r | ||
} |
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