From dd6eb132a5b11692b4b3aede8eef69bb92e18bcd Mon Sep 17 00:00:00 2001 From: 1Aron Date: Wed, 22 Feb 2023 17:09:50 +0800 Subject: [PATCH] Improve(Log): Prevent logging items with empty value --- packages/log/jest.config.ts | 12 +----------- packages/log/package.json | 1 + packages/log/src/tree.ts | 31 ++++++++++++++++++++++++------- packages/log/tests/tree.test.ts | 6 ++++++ packages/log/tsconfig.json | 1 + packages/techor/src/index.ts | 2 +- 6 files changed, 34 insertions(+), 19 deletions(-) create mode 100644 packages/log/tests/tree.test.ts diff --git a/packages/log/jest.config.ts b/packages/log/jest.config.ts index 0e8494b..f5a2c1f 100644 --- a/packages/log/jest.config.ts +++ b/packages/log/jest.config.ts @@ -1,14 +1,4 @@ /** @type {import('jest').Config} */ export default { - transform: { - '^.+\\.(t|j)sx?$': '@swc/jest' - }, - globals: { - 'ts-jest': { - tsConfig: { - importHelpers: true - } - } - }, - transformIgnorePatterns: ['node_modules/(?!callsites)/'] + preset: 'aron-jest' } diff --git a/packages/log/package.json b/packages/log/package.json index 836b3ad..54913c2 100644 --- a/packages/log/package.json +++ b/packages/log/package.json @@ -4,6 +4,7 @@ "scripts": { "build": "aron pack --platform=node", "dev": "npm run build -- --watch", + "test": "jest", "type-check": "tsc --noEmit", "lint": "eslint src" }, diff --git a/packages/log/src/tree.ts b/packages/log/src/tree.ts index 022a420..656978a 100644 --- a/packages/log/src/tree.ts +++ b/packages/log/src/tree.ts @@ -2,14 +2,31 @@ import treeify from 'object-treeify' import { paint } from './paint' const tree = (object: JSON | object) => { - console.log( - treeify(parseObject(JSON.parse(JSON.stringify(object))), { - spacerNeighbour: paint('..│..') + ' ', - keyNoNeighbour: paint('..└─..') + ' ', - keyNeighbour: paint('..├─..') + ' ', - separator: paint('..:..') + ' ' + const message = treeify(parseObject(JSON.parse(JSON.stringify(removeEmpty(object)))), { + spacerNeighbour: paint('..│..') + ' ', + keyNoNeighbour: paint('..└─..') + ' ', + keyNeighbour: paint('..├─..') + ' ', + separator: paint('..:..') + ' ' + }) + console.log(message) + return message +} + +function removeEmpty(data) { + //transform properties into key-values pairs and filter all the empty-values + const entries = Object.entries(data).filter(([, value]) => value != null) + //map through all the remaining properties and check if the value is an object. + //if value is object, use recursion to remove empty properties + const clean = entries + .map(([key, v]) => { + const value = typeof v == 'object' ? removeEmpty(v) : v + return [key, value] + }) + .filter(([key, v]) => { + return typeof v === 'object' ? Object.keys(v).length : true }) - ) + //transform the key-value pairs back to an object. + return Object.fromEntries(clean) } function parseObject(object) { diff --git a/packages/log/tests/tree.test.ts b/packages/log/tests/tree.test.ts new file mode 100644 index 0000000..79f52e4 --- /dev/null +++ b/packages/log/tests/tree.test.ts @@ -0,0 +1,6 @@ +import log from '@techor/log' + +it('prevent outputting items with empty value', () => { + expect(log.tree({ a: 1, b: null, c: { c1: null, c2: 1 }, d: { d1: null } })) + .not.toContain('c1') +}) \ No newline at end of file diff --git a/packages/log/tsconfig.json b/packages/log/tsconfig.json index ea54126..c4b32fa 100644 --- a/packages/log/tsconfig.json +++ b/packages/log/tsconfig.json @@ -1,6 +1,7 @@ { "extends": "../../tsconfig.json", "compilerOptions": { + "baseUrl": ".", "outDir": "dist" }, "include": [ diff --git a/packages/techor/src/index.ts b/packages/techor/src/index.ts index 6a0a9aa..48293cc 100644 --- a/packages/techor/src/index.ts +++ b/packages/techor/src/index.ts @@ -1,5 +1,5 @@ import upath from 'upath' -import log, { chalk } from '@techor/log' +import log from '@techor/log' import defaultOptions, { Options, Options as TechorOptions } from './options' import fg from 'fast-glob' import crossImport from 'cross-import'