-
Notifications
You must be signed in to change notification settings - Fork 56
/
textParsers.js
215 lines (180 loc) · 5.16 KB
/
textParsers.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
var array = require('postgres-array')
var arrayParser = require('./arrayParser')
var parseDate = require('postgres-date')
var parseInterval = require('postgres-interval')
var parseByteA = require('postgres-bytea')
function allowNull (fn) {
return function nullAllowed (value) {
if (value === null) return value
return fn(value)
}
}
function parseBool (value) {
if (value === null) return value
return value === 'TRUE' ||
value === 't' ||
value === 'true' ||
value === 'y' ||
value === 'yes' ||
value === 'on' ||
value === '1'
}
function parseBoolArray (value) {
if (!value) return null
return array.parse(value, parseBool)
}
function parseBaseTenInt (string) {
return parseInt(string, 10)
}
function parseIntegerArray (value) {
if (!value) return null
return array.parse(value, allowNull(parseBaseTenInt))
}
function parseBigIntegerArray (value) {
if (!value) return null
return array.parse(value, allowNull(function (entry) {
return parseBigInteger(entry).trim()
}))
}
var parsePointArray = function (value) {
if (!value) { return null }
var p = arrayParser.create(value, function (entry) {
if (entry !== null) {
entry = parsePoint(entry)
}
return entry
})
return p.parse()
}
var parseFloatArray = function (value) {
if (!value) { return null }
var p = arrayParser.create(value, function (entry) {
if (entry !== null) {
entry = parseFloat(entry)
}
return entry
})
return p.parse()
}
var parseStringArray = function (value) {
if (!value) { return null }
var p = arrayParser.create(value)
return p.parse()
}
var parseDateArray = function (value) {
if (!value) { return null }
var p = arrayParser.create(value, function (entry) {
if (entry !== null) {
entry = parseDate(entry)
}
return entry
})
return p.parse()
}
var parseIntervalArray = function (value) {
if (!value) { return null }
var p = arrayParser.create(value, function (entry) {
if (entry !== null) {
entry = parseInterval(entry)
}
return entry
})
return p.parse()
}
var parseByteAArray = function (value) {
if (!value) { return null }
return array.parse(value, allowNull(parseByteA))
}
var parseInteger = function (value) {
return parseInt(value, 10)
}
var parseBigInteger = function (value) {
var valStr = String(value)
if (/^\d+$/.test(valStr)) { return valStr }
return value
}
var parseJsonArray = function (value) {
if (!value) { return null }
return array.parse(value, allowNull(JSON.parse))
}
var parsePoint = function (value) {
if (value[0] !== '(') { return null }
value = value.substring(1, value.length - 1).split(',')
return {
x: parseFloat(value[0]),
y: parseFloat(value[1])
}
}
var parseCircle = function (value) {
if (value[0] !== '<' && value[1] !== '(') { return null }
var point = '('
var radius = ''
var pointParsed = false
for (var i = 2; i < value.length - 1; i++) {
if (!pointParsed) {
point += value[i]
}
if (value[i] === ')') {
pointParsed = true
continue
} else if (!pointParsed) {
continue
}
if (value[i] === ',') {
continue
}
radius += value[i]
}
var result = parsePoint(point)
result.radius = parseFloat(radius)
return result
}
var init = function (register) {
register(20, parseBigInteger) // int8
register(21, parseInteger) // int2
register(23, parseInteger) // int4
register(26, parseInteger) // oid
register(700, parseFloat) // float4/real
register(701, parseFloat) // float8/double
register(16, parseBool)
register(1082, parseDate) // date
register(1114, parseDate) // timestamp without timezone
register(1184, parseDate) // timestamp
register(600, parsePoint) // point
register(651, parseStringArray) // cidr[]
register(718, parseCircle) // circle
register(1000, parseBoolArray)
register(1001, parseByteAArray)
register(1005, parseIntegerArray) // _int2
register(1007, parseIntegerArray) // _int4
register(1028, parseIntegerArray) // oid[]
register(1016, parseBigIntegerArray) // _int8
register(1017, parsePointArray) // point[]
register(1021, parseFloatArray) // _float4
register(1022, parseFloatArray) // _float8
register(1231, parseStringArray) // _numeric
register(1014, parseStringArray) // char
register(1015, parseStringArray) // varchar
register(1008, parseStringArray)
register(1009, parseStringArray)
register(1040, parseStringArray) // macaddr[]
register(1041, parseStringArray) // inet[]
register(1115, parseDateArray) // timestamp without time zone[]
register(1182, parseDateArray) // _date
register(1185, parseDateArray) // timestamp with time zone[]
register(1186, parseInterval)
register(1187, parseIntervalArray)
register(17, parseByteA)
register(114, JSON.parse.bind(JSON)) // json
register(3802, JSON.parse.bind(JSON)) // jsonb
register(199, parseJsonArray) // json[]
register(3807, parseJsonArray) // jsonb[]
register(3907, parseStringArray) // numrange[]
register(2951, parseStringArray) // uuid[]
register(791, parseStringArray) // money[]
register(1183, parseStringArray) // time[]
register(1270, parseStringArray) // timetz[]
}
module.exports = {
init: init
}