forked from JairusSW/kati
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (69 loc) · 1.92 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
// TODO: Handle circular data
// Pre-alloc in memory. (faster)
const nullVal = `null`
// Precompile regular expressions
const reQuote = /\"/g;
// Much faster if functions are split up by types.
const fromString = (data) => {
// Catch a bug.
if (data.includes(`"`)) {
// Need replaceAll. Figure this out later.
return `"${data.replace(reQuote, '\\"')}"`
}
return `"${data}"`
}
const fromNumber = (data) => {
return `${data}`
}
const fromArray = (data) => {
const len = data.length - 1
if( len === -1 ) {
return '[]'
}
let result = '['
// Just loop through all the chunks and stringify them.
for (let i = 0; i < len; i++) {
const chunk = data[i]
result += `${stringify(chunk)},`
}
result += `${stringify(data[len])}]`
return result
}
const fromObject = (data) => {
const keys = Object.keys(data)
const len = keys.length - 1
if( len === -1 ) {
return '{}'
}
let result = '{'
const lastKey = keys[len]
// Just loop through all the keys and stringify them.
for (let i = 0; i < len; i++) {
const key = keys[i]
// Iterate through all but the last. (To keep the commas clean)
result += `${stringify(key)}:${stringify(data[key])},`
}
result += `${stringify(lastKey)}:${stringify(data[lastKey])}}`
return result
}
const stringify = (data) => {
let result = ''
if (typeof data === 'string') {
result += fromString(data)
} else if (Number.isFinite(data)) {
result += fromNumber(data)
} else if (data instanceof Array) {
result += fromArray(data)
} else if (data === true || data === false) {
result += data ? `true` : `false`
} else if (data instanceof Object) {
result += fromObject(data)
} else {
result += nullVal
}
return result
}
module.exports = {
stringify: stringify,
parse: JSON.parse
}