0.2.0 • Published 6 years ago
@trop/atom_parser v0.2.0
@trop/atom_parser
Parse string to primitive types such as number, boolean. It is quite simple but useful, save you from bored coding.
Tutorial
const atom_parser = require('@trop/atom_parser')
let target = {
name: 'Spider Man',
age: '26',
stuffs: 'bag,spider,armor',
address: {
country: 'USA',
country_code: '1'
}
}
let schema = [
['name', 'string'],
['age', 'number'],
['is_dead', 'boolean', false],
['stuffs', 'array<string>'],
['address.country', 'string'],
['address.country_code', 'number']
]
atom_parser.apply(target, schema)APIs
atom_parser.apply(target, schema)
Input
target/Object- Contains attributes need to parse as primitive typesschema/Array<Array<String, String, any>>- Contains list of schemasschema[][0]/String- Path of attribute, see argument path fromlodash.set()for mor informationschema[][1]/String- Type of attribute, one of:string- Do nothingnumber- Accept string contains digits and few special symbolsboolean- Accept string 'true' or 'false'array<string>- Accept string contains items, each items is divide by commaarray<number>array<boolean>
schema[][2]/String/undefined- Default value for attribute in case it is undefined
Process
- With each attributes which is specifics in schema, parse to target types
- Set parsed value to target immediately
- If an attribute is undefined then:
- If default value is specifics then set it to default value
- If default value is not specifics then do nothing
Output
undefined
Exception
TypeError- Source value is not a stringTypeError- Invalid target typeTypeError- Invalid number formatTypeError- Invalid boolean format