mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 12:20:40 -07:00
fix: reading .first, .last of Array, closes #175
This commit is contained in:
+14
-2
@@ -123,11 +123,23 @@ function readProperty (obj: Scope, key: string) {
|
|||||||
if (obj.hasOwnProperty(key)) return obj[key]
|
if (obj.hasOwnProperty(key)) return obj[key]
|
||||||
return obj.liquidMethodMissing(key)
|
return obj.liquidMethodMissing(key)
|
||||||
}
|
}
|
||||||
return key === 'size' ? readSize(obj) : obj[key]
|
if (key === 'size') return readSize(obj)
|
||||||
|
if (key === 'first') return readFirst(obj)
|
||||||
|
if (key === 'last') return readLast(obj)
|
||||||
|
return obj[key]
|
||||||
|
}
|
||||||
|
|
||||||
|
function readFirst (obj: Scope) {
|
||||||
|
if (_.isArray(obj)) return obj[0]
|
||||||
|
return obj['first']
|
||||||
|
}
|
||||||
|
|
||||||
|
function readLast (obj: Scope) {
|
||||||
|
if (_.isArray(obj)) return obj[obj.length - 1]
|
||||||
|
return obj['last']
|
||||||
}
|
}
|
||||||
|
|
||||||
function readSize (obj: Scope) {
|
function readSize (obj: Scope) {
|
||||||
if (!_.isNil(obj['size'])) return obj['size']
|
|
||||||
if (_.isArray(obj) || _.isString(obj)) return obj.length
|
if (_.isArray(obj) || _.isString(obj)) return obj.length
|
||||||
return obj['size']
|
return obj['size']
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,10 @@ describe('Context', function () {
|
|||||||
foo: 'zoo',
|
foo: 'zoo',
|
||||||
one: 1,
|
one: 1,
|
||||||
zoo: { size: 4 },
|
zoo: { size: 4 },
|
||||||
|
obj: {
|
||||||
|
first: 'f',
|
||||||
|
last: 'l'
|
||||||
|
},
|
||||||
bar: {
|
bar: {
|
||||||
zoo: 'coo',
|
zoo: 'coo',
|
||||||
'Mr.Smith': 'John',
|
'Mr.Smith': 'John',
|
||||||
@@ -124,6 +128,18 @@ describe('Context', function () {
|
|||||||
it('should return undefined if do not have size and length', async function () {
|
it('should return undefined if do not have size and length', async function () {
|
||||||
expect(ctx.get('one.size')).to.equal(undefined)
|
expect(ctx.get('one.size')).to.equal(undefined)
|
||||||
})
|
})
|
||||||
|
it('should read .first of array', async function () {
|
||||||
|
expect(ctx.get('bar.arr.first')).to.equal('a')
|
||||||
|
})
|
||||||
|
it('should read .first of object', async function () {
|
||||||
|
expect(ctx.get('obj.first')).to.equal('f')
|
||||||
|
})
|
||||||
|
it('should read .last of array', async function () {
|
||||||
|
expect(ctx.get('bar.arr.last')).to.equal('b')
|
||||||
|
})
|
||||||
|
it('should read .last of object', async function () {
|
||||||
|
expect(ctx.get('obj.last')).to.equal('l')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
describe('strictVariables', async function () {
|
describe('strictVariables', async function () {
|
||||||
let ctx: Context
|
let ctx: Context
|
||||||
|
|||||||
Reference in New Issue
Block a user