Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tbeseda/fancy-value-parser

Parse fancy string values to JS objects
https://github.com/tbeseda/fancy-value-parser

Last synced: about 1 month ago
JSON representation

Parse fancy string values to JS objects

Awesome Lists containing this project

README

        


fancy-value-parser 💍


Parse fancy string values to JS objects.

fancy-value-parser on npmjs.org »

## What?

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

```js
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.