From f904d798929faa7bf63ebd290913c98e6be51d41 Mon Sep 17 00:00:00 2001 From: Harminder virk Date: Thu, 26 Mar 2020 21:31:20 +0530 Subject: [PATCH] refactor: finalize StringifyObject API --- src/StringifiedObject/index.ts | 5 +-- test/stringified-object.spec.ts | 55 +++++++++++++++++++-------------- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/StringifiedObject/index.ts b/src/StringifiedObject/index.ts index f5809c1..6a23ab0 100644 --- a/src/StringifiedObject/index.ts +++ b/src/StringifiedObject/index.ts @@ -25,7 +25,8 @@ export class StringifiedObject { * stringifiedObject.add('username', `'virk'`) * ``` */ - public add (key: any, value: any) { + public add (key: any, value: any, isComputed: boolean = false) { + key = isComputed ? `[${key}]` : key this.obj += this.obj.length ? `, ${key}: ${value}` : `${key}: ${value}` } @@ -72,7 +73,7 @@ export class StringifiedObject { arg.properties.forEach((prop: any) => { const key = parser.utils.stringify(prop.key) const value = parser.utils.stringify(prop.value) - objectifyString.add(key, value) + objectifyString.add(key, value, prop.computed) }) } diff --git a/test/stringified-object.spec.ts b/test/stringified-object.spec.ts index d8cd338..df223bc 100644 --- a/test/stringified-object.spec.ts +++ b/test/stringified-object.spec.ts @@ -59,10 +59,11 @@ test.group('StringifiedObject', () => { test.group('StringifiedObject | fromAcornAst', () => { test('stringify object expression', (assert) => { - const parser = new Parser({}, { filename: 'eval.edge' }) + const parser = new Parser({}) const expression = parser.utils.transformAst( - parser.utils.generateAST('({ username: \'virk\' })', LOC, parser.options.filename), - parser.options.filename, + parser.utils.generateAST('({ username: \'virk\' })', LOC, 'eval.edge'), + 'eval.edge', + parser.stack, ) const props = StringifiedObject.fromAcornExpressions([expression], parser) @@ -70,32 +71,35 @@ test.group('StringifiedObject | fromAcornAst', () => { }) test('parse props with shorthand obj', (assert) => { - const parser = new Parser({}, { filename: 'eval.edge' }) + const parser = new Parser({}) const expression = parser.utils.transformAst( - parser.utils.generateAST('({ username })', LOC, parser.options.filename), - parser.options.filename, + parser.utils.generateAST('({ username })', LOC, 'eval.edge'), + 'eval.edge', + parser.stack, ) const props = StringifiedObject.fromAcornExpressions([expression], parser) - assert.equal(props, '{ username: ctx.resolve(\'username\') }') + assert.equal(props, '{ username: state.username }') }) test('parse props with computed obj', (assert) => { - const parser = new Parser({}, { filename: 'eval.edge' }) + const parser = new Parser({}) const expression = parser.utils.transformAst( - parser.utils.generateAST('({ [username]: username })', LOC, parser.options.filename), - parser.options.filename, + parser.utils.generateAST('({ [username]: username })', LOC, 'eval.edge'), + 'eval.edge', + parser.stack, ) const props = StringifiedObject.fromAcornExpressions([expression], parser) - assert.equal(props, '{ ctx.resolve(\'username\'): ctx.resolve(\'username\') }') + assert.equal(props, '{ [state.username]: state.username }') }) test('parse props with multiple obj properties', (assert) => { - const parser = new Parser({}, { filename: 'eval.edge' }) + const parser = new Parser({}) const expression = parser.utils.transformAst( - parser.utils.generateAST('({ username: \'virk\', age: 22 })', LOC, parser.options.filename), - parser.options.filename, + parser.utils.generateAST('({ username: \'virk\', age: 22 })', LOC, 'eval.edge'), + 'eval.edge', + parser.stack, ) const props = StringifiedObject.fromAcornExpressions([expression], parser) @@ -104,21 +108,23 @@ test.group('StringifiedObject | fromAcornAst', () => { }) test('parse props with shorthand and full properties', (assert) => { - const parser = new Parser({}, { filename: 'eval.edge' }) + const parser = new Parser({}) const expression = parser.utils.transformAst( - parser.utils.generateAST('({ username, age: 22 })', LOC, parser.options.filename), - parser.options.filename, + parser.utils.generateAST('({ username, age: 22 })', LOC, 'eval.edge'), + 'eval.edge', + parser.stack, ) const props = StringifiedObject.fromAcornExpressions([expression], parser) - assert.equal(props, '{ username: ctx.resolve(\'username\'), age: 22 }') + assert.equal(props, '{ username: state.username, age: 22 }') }) test('parse props with assignment expression', (assert) => { - const parser = new Parser({}, { filename: 'eval.edge' }) + const parser = new Parser({}) const expression = parser.utils.transformAst( - parser.utils.generateAST('(title = \'Hello\')', LOC, parser.options.filename), - parser.options.filename, + parser.utils.generateAST('(title = \'Hello\')', LOC, 'eval.edge'), + 'eval.edge', + parser.stack, ) const props = StringifiedObject.fromAcornExpressions([expression], parser) @@ -126,10 +132,11 @@ test.group('StringifiedObject | fromAcornAst', () => { }) test('parse props with more than one assignment expression', (assert) => { - const parser = new Parser({}, { filename: 'eval.edge' }) + const parser = new Parser({}) const expression = parser.utils.transformAst( - parser.utils.generateAST('(title = \'Hello\', body = \'Some content\')', LOC, parser.options.filename), - parser.options.filename, + parser.utils.generateAST('(title = \'Hello\', body = \'Some content\')', LOC, 'eval.edge'), + 'eval.edge', + parser.stack, ) const props = StringifiedObject.fromAcornExpressions(expression.expressions, parser)