if-option-generator
v1.0.6
Published
扁平配置生成器
Downloads
2
Readme
Option Generator Usage
OptionGenerator.g(config, seperator='.')
or OptionGenerator.generateOption(config, '.')
import OptionGenerator from 'if-option-generator'
const generator = new OptionGenerator({});
let option = generator.g({
'mails.summary.title': 'Hi World',
'persons.0.title': 'Alibob',
'persons.0.age': '18',
'persons.1.title': 'Jack',
'persons.1.age': '19',
}, '.')
console.log(option)
Print Result
{
mails: { summary: { title: 'Hi World' } },
persons: [ { title: 'Alibob', age: '18' }, { title: 'Jack', age: '19' } ]
}
Update option
update single key
generator.setV(key, value, target=this.option)
// Update
generator.setV('mails.summary.title', 'Update Test by setV')
// It will create some structure {skills: [ { skillName: 'Sport'} ]}
generator.setV('skills.0.skillName', 'Sport')
generator.setGroupV(group, target=this.option)
generator.setGroupV({
'mails.summary.title': 'Update Text',
})
As result :
{
mails: { summary: { title: 'Update Text' } }, // update
persons: [ { title: 'Alibob', age: '18' }, { title: 'Jack', age: '19' } ]
}
Export plain-object
generator.formatAsConfig(target=this.option, seperator='.')
let plain_config = generator.formatAsConfig()
console.log(plain_config)
{
'mails.summary.title': 'Update Text',
'persons.0.title': 'Alibob',
'persons.0.age': '18',
'persons.1.title': 'Jack',
'persons.1.age': '19'
}
query a struct
getV(key, target)
generator.getV('mails.summary') // { title: 'Update Text' }
generator.getV('mails.summary.title') // 'Update Text'
generator.getV('persons.0') // [...]
delete a struct
removeV(key, target)
generator.removeV('mails.summary')
// mails: {}
generator.removeV('persons.0')
// persons: [{title: 'Jack', age: '19'}]
Reactive with Vue 3
const optgtr = new OptionGenerator(reactive({}))
// optgtr.option will be reative
Why this?
Redis / Firebase / NoSql...