Skip to content

Commit

Permalink
Improve(Log): Prevent logging items with empty value
Browse files Browse the repository at this point in the history
  • Loading branch information
1aron committed Feb 22, 2023
1 parent 326e829 commit dd6eb13
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 19 deletions.
12 changes: 1 addition & 11 deletions packages/log/jest.config.ts
Original file line number Diff line number Diff line change
@@ -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'
}
1 change: 1 addition & 0 deletions packages/log/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"scripts": {
"build": "aron pack --platform=node",
"dev": "npm run build -- --watch",
"test": "jest",
"type-check": "tsc --noEmit",
"lint": "eslint src"
},
Expand Down
31 changes: 24 additions & 7 deletions packages/log/src/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions packages/log/tests/tree.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
1 change: 1 addition & 0 deletions packages/log/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"outDir": "dist"
},
"include": [
Expand Down
2 changes: 1 addition & 1 deletion packages/techor/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down

0 comments on commit dd6eb13

Please sign in to comment.