-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (122 loc) · 2.98 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
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
var from = require('from2')
var quote = require('quote-stream')
var through = require('through2')
var BGN_OBJ = '{'
var END_OBJ = '}'
var BGN_ARR = '['
var END_ARR = ']'
var SEP = ','
module.exports = rummage
function rummage (value) {
return from(function (size, cb) {
var self = this
read.call(this, value, function () {
self.push('\n')
cb(null, null)
})
})
}
function read (value, done) {
var self = this
if (isStream(value)) {
value.pipe(quote()).pipe(writer())
} else if (value && isStream(value._json)) {
value._json.pipe(writer())
} else if (value && isStream(value._string)) {
value._string.pipe(quote()).pipe(writer())
} else if (value && isStream(value._array)) {
value._array.pipe(array()).pipe(writer())
} else if (value && isStream(value._object)) {
value._object.pipe(object()).pipe(writer())
} else if (value && typeof value.toJSON === 'function') {
self.push(JSON.stringify(value.toJSON()))
done()
} else if (Array.isArray(value) || isObject(value)) {
walk.call(self, value, done)
} else {
self.push(JSON.stringify(value))
done()
}
function writer () {
return through.obj(function (chunk, enc, cb) {
self.push(chunk)
cb()
}, function (cb) {
cb()
done()
})
}
}
function walk (value, cb) {
var self = this
var keys = Object.keys(value)
var arr = Array.isArray(value)
if (arr) this.push(BGN_ARR)
else this.push(BGN_OBJ)
next()
function next () {
var k = keys.shift()
var v = value[k]
if (!isValid(v)) {
if (arr) v = null
else return next(value)
}
if (!arr) self.push(JSON.stringify(k) + ':')
read.call(self, v, done)
}
function done (err) {
if (err) return cb(err)
if (keys.length) {
self.push(SEP)
next(value)
} else {
if (arr) self.push(END_ARR)
else self.push(END_OBJ)
cb()
}
}
}
function array () {
var first = true
var t = through.obj(write, end)
t.push(BGN_ARR)
return t
function write (chunk, enc, cb) {
if (!isValid(chunk)) chunk = null
if (!first) this.push(SEP)
first = false
read.call(this, chunk, cb)
}
function end (cb) {
this.push(END_ARR)
cb()
}
}
function object () {
var first = true
var t = through.obj(write, end)
t.push(BGN_OBJ)
return t
function write (chunk, enc, cb) {
if (!isValid(chunk[1])) return cb()
if (!first) this.push(SEP)
first = false
this.push(JSON.stringify(String(chunk[0])) + ':')
read.call(this, chunk[1], cb)
}
function end (cb) {
this.push(END_OBJ)
cb()
}
}
function isValid (value) {
return typeof value === 'string' || typeof value === 'number' ||
typeof value === 'boolean' || value === null ||
value instanceof Date || Array.isArray(value) || isObject(value)
}
function isObject (value) {
return Object.prototype.toString.call(value) === '[object Object]'
}
function isStream (value) {
return value && typeof value.pipe === 'function'
}