# jsonc-pragma

> A simple, yet powerful pragma analyzer and utility library for JSON with Comments

Latest version **1.0.8** (published 2020-08-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install jsonc-pragma
pnpm add jsonc-pragma
yarn add jsonc-pragma
bun add jsonc-pragma
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.8 |
| Published | 2020-08-01 |
| First published | 2020-01-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 51.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | arnohovhannisyan |

## Links

- npm: https://www.npmjs.com/package/jsonc-pragma
- Repository: https://github.com/arnohovhannisyan/jsonc-pragma
- npm.io page: https://npm.io/package/jsonc-pragma

## Dependencies (1)

- [match-bracket](https://npm.io/package/match-bracket.md) ^1.0.0

## Recent versions

- 1.0.8 (latest) — 2020-08-01
- 1.0.7 — 2020-03-17
- 1.0.6 — 2020-03-16
- 1.0.5 — 2020-02-21
- 1.0.4 — 2020-02-20
- 1.0.3 — 2020-02-20
- 1.0.2 — 2020-01-18
- 1.0.1 — 2020-01-17
- 1.0.0 — 2020-01-17

## README

<div align="center">
	<!-- <img width="200" src="assets/logo.png"> -->
	<h1>jsonc-pragma</h1>
	<p>A simple, yet powerful pragma analyzer and utility library for JSON with Comments</p>
	<p>
		<a href="https://github.com/arnohovhannisyan/jsonc-pragma/actions">
			<img src="https://img.shields.io/github/workflow/status/arnohovhannisyan/jsonc-pragma/Tests" alt="Tests">
		</a>
		<a href="https://npmjs.com/package/jsonc-pragma">
			<img src="https://img.shields.io/npm/v/jsonc-pragma" alt="Version">
		</a>
		<a href="https://github.com/arnohovhannisyan/jsonc-pragma/blob/master/LICENSE">
			<img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License">
		</a>
		<a href="https://codecov.io/gh/arnohovhannisyan/jsonc-pragma">
			<img src="https://img.shields.io/codecov/c/github/arnohovhannisyan/jsonc-pragma" alt="Coverage">
		</a>
	</p>
</div>

## Installation

```sh
$ yarn add jsonc-pragma
```

or

```sh
$ npm install jsonc-pragma
```

## Usage

### `scan()`

#### `@param` contents

Type: `string | Buffer`

This is the contents of the JSON document.

#### `@example`

```js
import { scan } from "jsonc-pragma";

const contents = `{
	// @foo bar=5
	"example": "...",

	// @baz foo=test bar=abc
	"object": {
		"example": "..."
	}
}`;

scan(contents);

/* Returns:

[
	{
		start: 2,
		end: 2,
		args: { bar: "5" },
		name: "foo"
	},
	{
		start: 5,
		end: 7,
		args: { foo: "test", bar: "abc" },
		name: "baz"
	}
] 

*/
```

#### `@returns`

```ts
Array<ISection>
```

### `comment()`

#### `@param` contents

Type: `string | Buffer`

This is the contents of the JSON document.

#### `@param` selector

**OPTIONAL**

Type: `(section: ISection) => boolean`

This is a function that receives a section and determines whether that section should be commented by returning true (comment) or false (don't comment).

If omitted, all sections will be commented.

#### `@example`

```js
import { comment } from "jsonc-pragma";

const contents = `{
	// @foo bar=5
	"example": "...",

	// @foo bar=7
	"object": {
		"example": "..."
	},

	// @baz bar=7
	"notCommented": "..."
}`;

comment(
	contents,
	section => section.name === "foo" && Number(section.args.bar) > 6
);

/* Returns:

`{
	// @foo bar=5
	"example": "...",

	// @foo bar=7
	// "object": {
	//   "example": "..."
	// },

	// @baz bar=7
	"notCommented": "..."
}`

*/
```

#### `@returns` string

### `uncomment()`

#### `@param` contents

Type: `string | Buffer`

This is the contents of the JSON document.

#### `@param` selector

**OPTIONAL**

Type: `(section: ISection) => boolean`

This is a function that receives a section and determines whether that section should be uncommented by returning true (uncomment) or false (don't uncomment).

If omitted, all sections will be uncommented.

#### `@example`

```js
import { uncomment } from "jsonc-pragma";

const contents = `{
	// @foo bar=5
	// "example": "...",

	// @foo bar=7
	// "object": {
	//   "example": "..."
	// }
}`;

uncomment(contents, section => Number(section.args.bar) > 6);

/* Returns:

`{
	// @foo bar=5
	// "example": "...",

	// @foo bar=7
	"object": {
		"example": "..."
	}
}`

*/
```

#### `@returns` string

### `@typedef`

```ts
interface ISection {
	start: number;
	end: number;
	args: { [key: string]: string | undefined };
	name: string;
}
```

---
_Source: https://npm.io/package/jsonc-pragma · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
