Parser.js
6.91 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
'use strict'
/*
* haye
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const _ = require('lodash')
const util = require('../lib/util')
class Parser {
constructor () {
this.arrayInput = []
this.jsonInput = {}
this.qsInput = ''
this.pipeInput = ''
this.seperators = {
qsPair: ',',
qsArgs: '=',
pipePair: '|',
pipeArgs: ':'
}
this.qsReplaceRegex = new RegExp(`${this.seperators.qsPair}(?![^\\[]*\\])`, 'g')
}
/**
* Sets an array on the constructor to be parsed
* later.
*
* @param {Array} values
*
* @return {Object} Reference to this chaining
*/
fromArray (values) {
this.arrayInput = values
return this
}
/**
* Sets json object on the constructor to be parsed
* later.
*
* @param {Object} values
*
* @return {Object} Reference to this chaining
*/
fromJSON (values) {
this.jsonInput = values
return this
}
/**
* @see fromJSON
*/
fromJson (values) {
return this.fromJSON(values)
}
/**
* Sets string input on the constructor to be parsed
* later.
*
* @param {String} value
*
* @return {Object} Reference to this chaining
*/
fromQS (value) {
this.qsInput = value
return this
}
/**
* Sets string input on the constructor to be parsed
* later.
*
* @param {String} value
*
* @return {Object} Reference to this chaining
*/
fromPipe (value) {
this.pipeInput = value
return this
}
/**
* Build options by joining multiple values using
* a seperator
*
* @param {String} arraySeperator
* @param {Mixed} values
*
* @return {String}
*
* @private
*/
_buildOptions (arraySeperator, values) {
if (!util.existy(values)) {
return ''
}
return _.isArray(values) ? values.join(arraySeperator) : values
}
/**
* Build args by pasrsing a string using the
* args seperator.
*
* @param {String} argsSeperator
* @param {String} value
*
* @return {Array|String}
*
* @private
*/
_buildArgs (argsSeperator, value) {
if (!value) {
return null
}
const toArray = value.split(argsSeperator)
return _.size(toArray) === 1 ? toArray[0] : _.map(toArray, (item) => item.trim())
}
/**
* Parses an array to a piped string
*
* @param {Array} values
*
* @return {String}
*
* @private
*/
_parseHashToString (values, joinSeperator, callback) {
return _(values).map(callback).join(joinSeperator)
}
/**
* Parses a pipe string expression to an array
*
* @param {String} expression
*
* @return {Array}
*
* @private
*/
_parseStringToArray (expression, joinSeperator, callback) {
return _(expression.split(joinSeperator)).map(callback).value()
}
/**
* Parses a pipe string to JSON.
*
* @param {String} expression
*
* @return {Object}
*
* @private
*/
_parseStringToJson (expression, joinSeperator, callback) {
return _(expression.split(joinSeperator)).transform(callback, {}).value()
}
/**
* Pipes an array or an object to a pipe expression.
*
* @return {String}
*
* @throws {Error} If fromJSON or fromArray is not called previously.
*/
toPipe() {
if (_.size(this.arrayInput) > 0) {
return this._parseHashToString(this.arrayInput, this.seperators.pipePair, (item) => {
const options = this._buildOptions(',', item.args)
return options ? `${item.name}${this.seperators.pipeArgs}${options}` : item.name
})
}
if (_.size(this.jsonInput) > 0) {
return this._parseHashToString(this.jsonInput, this.seperators.pipePair, (value, key) => {
const options = this._buildOptions(',', value)
return options ? `${key}${this.seperators.pipeArgs}${options}` : key
})
}
throw new Error('Make sure to pass an object or array first using fromArray or fromJson method')
}
/**
* Pipes an array or an object to a string expression.
*
* @return {String}
*
* @throws {Error} If fromJSON or fromArray is not called previously.
*/
toQS() {
if (_.size(this.arrayInput) > 0) {
return this._parseHashToString(this.arrayInput, this.seperators.qsPair, (item) => {
const options = this._buildOptions(',', item.args)
const wrappedOptions = (_.isArray(item.args) && _.size(item.args)) ? `[${options}]` : options
return options ? `${item.name}${this.seperators.qsArgs}${wrappedOptions}` : item.name
})
}
if (_.size(this.jsonInput) > 0) {
return this._parseHashToString(this.jsonInput, this.seperators.qsPair, (value, key) => {
const options = this._buildOptions(',', value)
const wrappedOptions = (_.isArray(value) && _.size(value)) ? `[${options}]` : options
return options ? `${key}${this.seperators.qsArgs}${wrappedOptions}` : key
})
}
throw new Error('Make sure to pass an object or array first using fromArray or fromJson method')
}
/**
* Converts a string expression to an array
*
* @return {Array}
*
* @throws {Error} If fromPipe or fromQS is not called previously.
*/
toArray () {
if (this.pipeInput) {
return this._parseStringToArray(this.pipeInput, this.seperators.pipePair, (items) => {
const tokens = items.split(this.seperators.pipeArgs)
return { name: tokens[0].trim(), args: this._buildArgs(',', tokens[1]) }
})
}
if (this.qsInput) {
return this._parseStringToArray(this.qsInput, this.qsReplaceRegex, (items) => {
const tokens = items.split(this.seperators.qsArgs)
const unWrappedArgs = tokens[1] ? tokens[1].replace(/\[|\]/g, '') : null
return { name: tokens[0].trim(), args: this._buildArgs(',', unWrappedArgs) }
})
}
throw new Error('Make sure to pass an expression string using fromQS or fromPipe method')
}
/**
* Converts a string expression to json
*
* @return {Object}
*
* @throws {Error} If If fromPipe or fromQS is not called previously.
*/
toJSON () {
if (this.pipeInput) {
return this._parseStringToJson(this.pipeInput, this.seperators.pipePair, (result, items) => {
const tokens = items.split(this.seperators.pipeArgs)
result[tokens[0].trim()] = this._buildArgs(',', tokens[1])
return result
})
}
if (this.qsInput) {
return this._parseStringToJson(this.qsInput, this.qsReplaceRegex, (result, items) => {
const tokens = items.split(this.seperators.qsArgs)
const unWrappedArgs = tokens[1] ? tokens[1].replace(/\[|\]/g, '') : null
result[tokens[0].trim()] = this._buildArgs(',', unWrappedArgs)
})
}
throw new Error('Make sure to pass an expression string using fromQS or fromPipe method')
}
/**
* @see toJSON
*/
toJson () {
return this.toJSON()
}
}
module.exports = Parser