From 053db773cc059ca8bf514bd30fcf3223fad9e880 Mon Sep 17 00:00:00 2001 From: Jan Votava Date: Mon, 13 Aug 2018 11:42:00 +0200 Subject: [PATCH] Fix isEmpty on nested objects So ArrayProxy inside ObjectProxy works with ES5 getters Fixes #16878 --- packages/ember-metal/lib/is_empty.ts | 10 ++++------ packages/ember-runtime/tests/core/is_empty_test.js | 14 +++++++++++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/ember-metal/lib/is_empty.ts b/packages/ember-metal/lib/is_empty.ts index acabf59cd08..d54954537a2 100644 --- a/packages/ember-metal/lib/is_empty.ts +++ b/packages/ember-metal/lib/is_empty.ts @@ -52,18 +52,16 @@ export default function isEmpty(obj: any): boolean { if (typeof size === 'number') { return !size; } - } - - if (typeof obj.length === 'number' && objectType !== 'function') { - return !obj.length; - } - if (objectType === 'object') { let length = get(obj, 'length'); if (typeof length === 'number') { return !length; } } + if (typeof obj.length === 'number' && objectType !== 'function') { + return !obj.length; + } + return false; } diff --git a/packages/ember-runtime/tests/core/is_empty_test.js b/packages/ember-runtime/tests/core/is_empty_test.js index 51af870b46f..4066b2ec94b 100644 --- a/packages/ember-runtime/tests/core/is_empty_test.js +++ b/packages/ember-runtime/tests/core/is_empty_test.js @@ -1,15 +1,27 @@ import { isEmpty } from 'ember-metal'; import ArrayProxy from '../../lib/system/array_proxy'; +import ObjectProxy from '../../lib/system/object_proxy'; import { A as emberA } from '../../lib/mixins/array'; import { moduleFor, AbstractTestCase } from 'internal-test-helpers'; moduleFor( 'Ember.isEmpty', class extends AbstractTestCase { - ['@test Ember.isEmpty'](assert) { + ['@test Ember.isEmpty ArrayProxy'](assert) { let arrayProxy = ArrayProxy.create({ content: emberA() }); assert.equal(true, isEmpty(arrayProxy), 'for an ArrayProxy that has empty content'); } + + ['@test Ember.isEmpty ObjectProxy ArrayProxy'](assert) { + let arrayProxy = ArrayProxy.create({ content: emberA([]) }); + let objectProxy = ObjectProxy.create({ content: arrayProxy }); + + assert.equal( + true, + isEmpty(objectProxy), + 'for an ArrayProxy inside ObjectProxy that has empty content' + ); + } } );