Skip to content

Commit

Permalink
Merge pull request #6 from vitaly-t/nested
Browse files Browse the repository at this point in the history
implements #5
  • Loading branch information
vitaly-t committed Dec 23, 2020
2 parents fe6776c + fc9fd53 commit 7389a01
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "path-value",
"version": "0.4.0",
"version": "0.4.1",
"description": "Property path-to-value resolver, in TypeScript",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
8 changes: 6 additions & 2 deletions src/resolve-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ export function resolvePath(this: any, target: any, path: string | string[]): IP
}
break;
}
const v = isThis ? target : target[name];
value = typeof v === 'function' && invoke ? v.call(target) : v;
value = isThis ? target : target[name];
if (typeof value === 'function' && invoke) {
do {
value = value.call(target);
} while (typeof value === 'function');
}
if (value === undefined || value === null) {
i++;
if (value === undefined && i === len) {
Expand Down
20 changes: 20 additions & 0 deletions test/resolve-path.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,26 @@ describe('for special function', () => {
});
});

describe('nested functions', () => {
it('must resolve recursively', () => {
const obj = {
scope: {
value: 123
},
first() {
return this.second;
},
second() {
return this.third.bind(this.scope);
},
third() {
return (this as any).value;
}
};
expect(resolve(obj, 'first')).to.eql({chain: ['first'], idx: 0, exists: true, value: 123});
});
});

describe('complex', () => {
it('must resolve everything', () => {
const obj = {
Expand Down

0 comments on commit 7389a01

Please sign in to comment.