index.js
2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict'
/**
* @module parenthesis
*/
function parse (str, opts) {
// pretend non-string parsed per-se
if (typeof str !== 'string') return [str]
var res = [str]
if (typeof opts === 'string' || Array.isArray(opts)) {
opts = {brackets: opts}
}
else if (!opts) opts = {}
var brackets = opts.brackets ? (Array.isArray(opts.brackets) ? opts.brackets : [opts.brackets]) : ['{}', '[]', '()']
var escape = opts.escape || '___'
var flat = !!opts.flat
brackets.forEach(function (bracket) {
// create parenthesis regex
var pRE = new RegExp(['\\', bracket[0], '[^\\', bracket[0], '\\', bracket[1], ']*\\', bracket[1]].join(''))
var ids = []
function replaceToken(token, idx, str){
// save token to res
var refId = res.push(token.slice(bracket[0].length, -bracket[1].length)) - 1
ids.push(refId)
return escape + refId + escape
}
res.forEach(function (str, i) {
var prevStr
// replace paren tokens till there’s none
var a = 0
while (str != prevStr) {
prevStr = str
str = str.replace(pRE, replaceToken)
if (a++ > 10e3) throw Error('References have circular dependency. Please, check them.')
}
res[i] = str
})
// wrap found refs to brackets
ids = ids.reverse()
res = res.map(function (str) {
ids.forEach(function (id) {
str = str.replace(new RegExp('(\\' + escape + id + '\\' + escape + ')', 'g'), bracket[0] + '$1' + bracket[1])
})
return str
})
})
var re = new RegExp('\\' + escape + '([0-9]+)' + '\\' + escape)
// transform references to tree
function nest (str, refs, escape) {
var res = [], match
var a = 0
while (match = re.exec(str)) {
if (a++ > 10e3) throw Error('Circular references in parenthesis')
res.push(str.slice(0, match.index))
res.push(nest(refs[match[1]], refs))
str = str.slice(match.index + match[0].length)
}
res.push(str)
return res
}
return flat ? res : nest(res[0], res)
}
function stringify (arg, opts) {
if (opts && opts.flat) {
var escape = opts && opts.escape || '___'
var str = arg[0], prevStr
// pretend bad string stringified with no parentheses
if (!str) return ''
var re = new RegExp('\\' + escape + '([0-9]+)' + '\\' + escape)
var a = 0
while (str != prevStr) {
if (a++ > 10e3) throw Error('Circular references in ' + arg)
prevStr = str
str = str.replace(re, replaceRef)
}
return str
}
return arg.reduce(function f (prev, curr) {
if (Array.isArray(curr)) {
curr = curr.reduce(f, '')
}
return prev + curr
}, '')
function replaceRef(match, idx){
if (arg[idx] == null) throw Error('Reference ' + idx + 'is undefined')
return arg[idx]
}
}
function parenthesis (arg, opts) {
if (Array.isArray(arg)) {
return stringify(arg, opts)
}
else {
return parse(arg, opts)
}
}
parenthesis.parse = parse
parenthesis.stringify = stringify
module.exports = parenthesis