-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.js
More file actions
149 lines (146 loc) · 4.01 KB
/
object.js
File metadata and controls
149 lines (146 loc) · 4.01 KB
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
module.exports = {
isEmpty() {
return this.keys().length == 0
},
uc(keys = []) {
if (keys.isStr) keys = keys.arr()
if (keys.length == 0) keys = this.keys()
keys.forEach(k => this[k] = this[k].uc())
return this
},
lc(keys = []) {
if (keys.isStr) keys = keys.arr()
if (keys.length == 0) keys = this.keys()
keys.forEach(k => this[k] = this[k].lc())
return this
},
tc(keys = []) {
if (keys.isStr) keys = keys.arr()
if (keys.length == 0) keys = this.keys()
keys.forEach(k => this[k] = this[k].tc())
return this
},
keys() {
return Object.keys(this)
},
vals(fn) {
if (!fn) return this.keys().map(k => this[k])
this.keys().forEach(k => fn(this[k]))
},
slice(keys) {
if (keys.isStr) keys = keys.arr()
var r = (acc, k) => {
if (k in this) acc[k] = this[k]
return acc
}
return keys.reduce(r, {})
},
map(fn, acc = {}) {
var r = (acc, k) => (fn(this, k, acc), acc)
return this.keys().reduce(r, acc)
},
each(fn) {
this.keys().forEach(k => fn(k, this))
},
keyval(ks = '=', rs = '\n') {
if (typeof ks == 'string') {
var r = (o, k, acc) => {
acc.push(k + ks + o[k])
return acc
}
return this.map(r, []).join(rs)
}
else if (Array.isArray(ks)) {
var [key, val] = ks
var r = (o, k, acc) => {
acc.push({[key || 'k']: k, [val || 'v']: o[k]})
return acc
}
return this.map(r, [])
}
},
assign(...ls) {
return Object.assign(this, ...ls)
},
concat(...ls) {
return Object.assign({}, this, ...ls)
},
def(...ls) {
return Object.assign({}, ...ls, this)
},
mv(o) {
o.map((self, k, acc) => {
if (o[k]) this[o[k]] = this[k]
delete this[k]
})
return this
},
mvp(o) {
return this.map((self, k, acc) => {
if (k in o) {
if (o[k]) acc[o[k]] = self[k]
}
else acc[k] = self[k]
})
},
rm(...ls) {
if (ls.length == 1) ls = ls[0].arr()
ls.forEach(k => delete this[k])
return this
},
rmp(...ls) {
var ret = {}.concat(this)
if (ls.length == 1) ls = ls[0].arr()
ls.forEach(k => delete ret[k])
return ret
},
notIn(o) {
var ok = o.keys()
return this.keys().filter(k => ok.indexOf(k) == -1)
},
getpath(path) {
var {k, o} = mkpath(this, path)
return o[k]
},
setpath(path, v, noclobber = false) {
var {k, o} = mkpath(this, path)
if (!noclobber || !(k in o)) o[k] = v
return o[k]
},
json(safe = false) {
if (this instanceof Error) return JSON.stringify(this.obj())
return safe ? safeJSON(this) : JSON.stringify(this)
},
tee(path) {
JSON.stringify(this).tee(path, {clobber: true})
}
}
function mkpath(o, path) {
if (typeof(path) == 'string')
path = path.replace(/\./g, '/').split('/')
var k = path.pop()
path.forEach(k => { if (!o[k]) o[k] = {}; o = o[k] })
return {k, o}
}
// https://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json
function safeJSON(v) {
const cache = new Set()
return JSON.stringify(v, function (key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.has(value)) {
// Circular reference found
try {
// If this value does not reference a parent it can be deduped
return JSON.parse(JSON.stringify(value))
}
catch (err) {
// discard key if value cannot be deduped
return
}
}
// Store value in our set
cache.add(value)
}
return value
})
}