Define and build/create object.
# use npm
$ npm install --save simple-object-factory
# use yarn
$ yarn add simple-object-factory
import ObjectFactory from 'simple-object-factory'
// or, you can import each function individually.
import { define, build, create } from 'simple-object-factory'
type UserAttribute = { id: number, name: string, age: number }
class User {
constructor(public attr: UserAttribute) {
}
}
ObjectFactory
.define<UserAttribute>('user', ({ id }) => ({
id: id,
name: `userName${id}`,
age: 20
}))
.withTrait({
underAge: () => ({
age: 10
}),
senior: () => ({
age: 60
})
})
.onCreate((attr: UserAttribute) => new User(attr))
const userAttribute = build('user')
console.log(userAttribute)
// => { id: 1, name: 'userName1', age: 20 }
const user = create('user', ['underAge'])
console.log(user.attr)
// => { age: 10, id: 2, name: 'userName2' }
This library is broadly dividing into three functions.
-
define
It can define a function, receive a context, and returns an object. Following the definition, it can define the trait, resource, and function for creating an object. -
build
The function is building the object with a defined function. When building, it can pass the trait and optional values. -
create
The function is creating the object with a defined function. To call this function, "onCreate" or "onCreateWithClass" must be called at definition.
DefineContext
contains the following functions described below.
- withTrait
- onCreate
- onCreateWithClass
The definition of Context
and ObjectBuilderType
is as follows.
type Context = {
key: string
id: number
cycleOf: <T>(name: string) => T
}
type ObjectBuilderType<T = any> = (ctx: Context) => T
The values of Context
are as follows.
- key: Its value equals the key, the argument of define.
- id: It increments every time "define" or "create" is called.
- cycleOf: Returns the value of the array defined in "withResource".
It can call after ObjectFactory.define().
The argument is the object; the key is trait name, and the value is function receive Context
and returns an object.
It can call after ObjectFactory.define(). The argument is the object; the key is resource name, and the value is array.
It can call after ObjectFactory.define(). The argument is the function, receive the value of built and returns any value.
It equals onCreaate(attr => new clazz)
Pass the key; it returns the object defined in "define()".
Pass the key; it returns the object defined in "define()" and "onCreate()".