Skip to main content
Home

Built and signed on GitHub Actions

Works with
This package works with DenoIt is unknown whether this package works with Cloudflare Workers, Node.js
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
This package works with Deno
JSR Score94%
Publisheda year ago (0.0.2)

A utility library for working with JSONC files. Doesn't do much more than wrap npm:jsonc-parser, though, so for most people using that library instead would be the move.

class JSONCTweaker

A convenience class for working with JSONC files — based on the Microsoft JSONC parser (https://github.com/microsoft/node-jsonc-parser) but more convenient to use for certain specific tasks.

Constructors

new
JSONCTweaker(
input: string,
isFilePath?: boolean,
)

Initialize a new tweaker from the given text or file. Note that there are no coalesced writes, so every successful edit will write back to the file. (If you need granular control of filesystem writees, then use the text mode and deal with the file yourself.)

Properties

private
optional
_filePath: string
private
_jsonc: string
readonly
jsonc: string

Returns the current JSONC text

Methods

private
_writeIfFile(): Promise<void>
getValue(path: Segment[]): unknown

Get the value at the specified path in the JSONC document. Returns undefined if the path doesn't lead to a value.

insertArrayValue(
pathToArray: Segment[],
value: string,
outErrors?: Array<ParseError | Error>,
): Promise<boolean>

Insert the given value into the array at the given path. If the path does not exist in the receiver's JSONC, a new array will be created. Otherwise, the value will be inserted into the existing array.

Returns true if the edit succeeded, false otherwise. You can inspect the outErrors parameter to find out why the edit failed (maybe) — it may contain errors from the underlying Microsoft JSONC parser, or errors from this module.

insertArrayValueIfNotPresent(
pathToArray: Segment[],
value: string,
outErrors?: Array<ParseError | Error>,
): Promise<boolean>

Insert the given value into the array at the given path only if it doesn't already exist. This is a convenience wrapper around insertArrayValue with skipIfExists set to true.

Static Methods

Initialize a new tweaker from the given file — this means every change will write back to the file. There are no coalesced writes.

Initialize a new tweaker from the given text

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@axhxrx/jsonc

Import symbol

import { JSONCTweaker } from "@axhxrx/jsonc";
or

Import directly with a jsr specifier

import { JSONCTweaker } from "jsr:@axhxrx/jsonc";

Add Package

pnpm i jsr:@axhxrx/jsonc
or (using pnpm 10.8 or older)
pnpm dlx jsr add @axhxrx/jsonc

Import symbol

import { JSONCTweaker } from "@axhxrx/jsonc";

Add Package

yarn add jsr:@axhxrx/jsonc
or (using Yarn 4.8 or older)
yarn dlx jsr add @axhxrx/jsonc

Import symbol

import { JSONCTweaker } from "@axhxrx/jsonc";

Add Package

vlt install jsr:@axhxrx/jsonc

Import symbol

import { JSONCTweaker } from "@axhxrx/jsonc";

Add Package

npx jsr add @axhxrx/jsonc

Import symbol

import { JSONCTweaker } from "@axhxrx/jsonc";