0.0.1 • Published 6 years ago
litr v0.0.1
litr
litr evaluates JavaScript literals. litr doesn't use eval and is safe to use.
Internally, litr is a thin wrapper around a PEG.js parser, generated from a stripped-down version of the PEG Javscript Grammar.
example
const litr = require('litr');
const someJsString = `[
{ name: 'Venus', mass: 4.8675e+24, flags: 0b1101, population: 0, },
{ name: 'Earth', mass: 5.97237e+23, flags: 0b1000, population: 7734484600n, },
]`;
try {
const result = litr.parse(someJsString);
console.log(result);
} catch(err) {
console.log('SYNTAX ERROR!', err.message);
}
> [
> { name: 'Venus', mass: 4.8675e+24, flags: 13, population: 0 },
> { name: 'Earth', mass: 5.97237e+23, flags: 8, population: 7734484600n }
> ]use cases
- convert big amounts of data from Javascript to JSON
- deal with servers and APIs that return Javascript
- using user-provided data in calculations
- configuration files that contain values not easily expressed in JSON, e.g. regexes or bigints
what is supported
- decimal number literals (plain and scientific notation)
- hex, octal and binary literals
BigIntliterals (123456n)- "signed" number literals (
+123,-0xFEFE) - single and double quoted strings, including "wide" unicode escapes, e.g
"\u{1F44F}" RegExpliterals (no validity check)- arrays (with "elisions") and objects
usage
npm install litrFor browsers, grab litr.js.
litr provides a single function parse(string) which returns a Javascript value on success or throws a SyntaxError on failure.
0.0.1
6 years ago