Skip to content

Commit

Permalink
Add formatting for array results
Browse files Browse the repository at this point in the history
  • Loading branch information
keenondrums committed Apr 7, 2019
1 parent d8a2d34 commit 958a5bf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "class-logger",
"version": "1.0.0",
"version": "1.0.1",
"description": "Boilerplate-free decorator-based class logging",
"keywords": [
"decorator",
Expand Down
13 changes: 13 additions & 0 deletions src/formatter.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ describe(ClassLoggerFormatterService.name, () => {
})
expect(resStr).toBe(`. Res: ${stringify(resultObj)}`)
})
test('returns stringified array result', () => {
const resultArr = [
{
test: 42,
},
34,
]
const resStr = (classLoggerFormatterService as any).result({
...dataEnd,
result: resultArr,
})
expect(resStr).toBe(`. Res: [${stringify(resultArr[0])}, ${resultArr[1]}]`)
})
test('returns a serialized error result', () => {
class TestError extends Error {}
const result = new TestError()
Expand Down
13 changes: 7 additions & 6 deletions src/formatter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ export class ClassLoggerFormatterService implements IClassLoggerFormatter {
return error ? ' -> error' : ' -> done'
}
protected args({ args }: IClassLoggerFormatterStartData) {
return `. Args: [${this.argsToString(args)}]`
return `. Args: ${this.valueToString(args)}`
}
protected classInstance({ classInstance }: IClassLoggerFormatterStartData) {
return `. Class instance: ${this.classInstanceToString(classInstance)}`
}
protected result({ result }: IClassLoggerFormatterEndData) {
return `. Res: ${this.resultToString(result)}`
return `. Res: ${this.valueToString(result)}`
}
protected final() {
return '.'
Expand All @@ -91,16 +91,17 @@ export class ClassLoggerFormatterService implements IClassLoggerFormatter {
}
return stringify(classInsanceFiltered)
}
protected argsToString(args: any[]) {
return args.map((arg) => (typeof arg === 'object' ? stringify(arg) : arg.toString())).join(', ')
}
protected resultToString(res: any) {
protected valueToString(res: any): string {
if (typeof res !== 'object') {
return res.toString()
}
if (res instanceof Error) {
res = this.errorFormat(res)
}
if (Array.isArray(res)) {
const arrayWithStringifiedElements = res.map(this.valueToString)
return `[${arrayWithStringifiedElements.join(', ')}]`
}
return stringify(res)
}

Expand Down

0 comments on commit 958a5bf

Please sign in to comment.