Skip to content

Commit

Permalink
auto model
Browse files Browse the repository at this point in the history
  • Loading branch information
farwayer committed Sep 2, 2019
1 parent 02c6a66 commit 8906683
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ export declare interface Model<T extends Function> {
is(thing: any): boolean
}

export declare type ModelOptions = {
auto: boolean,
}
export declare function model<T extends Function>(target: T): T & Model<T> & PropertyDecorator
export declare function model(name: string): typeof model
export declare function model(name?: string, options?: ModelOptions): typeof model
export declare function model(options?: ModelOptions): typeof model
export declare function prop(...args: any[]): any
export declare const view: MethodDecorator

Expand Down
16 changes: 14 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ const ExcludeKeys = [
TypeKey, PropsKey, ViewsKey,
]

export const model = classDecorator((Class, name = Class.name) => {
export const model = classDecorator((
Class,
name = Class.name,
options = {auto: false},
) => {
if (is.obj(name)) {
options = merge(options, name)
name = Class.name
}

const {preProcessSnapshot, postProcessSnapshot} = Class
// TS class property initializers and defaults from constructor
const values = new Class()
Expand Down Expand Up @@ -67,7 +76,10 @@ export const model = classDecorator((Class, name = Class.name) => {

Model = Model.preProcessSnapshot(snapshot => {
snapshot = preProcessSnapshot ? preProcessSnapshot(snapshot) : snapshot
if (!is.obj(snapshot)) return snapshot
if (!is.obj(snapshot)) {
if (!options.auto) return snapshot
snapshot = {}
}
return merge(values, snapshot)
})

Expand Down
14 changes: 14 additions & 0 deletions test/js/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ describe('basic', () => {
getMembers(base).name.should.be.equal('User')
})

it('auto model', () => {
@model({auto: true}) class Base {
@t.str str = 'str'
}

@model class Container {
@Base base
}

const cont = Container.create()
cont.should.have.property('base')
cont.base.str.should.be.equal('str')
})

it('prop', () => {
@model class Base {
@prop(t.str) prop
Expand Down
16 changes: 16 additions & 0 deletions test/ts/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ describe('basic', () => {
getMembers(base).name.should.be.equal('User')
})

it('auto model', () => {
class MBase {
@t.str str = 'str'
}
const Base = model({auto: true})(MBase)

class MContainer {
@Base base
}
const Container = model(MContainer)

const cont = Container.create()
cont.should.have.property('base')
cont.base.str.should.be.equal('str')
})

it('prop', () => {
class MBase {
@prop(t.str) prop
Expand Down

0 comments on commit 8906683

Please sign in to comment.