Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/daiyam/node-balanced-pairs


https://github.com/daiyam/node-balanced-pairs

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

        

[@daiyam/balanced-pairs](https://github.com/daiyam/node-balanced-pairs)
=======================================================================

[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE)
[![NPM Version](https://img.shields.io/npm/v/@daiyam/balanced-pairs.svg?colorB=green)](https://www.npmjs.com/package/@daiyam/balanced-pairs)
[![License](https://img.shields.io/badge/donate-ko--fi-green)](https://ko-fi.com/daiyam)
[![License](https://img.shields.io/badge/donate-liberapay-green)](https://liberapay.com/daiyam/donate)
[![License](https://img.shields.io/badge/donate-paypal-green)](https://paypal.me/daiyam99)

Find your brackets and quotes in pairs

Getting Started
---------------

With [node](http://nodejs.org) previously installed:

npm install @daiyam/balanced-pairs

```typescript
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](https://opensource.org/licenses/MIT).