fix: size filter does not respect Objects, fixes #385

This commit is contained in:
Harttle
2021-12-07 23:05:46 +08:00
committed by Harttle
parent e2ef236f68
commit 6c114267a5
2 changed files with 11 additions and 2 deletions
+2 -1
View File
@@ -104,6 +104,7 @@ function readLast (obj: Scope) {
}
function readSize (obj: Scope) {
if (obj.hasOwnProperty('size') || obj['size'] !== undefined) return obj['size']
if (isArray(obj) || isString(obj)) return obj.length
return obj['size']
if (typeof obj === 'object') return Object.keys(obj).length
}
+9 -1
View File
@@ -11,6 +11,7 @@ describe('Context', function () {
foo: 'zoo',
one: 1,
zoo: { size: 4 },
map: new Map([['foo', 'FOO']]),
obj: {
first: 'f',
last: 'l'
@@ -33,7 +34,7 @@ describe('Context', function () {
it('should read nested property', async function () {
expect(ctx.get(['obj', 'first'])).to.equal('f')
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 () {
expect(ctx.get(['notdefined'])).to.equal(undefined)
@@ -55,6 +56,13 @@ describe('Context', function () {
it('should return array length as size', async function () {
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 () {
expect(ctx.get(['bar', 'arr', 'first'])).to.equal('a')
})