mirror of
https://github.com/harttle/liquidjs.git
synced 2026-10-03 00:55:13 -07:00
refactor access seq parsing
This commit is contained in:
+36
-38
@@ -100,49 +100,47 @@ var Scope = {
|
|||||||
propertyAccessSeq: function (str) {
|
propertyAccessSeq: function (str) {
|
||||||
var seq = []
|
var seq = []
|
||||||
var name = ''
|
var name = ''
|
||||||
for (var i = 0; i < str.length; i++) {
|
var j
|
||||||
if (str[i] === '[') {
|
var i = 0
|
||||||
seq.push(name)
|
while (i < str.length) {
|
||||||
name = ''
|
switch (str[i]) {
|
||||||
|
case '[':
|
||||||
|
push()
|
||||||
|
|
||||||
var delemiter = str[i + 1]
|
var delemiter = str[i + 1]
|
||||||
if (delemiter !== "'" && delemiter !== '"') {
|
if (/['"]/.test(delemiter)) { // foo["bar"]
|
||||||
// foo[bar.coo]
|
j = str.indexOf(delemiter, i + 2)
|
||||||
var j = matchRightBracket(str, i + 1)
|
assert(j !== -1, `unbalanced ${delemiter}: ${str}`)
|
||||||
assert(j !== -1, `unbalanced []: ${str}`)
|
name = str.slice(i + 2, j)
|
||||||
name = str.slice(i + 1, j)
|
push()
|
||||||
if (lexical.isInteger(name)) {
|
i = j + 2
|
||||||
// foo[1]
|
} else { // foo[bar.coo]
|
||||||
seq.push(name)
|
j = matchRightBracket(str, i + 1)
|
||||||
} else {
|
assert(j !== -1, `unbalanced []: ${str}`)
|
||||||
// foo["bar"]
|
name = str.slice(i + 1, j)
|
||||||
seq.push(this.get(name))
|
if (!lexical.isInteger(name)) { // foo[bar] vs. foo[1]
|
||||||
|
name = this.get(name)
|
||||||
|
}
|
||||||
|
push()
|
||||||
|
i = j + 1
|
||||||
}
|
}
|
||||||
name = ''
|
break
|
||||||
i = j
|
case '.':// foo.bar, foo[0].bar
|
||||||
} else {
|
push()
|
||||||
// foo["bar"]
|
i++
|
||||||
j = str.indexOf(delemiter, i + 2)
|
break
|
||||||
assert(j !== -1, `unbalanced ${delemiter}: ${str}`)
|
default:// foo.bar
|
||||||
name = str.slice(i + 2, j)
|
name += str[i]
|
||||||
seq.push(name)
|
i++
|
||||||
name = ''
|
|
||||||
i = j + 2
|
|
||||||
}
|
|
||||||
} else if (str[i] === '.') {
|
|
||||||
// foo.bar
|
|
||||||
// foo.bar[0].foo
|
|
||||||
// In the case of foo.bar[0].foo, must check length because
|
|
||||||
// name will be empty after the closing `]` is handled above.
|
|
||||||
if (name.length) seq.push(name)
|
|
||||||
name = ''
|
|
||||||
} else {
|
|
||||||
// foo.bar
|
|
||||||
name += str[i]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (name.length) seq.push(name)
|
push()
|
||||||
return seq
|
return seq
|
||||||
|
|
||||||
|
function push () {
|
||||||
|
if (name.length) seq.push(name)
|
||||||
|
name = ''
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,6 +42,10 @@ describe('scope', function () {
|
|||||||
expect(scope.propertyAccessSeq('bar["foo"].zoo'))
|
expect(scope.propertyAccessSeq('bar["foo"].zoo'))
|
||||||
.to.deep.equal(['bar', 'foo', 'zoo'])
|
.to.deep.equal(['bar', 'foo', 'zoo'])
|
||||||
})
|
})
|
||||||
|
it('should handle nested access 4', function () {
|
||||||
|
expect(scope.propertyAccessSeq('foo[0].bar'))
|
||||||
|
.to.deep.equal(['foo', '0', 'bar'])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('#get()', function () {
|
describe('#get()', function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user