diff --git a/package.json b/package.json index e9e7ad8..e501caa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "class-logger", - "version": "1.0.0", + "version": "1.0.1", "description": "Boilerplate-free decorator-based class logging", "keywords": [ "decorator", diff --git a/src/formatter.service.spec.ts b/src/formatter.service.spec.ts index 7fe1059..c128d28 100644 --- a/src/formatter.service.spec.ts +++ b/src/formatter.service.spec.ts @@ -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() diff --git a/src/formatter.service.ts b/src/formatter.service.ts index 7334362..5c12cf0 100644 --- a/src/formatter.service.ts +++ b/src/formatter.service.ts @@ -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 '.' @@ -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) }