Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
🐛 Fix removeEmpty for primitive types
Browse files Browse the repository at this point in the history
  • Loading branch information
tbunata committed Jul 18, 2022
1 parent d2736ff commit 7f4db3b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import loggerFactory from './index';

const logger = loggerFactory({
enableFields: ['req.hello', 'req.password'],
config: {
redact: ['*.*.password'],
},
});

const tmp = {
body: 'aces high',
password: 'sensitive',
somethingTmp: {
password: 'tajne',
},
};

logger.info({ tmp: tmp, something: 666 });

logger.info({ req: { body: 42, method: 'POST' } });
logger.info({ req: { body: 0, method: 'POST' } });
logger.info({ req: { body: Symbol(), method: 'POST' } });
logger.info({ req: { body: 42.69, method: 'POST' } });
logger.info({ req: { body: false, method: 'POST' } });
logger.info({ req: { body: '', method: 'POST' } });
logger.info({ req: { body: null, method: 'POST' } });
logger.info({ req: { body: undefined, method: 'POST' } });
logger.info({ req: { body: {}, method: 'POST' } });
logger.info({ req: { body: { a: 'a' }, method: 'POST' } });
logger.info({ req: { body: [], method: 'POST' } });
logger.info({ req: { body: [1], method: 'POST' } });

import isEmptyObject = require('lodash.isempty');

console.log(isEmptyObject('A'));
console.log(isEmptyObject(''));
54 changes: 54 additions & 0 deletions src/tests/serializers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,57 @@ test('req body string serialization', () => {

logger.info({ req });
});

const nonEmptyValues = [42, 0, 42.69, false, 'hello', { a: 'b' }, [1]];
test.each(nonEmptyValues)('req body serialization - nonempty values: %s', (value) => {
const loggerWrites = jest.fn();
const req = {
method: 'GET',
url: 'www.example.com',
body: value,
};

const logger = loggerFactory({
streams: [
{
stream: new Writable({
write: (chunk, _encoding, next) => {
const json = JSON.parse(chunk);
expect(json.req.body).toEqual(value);
loggerWrites();
next();
},
}),
},
],
});

logger.info({ req });
});

const emptyValues = [null, undefined, {}, [], ''];
test.each(emptyValues)('req body serialization - empty values: %s', (value) => {
const loggerWrites = jest.fn();
const req = {
method: 'GET',
url: 'www.example.com',
body: value,
};

const logger = loggerFactory({
streams: [
{
stream: new Writable({
write: (chunk, _encoding, next) => {
const json = JSON.parse(chunk);
expect(json.req.body).toBe(undefined);
loggerWrites();
next();
},
}),
},
],
});

logger.info({ req });
});
11 changes: 10 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { Request } from 'express';
import { Dictionary } from 'lodash';
import isEmpty = require('lodash.isempty');
import isEmptyObject = require('lodash.isempty');
import omit = require('omit-deep');

const isPrimitive = (val: any) => val !== Object(val);

const isEmpty = (val: any) => {
if (isPrimitive(val)) {
return val === null || val === undefined || val === '';
}
return isEmptyObject(val);
};

const removeEmpty = (obj: Dictionary<any>): object =>
omit(
obj,
Expand Down

0 comments on commit 7f4db3b

Please sign in to comment.