@ipoa/props
v1.0.3
Published
> Access Data Normalized Tool
Downloads
2
Readme
@ipoa/props
Access Data Normalized Tool
Install
npm i @ipoa/props --save
Example Usage
props.normalizeProps(data,props,options)
- data
<Array|Object>
- props
<Object>
- options
<Object>
- deep
<Boolean>
This option is deep handles inner objects.Default:true
. - reverseProps
<Boolean>
This option is reverseprops
option. Default:false
. - replace
<Boolean>
This option is replace new props data records. Default:true
.
- deep
var { normalizeProps } = require('@ipoa/props')
const data1 = [{ name: '电费', list: [{ name: '电表' }] }]
const props = { name: 'label', list: 'children' }
// option: { deep: true, reverseProps: false, replace: true } }
console.log(normalizeProps(data1, props, { replace: true }))
// expect [{ 'children': [{ 'label': '电表' }], 'label': '电费' }]
// option: { deep: false, reverseProps: false, replace: true }
const props = { name: 'label', list: 'children' }
console.log(normalizeProps(data1, props, { replace: true, deep: false }))
// expect [{ 'children': [{ 'name': '电表' }], 'label': '电费' }]
// option: { deep: true, reverseProps: true, replace: true }
const reverseProps = { label: 'name', children: 'list' }
console.log(normalizeProps(data1, reverseProps, { replace: true, reverseProps: true }))
// expect [{ 'children': [{ 'label': '电表' }], 'label': '电费' }]
// option: { deep: true, reverseProps: false, replace: false }
const props = { name: 'label', list: 'children' }
console.log(normalizeProps(data1, props))
// expect [{ 'name': '电费', 'list': [{ 'name': '电表' }], 'label': '电费', 'children': [{ 'name': '电表', 'label': '电表' }] }]
// props parameter with function
const data1 = { name: '电费', list: [{ name: '电表', value: '456' }] }
const props = {
name: 'label', list: {
label: 'children',
handler: (field: any) => {
field.value = Number(field.value)
return field
},
}
}
console.log(normalizeProps(data1, props, { replace: true }))
// expect { 'children': [{ name: '电表', value: 456 }], 'label': '电费' }
props.absorbProps(data,absorb,options)
- data
<Object>
- absorb
<Array>
- options
<Object>
- props
<Object>
Modify the key name of the key or value.Default:{ key: 'label', value: 'value' }
. - propKey
<Boolean|Array>
absorb key.Default:true
- props
var { absorbProps } = require('@ipoa/props')
const data = {
'ADJUST_AMOUNT': '0.00',
'RECEIVED_AMOUNT': '3145.80',
'SUM_AMOUNT': '3145.80',
'RECEIVABLE': '3145.80',
}
// options: { propKey: false }
const absorb1 = ['ADJUST_AMOUNT', 'RECEIVED_AMOUNT', 'SUM_AMOUNT', 'RECEIVABLE']
console.log(absorbProps(data, absorb1))
// expect ['0.00', '3145.80', '3145.80', '3145.80']
// options: { propKey: true }
const absorb1 = ['ADJUST_AMOUNT', 'RECEIVED_AMOUNT']
console.log(absorbProps(data, absorb1, { propKey: true }))
// expect [{ 'label': 'ADJUST_AMOUNT', 'value': '0.00' }, { 'label': 'RECEIVED_AMOUNT', 'value': '3145.80' }]
// options: { propKey: [] }
const absorb1 = ['ADJUST_AMOUNT', 'RECEIVED_AMOUNT']
console.log(absorbProps(data, absorb1, { propKey: ['调整金额', '收到金额'] }))
// expect [{ 'label': '调整金额', 'value': '0.00' }, { 'label': '收到金额', 'value': '3145.80' }]
// options: { propKey: true, props: { key: 'name' } }
const absorb1 = [{ prop: 'ADJUST_AMOUNT', label: '调整金额' }, { prop: 'RECEIVED_AMOUNT' }]
console.log(absorbProps(data, absorb1, { propKey: true, props: { key: 'name' } }))
// [{ 'name': '调整金额', 'value': '0.00' }, { 'name': 'RECEIVED_AMOUNT', 'value': '3145.80' }]
// options: { propKey: true }
const absorb1 = [{ prop: 'ADJUST_AMOUNT', label: '调整金额' }, { prop: 'RECEIVED_AMOUNT' }]
console.log(absorbProps(data, absorb1, { propKey: true, props: { key: 'name' } }))
// [{ 'label': '调整金额', 'value': '0.00' }, { 'label': 'RECEIVED_AMOUNT', 'value': '3145.80' }]
Browser Support
<script src="https://cdn.jsdelivr.net/npm/@ipoa/props@latest/dist/index.global.legacy.js"></script>