数据转换器,解耦前后端开发,提升开发效率
✅ why we need ducker-model ? see the article
import Model from 'ducker-model'
// 1.定义property
const property = {
id: String,
name: Number,
avatar: Object,
}
// 2.实例化model
const instanceModel = new Model(property)
// 3.定义数据源
const dataSource = {
id: 123,
name: 'cuiyuteng',
avatar: {uri:'http://xxxx.png'}
}
// 4.调用objectWithKeyValues方法解析数据
const modelData = instanceModel.objectWithKeyValues(dataSource)
// modelData--> {"id":"","name":0,"avatar":{uri:'http://xxxx.png'}}
import Model from 'ducker-model'
// 1.定义property
const property = {
id: String,
name: String,
avatar: String,
loginTime: String,
price: String,
info: {
sex: Number,
real: {
real_name: String
}
}
}
// 2.定义replacedKeyFromPropertyName
const replacedKeyFromPropertyName = {
id: {
property: "uuid",
defaultValue: '100'
},
name: "buyer.shopinfo.nickname",
loginTime: {
property: "loginTime",
format: "l"
},
price: {
property: "price",
unit: 'B'
},
avatar: {
property: [
"avatar", "file.avatar"
],
computed: ([a0, a1]) => {
return a0 || a1 || ''
}
},
info: {
sex: {
property: "file.sex"
},
real: {
real_name: {
property: "file.real.real_name"
}
}
}
}
// 3.实例化model
const instanceModel = new Model(property,replacedKeyFromPropertyName)
// 4.定义数据源
const dataSource = {
uuid: 123,
buyer: {
shopinfo: {
nickname: "张三"
}
},
loginTime: "1566978591904",
price: "12300",
avatar: 'http://a.png',
file: {
avatar: 'http://b.png',
sex: 1,
real: {
real_name: 'P'
}
}
}
// 5.调用objectWithKeyValues方法解析数据
const modelData = instanceModel.objectWithKeyValues(dataSource)
// modelData--> {"id":"100","name":"张三","avatar":"http://a.png","loginTime":"2019-08-28","price":"123.00","info":{"sex":1,"real":{"real_name":"P"}}}
import Model, { valueForPath } from 'ducker-model'
// 1.定义property
const property = {
uid: valueForPath(Number, "user.id"),
}
// 2.实例化model
const instanceModel = new Model(property)
// 3.定义数据源
const dataSource = {
user: {
id: 123456
},
}
// 4.调用objectWithKeyValues方法解析数据
const modelData = instanceModel.objectWithKeyValues(dataSource)
// modelData--> {"uid":123456}
import Model from 'ducker-model'
// 1.定义property
const property = {
id: String,
name: String,
avatar: String,
}
// 2.定义replacedKeyFromPropertyName
const replacedKeyFromPropertyName = {
id: {
property: "uuid",
},
name: "buyer.shopinfo.nickname",
avatar: {
property: "file.url"
},
}
// 3.实例化model
const instanceModel = new Model(property,replacedKeyFromPropertyName)
// 4.定义数据源
const dataSource = {
uuid: '123456',
buyer:{
shopinfo:{
nickname: 'xxx'
}
},
file: {
url:'http://xxxx'
},
}
// 5.调用objectArrayWithKeyValuesArray方法解析数据
const modelData = instanceModel.objectArrayWithKeyValuesArray([dataSource,dataSource,dataSource])
// modelData--> [{"id":"123456","name":"xxx","avatar":"http://xxxx"},{"id":"123456","name":"xxx","avatar":"http://xxxx"},{"id":"123456","name":"xxx","avatar":"http://xxxx"}]
import Model, { valueWithArray } from 'ducker-model'
// 1.定义property
const property = {
data: valueWithArray({
time: String,
to: Number
}),
source: valueWithArray(String),
object: valueWithArray(Object),
}
// 2.定义replacedKeyFromPropertyName
const replacedKeyFromPropertyName = {
data: {
property: "data",
children: {
time: {
property: "time",
},
to: {
property: "to",
},
}
},
source: {
property: "datasource",
},
object: {
property: "objectDataSource",
},
}
// 3.实例化model
const instanceModel = new Model(property,replacedKeyFromPropertyName)
// 4.定义数据源
const dataSource = {
data: [{
time: '1231512313',
to: 'troila'
}],
datasource: ['1', '2', '3'],
objectDataSource: [{ a: 1 }, { b: 2 }],
}
// 5.调用objectWithKeyValues方法解析数据
const modelData = instanceModel.objectWithKeyValues(dataSource)
// modelData--> {"data":[{"time":"1231512313","to":0}],"source":["1","2","3"],"object":[{ "a": 1 }, { "b": 2 }]}
import Model, { valueForPathWithArray } from 'ducker-model'
// 1.定义property
const property = {
test: valueForPathWithArray(Number, "testDataSource"),
}
// 3.实例化model
const instanceModel = new Model(property)
// 4.定义数据源
const dataSource = {
testDataSource: [4, 5, 6],
}
// 5.调用objectWithKeyValues方法解析数据
const modelData = instanceModel.objectWithKeyValues(dataSource)
// modelData--> {"test":[4,5,6]}
import Model, { valueWithArray } from 'ducker-model'
// 1.定义property
const property = {
lastTest: valueWithArray({
title: String,
data: valueWithArray({
price: Number,
content: String
})
}),
}
// 2.定义replacedKeyFromPropertyName
const replacedKeyFromPropertyName = {
lastTest: {
property: "order",
children: {
title: {
property: "title"
},
data: {
property: "dataSource",
children: {
price: {
property: "Price",
},
content: {
property: "mrk",
},
}
}
},
}
}
// 3.实例化model
const instanceModel = new Model(property,replacedKeyFromPropertyName)
// 4.定义数据源
const dataSource = {
order: [{
title: 'order-1',
dataSource: [{
Price: 100,
mrk: '这是订单1'
}, {
Price: 200,
mrk: '这是订单2'
}]
}]
}
// 5.调用objectWithKeyValues方法解析数据
const modelData = instanceModel.objectWithKeyValues(dataSource)
// modelData--> {"lastTest":[{"title":"order-1",data:[{"price":100,"content":"这是订单1"},{"price":200,"content":"这是订单2"}]}]}
objectWithKeyValues
- params: Object
- return: Object
const modelData = instanceModel.objectWithKeyValues(dataSource)
objectArrayWithKeyValuesArray
- params: Array
- return: Array
const modelData = instanceModel.objectArrayWithKeyValuesArray([dataSource,dataSource,dataSource])
-
type为Date的属性,增加format字段,支持多种内置数据格式,默认为"l",可以选择的格式如下:
- "l": "YYYY-MM-DD",
- "ll": "YYYY 年 MM 月 DD 日",
- "k": "YYYY-MM-DD hh:mm",
- "kk": "YYYY 年 MM 月 DD 日 hh 点 mm 分",
- "kkk": "YYYY 年 MM 月 DD 日 hh 点 mm 分 q",
- "f": "YYYY-MM-DD hh:mm:ss",
- "ff": "YYYY 年 MM 月 DD 日 hh 点 mm 分 ss 秒",
- "fff": "YYYY 年 MM 月 DD 日 hh 点 mm 分 ss 秒 星期 w",
- "n": "MM-DD",
- "nn": "MM 月 DD 日"
-
添加了unit字段的,代表该属性值表示一个价格,模型内置十、百、千、万单位,可以快捷的将价格字段进行转换,例如1000 -> 100
-
属性定义增加computed,值为函数,可以用来自定义数据格式化处理
-
property,值可以为一个数组,传入多个路径,此时可以通过定义 computed 方法来组合计算值
👤 skinner
- Github: @simplefeel
Give a ⭐️ if this project helped you!
Thanks goes to these wonderful people (emoji key):
李杰 📖 |
This project follows the all-contributors specification. Contributions of any kind welcome!