Skip to content

Commit

Permalink
Made config.headings optional to be able to log only the data 🛰 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
01taylop committed Nov 26, 2022
1 parent 46f34f6 commit 2a73dc7
Show file tree
Hide file tree
Showing 5 changed files with 1,287 additions and 964 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

Space Log is a simple function which logs tabular data, similar to that of `yarn outdated`.

You could - and probably should - just use `console.table` unless you appreciate a borderless display of data.

![Regions of Japan](https://github.com/01taylop/space-log/blob/main/assets/results.png?raw=true)

## How to use Space Log?

Space Log exposes a function, `spaceLog`, which can be imported as a named or default export. Both commonJS and ESM are supported.

The `spaceLog` function expects two arguments; config and data.
The `spaceLog` function has two arguments;

- `config.columnKeys`: An array of keys, i.e. the `data[key]` of each column.

- `config.headings`: An array of headings, i.e. the title of each column.
- `config.headings`: An optional array of headings, i.e. the title of each column. If no headings are provided, only the data will be output.

- `data`: An array of objects containing the data to log.
- A "theme" can be provided for a given key in a row by adding a property in the format `${key}Theme`. For example, to indicate that the most populous region of Japan is Kantō, `populationTheme` uses [chalk](https://www.npmjs.com/package/chalk) to render green text.
- A "theme" can be provided for a corresponding key by adding a property in the format `${key}Theme`. In the code below, `populationTheme` uses [chalk](https://www.npmjs.com/package/chalk) to render green text to indicate that the most populous region of Japan is Kantō.

```js
import chalk from chalk
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "space-log",
"description": "A simple utility for logging tabular data to the terminal.",
"version": "1.0.1",
"version": "1.1.0",
"type": "module",
"main": "./lib/index.cjs",
"exports": {
Expand All @@ -26,13 +26,13 @@
"chalk": "4.1.2"
},
"devDependencies": {
"@babel/cli": "7.17.10",
"@babel/core": "7.17.10",
"@babel/plugin-proposal-optional-chaining": "7.16.7",
"@babel/plugin-transform-modules-commonjs": "7.17.9",
"@babel/preset-env": "7.17.10",
"babel-jest": "28.1.0",
"jest": "28.1.0",
"@babel/cli": "7.19.3",
"@babel/core": "7.20.2",
"@babel/plugin-proposal-optional-chaining": "7.18.9",
"@babel/plugin-transform-modules-commonjs": "7.19.6",
"@babel/preset-env": "7.20.2",
"babel-jest": "29.3.1",
"jest": "29.3.1",
"rimraf": "3.0.2"
},
"author": "Patrick Taylor <hello@patricktaylor.dev>",
Expand Down
26 changes: 17 additions & 9 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,32 @@ const spaceLog = (config, data) => {
try {
const { columnKeys, headings } = config

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

// Intentional Spacing
console.log('')
if (hasHeadings) {
console.log('')
}

// Calculate Column Width
const columnWidths = {}

columnKeys.forEach((key, index) => {
const headingLength = headings[index]?.length || defaultHeading.length
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
})

// Log Headings
const headingLine = columnKeys.map((key, index) => {
const title = headings[index] || defaultHeading
const spacing = columnWidths[key] - title.length
return `${chalk.underline(title)}${' '.repeat(spacing)}`
}).join('')
console.log(headingLine.trim())
if (hasHeadings) {
const headingLine = columnKeys.map((key, index) => {
const title = headings[index] || defaultHeading
const spacing = columnWidths[key] - title.length
return `${chalk.underline(title)}${' '.repeat(spacing)}`
}).join('')
console.log(headingLine.trim())
}

// Log Data
data.forEach(item => {
Expand All @@ -41,7 +47,9 @@ const spaceLog = (config, data) => {
})

// Intentional Spacing
console.log('')
if (hasHeadings) {
console.log('')
}
} catch (error) {
console.error(error.message)
}
Expand Down
40 changes: 40 additions & 0 deletions tests/index.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,46 @@ describe('spaceLog', () => {
const mockedErrorLog = jest.spyOn(console, 'error').mockImplementation(text => text)
const mockedInfoLog = jest.spyOn(console, 'info').mockImplementation(text => text)

const testData = [{
capital: 'Brasília',
country: 'Brazil',
flag: '🇧🇷'
}, {
capital: 'Tokyo',
country: 'Japan',
flag: '🇯🇵'
}, {
capital: 'Seoul',
country: 'South Korea',
flag: '🇰🇷'
}]

it('should log a spaced table with headings', () => {
spaceLog({
columnKeys: ['country', 'capital', 'flag'],
headings: ['Country', 'Capital', 'Flag'],
}, testData)

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, '')
expect(mockedConsoleLog).toHaveBeenCalledTimes(6)
})

it('should log a spaced table without headings', () => {
spaceLog({
columnKeys: ['country', 'capital', 'flag'],
}, testData)

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

it('should log the data for the given config', () => {
const data = [{
bar: 'Bar1',
Expand Down
Loading

0 comments on commit 2a73dc7

Please sign in to comment.