-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (80 loc) · 1.81 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
let ALPHA = '[A-z]'
let NOT_ALPHA = '[^A-z]'
let WORD = '\\w'
let NOT_WORD = '\\W'
let NUMBER = '\\d'
let NOT_NUMBER = '\\D'
let WHITE_SPACE = '\\s'
let NOT_WHITE_SPACE = '\\S'
let ANY = '.'
let START = '^'
let END = '$'
let LAZY = '?'
let GROUP = '?:'
let whole = v => `${START}${v}${END}`
let repeat = (v, start, end) => {
let finish = end === Infinity ? '' : end
return `${v}${start == null ? '' : `{${start}`}${finish != null ? `,${finish}` : ''}${
start == null ? '' : '}'
}`
}
let numeric = repeat.bind(null, NUMBER)
let alpha = repeat.bind(null, ALPHA)
let and = (...rest) => rest.join('')
let or = (...rest) => rest.join('|')
let wildcard = (v, lazy) => `${v}*${lazy ? LAZY : ''}`
let extra = (v, lazy) => `${v}+${lazy ? LAZY : ''}`
let capture = (v, name) =>
v && v.length ? `(${typeof name === 'string' ? `?<${name}>` : ''}${v})` : ''
let group = v => (v && v.length ? `(${GROUP}${v})` : '')
let ALL = capture(or(ANY, WHITE_SPACE)) // matches any character or whitespace
let look = (posOrNeg, behindOrAhead) => text =>
`(?${behindOrAhead ? '<' : ''}${posOrNeg ? '=' : '!'}${text})`
let looker = bOa =>
Object.assign(look(true, bOa), {
positive: look(true, bOa),
negative: look(false, bOa)
})
let regex = (...args) => new RegExp(...args)
module.exports = exports.default = {
whole,
repeat,
alpha,
numeric,
and,
or,
wildcard,
extra,
capture,
group,
look: {
ahead: looker(false),
behind: looker(true)
},
matchers: {
ALL,
ANY,
LAZY,
GROUP,
ALPHA,
NUMBER,
WORD,
WHITE_SPACE,
START,
END,
not: {
ALPHA: NOT_ALPHA,
NUMBER: NOT_NUMBER,
WORD: NOT_WORD,
WHITE_SPACE: NOT_WHITE_SPACE
}
},
flags: {
GLOBAL: 'g',
MULTI_LINE: 'm',
INSENSITIVE: 'i',
STICKY: 'y',
UNICODE: 'u'
},
regex
}