https://github.com/elzup/peeler
parser for only bracket.
https://github.com/elzup/peeler
bootstrap brackets nest parser syntax tree
Last synced: 8 months ago
JSON representation
parser for only bracket.
- Host: GitHub
- URL: https://github.com/elzup/peeler
- Owner: elzup
- License: mit
- Created: 2018-06-25T02:09:13.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2025-07-01T00:53:50.000Z (8 months ago)
- Last Synced: 2025-07-01T01:38:49.980Z (8 months ago)
- Topics: bootstrap, brackets, nest, parser, syntax, tree
- Language: TypeScript
- Homepage:
- Size: 671 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# peeler [](https://badge.fury.io/js/elzup) 
[](https://nodei.co/npm/peeler/)
> parser for only bracket.
## Install
```
$ npm install peeler
$ yarn add peeler
```
## Usage
```js
import peeler from 'peeler'
```
```js
peeler('before(hit)after')
[ { nodeType: 'text',
pos: { start: 0, end: 6, depth: 0 },
content: 'before' },
{ nodeType: 'bracket',
pos: { start: 6, end: 10, depth: 0 },
open: '(',
close: ')',
nodes:
[ { nodeType: 'text',
pos: { start: 7, end: 10, depth: 1 },
content: 'hit' } ] },
{ nodeType: 'text',
pos: { start: 11, end: 16, depth: 0 },
content: 'after' } ]
```
```js
peeler('aa(bb{cc}bb)aa')
[ { nodeType: 'text',
pos: { start: 0, end: 2, depth: 0 },
content: 'aa' },
{ nodeType: 'bracket',
pos: { start: 2, end: 11, depth: 0 },
open: '(',
close: ')',
nodes:
[ { nodeType: 'text',
pos: { start: 3, end: 5, depth: 1 },
content: 'bb' },
{ nodeType: 'bracket',
pos: { start: 5, end: 8, depth: 1 },
open: '{',
close: '}',
nodes:
[ { nodeType: 'text',
pos: { start: 6, end: 8, depth: 2 },
content: 'cc' } ] },
{ nodeType: 'text',
pos: { start: 9, end: 11, depth: 1 },
content: 'bb' } ] },
{ nodeType: 'text',
pos: { start: 12, end: 14, depth: 0 },
content: 'aa' } ]
```
## API
### peeler(name, [option])
#### input
Type: `string`
text to parse.
#### return
Type: PNode[]
```
type PNode = PNodeText | PNodeBracket
type PNodeText = {
nodeType: 'text', // plain text part
content: string, // innerText
pos: {
start: number, // position of text
end: number,
depth: number, // nest count
}
}
type PNodeBracket = {
nodeType: 'bracket',
open: string, // bracket charactor
close: string,
nodes: PNode[], // children nodes
pos: {
start: number,
end: number,
depth: number,
},
}
```
### pair option
default `['()', '{}', '[]']`
```js
> peeler('[(__)]', { pairs: ['[]', '<>'] }) // skip '(' bracket
[ { nodeType: 'bracket',
pos: { start: 0, end: 5, depth: 0 },
open: '[',
close: ']',
nodes:
[ { nodeType: 'text',
pos: { start: 1, end: 5, depth: 1 },
content: '(__)' } ] } ]
```
### quotes option
default `[]`
```js
> peeler(`( < escape [[( > " ' \\" " )`, {
quotes: [ `"`, `'`, `<>`, /* pair enable */ ],
})
[ { nodeType: 'bracket',
open: '(',
close: ')',
nodes:
[ { nodeType: 'text',
pos: { start: 1, end: 26, depth: 1 },
content: ` < escape [[( > " ' \\" " ` } ],
pos: { start: 0, depth: 0, end: 26 },
content: `( < escape [[( > " ' \\" " )`,
innerContent: ` < escape [[( > " ' \\" " ` } ]
```
### default Options
```
const defaultOptions: Options = {
pairs: ['()', '{}', '[]'],
nestMax: 100,
escape: '\\',
includeEmpty: false,
quotes: [],
}
```
## more example
```js
import peeler from '.'
import type { PNode } from './types'
const print = (node: PNode) => {
const nest = '- '.repeat(node.pos.depth)
if (node.nodeType === 'text') {
console.log(nest + node.content)
return
} else {
console.log(nest + node.open)
node.nodes.map(print)
console.log(nest + node.close)
}
}
peeler(`(hello(world(\\\\('ω'\\)/){[A](B)}))`).map(print)
```
```
(
- hello
- (
- - world
- - (
- - - \('ω')/
- - )
- - {
- - - [
- - - - A
- - - ]
- - - (
- - - - B
- - - )
- - }
- )
)
```
## Related works
- [texter](https://github.com/elzup/texter)
## License
MIT © [elzup](https://elzup.com)
## Contributors
Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
| [
elzup](https://elzup.com)
[💻](https://github.com/elzup/peeler/commits?author=elzup "Code") [⚠️](https://github.com/elzup/peeler/commits?author=elzup "Tests") |
| :---: |
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!