-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
254 lines (208 loc) · 6.23 KB
/
index.ts
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
export type JsonValue =
| string
| number
| null
| boolean
| JsonValue[]
| { [property: string]: JsonValue }
export type JsonKey = string | number
export enum JsonError {
/// Type is not json encodable
unsupportedType,
/// Out of bound access to list
indexOutOfBounds,
/// Unexpected type
wrongType,
/// Entry does not exists
notExist
}
export class JsonException extends Error {
readonly error: JsonError
constructor (error: JsonError, reason?: string, stack?: string) {
super()
this.error = error
this.name = JsonError[error]
this.stack = stack
switch (error) {
case JsonError.unsupportedType:
reason = reason ?? 'JSON Error: not a valid JSON value'
break
case JsonError.indexOutOfBounds:
reason = reason ?? 'JSON Error: index out of bounds'
break
case JsonError.wrongType:
reason =
reason ??
'JSON Error: either key is not a index type or value is not indexable'
break
case JsonError.notExist:
reason = reason ?? "JSON Error: key does't not exists"
break
}
}
}
export default class TypedJson {
readonly rawValue: JsonValue
readonly exception?: JsonException
constructor (rawValue: JsonValue, exception?: JsonException) {
this.rawValue = rawValue
this.exception = exception
}
static fromString (json: string) {
return new TypedJson(JSON.parse(json))
}
get (keyOrPath: JsonKey | Array<JsonKey>): TypedJson {
if (Array.isArray(keyOrPath)) {
if (keyOrPath.length == 0) {
throw new Error("Path can't be empty")
}
return keyOrPath.length > 1
? this.get(keyOrPath[0]).get(keyOrPath.slice(1))
: this.get(keyOrPath[0])
}
if (Array.isArray(this.rawValue)) {
const array = this.rawValue as JsonValue[]
let index: number
if (typeof keyOrPath === 'string') {
index = parseInt(keyOrPath)
if (isNaN(index)) {
return new TypedJson(
null,
new JsonException(
JsonError.wrongType,
`JSON Error: index must be a number, string given`
)
)
}
} else {
index = (keyOrPath as number) | 0 // Ensure integer
}
if (index < 0 || index >= array.length) {
return new TypedJson(
null,
new JsonException(JsonError.indexOutOfBounds)
)
}
return new TypedJson(array[index])
} else if (typeof this.rawValue === 'object' && this.rawValue !== null) {
const map = this.rawValue as { [property: string]: JsonValue }
let index: string =
typeof keyOrPath === 'string' ? keyOrPath : `${keyOrPath}`
let result = new TypedJson(map[index])
if (!map.hasOwnProperty(index)) {
return new TypedJson(
null,
new JsonException(
JsonError.notExist,
`JSON Error: key ${index} does not exists`
)
)
}
return result
}
return new TypedJson(null, new JsonException(JsonError.wrongType))
}
exists (key: JsonKey, notNull: boolean = true): boolean {
return (
this.get(key).exception === undefined &&
(!notNull || this.get(key).rawValue !== null)
)
}
string (): string | undefined {
return typeof this.rawValue === 'string' ? this.rawValue : undefined
}
stringValue (): string {
if (typeof this.rawValue === 'string') {
return this.rawValue
} else if (['boolean', 'number'].indexOf(typeof this.rawValue) >= 0) {
return `${this.rawValue}`
}
return ''
}
boolean (): boolean | undefined {
return typeof this.rawValue === 'boolean' ? this.rawValue : undefined
}
booleanValue (): boolean {
if (typeof this.rawValue === 'boolean') {
return this.rawValue
} else if (typeof this.rawValue === 'number') {
return this.rawValue === 1
} else if (typeof this.rawValue === 'string') {
return (
['true', 'y', 't', 'yes', '1'].indexOf(this.rawValue.toLowerCase()) >= 0
)
}
return false
}
integer (): number | undefined {
return typeof this.rawValue === 'number' && Number.isInteger(this.rawValue)
? this.rawValue
: undefined
}
integerValue (): number {
if (typeof this.rawValue === 'number') {
return Number.isInteger(this.rawValue)
? this.rawValue
: Math.round(this.rawValue)
} else if (typeof this.rawValue === 'boolean') {
return this.rawValue ? 1 : 0
} else if (typeof this.rawValue === 'string') {
let parsed = parseInt(this.rawValue)
return isNaN(parsed) ? 0 : parsed
}
return 0
}
float (): number | undefined {
return typeof this.rawValue === 'number' && !Number.isInteger(this.rawValue)
? this.rawValue
: undefined
}
floatValue (): number {
if (typeof this.rawValue === 'number') {
return this.rawValue
} else if (typeof this.rawValue === 'boolean') {
return this.rawValue ? 1 : 0
} else if (typeof this.rawValue === 'string') {
let parsed = parseFloat(this.rawValue)
return isNaN(parsed) ? 0 : parsed
}
return 0
}
array (): TypedJson[] | undefined {
return Array.isArray(this.rawValue)
? this.rawValue.map(value => new TypedJson(value))
: undefined
}
arrayValue (): TypedJson[] {
return this.array() ?? []
}
arrayRaw (): JsonValue[] | undefined {
return Array.isArray(this.rawValue) ? this.rawValue : undefined
}
arrayRawValue (): JsonValue[] {
return this.arrayRaw() ?? []
}
object (): { [property: string]: TypedJson } | undefined {
if (typeof this.rawValue !== 'object' || Array.isArray(this.rawValue)) {
return undefined
}
let obj: any = {}
Object.entries(this.rawValue ?? []).forEach(item => {
obj[item[0]] = new TypedJson(item[1])
})
return obj
}
objectValue (): { [property: string]: TypedJson } {
return this.object() ?? {}
}
objectRaw (): { [property: string]: JsonValue } | undefined {
return this.rawValue !== null &&
typeof this.rawValue === 'object' &&
!Array.isArray(this.rawValue)
? this.rawValue
: undefined
}
objectRawValue (): { [property: string]: JsonValue } {
return this.objectRaw() ?? {}
}
}