-
Notifications
You must be signed in to change notification settings - Fork 39
/
parser.js
190 lines (160 loc) · 5.27 KB
/
parser.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
import {type, typeMatcher} from './type'
export class TypeParser {
validDataTypes = ['String', 'Array', 'SimpleObject', 'ComplexObject', 'Primitive']
constructor (data) {
this.data = data
}
// ==========================================================================
// Validation
// ==========================================================================
validateDataType () {
const dataType = this.data.dataType
if (dataType && !this.validDataTypes.includes(dataType)) {
throw new RangeError(`dataType was ${dataType}; expected one of ${this.validDataTypes}`)
}
}
validateParseType () {
const predicate = this.data.predicate
if (predicate && !(predicate instanceof RegExp || typeof predicate === 'function')) {
throw new TypeError(`predicate was ${typeof predicate}; expected RegExp or function`)
}
}
validatePropertyConstraint () {
const propertyConstraint = this.data.propertyConstraint
if (propertyConstraint && typeof propertyConstraint !== 'object') {
throw new TypeError(`propertyConstraint was ${typeof propertyConstraint}; expected array or object`)
}
}
validateElementConstraint () {
const elementConstraint = this.data.elementConstraint
if (elementConstraint && typeof elementConstraint !== 'string') {
throw new TypeError(`elementConstraint was ${typeof elementConstraint}; expected string`)
}
}
validateExtends () {
const extend = this.data.extends
if (extend && typeof extend !== 'string') {
throw new TypeError(`extends was ${typeof extend}; expected string`)
}
}
validate () {
if (this.data !== null && typeof this.data !== 'object') {
throw new TypeError(`typeParser was ${typeof this.data}; expected object`)
}
this.validateDataType()
this.validateParseType()
this.validatePropertyConstraint()
this.validateElementConstraint()
this.validateExtends()
}
// ==========================================================================
// Simplification helpers
// ==========================================================================
parsePropertyConstraint () {
let constraints = [].concat(this.data.propertyConstraint || [])
return constraints.map(({props, match = 'every', value = () => true}) => {
props = [].concat(props)
return input => props[match](prop => prop in input && value(input[prop]))
})
}
parseElementConstraint () {
let constraint = this.data.elementConstraint
return !constraint ? [] : [input => input.every(entry => type(entry) === constraint)]
}
parsePredicate () {
if (this.data.predicate instanceof RegExp) {
return [this.data.predicate.test.bind(this.data.predicate)]
} else if (this.data.predicate) {
return [this.data.predicate]
} else {
return []
}
}
getCombinedPredicate () {
let predicates = [
...this.parsePredicate(),
...this.parsePropertyConstraint(),
...this.parseElementConstraint()
]
if (predicates.length === 0) {
return () => true
} else if (predicates.length === 1) {
return predicates[0]
} else {
return input => predicates.every(predicate => predicate(input))
}
}
getDataType () {
if (this.data.dataType) {
return this.data.dataType
} else if (this.data.predicate instanceof RegExp) {
return 'String'
} else if (this.data.elementConstraint) {
return 'Array'
} else {
return 'Primitive'
}
}
// ==========================================================================
// Data simplification
// ==========================================================================
get dataType () {
return this.getDataType()
}
get predicate () {
return this.getCombinedPredicate()
}
get extends () {
return this.data.extends
}
}
export class DataParser {
constructor (parser, {async} = {}) {
this.parser = parser
this.async = async
}
// ==========================================================================
// Validation
// ==========================================================================
validate () {
const parser = this.parser
if (typeof parser !== 'function') {
throw new TypeError(`parser was ${typeof parser}; expected function`)
}
}
}
export class FormatParser {
constructor (format, parsers = {}) {
this.format = format
if (parsers.parseType) {
this.typeParser = new TypeParser(parsers.parseType)
}
if (parsers.parse) {
this.dataParser = new DataParser(parsers.parse, {async: false})
}
if (parsers.parseAsync) {
this.asyncDataParser = new DataParser(parsers.parseAsync, {async: true})
}
}
// ==========================================================================
// Validation
// ==========================================================================
validateFormat () {
const format = this.format
if (!typeMatcher.test(format)) {
throw new TypeError(`format name was "${format}"; didn't match expected pattern`)
}
}
validate () {
this.validateFormat()
if (this.typeParser) {
this.typeParser.validate()
}
if (this.dataParser) {
this.dataParser.validate()
}
if (this.asyncDataParser) {
this.asyncDataParser.validate()
}
}
}