@daiyam/balanced-pairs
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

@daiyam/balanced-pairs

MIT licensed NPM Version License License License

Find your brackets and quotes in pairs

Getting Started

With node previously installed:

npm install @daiyam/balanced-pairs
import { matchPair } from '@daiyam/balanced-pairs';

const JSON_WITH_COMMENTS_CONFIG = {
	pairs: {
		blocks: [
			['{', '}'],
			['[', ']'],
		],
		strings: [
			['"', '"', '\\"', '\\\\'],
			['\'', '\'', '\'', '\\\\'],
			['`', '`', '`', '\\\\'],
		],
	},
	comments: {
		lines: [
			'//',
		],
		blocks: [
			['/*', '*/'],
		],
	},
};

/*
 * @param {string} text - The JSON string
 * @param {number} index - The index of the first character of the pair
 * @return {number} The index of the last character of the pair
*/
function matchPairPerIndex(text: string, index: number): number {
    return matchPair(text, index, JSON_WITH_COMMENTS_CONFIG);
}

/*
 * @param {string | string[]} text - The JSON string
 * @param {line: number, column: number} position - The position of first character of the pair
 * @return {line: number, column: number} The position of the last character of the pair
*/
function matchPairPerPosition(text: string | string[], position: {line: number, column: number}): {line: number, column: number} {
    return matchPair(text, position, JSON_WITH_COMMENTS_CONFIG);
}

Donations

Support this project by becoming a financial contributor.

Ko-fi ko-fi.com/daiyam
Liberapay liberapay.com/daiyam/donate
PayPal paypal.me/daiyam99

License

Copyright © 2021-present Baptiste Augrain

Licensed under the MIT license.

Package Sidebar

Install

npm i @daiyam/balanced-pairs

Weekly Downloads

1

Version

0.1.1

License

MIT

Unpacked Size

18.3 kB

Total Files

19

Last publish

Collaborators

  • daiyam