Skip to content

Commit

Permalink
refactor: finalize StringifyObject API
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 26, 2020
1 parent 62a50a6 commit f904d79
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
5 changes: 3 additions & 2 deletions src/StringifiedObject/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
}

Expand Down Expand Up @@ -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)
})
}

Expand Down
55 changes: 31 additions & 24 deletions test/stringified-object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,43 +59,47 @@ 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)
assert.equal(props, '{ username: \'virk\' }')
})

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)
Expand All @@ -104,32 +108,35 @@ 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)
assert.equal(props, '{ title: \'Hello\' }')
})

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)
Expand Down

0 comments on commit f904d79

Please sign in to comment.