Skip to content

Commit

Permalink
Fix isEmpty on nested objects
Browse files Browse the repository at this point in the history
So ArrayProxy inside ObjectProxy works with ES5 getters
Fixes emberjs#16878
  • Loading branch information
janvotava committed Aug 13, 2018
1 parent 47aa3e4 commit 053db77
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
10 changes: 4 additions & 6 deletions packages/ember-metal/lib/is_empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
14 changes: 13 additions & 1 deletion packages/ember-runtime/tests/core/is_empty_test.js
Original file line number Diff line number Diff line change
@@ -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'
);
}
}
);

0 comments on commit 053db77

Please sign in to comment.