-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathBuilder.js
810 lines (689 loc) · 21.8 KB
/
Builder.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
'use strict'
const SyntaxException = require('./Exceptions/Syntax')
const BuilderException = require('./Exceptions/Builder')
const ImplementationException = require('./Exceptions/Implementation')
const NON_LITERAL_CHARACTERS = '[\\^$.|?*+()/'
const METHOD_TYPE_BEGIN = 0b00001
const METHOD_TYPE_CHARACTER = 0b00010
const METHOD_TYPE_GROUP = 0b00100
const METHOD_TYPE_QUANTIFIER = 0b01000
const METHOD_TYPE_ANCHOR = 0b10000
const METHOD_TYPE_UNKNOWN = 0b11111
const METHOD_TYPES_ALLOWED_FOR_CHARACTERS = METHOD_TYPE_BEGIN | METHOD_TYPE_ANCHOR | METHOD_TYPE_GROUP | METHOD_TYPE_QUANTIFIER | METHOD_TYPE_CHARACTER
const simpleMapper = {
'startsWith': {
'add': '^',
'type': METHOD_TYPE_ANCHOR,
'allowed': METHOD_TYPE_BEGIN
},
'mustEnd': {
'add': '$',
'type': METHOD_TYPE_ANCHOR,
'allowed': METHOD_TYPE_CHARACTER | METHOD_TYPE_QUANTIFIER | METHOD_TYPE_GROUP
},
'onceOrMore': {
'add': '+',
'type': METHOD_TYPE_QUANTIFIER,
'allowed': METHOD_TYPE_CHARACTER | METHOD_TYPE_GROUP
},
'neverOrMore': {
'add': '*',
'type': METHOD_TYPE_QUANTIFIER,
'allowed': METHOD_TYPE_CHARACTER | METHOD_TYPE_GROUP
},
'any': {
'add': '.',
'type': METHOD_TYPE_CHARACTER,
'allowed': METHOD_TYPES_ALLOWED_FOR_CHARACTERS
},
'backslash': {
'add': '\\\\',
'type': METHOD_TYPE_CHARACTER,
'allowed': METHOD_TYPES_ALLOWED_FOR_CHARACTERS
},
'tab': {
'add': '\\t',
'type': METHOD_TYPE_CHARACTER,
'allowed': METHOD_TYPES_ALLOWED_FOR_CHARACTERS
},
'verticalTab': {
'add': '\\v',
'type': METHOD_TYPE_CHARACTER,
'allowed': METHOD_TYPES_ALLOWED_FOR_CHARACTERS
},
'newLine': {
'add': '\\n',
'type': METHOD_TYPE_CHARACTER,
'allowed': METHOD_TYPES_ALLOWED_FOR_CHARACTERS
},
'carriageReturn': {
'add': '\\r',
'type': METHOD_TYPE_CHARACTER,
'allowed': METHOD_TYPES_ALLOWED_FOR_CHARACTERS
},
'whitespace': {
'add': '\\s',
'type': METHOD_TYPE_CHARACTER,
'allowed': METHOD_TYPES_ALLOWED_FOR_CHARACTERS
},
'noWhitespace': {
'add': '\\S',
'type': METHOD_TYPE_CHARACTER,
'allowed': METHOD_TYPES_ALLOWED_FOR_CHARACTERS
},
'anyCharacter': {
'add': '\\w',
'type': METHOD_TYPE_CHARACTER,
'allowed': METHOD_TYPES_ALLOWED_FOR_CHARACTERS
},
'noCharacter': {
'add': '\\W',
'type': METHOD_TYPE_CHARACTER,
'allowed': METHOD_TYPES_ALLOWED_FOR_CHARACTERS
},
'word': {
'add': '\\b',
'type': METHOD_TYPE_CHARACTER,
'allowed': METHOD_TYPE_BEGIN
},
'nonWord': {
'add': '\\B',
'type': METHOD_TYPE_CHARACTER,
'allowed': METHOD_TYPE_BEGIN
}
}
class Builder {
/**
* @constructor
*/
constructor() {
/** @var {array} _regEx Regular Expression being built. */
this._regEx = []
/** @var {string} _modifiers Raw modifier to apply on. */
this._modifiers = 'g'
/** @var {number} _lastMethodType Type of last method, to avoid invalid builds. */
this._lastMethodType = METHOD_TYPE_BEGIN
/** @var {RegExp|null} _result Regular Expression Object built. */
this._result = null
/** @var {string} _group Desired group, if any */
this._group = '%s'
/** @var {string} _implodeString String to join with. */
this._implodeString = ''
/** @var {array} _captureNames Save capture names to map */
this._captureNames = []
}
/**********************************************************/
/* CHARACTERS */
/**********************************************************/
/**
* Add raw Regular Expression to current expression.
*
* @param {string|RegExp} regularExpression
* @throws {BuilderException}
* @return {Builder}
*/
raw(regularExpression) {
regularExpression = regularExpression instanceof RegExp ?
regularExpression.toString().slice(1, -1) :
regularExpression
this._lastMethodType = METHOD_TYPE_UNKNOWN
this.add(regularExpression)
if (!this._isValid()) {
this._revertLast()
throw new BuilderException('Adding raw would invalidate this regular expression. Reverted.')
}
return this
}
/**
* Literally match one of these characters.
*
* @param {string} chars
* @return {Builder}
*/
oneOf(chars) {
this._validateAndAddMethodType(METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS)
let result = chars.split('').map((character) => this.escape(character)).join('')
result = result.replace('-', '\\-').replace(']', '\\]')
return this.add(`[${result}]`)
}
/**
* Literally match a character that is not one of these characters.
*
* @param {string} chars
* @return {Builder}
*/
noneOf(chars) {
this._validateAndAddMethodType(METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS)
let result = chars.split('').map((character) => this.escape(character)).join('')
result = result.replace('-', '\\-').replace(']', '\\]')
return this.add(`[^${result}]`)
}
/**
* Literally match all of these characters in that order.
*
* @param {string} chars One or more characters
* @return {Builder}
*/
literally(chars) {
this._validateAndAddMethodType(METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS)
const result = chars.split('').map((character) => this.escape(character)).join('')
return this.add(`(?:${result})`)
}
/**
* Match any digit (in given span). Default will be a digit between 0 and 9.
*
* @param {number} min
* @param {number} max
* @return {Builder}
*/
digit(min = 0, max = 9) {
this._validateAndAddMethodType(METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS)
return this.add(`[${min}-${max}]`)
}
/**
* Match any non-digit character (in given span). Default will be any character not between 0 and 9.
*
* @return {Builder}
*/
noDigit() {
this._validateAndAddMethodType(METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS)
return this.add('[^0-9]')
}
/**
* Match any uppercase letter (between A to Z).
*
* @param {string} min
* @param {string} max
* @return {Builder}
*/
uppercaseLetter(min = 'A', max = 'Z') {
return this.add(`[${min}-${max}]`)
}
/**
* Match any lowercase letter (bwteen a to z).
* @param {string} min
* @param {string} max
* @return {Builder}
*/
letter(min = 'a', max = 'z') {
this._validateAndAddMethodType(METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS)
return this.add(`[${min}-${max}]`)
}
/**********************************************************/
/* GROUPS */
/**********************************************************/
/**
* Match any of these conditions.
*
* @param {Closure|Builder|string} conditions Anonymous function with its Builder as first parameter.
* @return {Builder}
*/
anyOf(conditions) {
this._validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS)
return this._addClosure(new Builder()._extends('(?:%s)', '|'), conditions)
}
/**
* Match all of these conditions, but in a non capture group.
*
* @param {Closure|Builder|string} conditions Anonymous function with its Builder as a first parameter.
* @return {Builder}
*/
group(conditions) {
this._validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS)
return this._addClosure(new Builder()._extends('(?:%s)'), conditions)
}
/**
* Match all of these conditions, Basically reverts back to the default mode, if coming from anyOf, etc.
*
* @param {Closure|Builder|string} conditions
* @return {Builder}
*/
and(conditions) {
this._validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS)
return this._addClosure(new Builder(), conditions)
}
/**
* Positive lookahead. Match the previous condition only if followed by given conditions.
*
* @param {Closure|Builder|string} condition Anonymous function with its Builder as a first parameter.
* @return {Builder}
*/
ifFollowedBy(conditions) {
this._validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS)
return this._addClosure(new Builder()._extends('(?=%s)'), conditions)
}
/**
* Negative lookahead. Match the previous condition only if NOT followed by given conditions.
*
* @param {Closure|Builder|string} condition Anonymous function with its Builder as a first parameter.
* @return {Builder}
*/
ifNotFollowedBy(conditions) {
this._validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS)
return this._addClosure(new Builder()._extends('(?!%s)'), conditions)
}
/**
* Create capture group of given conditions.
*
* @param {Closure|Builder|string} condition Anonymous function with its Builder as a first parameter.
* @param {String} name
* @return {Builder}
*/
capture(conditions, name) {
if (name) {
this._captureNames.push(name)
}
this._validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS)
return this._addClosure(new Builder()._extends('(%s)'), conditions)
}
/**********************************************************/
/* QUANTIFIERS */
/**********************************************************/
/**
* Make the last or given condition optional.
*
* @param {null|Closure|Builder|string} conditions Anonymous function with its Builder as a first parameter.
* @return {Builder}
*/
optional(conditions = null) {
this._validateAndAddMethodType(METHOD_TYPE_QUANTIFIER, METHOD_TYPE_CHARACTER | METHOD_TYPE_GROUP)
if (!conditions) {
return this.add('?')
}
return this._addClosure(new Builder()._extends('(?:%s)?'), conditions)
}
/**
* Previous match must occur so often.
*
* @param {number} min
* @param {number} max
* @return {Builder}
*/
between(min, max) {
this._validateAndAddMethodType(METHOD_TYPE_QUANTIFIER, METHOD_TYPE_CHARACTER | METHOD_TYPE_GROUP)
return this.add(`{${min},${max}}`)
}
/**
* Previous match must occur at least this often.
*
* @param {number} min
* @return {Builder}
*/
atLeast(min) {
this._validateAndAddMethodType(METHOD_TYPE_QUANTIFIER, METHOD_TYPE_CHARACTER | METHOD_TYPE_GROUP)
return this.add(`{${min},}`)
}
/**
* Previous match must occur exactly once.
*
* @return {Builder}
*/
once() {
return this.exactly(1)
}
/**
* Previous match must occur exactly twice.
*
* @return {Builder}
*/
twice() {
return this.exactly(2)
}
/**
* Previous match must occur exactly this often.
*
* @param {number} count
* @return {Builder}
*/
exactly(count) {
this._validateAndAddMethodType(METHOD_TYPE_QUANTIFIER, METHOD_TYPE_CHARACTER | METHOD_TYPE_GROUP)
return this.add(`{${count}}`)
}
/**
* Match less chars instead of more (lazy).
*
* @return {Builder}
* @throws {ImplementationException}
*/
lazy() {
const chars = '+*}?'
const raw = this.getRawRegex()
const last = raw.substr(-1)
const lastMethodType = this._lastMethodType
this._lastMethodType = METHOD_TYPE_QUANTIFIER
if (!chars.includes(last)) {
if (last === ')' && chars.includes(raw.substr(-2, 1))) {
const target = lastMethodType === METHOD_TYPE_GROUP ? this._revertLast().slice(0, -1) + '?)' : '?'
return this.add(target)
}
throw new ImplementationException('Cannot apply laziness at this point. Only applicable after quantifier.')
}
return this.add('?')
}
/**
* Match up to the given condition.
*
* @param {Closure|Builder|string} toCondition
* @return {Builder}
*/
until(toCondition) {
this.lazy()
this._validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS)
return this._addClosure(new Builder(), toCondition)
}
/**********************************************************/
/* MODIFIER MAPPER */
/**********************************************************/
multiLine() {
return this._addUniqueModifier('m')
}
caseInsensitive() {
return this._addUniqueModifier('i')
}
// Todo
// unicode()
// sticky()
/**********************************************************/
/* SIMPLE MAPPER */
/**********************************************************/
startsWith() {
return this._addFromMapper('startsWith')
}
mustEnd() {
return this._addFromMapper('mustEnd')
}
onceOrMore() {
return this._addFromMapper('onceOrMore')
}
neverOrMore() {
return this._addFromMapper('neverOrMore')
}
any() {
return this._addFromMapper('any')
}
backslash() {
return this._addFromMapper('backslash')
}
tab() {
return this._addFromMapper('tab')
}
verticalTab() {
return this._addFromMapper('verticalTab')
}
newLine() {
return this._addFromMapper('newLine')
}
whitespace() {
return this._addFromMapper('whitespace')
}
noWhitespace() {
return this._addFromMapper('noWhitespace')
}
anyCharacter() {
return this._addFromMapper('anyCharacter')
}
noCharacter() {
return this._addFromMapper('noCharacter')
}
word() {
return this._addFromMapper('word')
}
nonWord() {
return this._addFromMapper('nonWord')
}
/**********************************************************/
/* INTERNAL METHODS */
/**********************************************************/
/**
* Escape specific character.
*
* @param {string} character
* @return {string}
*/
escape(character) {
return (NON_LITERAL_CHARACTERS.includes(character) ? '\\' : '') + character
}
/**
* Get the raw regular expression string.
*
* @return string
*/
getRawRegex() {
return this._group.replace('%s', this._regEx.join(this._implodeString))
}
/**
* Get all set modifiers.
*
* @return {string}
*/
getModifiers() {
return this._modifiers
}
/**
* Add condition to the expression query.
*
* @param {string} condition
* @return {Builder}
*/
add(condition) {
this._result = null // Reset result to make up a new one.
this._regEx.push(condition)
return this
}
/**
* Validate method call. This will throw an exception if the called method makes no sense at this point.
* Will add the current type as the last method type.
*
* @param {number} type
* @param {number} allowed
* @param {string} methodName
*/
_validateAndAddMethodType(type, allowed, methodName) {
if (allowed & this._lastMethodType) {
this._lastMethodType = type
return
}
const message = {
[METHOD_TYPE_BEGIN]: 'at the beginning',
[METHOD_TYPE_CHARACTER]: 'after a literal character',
[METHOD_TYPE_GROUP]: 'after a group',
[METHOD_TYPE_QUANTIFIER]: 'after a quantifier',
[METHOD_TYPE_ANCHOR]: 'after an anchor'
}[this._lastMethodType]
throw new ImplementationException(
`Method ${methodName} is not allowed ${message || 'here'}`
)
}
/**
* Add the value form simple mapper to the regular expression.
*
* @param {string} name
* @return {Builder}
* @throws {BuilderException}
*/
_addFromMapper(name) {
const item = simpleMapper[name]
if (!item) {
throw new BuilderException('Unknown mapper.')
}
this._validateAndAddMethodType(item.type, item.allowed, name)
return this.add(item.add)
}
/**
* Add a specific unique modifier. This will ignore all modifiers already set.
*
* @param {string} modifier
* @return {Builder}
*/
_addUniqueModifier(modifier) {
this._result = null
if (!this._modifiers.includes(modifier)) {
this._modifiers += modifier
}
return this
}
/**
* Build the given Closure or string and append it to the current expression.
*
* @param {Builder} builder
* @param {Closure|Builder|string} conditions Either a closure, literal character string or another Builder instance.
*/
_addClosure(builder, conditions) {
if (typeof conditions === 'string') {
builder.literally(conditions)
} else if (conditions instanceof Builder) {
builder.raw(conditions.getRawRegex())
} else {
conditions(builder)
}
return this.add(builder.getRawRegex())
}
/**
* Get and remove last added element.
*
* @return {string}
*/
_revertLast() {
return this._regEx.pop()
}
/**
* Build and return the resulting RegExp object. This will apply all the modifiers.
*
* @return {RegExp}
* @throws {SyntaxException}
*/
get() {
if (this._isValid()) {
return this._result
} else {
throw new SyntaxException('Generated expression seems to be invalid.')
}
}
/**
* Validate regular expression.
*
* @return {boolean}
*/
_isValid() {
if (this._result) {
return true
} else {
try {
this._result = new RegExp(this.getRawRegex(), this.getModifiers())
return true
} catch (e) {
return false
}
}
}
/**
* Extends self to match more cases.
*
* @param {string} group
* @param {string} implodeString
* @return {Builder}
*/
_extends(group, implodeString = '') {
this._group = group
this._implodeString = implodeString
return this
}
/**
* Clone a new builder object.
*
* @return {Builder}
*/
clone() {
const clone = new Builder()
// Copy deeply
clone._regEx = Array.from(this._regEx)
clone._modifiers = this._modifiers
clone._lastMethodType = this._lastMethodType
clone._group = this._group
clone._captureNames = Array.from(this._captureNames)
return clone
}
/**
* Remote specific flag.
*
* @param {string} flag
* @return {Builder}
*/
removeModifier(flag) {
this._modifiers = this._modifiers.replace(flag, '')
this._result = null
return this
}
/**********************************************************/
/* REGEX METHODS */
/**********************************************************/
exec() {
const regexp = this.get()
return regexp.exec.apply(regexp, arguments)
}
test() {
const regexp = this.get()
return regexp.test.apply(regexp, arguments)
}
/**********************************************************/
/* ADDITIONAL METHODS */
/**********************************************************/
/**
* Just like test in RegExp, but reset lastIndex.
*
* @param {string} target
* @return {boolean}
*/
isMatching(target) {
const result = this.test(target)
this.get().lastIndex = 0
return result
}
/**
* Map capture index to name.
* When `exec` give the result like: [ 'aa ', 'aa', index: 0, input: 'aa bb cc dd' ]
* Then help to resolve to return: [ 'aa ', 'aa', index: 0, input: 'aa bb cc dd', [captureName]: 'aa' ]
*
* @param {object} result
* @return {object}
*/
_mapCaptureIndexToName(result) {
const names = this._captureNames
// No match
if (!result) {return null}
return Array.prototype.reduce.call(result.slice(1), (result, current, index) => {
if (names[index]) {
result[names[index]] = current || ''
}
return result
}, result)
}
/**
* Just like match in String, but reset lastIndex.
*
* @param {string} target
* @return {array|null}
*/
getMatch(target) {
const regex = this.get()
const result = regex.exec(target)
regex.lastIndex = 0
return this._mapCaptureIndexToName(result)
}
/**
* Get all matches, just like loop for RegExp.exec.
* @param {string} target
*/
getMatches(target) {
const result = []
const regex = this.get()
let temp = null
while (temp = regex.exec(target)) {
temp = this._mapCaptureIndexToName(temp)
result.push(temp)
}
regex.lastIndex = 0
return result
}
}
module.exports = Builder