-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·124 lines (123 loc) · 3.08 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
#!/usr/bin/env node
'use strict'
const isStringOrNumber = (o) => o &&
Object.prototype.toString.call(o) === '[object String]' ||
!isNaN(parseInt(o, 10))
const getCircularReplacer = () => {
const seen = new WeakSet()
return (key, value) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) {
return '--circular--'
}
seen.add(value)
}
return value
}
}
const excludeReplacer = (keys) => (key, value) =>
keys.includes(key) ? undefined : value
const includeReplacer = (keys) => (key, value) =>
keys.includes(key) || key === '' ? value : undefined
const getMultiReplacer = (replacers) => (key, value) => {
let result
for (const r of replacers) {
if (r) {
result = r(key, result || value)
if (result === undefined) {
return result
}
} else {
result = result || value
}
}
return result
}
class AbcStringify {
constructor(options) {
options = options || {}
this.replacer
this.errorReplacer = (key, value) =>
value && value.name && value.message && value.stack ? {
name: value.name,
message: value.message,
stack: value.stack
} :
value
this.replacerArrayExcludes = false
this.includeKeys = []
this.excludeKeys = []
this.replaceValues = []
this.exactMatch = false
this.replacedValue = '***'
this.space = null
this.replaceValuesReplacer = (key, value) => {
for (const ev of this.replaceValues) {
if (Object.prototype.toString.call(value) === '[object String]') {
if (this.exactMatch) {
if (value === ev) {
return this.replacedValue
}
} else {
value = value.replace(ev, this.replacedValue)
}
}
}
return value
}
Object.assign(this, options)
if (this.replacerArrayExcludes && !this.replacer) {
this.replacer = []
}
}
stringify(value, replacer, space) {
if (isStringOrNumber(replacer)) {
space = replacer
replacer = null
}
replacer = replacer || this.replacer
if (Array.isArray(replacer)) {
if (this.replacerArrayExcludes) {
replacer = replacer.concat(this.excludeKeys)
replacer = excludeReplacer(replacer)
} else {
replacer = replacer.concat(this.includeKeys)
replacer = includeReplacer(replacer)
}
}
return JSON.stringify(
value,
getMultiReplacer([
getCircularReplacer(),
replacer,
this.replaceValuesReplacer,
this.errorReplacer,
]),
space || this.space)
}
}
exports = module.exports = AbcStringify
if (require.main === module) {
let ss = new AbcStringify()
const o = {
name: 'abc stringify',
error: new Error('something bad happened'),
address: {
street: '123 string st',
city: 'hershey',
st: 'pa',
zip: '17033'
},
nest: {
really: {
deep: {
stuff: ['one', 2, {
another: 'thing here'
}]
}
}
}
}
o.circular = o
console.log(ss.stringify(o, 2))
}