3.1.6 • Published 10 days ago

@dldc/literal-parser v3.1.6

Weekly downloads
-
License
MIT
Repository
github
Last release
10 days ago

🔎 literal-parser

A small library to parse and serialize JavaScript array/object literal.

This is like a JSON.parse / JSON.serialize but for JavaScript object instead of JSON objects.

Gist

import Parser from '@dldc/literal-parser';

Parser.parse('{ some: ["object", { literal: true }] }');
// return an object { some: ["object", { literal: true }] }

Supported features

Take a look at the tests see what is supported.

API

Parser.parse(str)

Parse the string, expect the string to contain only one expression and throw otherwise.

Return the parsed object.

Parser.parseOne(str)

Parse one expression then stop.

Returns a object with { value, length } where value is the parsed expression and length is the number of character parsed.

Parser.parseOne('{ props: true }} something="else" />');
// { value: { props: true }, length: 15 }

Serializer.serialize(obj, format?)

Print an object.

format is optional and can be one of the following:

{ mode: 'line' } | 'line'

Print on a single line with spaces. This is the default format

{ mode: 'compact' } | 'compact'

Print on a single line without any spaces.

{ mode: 'indent' } | 'indent' | number

Similar to JSON.stringify(obj, null, indent).

Options:

  • space (default: 2)

{ mode: 'pretty' } | 'pretty'

Inspired by prettier, this mode will try to print objects and arrays on a single line, if the result is bigger than the threshold then it's splitted into multiple lines.

Options:

  • space (default: 2)
  • threshold (default: 80)
3.1.6

10 days ago

3.1.5

28 days ago

3.1.4

3 months ago

3.1.3

5 months ago

3.1.2

7 months ago

3.1.1

8 months ago

3.1.0

8 months ago

3.0.7

10 months ago

3.0.6

11 months ago