Skip to content

Commit

Permalink
Merge pull request #15692 from bekzod/cleanup-getpath
Browse files Browse the repository at this point in the history
cleanup `_getPath`
  • Loading branch information
mmun committed Jun 8, 2018
2 parents 0220419 + 4d576b0 commit 720cb17
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
16 changes: 1 addition & 15 deletions packages/ember-metal/lib/property_get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ import { isPath } from './path_cache';
import { tagForProperty } from './tags';
import { getCurrentTracker } from './tracked';

const ALLOWABLE_TYPES = {
object: true,
function: true,
string: true,
};

export const PROXY_CONTENT = symbol('PROXY_CONTENT');

export let getPossibleMandatoryProxyValue: (obj: object, keyName: string) => any;
Expand Down Expand Up @@ -178,24 +172,16 @@ export function _getPath<T extends object>(root: T, path: string): any {
let parts = path.split('.');

for (let i = 0; i < parts.length; i++) {
if (!isGettable(obj)) {
if (obj === undefined || obj === null || (obj as MaybeHasIsDestroyed).isDestroyed) {
return undefined;
}

obj = get(obj, parts[i]);

if (obj && (obj as MaybeHasIsDestroyed).isDestroyed) {
return undefined;
}
}

return obj;
}

function isGettable(obj: any): boolean {
return obj !== undefined && obj !== null && ALLOWABLE_TYPES[typeof obj];
}

/**
Retrieves the value of a property from an Object, or a default value in the
case that the property returns `undefined`.
Expand Down
19 changes: 19 additions & 0 deletions packages/ember-metal/tests/accessors/get_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ENV } from 'ember-environment';
import { Object as EmberObject } from 'ember-runtime';
import { get, getWithDefault, Mixin, observer, computed } from '../..';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';
import { run } from '@ember/runloop';

function aget(x, y) {
return x[y];
Expand Down Expand Up @@ -98,6 +100,23 @@ moduleFor(
}
}

['@test get works with paths correctly'](assert) {
let func = function() {};
func.bar = 'awesome';

let destroyedObj = EmberObject.create({ bar: 'great' });
run(() => destroyedObj.destroy());

assert.equal(get({ foo: null }, 'foo.bar'), undefined);
assert.equal(get({ foo: { bar: 'hello' } }, 'foo.bar.length'), 5);
assert.equal(get({ foo: func }, 'foo.bar'), 'awesome');
assert.equal(get({ foo: func }, 'foo.bar.length'), 7);
assert.equal(get({}, 'foo.bar.length'), undefined);
assert.equal(get(function() {}, 'foo.bar.length'), undefined);
assert.equal(get('', 'foo.bar.length'), undefined);
assert.equal(get({ foo: destroyedObj }, 'foo.bar'), undefined);
}

['@test warn on attempts to call get with no arguments']() {
expectAssertion(function() {
get('aProperty');
Expand Down

0 comments on commit 720cb17

Please sign in to comment.