From 4c719e29ee15b545a1f6485368ad3d13cffdd8a9 Mon Sep 17 00:00:00 2001 From: Alexey Prokhorov Date: Wed, 10 Jan 2024 17:33:08 +0500 Subject: [PATCH] fix(transform): Transform data and primirives correctly, fixes #394 --- package.json | 2 +- src/node/transforms/__specs__/object.spec.js | 15 ++++++--- src/node/transforms/object.js | 4 +++ src/renderer/lib/transports/ipc.js | 32 ++++++++++++++++---- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 68f21ef..db29b47 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "electron-log", - "version": "5.1.0-beta.1", + "version": "5.1.0-beta.2", "description": "Just a simple logging module for your Electron application", "main": "src/index.js", "browser": "src/renderer/index.js", diff --git a/src/node/transforms/__specs__/object.spec.js b/src/node/transforms/__specs__/object.spec.js index a4255e0..55580ce 100644 --- a/src/node/transforms/__specs__/object.spec.js +++ b/src/node/transforms/__specs__/object.spec.js @@ -18,24 +18,29 @@ describe('transform/object', () => { describe('serialize', () => { it('should serialize object', () => { - expect(serialize(null, { a: 1 })).toEqual({ a: 1 }); + expect(serialize('', { a: 1 })).toEqual({ a: 1 }); }); it('should serialize errors', () => { - expect(serialize(null, new Error('test'))).toMatch('Error: test\n'); + expect(serialize('', new Error('test'))).toMatch('Error: test\n'); }); it('should serialize functions', () => { - expect(serialize(null, () => 1)).toEqual('[function] () => 1'); + expect(serialize('', () => 1)).toEqual('[function] () => 1'); + }); + + it('should serialize Date', () => { + expect(serialize('', new Date('2000-01-01T00:00:00.000Z'))) + .toEqual('2000-01-01T00:00:00.000Z'); }); it('should serialize set', () => { - expect(serialize(null, new Set([1]))).toEqual([1]); + expect(serialize('', new Set([1]))).toEqual([1]); }); it('should serialize map', () => { if (Object.fromEntries) { - expect(serialize(null, new Map([['a', 1]]))).toEqual({ a: 1 }); + expect(serialize('', new Map([['a', 1]]))).toEqual({ a: 1 }); } }); }); diff --git a/src/node/transforms/object.js b/src/node/transforms/object.js index 3b97493..b9cd4d1 100644 --- a/src/node/transforms/object.js +++ b/src/node/transforms/object.js @@ -121,6 +121,10 @@ function serialize(key, value, options = {}) { return `[function] ${value.toString()}`; } + if (value instanceof Date) { + return value.toISOString(); + } + if (serializeMapAndSet && value instanceof Map && Object.fromEntries) { return Object.fromEntries(value); } diff --git a/src/renderer/lib/transports/ipc.js b/src/renderer/lib/transports/ipc.js index 4a32e9d..819e12d 100644 --- a/src/renderer/lib/transports/ipc.js +++ b/src/renderer/lib/transports/ipc.js @@ -9,20 +9,27 @@ function ipcTransportRendererFactory(logger) { depth: 5, serializeFn(data, { depth = 5, seen = new WeakSet() } = {}) { - if (depth < 1) { - return `[${typeof data}]`; + if (seen.has(data)) { + return '[Circular]'; } - if (seen.has(data)) { - return data; + if (depth < 1) { + if (isPrimitive(data)) { + return data; + } + + if (Array.isArray(data)) { + return '[Array]'; + } + + return `[${typeof data}]`; } if (['function', 'symbol'].includes(typeof data)) { return data.toString(); } - // Primitive types (including null and undefined) - if (Object(data) !== data) { + if (isPrimitive(data)) { return data; } @@ -39,6 +46,10 @@ function ipcTransportRendererFactory(logger) { )); } + if (data instanceof Date) { + return data.toISOString(); + } + if (data instanceof Error) { return data.stack; } @@ -99,3 +110,12 @@ function ipcTransportRendererFactory(logger) { } } } + +/** + * Is type primitive, including null and undefined + * @param {any} value + * @returns {boolean} + */ +function isPrimitive(value) { + return Object(value) !== value; +}