1.0.0 • Published 5 months ago

fancy-value-parser v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

What?

Transform fancy string values to usable JS objects.
Helpful for parsing HTML attribute values in custom elements/web components.

import parseOption from 'fancy-value-parser'

parseOption('foo')       // { type: null, value: 'foo' }
parseOption('foo:666')   // { type: 'pair', foo: 666, value: [{⋯}, {⋯}] }
parseOption('0-42')      // { type: 'range', value: [0, 42] }
parseOption('true')      // { type: 'boolean', value: true }
parseOption('/foo/')     // { type: 'regex', value: /foo/ }
parseOption('{foo,bar}') // { type: 'set', value: [{⋯}, {⋯}] }

In / Out

in (as a string)out (structured JS)
foobar{ type: null, value: 'foobar' }
'foobar'{ type: 'string', value: 'foobar' }
/regex/{ type: 'regex', value: /regex/ }
303{ type: 'number', value: 303 }
3-5{ type: 'range', value: [3, 5] }
true{ type: 'boolean', value: true }
foo:bar{ type: 'pair', foo: 'bar', value: [{⋯}, {⋯}] }
{1, /no[pe]/, foo:bar}{ type: 'set', value: [{⋯}, {⋯}, {⋯}] }

Notes

  • Various types can be combined with simple pairs and sets.
  • Pairs are read left-to-right.
  • Sets cannot contain other sets.

Not yet supported

  • key-value pairs like foo:bar
  • booleans
  • dates
  • floating point numbers

I'm hesitant to support more complex types; at that point, the state being conveyed shouldn't be transported as strings.
Clearly this ☝️ goal has been abandoned with the addition of pairs and sets. But there are good use cases and they are well tested.

1.0.0

5 months ago

0.3.0

6 months ago

0.2.0

6 months ago

0.1.0

6 months ago