Skip to content

Commit

Permalink
Added spaceSize option to increase spacing between columns 🪐 (#10)
Browse files Browse the repository at this point in the history
* Added `spaceSize` option to increase the space between cells 🪐

* Added a TypeScript definition 👽

* Added documentation for spaceSize 🌌

* Rename type definition file to index 📇

* Prepare release 🚀
  • Loading branch information
01taylop authored Jul 16, 2024
1 parent ba627b5 commit 73eeec2
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ The `spaceLog` function has two required arguments; `config` and `data`.

- `headings`: An optional array of headings to use as the title of each column. If no headings are provided, only the data will be included in the output.

- `spaceSize`: Specifies the spacing between columns. Default value is `1`.

#### Data (array)

An array of objects containing the data to log.
Expand Down
14 changes: 14 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
interface SpaceLogConfig {
columnKeys: Array<string>
headings?: Array<string>
spaceSize?: number
}

interface SpaceLogDataItem {
[key: string]: any
}

declare function spaceLog(config: SpaceLogConfig, data: Array<SpaceLogDataItem>): void

export { spaceLog }
export default spaceLog
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
"type": "git",
"url": "git+https://github.com/01taylop/space-log.git"
},
"version": "1.1.1",
"version": "1.2.0",
"type": "module",
"main": "./lib/index.js",
"types": "./index.d.ts",
"exports": {
".": {
"import": "./lib/index.js",
"require": "./lib/index.cjs"
}
},
"files": [
"lib"
"lib",
"index.d.ts"
],
"scripts": {
"build:cjs": "babel src -d lib --env-name cjs --out-file-extension .cjs",
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const defaultHeading = 'Unknown'

const spaceLog = (config, data) => {
try {
const { columnKeys, headings } = config
const { columnKeys, headings, spaceSize = 1 } = config

const hasHeadings = !!(headings && headings.length)

Expand All @@ -20,7 +20,7 @@ const spaceLog = (config, data) => {
const headingLength = hasHeadings ? (headings[index]?.length || defaultHeading.length) : 0
const dataLengths = data.map(item => item[key]?.length || 0)

columnWidths[key] = Math.max(headingLength, ...dataLengths) + 1
columnWidths[key] = Math.max(headingLength, ...dataLengths) + spaceSize
})

// Log Headings
Expand Down
16 changes: 16 additions & 0 deletions tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ describe('spaceLog', () => {
expect(mockedConsoleLog).toHaveBeenNthCalledWith(6, '')
})

it('logs a table with headings and extra space', () => {
spaceLog({
columnKeys: ['country', 'capital', 'flag'],
headings: ['Country', 'Capital', 'Flag'],
spaceSize: 2,
}, testData)

expect(mockedConsoleLog).toHaveBeenCalledTimes(6)
expect(mockedConsoleLog).toHaveBeenNthCalledWith(1, '')
expect(mockedConsoleLog).toHaveBeenNthCalledWith(2, '_Country_ _Capital_ _Flag_')
expect(mockedConsoleLog).toHaveBeenNthCalledWith(3, 'Brazil Brasília 🇧🇷')
expect(mockedConsoleLog).toHaveBeenNthCalledWith(4, 'Japan Tokyo 🇯🇵')
expect(mockedConsoleLog).toHaveBeenNthCalledWith(5, 'South Korea Seoul 🇰🇷')
expect(mockedConsoleLog).toHaveBeenNthCalledWith(6, '')
})

it('logs a table with a missing heading', () => {
spaceLog({
columnKeys: ['country', 'capital', 'flag'],
Expand Down

0 comments on commit 73eeec2

Please sign in to comment.