diff --git a/README.md b/README.md index 1b05516..3e8c8d5 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..2479d38 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,14 @@ +interface SpaceLogConfig { + columnKeys: Array + headings?: Array + spaceSize?: number +} + +interface SpaceLogDataItem { + [key: string]: any +} + +declare function spaceLog(config: SpaceLogConfig, data: Array): void + +export { spaceLog } +export default spaceLog diff --git a/package.json b/package.json index ed20af4..fbbd494 100644 --- a/package.json +++ b/package.json @@ -5,9 +5,10 @@ "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", @@ -15,7 +16,8 @@ } }, "files": [ - "lib" + "lib", + "index.d.ts" ], "scripts": { "build:cjs": "babel src -d lib --env-name cjs --out-file-extension .cjs", diff --git a/src/index.js b/src/index.js index 2515caf..852d332 100644 --- a/src/index.js +++ b/src/index.js @@ -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) @@ -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 diff --git a/tests/index.spec.js b/tests/index.spec.js index 92f15ee..30c2e7c 100644 --- a/tests/index.spec.js +++ b/tests/index.spec.js @@ -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'],