-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
type improvements, added remote and order
- Loading branch information
1 parent
9362f40
commit 2206ad5
Showing
10 changed files
with
179 additions
and
23 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,75 @@ | ||
|
||
|
||
/** | ||
* What should a progression do? | ||
* | ||
* - Be able to create a variant | ||
* - maybe this isn't necessary. | ||
* - do a .compare(a: T, b: T) and get back a number; | ||
*/ | ||
|
||
import {remote, Remote} from './remote'; | ||
import {Func} from './util'; | ||
import {Creators, KeysOf, Property, VariantCreator, VariantModule, VariantOf} from './variant'; | ||
|
||
export enum CompareResult { | ||
Lesser = -1, | ||
Equal, | ||
Greater | ||
} | ||
|
||
/** | ||
* A valid entry for `variantList` | ||
*/ | ||
type validListType = VariantCreator<any, Func, any> | string; | ||
|
||
/** | ||
* Convert entries for a `variantList` to the same type. | ||
*/ | ||
type Variantify<T extends validListType> = T extends string ? VariantCreator<T> : T; | ||
|
||
interface Order<T extends VariantModule<K>, K extends string = 'type'> extends Remote<T, K> { | ||
compare: ( | ||
a: VariantOf<T, undefined, K> | Creators<T, K>[keyof T] | KeysOf<T, K>, | ||
b: VariantOf<T, undefined, K> | Creators<T, K>[keyof T] | KeysOf<T, K>, | ||
) => CompareResult; | ||
index: (a: VariantOf<T, undefined, K> | Creators<T, K>[keyof T] | KeysOf<T, K>) => number; | ||
} | ||
|
||
export function order< | ||
T extends VariantModule<K>, | ||
L extends readonly (KeysOf<T, K> | Property<K, KeysOf<T, K>>)[], | ||
K extends string = 'type' | ||
>(vmod: T, order: L): Order<T, K> { | ||
let rawStringOrder = order.map(i => typeof i === 'string' ? i : i.type); | ||
const keyType = Object.values(vmod)[0].key; | ||
return { | ||
...remote(vmod, keyType), | ||
compare: (a, b) => { | ||
const ai = rawStringOrder.findIndex(i => i === getType(a, keyType)); | ||
const bi = rawStringOrder.findIndex(i => i === getType(b, keyType)); | ||
const diff = ai - bi; | ||
return diff === 0 ? diff : (diff / Math.abs(diff)) as CompareResult; | ||
}, | ||
index: a => rawStringOrder.findIndex(i => i === getType(a)), | ||
} | ||
} | ||
|
||
function getType< | ||
T extends VariantModule<K>, | ||
O extends VariantOf<T, undefined, K>, | ||
C extends Creators<T, K>, | ||
S extends KeysOf<T, K>, | ||
K extends string = 'type', | ||
>(object: O | C | S, typeKey?: K): string { | ||
const key = typeKey ?? 'type' as K; | ||
if (typeof object === 'string') { | ||
return object; | ||
} else { | ||
if (typeof object === 'function') { | ||
return (object as VariantCreator<string, Func, K>).type; | ||
} else { | ||
return (object as Property<K, string>)[key]; | ||
} | ||
} | ||
} |
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,34 @@ | ||
import {match, Handler} from './match'; | ||
import {isType} from './tools'; | ||
import {Property, Variant, VariantModule, VariantOf} from './variant'; | ||
|
||
|
||
type IsFunctions<T extends VariantModule<K>, K extends string = 'type'> = { | ||
[P in keyof T]: <O extends Property<K, string>>(object: O | {} | null | undefined) => object is VariantOf<T, P, K>; | ||
} | ||
function isFunctions<T extends VariantModule<K>, K extends string = 'type'>(vmod: T) { | ||
const keys = Object.keys(vmod) as Array<string & keyof T>; | ||
return keys.reduce((acc, key) => { | ||
return { | ||
...acc, | ||
[key]: isType(key), | ||
} | ||
}, {}) as IsFunctions<T, K>; | ||
} | ||
|
||
export interface Remote<T extends VariantModule<K>, K extends string = 'type'> { | ||
key: K; | ||
is: IsFunctions<T, K>; | ||
new: T; | ||
match: <H extends Handler<T>> (obj: VariantOf<T, undefined, K>, handler: H) => ReturnType<H[keyof H]> | ||
} | ||
|
||
export function remote<T extends VariantModule<K>, K extends string = 'type'>(vmod: T, keyProp?: K): Remote<T, K> { | ||
const key = keyProp ?? 'type' as K; | ||
return { | ||
key, | ||
is: isFunctions(vmod), | ||
new: vmod, | ||
match, | ||
} | ||
} |
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,29 @@ | ||
import {CompareResult, order} from './order'; | ||
import {Animal, cerberus} from './__test__/animal'; | ||
|
||
const rank = order(Animal, [ | ||
'dog', | ||
'cat', | ||
'snake', | ||
]) | ||
|
||
const perseus = rank.new.cat({name: 'Perseus', furnitureDamaged: 0}); | ||
|
||
test('order new obj', () => { | ||
expect(perseus.name).toBe('Perseus'); | ||
expect(perseus.furnitureDamaged).toBe(0); | ||
}) | ||
|
||
test('order compare', () => { | ||
expect(rank.compare(perseus, cerberus)).toBe(CompareResult.Greater); | ||
}) | ||
|
||
test('order compare', () => { | ||
expect(rank.compare(Animal.cat, cerberus)).toBe(CompareResult.Greater); | ||
}) | ||
|
||
test('order index', () => { | ||
expect(rank.index(cerberus)).toBe(0); | ||
expect(rank.index('cat')).toBe(1); | ||
expect(rank.index(Animal.snake)).toBe(2); | ||
}) |
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,31 @@ | ||
import {remote} from '.'; | ||
import {just} from './match'; | ||
import {Animal, cerberus} from './__test__/animal' | ||
|
||
const $Animal = remote(Animal); | ||
|
||
test('remote', () => { | ||
expect($Animal.is.cat(cerberus)).toBeFalsy(); | ||
expect($Animal.is.dog(cerberus)).toBeTruthy(); | ||
}); | ||
|
||
test('remote is narrows', () => { | ||
const a = cerberus as Animal; | ||
if ($Animal.is.cat(a)) { | ||
const result = a.furnitureDamaged; | ||
// this object doesn't have this type, but I can access it. Narrowing works. | ||
expect(result).toBeUndefined(); | ||
} else { | ||
const result = a.name; | ||
expect(result).toBe('Cerberus'); | ||
} | ||
}) | ||
|
||
|
||
test('remote match', () => { | ||
const result = $Animal.match(cerberus, { | ||
cat: just(4), | ||
dog: just(5), | ||
snake: just('jo'), | ||
}) | ||
}) |
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