0.1.1 • Published 3 years ago

@daiyam/balanced-pairs v0.1.1

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

@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.

License

Copyright © 2021-present Baptiste Augrain

Licensed under the MIT license.