-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
245 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { AttributeDefinition, AttributeType, Dict } from './types' | ||
|
||
export type AttributesOf<T> = T extends Trait<infer A, any> ? A : never | ||
export type OptionsOf<T> = T extends Trait<any, infer O> ? O : never | ||
|
||
export class Trait<TRes extends object = Dict, TTrans extends object = Dict> { | ||
static create<TRes extends object = Dict, TTrans extends object = Dict>() { | ||
return new Trait<TRes, TTrans>() | ||
} | ||
|
||
static compose<TRes extends object = Dict, TTrans extends object = Dict>( | ||
...traits: Trait<Partial<TRes>, Partial<TTrans>>[] | ||
) { | ||
return traits.reduce((composedTrait, trait) => composedTrait.compose(trait)) | ||
} | ||
|
||
constructor(public attributes: AttributeDefinition<TRes, TTrans>[] = []) {} | ||
|
||
compose<F extends Trait<any, any>>( | ||
trait: F | ||
): Trait<TRes & AttributesOf<F>, TTrans & OptionsOf<F>> { | ||
type AF = AttributesOf<F> | ||
type AO = OptionsOf<F> | ||
type AR = TRes & AttributesOf<F> | ||
type OR = TTrans & OptionsOf<F> | ||
return new Trait<AR, OR>( | ||
(this.attributes as ( | ||
| AttributeDefinition<TRes, TTrans> | ||
| AttributeDefinition<AF, AO> | ||
)[]).concat(trait.attributes) | ||
) | ||
} | ||
|
||
seq<K extends keyof TRes>( | ||
key: K | ||
): (definition: (n: number, attrs: TRes & TTrans) => TRes[K]) => Trait<TRes, TTrans> { | ||
return definition => { | ||
return new Trait( | ||
this.attributes.concat({ type: AttributeType.Sequence, key, get: definition, seq: 0 }) | ||
) | ||
} | ||
} | ||
|
||
opt<K extends keyof TTrans>( | ||
key: K | ||
): (definition: (attrs: TRes & TTrans) => TTrans[K]) => Trait<TRes, TTrans> { | ||
return definition => { | ||
return new Trait( | ||
this.attributes.concat({ type: AttributeType.Transient, key, get: definition }) | ||
) | ||
} | ||
} | ||
|
||
attr<K extends keyof TRes>( | ||
key: K | ||
): (definition: (attrs: TRes & TTrans) => TRes[K]) => Trait<TRes, TTrans> { | ||
return definition => { | ||
return new Trait( | ||
this.attributes.concat({ type: AttributeType.Property, key, get: definition }) | ||
) | ||
} | ||
} | ||
} |
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,43 @@ | ||
import { Factory } from './factory' | ||
import { Trait } from './trait' | ||
|
||
export type Dict = { [key: string]: any } | ||
|
||
export type AfterCallback<TRes, TTrans, R extends TRes = TRes> = ( | ||
entity: TRes, | ||
evaluator: TRes & TTrans | ||
) => R | ||
|
||
export enum AttributeType { | ||
Sequence = 'SEQ', | ||
Property = 'PROP', | ||
Transient = 'TRANS' | ||
} | ||
|
||
export type AttributeDefinition<TRes = Dict, TTrans = Dict> = | ||
| PropertyDefinition<TRes, TTrans> | ||
| SequenceDefinition<TRes, TTrans> | ||
| TransientDefinition<TRes, TTrans> | ||
|
||
export type PropertyDefinition<TRes = Dict, TTrans = Dict> = { | ||
type: AttributeType.Property | ||
key: keyof TRes | ||
get: (attrs: TRes & TTrans) => TRes[keyof TRes] | ||
} | ||
export type SequenceDefinition<TRes = Dict, TTrans = Dict> = { | ||
type: AttributeType.Sequence | ||
key: keyof TRes | ||
get: (n: number, attrs: TRes & TTrans) => TRes[keyof TRes] | ||
seq: number | ||
} | ||
export type TransientDefinition<TRes = Dict, TTrans = Dict> = { | ||
type: AttributeType.Transient | ||
key: keyof TTrans | ||
get: (attrs: TRes & TTrans) => TTrans[keyof TTrans] | ||
} | ||
|
||
export type TraitOf<F> = Trait<Partial<AttributesOf<F>>, Partial<OptionsOf<F>>> | ||
export type AttributesOf<F> = F extends Factory<infer A, any> ? A : never | ||
export type OptionsOf<F> = F extends Factory<any, infer O> ? O : never | ||
|
||
export type TraitFactory<F> = (t: TraitOf<F>) => TraitOf<F> |
Oops, something went wrong.