Skip to content

Commit 74f57f8

Browse files
committed
feat: allow masking objects with backslash
1 parent 5cd8b74 commit 74f57f8

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

lib/compiler.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var util = require('./util')
2-
var TERMINALS = { ',': 1, '/': 2, '(': 3, ')': 4, '*': 5 }
2+
var TERMINALS = { ',': 1, '/': 2, '(': 3, ')': 4 }
33
var ESCAPE_CHAR = '\\'
4+
var WILDCARD_CHAR = '*'
45

56
module.exports = compile
67

@@ -46,13 +47,15 @@ function scan (text) {
4647

4748
for (; i < len; i++) {
4849
ch = text.charAt(i)
49-
if (ch === ESCAPE_CHAR) {
50+
if (ch === ESCAPE_CHAR && !escape) {
5051
escape = true
5152
continue
5253
}
53-
if (TERMINALS[ch] && escape !== true) {
54+
if (TERMINALS[ch] && !escape) {
5455
maybePushName()
5556
tokens.push({ tag: ch })
57+
} else if (ch === WILDCARD_CHAR && !name.length && !escape) {
58+
tokens.push({ tag: ch, value: ch })
5659
} else {
5760
name += ch
5861
}
@@ -98,9 +101,11 @@ function _buildTree (tokens, parent) {
98101

99102
function _addToken (token, props) {
100103
var prop = { type: token.type }
101-
if (token.tag === '*') prop.filter = true
102104
if (!util.isEmpty(token.properties)) {
103105
prop.properties = token.properties
104106
}
105-
props[token.value || token.tag] = prop
107+
if (token.tag === WILDCARD_CHAR) {
108+
prop.filter = true
109+
}
110+
props[token.value] = prop
106111
}

test/compiler-test.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,19 @@ tests = {
152152
'*': { type: 'object' },
153153
'(': { type: 'object' },
154154
')': { type: 'object' },
155-
',': { type: 'object' },
155+
',': { type: 'object' }
156156
}
157157
}
158+
},
159+
'\\\\': {
160+
'\\': {
161+
type: 'object'
162+
}
163+
},
164+
'foo*bar': {
165+
'foo*bar': {
166+
type: 'object'
167+
}
158168
}
159169
}
160170

test/index-test.js

+11
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,17 @@ tests = [{
211211
'*': 101,
212212
some: 'visible'
213213
}
214+
}, {
215+
m: 'some,\\\\',
216+
o: {
217+
'\\': 120,
218+
beta: 'hidden',
219+
some: 'visible'
220+
},
221+
e: {
222+
'\\': 120,
223+
some: 'visible'
224+
}
214225
}]
215226

216227
describe('json-mask', function () {

0 commit comments

Comments
 (0)