Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/tbeseda/fancy-value-parser
- Owner: tbeseda
- Created: 2023-11-29T19:34:10.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-12-15T01:44:12.000Z (about 1 year ago)
- Last Synced: 2024-11-10T15:50:54.782Z (3 months ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/fancy-value-parser
- Size: 389 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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.