A library that consumes strings and converts them into string definitions.
Additionally, it formats and adds an argument string for easy code generating.
npm i --save field-string-parser
or yarn add field-string-parser
Usage is pretty straight forward. You can reuse instances, or use the static method.
Fields can be passed in using JSON, or the ezon format.
Examples of valid formats:
'username,password:(size:50),active:boolean'
or
'username:string,password:field(size:50),active:field(type:boolean)'
Here's an example using the static parse method.
const parsedFields = FieldStringParser.parse('username,password:(size:50),active:boolean');
console.log(parsedFields);
// Generates the following
{
username: {
argumentString: '{ type: \'string\' }',
definition : { type: 'string' }
},
password: {
argumentString: '{ size: 50, type: \'string\' }',
definition : { size: 50, type: 'string' }
},
active : {
argumentString: '{ type: \'boolean\' }',
definition : { type: 'boolean' }
}
}
Here's an example using the instance approach.
const fieldParser = new FieldParser('username,password:(size:50),active:boolean');
const parsedFields = fieldStringParser.parse();
console.log(parsedFields);
// Generates the following
{
username: {
argumentString: '{ type: \'string\' }',
definition : { type: 'string' }
},
password: {
argumentString: '{ size: 50, type: \'string\' }',
definition : { size: 50, type: 'string' }
},
active : {
argumentString: '{ type: \'boolean\' }',
definition : { type: 'boolean' }
}
}
MIT