Skip to content

Commit

Permalink
Convert IndexBy
Browse files Browse the repository at this point in the history
  • Loading branch information
alex.gill@penna.com committed Dec 17, 2018
1 parent 65428d1 commit 6f8e3e7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 26 deletions.
30 changes: 11 additions & 19 deletions src/IndexBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,21 @@ import { MaybeSelector } from "./MaybeSelector";
import { Selector } from "./Selector";
import { GetSignature } from "./Get";
import { StringProperty } from "./util";
import { Composable } from "./Composable";
import { Dimensionality, Structure } from "./Discriminants";

export type IndexBy<A, B, Params extends {} = {}> =
export type IndexByOverloads<D extends Dimensionality, S extends Structure, A, B, Params extends {}> =
B extends { [key: string]: infer C }
? IndexByOverloads<A, B, C, Params>
: never

export interface IndexByOverloads<A, B, C, Params> {
<K extends string>(key: K): MaybeSelector<A, C, Params & StringProperty<K>>
<K extends string>(key: K, defaultValue: C): Selector<A, C, Params & StringProperty<K>>
<K extends string>(key: K, getDefault: GetSignature<B, C, Params & StringProperty<K>>): Selector<A, C, Params & StringProperty<K>>
}

export type IndexByMaybe<A, B, Params extends {} = {}> =
B extends { [key: string]: infer C }
? IndexByMaybeOverloads<A, B, C, Params>
? {
<K extends string>(key: K)
: Composable.ComposeResult<A, C, Params & StringProperty<K>, D, Dimensionality.Maybe, S, Structure.Select>
<K extends string>(key: K, defaultValue: C)
: Composable.ComposeResult<A, C, Params & StringProperty<K>, D, Dimensionality.Single, S, Structure.Select>
<K extends string>(key: K, getDefault: GetSignature<B, C, Params & StringProperty<K>>)
: Composable.ComposeResult<A, C, Params & StringProperty<K>, D, Dimensionality.Single, S, Structure.Select>
}
: never

export interface IndexByMaybeOverloads<A, B, C, Params> {
<K extends string>(key: K): MaybeSelector<A, C, Params & StringProperty<K>>
<K extends string>(key: K, defaultValue: C): MaybeSelector<A, C, Params & StringProperty<K>>
<K extends string>(key: K, getDefault: GetSignature<B, C, Params & StringProperty<K>>): MaybeSelector<A, C, Params & StringProperty<K>>
}

export namespace IndexBy {
const nullIfUndefined = <T>(x: T) => x === undefined ? null : x

Expand Down
1 change: 0 additions & 1 deletion src/MaybeSelector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Get } from './Get';
import { MaybeSelector } from './MaybeSelector';
import { Converter } from './Converter';
import { MaybeConverter } from './MaybeConverter';
import { IndexBy } from './IndexBy';
import { Biselect } from '.';
import { MaybeGet } from './MaybeGet';

Expand Down
4 changes: 2 additions & 2 deletions src/MaybeSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Selector } from './Selector'
import { MaybeConverter } from './MaybeConverter'
import { Converter } from './Converter'
import { MaybeSelectorPropOverloads, Prop } from './Prop'
import { IndexBy, IndexByMaybe } from './IndexBy'
import { IndexBy, IndexByOverloads } from './IndexBy'
import { Choose, ChooseOverloads } from './Choose';
import { Extension } from './Extension';
import { Memoize } from './Memoize';
Expand Down Expand Up @@ -36,7 +36,7 @@ export interface MaybeSelector<A, B, Params extends {} = {}> {
modify: Modify<A, B, Params>
compose: MaybeSelectorCompose<A, B, Params>
prop: MaybeSelectorPropOverloads<A, B, Params>
indexBy: IndexByMaybe<A, B, Params>
indexBy: IndexByOverloads<Dimensionality.Maybe, Structure.Select, A, B, Params>
choose: ChooseOverloads<Dimensionality.Maybe, Structure.Select, A, B, Params>
ifDefined: IfDefinedOverloads<Dimensionality.Maybe, Structure.Select, A, B, Params>
merge: Merge<A, B, Params>
Expand Down
5 changes: 3 additions & 2 deletions src/Root.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { IndexBy } from "./IndexBy";
import { IndexBy, IndexByOverloads } from "./IndexBy";
import { SelectorPropOverloads, Prop } from "./Prop";
import { MaybeConverter } from "./MaybeConverter";
import { Choose } from "./Choose";
import { Extension } from "./Extension";
import { Memoize } from "./Memoize";
import { Debug } from "./Debug";
import { Dimensionality, Structure } from './Discriminants';

export interface Root<A> {
indexBy: IndexBy<A, A>
indexBy: IndexByOverloads<Dimensionality.Single, Structure.Convert, A, A, {}>
prop: SelectorPropOverloads<A, A>
choose<B extends A>(typeGuard: (a: A) => a is B): MaybeConverter<A, B>
extend: (newExt: Extension) => Root<A>
Expand Down
4 changes: 2 additions & 2 deletions src/Selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { MaybeConverter } from './MaybeConverter'
import { Converter } from './Converter'
import { Composable } from './Composable'
import { SelectorPropOverloads, Prop } from './Prop'
import { IndexBy } from './IndexBy';
import { IndexBy, IndexByOverloads } from './IndexBy';
import { Choose, ChooseOverloads } from './Choose';
import { Extension } from './Extension';
import { Memoize } from './Memoize';
Expand Down Expand Up @@ -35,7 +35,7 @@ export interface Selector<A, B, Params extends {} = {}> {
modify: Modify<A, B, Params>
compose: SelectorCompose<A, B, Params>
prop: SelectorPropOverloads<A, B, Params>
indexBy: IndexBy<A, B, Params>
indexBy: IndexByOverloads<Dimensionality.Single, Structure.Select, A, B, Params>
choose: ChooseOverloads<Dimensionality.Single, Structure.Select, A, B, Params>
ifDefined: IfDefinedOverloads<Dimensionality.Single, Structure.Select, A, B, Params>
merge: Merge<A, B, Params>
Expand Down

0 comments on commit 6f8e3e7

Please sign in to comment.