@daiyam/balanced-pairs

Find your brackets and quotes in pairs

Usage no npm install needed!

<script type="module">
  import daiyamBalancedPairs from 'https://cdn.skypack.dev/@daiyam/balanced-pairs';
</script>

README

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