Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor using typescript, while supporting esm/cjs #24

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
coverage/
node_modules/
yarn.lock
.idea/
dist/
32 changes: 18 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,42 @@
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"files": [
"index.d.ts",
"index.js"
"src/index.ts"
],
"dependencies": {
"@types/repeat-string": "^1.0.0",
"repeat-string": "^1.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"@types/jest": "^26.0.23",
"c8": "^7.0.0",
"chalk": "^4.0.0",
"jest": "^26.6.3",
"nyc": "^15.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"rollup": "^2.47.0",
"rollup-plugin-typescript2": "^0.30.0",
"strip-ansi": "^6.0.0",
"tape": "^5.0.0",
"ts-jest": "^26.5.6",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.38.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test": "npm run build && npm run format && npm run test-coverage"
"dev": "rimraf dist && rollup -c -w",
"build": "rimraf dist && rollup -c",
"format": "remark . -qfo && prettier . -w --loglevel warn",
"test-api": "jest --all",
"test-coverage": "jest --all --collect-coverage",
"test": "npm run format && npm run test-coverage"
},
"jest": {
"preset": "ts-jest"
},
"remarkConfig": {
"plugins": [
Expand Down
18 changes: 18 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import typescript from 'rollup-plugin-typescript2'

export default {
output: [
{
file: './dist/index.js',
format: 'cjs',
sourcemap: true
},
{
file: './dist/index.esm.js',
format: 'esm',
sourcemap: true
}
],
input: './src/index.ts',
plugins: [typescript()]
}
307 changes: 307 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
import chalk from 'chalk'
import strip from 'strip-ansi'
import {markdownTable} from '../index'

describe('markdownTable()', () => {
it('should create a table', () => {
expect(
markdownTable([
['Branch', 'Commit'],
['main', '0123456789abcdef'],
['staging', 'fedcba9876543210']
])
).toBe(
[
'| Branch | Commit |',
'| ------- | ---------------- |',
'| main | 0123456789abcdef |',
'| staging | fedcba9876543210 |'
].join('\n')
)
})
it('should serialize values', () => {
expect(
markdownTable([
['Type', 'Value'],
['string', 'alpha'],
['number', 1],
['boolean', true],
['undefined', undefined],
['null', null],
['Array', [1, 2, 3]]
])
).toBe(
[
'| Type | Value |',
'| --------- | ----- |',
'| string | alpha |',
'| number | 1 |',
'| boolean | true |',
'| undefined | |',
'| null | |',
'| Array | 1,2,3 |'
].join('\n')
)
})

it('should work correctly when cells are missing', () => {
expect(
markdownTable(
[
['A', 'B', 'C'],
['a', 'b', 'c'],
['a', 'b'],
['a'],
[],
['a', 'b', ''],
['', 'b', 'c'],
['a', '', ''],
['', '', 'c'],
['', '', '']
],
{align: 'c'}
)
).toBe(
[
'| A | B | C |',
'| :-: | :-: | :-: |',
'| a | b | c |',
'| a | b | |',
'| a | | |',
'| | | |',
'| a | b | |',
'| | b | c |',
'| a | | |',
'| | | c |',
'| | | |'
].join('\n')
)
})
it('should align left and right', () => {
expect(
markdownTable(
[
['Beep', 'No.'],
['boop', '33450'],
['foo', '1006'],
['bar', '45']
],
{align: ['l', 'r']}
)
).toBe(
[
'| Beep | No. |',
'| :--- | ----: |',
'| boop | 33450 |',
'| foo | 1006 |',
'| bar | 45 |'
].join('\n')
)
})
it('should align center', () => {
expect(
markdownTable(
[
['Beep', 'No.', 'Boop'],
['beep', '1024', 'xyz'],
['boop', '3388450', 'tuv'],
['foo', '10106', 'qrstuv'],
['bar', '45', 'lmno']
],
{align: ['l', 'c', 'l']}
)
).toBe(
[
'| Beep | No. | Boop |',
'| :--- | :-----: | :----- |',
'| beep | 1024 | xyz |',
'| boop | 3388450 | tuv |',
'| foo | 10106 | qrstuv |',
'| bar | 45 | lmno |'
].join('\n')
)
})
it('should accept a single value', () => {
expect(
markdownTable(
[
['Very long', 'Even longer'],
['boop', '33450'],
['foo', '1006'],
['bar', '45']
],
{align: 'c'}
)
).toBe(
[
'| Very long | Even longer |',
'| :-------: | :---------: |',
'| boop | 33450 |',
'| foo | 1006 |',
'| bar | 45 |'
].join('\n')
)
})
it('should accept multi-character values', () => {
expect(
markdownTable(
[
['Beep', 'No.', 'Boop'],
['beep', '1024', 'xyz'],
['boop', '3388450', 'tuv'],
['foo', '10106', 'qrstuv'],
['bar', '45', 'lmno']
],
{align: ['left', 'center', 'right']}
)
).toBe(
[
'| Beep | No. | Boop |',
'| :--- | :-----: | -----: |',
'| beep | 1024 | xyz |',
'| boop | 3388450 | tuv |',
'| foo | 10106 | qrstuv |',
'| bar | 45 | lmno |'
].join('\n')
)
})
it('should create a table without padding', () => {
expect(
markdownTable(
[
['Branch', 'Commit'],
['main', '0123456789abcdef'],
['staging', 'fedcba9876543210']
],
{padding: false}
)
).toBe(
[
'|Branch |Commit |',
'|-------|----------------|',
'|main |0123456789abcdef|',
'|staging|fedcba9876543210|'
].join('\n')
)
})
it('should create a table without aligned delimiters', () => {
expect(
markdownTable(
[
['Branch', 'Commit', 'Beep', 'No.', 'Boop'],
['main', '0123456789abcdef', 'beep', '1024', 'xyz'],
['staging', 'fedcba9876543210', 'boop', '3388450', 'tuv']
],
{alignDelimiters: false, align: ['', 'l', 'c', 'r']}
)
).toBe(
[
'| Branch | Commit | Beep | No. | Boop |',
'| - | :- | :-: | -: | - |',
'| main | 0123456789abcdef | beep | 1024 | xyz |',
'| staging | fedcba9876543210 | boop | 3388450 | tuv |'
].join('\n')
)
})
it('handles short rules and missing elements for tables w/o aligned delimiters', () => {
expect(
markdownTable(
[
['A'],
['', '0123456789abcdef'],
['staging', 'fedcba9876543210'],
['develop']
],
{alignDelimiters: false}
)
).toBe(
[
'| A | |',
'| - | - |',
'| | 0123456789abcdef |',
'| staging | fedcba9876543210 |',
'| develop | |'
].join('\n')
)
})
it('should create rows without starting delimiter', () => {
expect(
markdownTable(
[
['Branch', 'Commit'],
['main', '0123456789abcdef'],
['staging', 'fedcba9876543210'],
['develop']
],
{delimiterStart: false}
)
).toBe(
[
'Branch | Commit |',
'------- | ---------------- |',
'main | 0123456789abcdef |',
'staging | fedcba9876543210 |',
'develop | |'
].join('\n')
)
})
it('should create rows without ending delimiter', () => {
expect(
markdownTable(
[
['Branch', 'Commit'],
['main', '0123456789abcdef'],
['staging', 'fedcba9876543210'],
['develop']
],
{delimiterEnd: false}
)
).toBe(
[
'| Branch | Commit',
'| ------- | ----------------',
'| main | 0123456789abcdef',
'| staging | fedcba9876543210',
'| develop |'
].join('\n')
)
})
it('should use `stringLength` to detect cell lengths', () => {
expect(
strip(
markdownTable(
[
['A', 'B', 'C'],
[chalk.red('Red'), chalk.green('Green'), chalk.blue('Blue')],
[chalk.bold('Bold'), chalk.underline(''), chalk.italic('Italic')],
[
chalk.inverse('Inverse'),
chalk.strikethrough('Strike'),
chalk.hidden('Hidden')
],
['bar', '45', 'lmno']
],
{align: ['', 'c', 'r'], stringLength}
)
)
).toBe(
[
'| A | B | C |',
'| ------- | :----: | -----: |',
'| Red | Green | Blue |',
'| Bold | | Italic |',
'| Inverse | Strike | Hidden |',
'| bar | 45 | lmno |'
].join('\n')
)
})
})

/**
* Get the length of a string, minus ANSI color characters.
* @param {string} value
* @returns {number}
*/
function stringLength(value) {
return strip(value).length
}
Loading