-
-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathserializer.js
167 lines (152 loc) · 4.25 KB
/
serializer.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
'use strict'
// eslint-disable-next-line
const STR_ESCAPE = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?:[^\ud800-\udbff]|^)[\udc00-\udfff]/
module.exports = class Serializer {
constructor (options) {
switch (options && options.rounding) {
case 'floor':
this.parseInteger = Math.floor
break
case 'ceil':
this.parseInteger = Math.ceil
break
case 'round':
this.parseInteger = Math.round
break
case 'trunc':
default:
this.parseInteger = Math.trunc
break
}
this._options = options
}
asInteger (i) {
if (typeof i === 'number') {
if (i === Infinity || i === -Infinity) {
throw new Error(`The value "${i}" cannot be converted to an integer.`)
}
if (Number.isInteger(i)) {
return '' + i
}
if (Number.isNaN(i)) {
throw new Error(`The value "${i}" cannot be converted to an integer.`)
}
return this.parseInteger(i)
} else if (i === null) {
return '0'
} else if (typeof i === 'bigint') {
return i.toString()
} else {
/* eslint no-undef: "off" */
const integer = this.parseInteger(i)
if (Number.isFinite(integer)) {
return '' + integer
} else {
throw new Error(`The value "${i}" cannot be converted to an integer.`)
}
}
}
asNumber (i) {
const num = Number(i)
if (Number.isNaN(num)) {
throw new Error(`The value "${i}" cannot be converted to a number.`)
} else if (!Number.isFinite(num)) {
return 'null'
} else {
return '' + num
}
}
asBoolean (bool) {
return bool && 'true' || 'false' // eslint-disable-line
}
asDateTime (date) {
if (date === null) return '""'
if (date instanceof Date) {
return '"' + date.toISOString() + '"'
}
if (typeof date === 'string') {
return '"' + date + '"'
}
throw new Error(`The value "${date}" cannot be converted to a date-time.`)
}
asDate (date) {
if (date === null) return '""'
if (date instanceof Date) {
return '"' + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(0, 10) + '"'
}
if (typeof date === 'string') {
return '"' + date + '"'
}
throw new Error(`The value "${date}" cannot be converted to a date.`)
}
asTime (date) {
if (date === null) return '""'
if (date instanceof Date) {
return '"' + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(11, 19) + '"'
}
if (typeof date === 'string') {
return '"' + date + '"'
}
throw new Error(`The value "${date}" cannot be converted to a time.`)
}
asString (str) {
if (typeof str !== 'string') {
if (str === null) {
return '""'
}
if (str instanceof Date) {
return '"' + str.toISOString() + '"'
}
if (str instanceof RegExp) {
str = str.source
} else {
str = str.toString()
}
}
if (str.length < 42) {
return this.asStringSmall(str)
} else if (STR_ESCAPE.test(str) === false) {
return '"' + str + '"'
} else {
return JSON.stringify(str)
}
}
// magically escape strings for json
// relying on their charCodeAt
// everything below 32 needs JSON.stringify()
// every string that contain surrogate needs JSON.stringify()
// 34 and 92 happens all the time, so we
// have a fast case for them
asStringSmall (str) {
const len = str.length
let result = ''
let last = -1
let point = 255
// eslint-disable-next-line
for (var i = 0; i < len; i++) {
point = str.charCodeAt(i)
if (point < 32) {
return JSON.stringify(str)
}
if (point >= 0xD800 && point <= 0xDFFF) {
// The current character is a surrogate.
return JSON.stringify(str)
}
if (
point === 0x22 || // '"'
point === 0x5c // '\'
) {
last === -1 && (last = 0)
result += str.slice(last, i) + '\\'
last = i
}
}
return (last === -1 && ('"' + str + '"')) || ('"' + result + str.slice(last) + '"')
}
getState () {
return this._options
}
static restoreFromState (state) {
return new Serializer(state)
}
}