From 64dd057552cc879882810af8053aee836efe9c0e Mon Sep 17 00:00:00 2001 From: harttle Date: Fri, 22 Mar 2019 21:16:25 +0800 Subject: [PATCH] refactor: Context#propertyAccessSeq => parseProp --- src/context/context.ts | 8 ++++---- test/unit/context/{scope.ts => context.ts} | 18 +++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) rename test/unit/context/{scope.ts => context.ts} (92%) diff --git a/src/context/context.ts b/src/context/context.ts index edd9562a0..8a72e5ceb 100644 --- a/src/context/context.ts +++ b/src/context/context.ts @@ -22,8 +22,8 @@ export default class Context { .reduce((ctx, val) => __assign(ctx, val), {}) } async get (path: string) { - const paths = await this.propertyAccessSeq(path) - let ctx = this.findContextFor(paths[0]) || this.environments + const paths = await this.parseProp(path) + let ctx = this.findScope(paths[0]) || this.environments for (const path of paths) { ctx = this.readProperty(ctx, path) if (_.isNil(ctx) && this.opts.strictVariables) { @@ -45,7 +45,7 @@ export default class Context { } return this.scopes.splice(i, 1)[0] } - findContextFor (key: string) { + findScope (key: string) { for (let i = this.scopes.length - 1; i >= 0; i--) { const candidate = this.scopes[i] if (key in candidate) { @@ -73,7 +73,7 @@ export default class Context { * accessSeq("foo['b]r']") // ['foo', 'b]r'] * accessSeq("foo[bar.coo]") // ['foo', 'bar'], for bar.coo == 'bar' */ - async propertyAccessSeq (str: string) { + async parseProp (str: string) { str = String(str) const seq: string[] = [] let name = '' diff --git a/test/unit/context/scope.ts b/test/unit/context/context.ts similarity index 92% rename from test/unit/context/scope.ts rename to test/unit/context/context.ts index 1b42a107c..af2a261b8 100644 --- a/test/unit/context/scope.ts +++ b/test/unit/context/context.ts @@ -22,39 +22,39 @@ describe('scope', function () { describe('#propertyAccessSeq()', function () { it('should handle dot syntax', async function () { - expect(await ctx.propertyAccessSeq('foo.bar')) + expect(await ctx.parseProp('foo.bar')) .to.deep.equal(['foo', 'bar']) }) it('should handle [] syntax', async function () { - expect(await ctx.propertyAccessSeq('foo["bar"]')) + expect(await ctx.parseProp('foo["bar"]')) .to.deep.equal(['foo', 'bar']) }) it('should handle [] syntax', async function () { - expect(await ctx.propertyAccessSeq('foo[foo]')) + expect(await ctx.parseProp('foo[foo]')) .to.deep.equal(['foo', 'zoo']) }) it('should handle nested access 1', async function () { - expect(await ctx.propertyAccessSeq('foo[bar.zoo]')) + expect(await ctx.parseProp('foo[bar.zoo]')) .to.deep.equal(['foo', 'coo']) }) it('should handle nested access 2', async function () { - expect(await ctx.propertyAccessSeq('foo[bar["zoo"]]')) + expect(await ctx.parseProp('foo[bar["zoo"]]')) .to.deep.equal(['foo', 'coo']) }) it('should handle nested access 3', async function () { - expect(await ctx.propertyAccessSeq('bar["foo"].zoo')) + expect(await ctx.parseProp('bar["foo"].zoo')) .to.deep.equal(['bar', 'foo', 'zoo']) }) it('should handle nested access 4', async function () { - expect(await ctx.propertyAccessSeq('foo[0].bar')) + expect(await ctx.parseProp('foo[0].bar')) .to.deep.equal(['foo', '0', 'bar']) }) it('should handle nested access 5', async function () { - expect(await ctx.propertyAccessSeq('foo[one].bar')) + expect(await ctx.parseProp('foo[one].bar')) .to.deep.equal(['foo', '1', 'bar']) }) it('should handle nested access 6', async function () { - expect(await ctx.propertyAccessSeq('foo[two].bar')) + expect(await ctx.parseProp('foo[two].bar')) .to.deep.equal(['foo', 'undefined', 'bar']) }) })