Skip to content

Commit

Permalink
added get to sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
paarthenon committed Dec 31, 2020
1 parent 9442ab7 commit 5bc2161
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
27 changes: 8 additions & 19 deletions src/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ type validListType = VariantCreator<string, Func, any> | string;
type Variantify<T extends validListType> = T extends string ? VariantCreator<T> : T;

/**
* @alpha
*
*/
// L extends ValidTypeInput<K, KeysOf<T, K>>[],
export interface Sequence<
T extends VariantModule<K>,
L extends ValidTypeInput<K, KeysOf<T, K>>[],
Expand All @@ -65,7 +65,9 @@ export interface Sequence<
* Get the index of some type in the sequence.
*/
index: (a: ValidTypeInput<K, KeysOf<RT, K>>) => number;
get: (index: number) => T[keyof T];
readonly length: number;
readonly types: KeysOf<T, K>[];
}

function _sequenceFromScratch<L extends ValidTypeCreatorInput<'type', T>[], T extends string>(list: L): Sequence<
Expand Down Expand Up @@ -103,7 +105,12 @@ function _sequence<
const diff = ai - bi;
return diff === 0 ? diff : (diff / Math.abs(diff)) as CompareResult;
},
get(i: number) {
const type = this.types[i];
return (this.new as VariantModule<K>)[type] as any;
},
index: a => rawStringOrder.findIndex(i => i === getType(a)),
types: rawStringOrder,
}
}

Expand Down Expand Up @@ -144,21 +151,3 @@ function getType<
}
}
}


type AAA = 'a' | 'b';

type aa = [...(AAA[])]

type ExactLength<T extends readonly ValidTypeInput[], N extends number> = T['length'] extends N ? T : never;

// export function doThing<L extends [...K]>(list: L): Record<ExtractKeys<L>, unknown> extends Record<AAA, unknown> ? true : false {
// return undefined as any;
// }

// type aaaa = Record<AAA, unknown>;
// const a = doThing([
// 'a',
// 'b',
// ])

5 changes: 5 additions & 0 deletions src/variant.order.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ test('order index', () => {
expect(rank.index(cerberus)).toBe(0);
expect(rank.index('cat')).toBe(1);
expect(rank.index(Animal.snake)).toBe(2);
})

test('get', () => {
expect(rank.get(0).type).toBe('dog');
expect(rank.types[0]).toBe('dog');
})

0 comments on commit 5bc2161

Please sign in to comment.