This repository was archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodebase.go
More file actions
208 lines (177 loc) Β· 4.58 KB
/
codebase.go
File metadata and controls
208 lines (177 loc) Β· 4.58 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
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
package jsonfilter
// "What the fuck is this??" You might asked.
// Well, I've implemented a Golang version of the compiler
// but it didn't work so well, and I've spent few days working on it,
// yet it still has no progress.
//
// So- yes. Otto (github.com/robertkrimen/otto) is a good JavaScript interpreter.
// The following code from https://github.com/nemtsov/json-mask, man, the author is a nice person.
var js = `
/**
* util.js
*/
var ObjProto = Object.prototype
function isEmpty (obj) {
if (obj == null) return true
if (isArray(obj) ||
(typeof obj === 'string')) return (obj.length === 0)
for (var key in obj) if (has(obj, key)) return false
return true
}
function isArray (obj) {
return ObjProto.toString.call(obj) === '[object Array]'
}
function isObject (obj) {
return (typeof obj === 'function') || (typeof obj === 'object' && !!obj)
}
function has (obj, key) {
return ObjProto.hasOwnProperty.call(obj, key)
}
/**
* filter.js
*/
function filter (obj, compiledMask) {
return isArray(obj)
? _arrayProperties(obj, compiledMask)
: _properties(obj, compiledMask)
}
// wrap array & mask in a temp object;
// extract results from temp at the end
function _arrayProperties (arr, mask) {
var obj = _properties({ _: arr }, {
_: {
type: 'array',
properties: mask
}
})
return obj && obj._
}
function _properties (obj, mask) {
var maskedObj, key, value, ret, retKey, typeFunc
if (!obj || !mask) return obj
if (isArray(obj)) maskedObj = []
else if (isObject(obj)) maskedObj = {}
for (key in mask) {
if (!has(mask, key)) continue
value = mask[key]
ret = undefined
typeFunc = (value.type === 'object') ? _object : _array
if (key === '*') {
ret = _forAll(obj, value.properties, typeFunc)
for (retKey in ret) {
if (!has(ret, retKey)) continue
maskedObj[retKey] = ret[retKey]
}
} else {
ret = typeFunc(obj, key, value.properties)
if (typeof ret !== 'undefined') maskedObj[key] = ret
}
}
return maskedObj
}
function _forAll (obj, mask, fn) {
var ret = {}
var key
var value
for (key in obj) {
if (!has(obj, key)) continue
value = fn(obj, key, mask)
if (typeof value !== 'undefined') ret[key] = value
}
return ret
}
function _object (obj, key, mask) {
var value = obj[key]
if (isArray(value)) return _array(obj, key, mask)
return mask ? _properties(value, mask) : value
}
function _array (object, key, mask) {
var ret = []
var arr = object[key]
var obj
var maskedObj
var i
var l
if (!isArray(arr)) return _properties(arr, mask)
if (isEmpty(arr)) return arr
for (i = 0, l = arr.length; i < l; i++) {
obj = arr[i]
maskedObj = _properties(obj, mask)
if (typeof maskedObj !== 'undefined') ret.push(maskedObj)
}
return ret.length ? ret : undefined
}
/**
* compiler.js
*/
var TERMINALS = { ',': 1, '/': 2, '(': 3, ')': 4 }
function compile (text) {
if (!text) return null
return parse(scan(text))
}
function scan (text) {
var i = 0
var len = text.length
var tokens = []
var name = ''
var ch
function maybePushName () {
if (!name) return
tokens.push({ tag: '_n', value: name })
name = ''
}
for (; i < len; i++) {
ch = text.charAt(i)
if (TERMINALS[ch]) {
maybePushName()
tokens.push({ tag: ch })
} else {
name += ch
}
}
maybePushName()
return tokens
}
function parse (tokens) {
return _buildTree(tokens, {})
}
function _buildTree (tokens, parent) {
var props = {}
var token
while ((token = tokens.shift())) {
if (token.tag === '_n') {
token.type = 'object'
token.properties = _buildTree(tokens, token)
if (parent.hasChild) {
_addToken(token, props)
return props
}
} else if (token.tag === ',') {
return props
} else if (token.tag === '(') {
parent.type = 'array'
continue
} else if (token.tag === ')') {
return props
} else if (token.tag === '/') {
parent.hasChild = true
continue
}
_addToken(token, props)
}
return props
}
function _addToken (token, props) {
props[token.value] = { type: token.type }
if (!isEmpty(token.properties)) {
props[token.value].properties = token.properties
}
}
/**
* index.js
*/
function mask (obj, mask) {
return filter(obj, compile(mask)) || null
}
var result = JSON.stringify(mask(JSON.parse(inputObj), inputMask))
`