mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 12:20:40 -07:00
fix: size filter does not respect Objects, fixes #385
This commit is contained in:
@@ -104,6 +104,7 @@ function readLast (obj: Scope) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function readSize (obj: Scope) {
|
function readSize (obj: Scope) {
|
||||||
|
if (obj.hasOwnProperty('size') || obj['size'] !== undefined) return obj['size']
|
||||||
if (isArray(obj) || isString(obj)) return obj.length
|
if (isArray(obj) || isString(obj)) return obj.length
|
||||||
return obj['size']
|
if (typeof obj === 'object') return Object.keys(obj).length
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ describe('Context', function () {
|
|||||||
foo: 'zoo',
|
foo: 'zoo',
|
||||||
one: 1,
|
one: 1,
|
||||||
zoo: { size: 4 },
|
zoo: { size: 4 },
|
||||||
|
map: new Map([['foo', 'FOO']]),
|
||||||
obj: {
|
obj: {
|
||||||
first: 'f',
|
first: 'f',
|
||||||
last: 'l'
|
last: 'l'
|
||||||
@@ -33,7 +34,7 @@ describe('Context', function () {
|
|||||||
it('should read nested property', async function () {
|
it('should read nested property', async function () {
|
||||||
expect(ctx.get(['obj', 'first'])).to.equal('f')
|
expect(ctx.get(['obj', 'first'])).to.equal('f')
|
||||||
expect(ctx.get(['obj', 'last'])).to.equal('l')
|
expect(ctx.get(['obj', 'last'])).to.equal('l')
|
||||||
expect(ctx.get(['obj', 'size'])).to.equal(undefined)
|
expect(ctx.get(['obj', 'size'])).to.equal(2)
|
||||||
})
|
})
|
||||||
it('undefined property should yield undefined', async function () {
|
it('undefined property should yield undefined', async function () {
|
||||||
expect(ctx.get(['notdefined'])).to.equal(undefined)
|
expect(ctx.get(['notdefined'])).to.equal(undefined)
|
||||||
@@ -55,6 +56,13 @@ describe('Context', function () {
|
|||||||
it('should return array length as size', async function () {
|
it('should return array length as size', async function () {
|
||||||
expect(ctx.get(['bar', 'arr', 'size'])).to.equal(2)
|
expect(ctx.get(['bar', 'arr', 'size'])).to.equal(2)
|
||||||
})
|
})
|
||||||
|
it('should return map size as size', async function () {
|
||||||
|
expect(ctx.get(['map', 'size'])).to.equal(1)
|
||||||
|
})
|
||||||
|
it('should return undefined if not have a size', async function () {
|
||||||
|
expect(ctx.get(['one', 'size'])).to.equal(undefined)
|
||||||
|
expect(ctx.get(['non-exist', 'size'])).to.equal(undefined)
|
||||||
|
})
|
||||||
it('should read .first of array', async function () {
|
it('should read .first of array', async function () {
|
||||||
expect(ctx.get(['bar', 'arr', 'first'])).to.equal('a')
|
expect(ctx.get(['bar', 'arr', 'first'])).to.equal('a')
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user