Skip to content

Commit

Permalink
fix(transform): Transform data and primirives correctly, fixes #394
Browse files Browse the repository at this point in the history
  • Loading branch information
megahertz committed Jan 10, 2024
1 parent 0f9d340 commit 4c719e2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
15 changes: 10 additions & 5 deletions src/node/transforms/__specs__/object.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
});
});
Expand Down
4 changes: 4 additions & 0 deletions src/node/transforms/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
32 changes: 26 additions & 6 deletions src/renderer/lib/transports/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -39,6 +46,10 @@ function ipcTransportRendererFactory(logger) {
));
}

if (data instanceof Date) {
return data.toISOString();
}

if (data instanceof Error) {
return data.stack;
}
Expand Down Expand Up @@ -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;
}

0 comments on commit 4c719e2

Please sign in to comment.