-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
184 lines (141 loc) · 4.77 KB
/
index.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'use strict'
const DEFAULT_PUNCTUATION = ',;:.?!'
const isBoolean = x => typeof x === 'boolean'
const isFunction = x => typeof x === 'function'
const isObjectLiteral = x => (x.constructor || {}).name === 'Object'
const isPunctuation = x => isString(x) && Array.from(x).every(x => DEFAULT_PUNCTUATION.includes(x))
const isString = x => typeof x === 'string'
const isUndefined = x => typeof x === 'undefined'
const reworder = (config, options = {}) => {
config = [].concat(config).filter(Boolean)
if (!config.length || !config.every(isObjectLiteral)) {
throw new Error('Expected config to be a non-empty array of object literals')
}
if (!isObjectLiteral(options)) {
throw new Error('Expected options to be an object literal')
}
if (
!isUndefined(options.caseInsensitive) &&
!isBoolean(options.caseInsensitive)
) {
throw new Error('Expected options.caseInsensitive to be a boolean')
}
if (
!isUndefined(options.includePunctuation) &&
!isBoolean(options.includePunctuation) &&
!isPunctuation(options.includePunctuation)
) {
throw new Error('Expected options.includePunctuation to be a boolean or string containing punctuation')
}
if (
!isUndefined(options.variableSpacing) &&
!isBoolean(options.variableSpacing)
) {
throw new Error('Expected options.variableSpacing to be a boolean')
}
const infos = []
const pattern = []
const patterns = []
const regexOptions = 'g' + (options.caseInsensitive ? 'i' : '')
let index = 0
let isRegex
let regex
for (let { key, keys, value, transform, ...rest } of config) {
if (value && !isString(value)) {
throw new Error('Config value must be a string')
}
if (transform && !isFunction(transform)) {
throw new Error('Config transform must be a function')
}
if (!value && !transform) {
throw new Error('Config value or transform must be specified')
}
keys = [].concat(key).concat(keys).filter(Boolean)
for (let key of keys) {
if (key instanceof RegExp) {
isRegex = true
key = key.source.replace(/\(/g, '(?:')
} else if (typeof key === 'string') {
isRegex = false
} else {
throw new Error('Config key must be string or RegExp')
}
if (options.includePunctuation) {
const punctuation = isString(options.includePunctuation)
? options.includePunctuation
: DEFAULT_PUNCTUATION
key = key.replace(/ /g, `[${punctuation}] `)
}
if (options.variableSpacing) {
key = key.replace(/ /g, ' +')
}
regex = new RegExp(`^${key}$`, regexOptions)
for (const pattern of patterns) {
const isConflict = (
(pattern.isRegex && pattern.regex.test(key)) ||
(isRegex && regex.test(pattern.key))
)
if (isConflict) {
const patternKey = pattern.isRegex ? `/${pattern.key}/` : `"${pattern.key}"`
const thisKey = isRegex ? `/${key}/` : `"${key}"`
throw new Error(
`Conflict: {${patternKey}: "${pattern.value}"}, {${thisKey}: "${value}"}`
)
}
}
pattern.push(`(\\b${key}\\b)`)
patterns.push({ isRegex, key, regex, value })
++index
}
infos.push({ ...rest, value, transform, index })
}
const lastInfoIndex = ++index
regex = new RegExp(pattern.join('|'), regexOptions)
const getInfo = index => {
let i
for (i = 0; i < infos.length && infos[i].index <= index; i++) {
if (infos[i].index === index) break
}
return infos[i]
}
const reword = input => {
const matches = []
const promises = []
let match
let net = 0
let output = input
const handleMatch = (key, index, value, rest) => {
const adjustedIndex = index - net
output = (
output.slice(0, adjustedIndex) +
value +
output.slice(adjustedIndex + key.length)
)
matches.push({ ...rest, key, index, value })
net += key.length - value.length
}
while ((match = regex.exec(input))) {
const key = match[0]
const index = match.index
const infoIndex = match.slice(1, lastInfoIndex).findIndex(Boolean) + 1
let { value, transform, ...rest } = getInfo(infoIndex)
value = value || transform(match[0])
if (value instanceof Promise) {
const promise = value.then(value => ({ ...rest, key, index, value }))
promises.push(promise)
} else {
handleMatch(key, index, value, rest)
}
}
if (!promises.length) return { input, matches, output }
return Promise.all(promises).then(results => {
for (const { key, index, value } of results) {
handleMatch(key, index, value)
}
return { input, matches, output }
})
}
reword.regex = regex
return reword
}
module.exports = reworder