diff --git a/src/context/context.ts b/src/context/context.ts index 379f75da0..bbce304a7 100644 --- a/src/context/context.ts +++ b/src/context/context.ts @@ -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 } diff --git a/test/unit/context/context.ts b/test/unit/context/context.ts index 432859150..e569ec282 100644 --- a/test/unit/context/context.ts +++ b/test/unit/context/context.ts @@ -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') })