3.1.1 • Published 2 years ago

@spacingbat3/jsonc-parser v3.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

JSONC Parser

A simple module to parse JSON with comments files with the magic of RegExp.

It allows for the specifying your own comments RegExp's as I couldn't found any information about JSONC being standarized (so for instance, if your JSONC file has single-line comments starting with hashtag, you can include that as an additional rule as well).

Usage

CommonJS

const JSONC = require('@spacingbat3/jsonc-parser').default

const parsedObject = JSONC.parse('file.jsonc')

console.log(parsedObject) // e.g. { "foo": "bar" }

ESM / TypeScript

import JSONC from '@spacingbat3/jsonc-parser'

/* NOTE: `pasedObject` is of the unknown type! */
const parsedObject = JSONC.parse('file.jsonc')

console.log(parsedObject) // e.g. { "foo": "bar" }

Custom comments formats

By the default, jsonc-parser only accepts the comments that are valid for the regular JavaScript files, but if you want to parse the other comments formats, you may specify them as shown below:

import JSONC from '@spacingbat3/jsonc-parser'

/* 
 * Removes the JavaScript-like comments and `sh`-like comments
 * (e.g. `{ "foo": "bar" # comment }`).
 */
const parsedObject = JSONC.parse('file.jsonc', /#.*/g)

console.log(parsedObject) // e.g. { "foo": "bar" }
3.1.1

2 years ago

3.1.0

2 years ago

3.0.0

2 years ago

2.0.0

2 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago