obj-gen-9
v1.3.0
Published
Generate objects based on generator and JSON
Downloads
4
Readme
obj-gen-9
This module allows you to generate objects based on the generator and JSON.
中文说明
Install
$ npm i obj-gen-9 --save
Usage
'use strict';
const generate = require('obj-gen-9');
const config = {
$generator: text => {
return function(name) {
console.log(text + ' ' + name + '!');
};
},
$members: [ 'hello', 'hi' ],
};
const greet = generate(config);
greet.hello('sachiko');
greet.hi('momoka');
'use strict';
const generate = require('obj-gen-9');
const config = {
project: {
$generator: (obj, config) => {
if (obj.cmd) {
return function() {
console.log(config.projectName);
console.log('cd ' + this.path);
console.log(obj.cmd);
};
}
return obj.value;
},
$members: [
{
$key: 'run',
cmd: 'node index.js',
}, {
$key: 'test',
cmd: 'node test.js',
}, {
$key: 'path',
value: './',
},
],
projectName: 'TestProject',
},
};
const work = generate(config);
work.project.test();
work.project.run();
work.project.path = './obj-gen';
work.project.test();
More usage can be found in test.js
Attributes
| name | format | description | |:-----|:-------|:------------| | $generator | (member, config)=>{} | Generator, when there is this property under the object, it will generate an object based on $members | | $keygen | (obj)=>{} | Key generator, when member is an object, calculate the key according to member, the default is (obj)=>obj.$key | | $members | Array | Each object in the array will become the member of $generator | | $key | String | When $keygen is not specified, member defaults to $key as key |
Unit tests
npm test