diff --git a/html_table_to_markdown.js b/html_table_to_markdown.js index 3393da4..7a71183 100644 --- a/html_table_to_markdown.js +++ b/html_table_to_markdown.js @@ -1,7 +1,9 @@ const htmlEntities = require('html-entities'); +const justify = require('justify-text'); module.exports = { + max_column_width: 20, clean(str) { str = str.replace(/<\/?[^>]+(>|$)/g, ""); str = str.replace(/(\r\n|\n|\r)/gm, ""); @@ -54,13 +56,20 @@ module.exports = { for (let c=0;ccolumn_widths[c]) { - column_widths[c]=l; + if (l > this.max_column_width) { + column_widths[c] = this.max_column_width; + } + else { + column_widths[c] = l; + } } } } + + // justify for (let r=0;r "text " + +ljust('text', 6, '0'); +// => "text00" + +rjust('text', 8); +// => " text" + +rjust('longtext', 7); +// => "longtext", i.e. unchanged because it is already more than 7 characters + +rjust('text', 9, '.'); +// => ".....text" + +rjust(936, 5); +// => " 936" + +ljust(780.25, 8); +// => "780.25 " +``` + +## License + +MIT diff --git a/node_modules/justify-text/index.d.ts b/node_modules/justify-text/index.d.ts new file mode 100644 index 0000000..47f2add --- /dev/null +++ b/node_modules/justify-text/index.d.ts @@ -0,0 +1,4 @@ +declare module "justify-text" { + export function ljust(input: string | number, width: number, padding?: string); + export function rjust(input: string | number, width: number, padding?: string); +} diff --git a/node_modules/justify-text/index.js b/node_modules/justify-text/index.js new file mode 100644 index 0000000..e168484 --- /dev/null +++ b/node_modules/justify-text/index.js @@ -0,0 +1,22 @@ +function padding(str, width, fillChar) { + const padBytes = width - str.length; + let padding = ''; + + for (let i = 0; i < padBytes; ++i) { + padding += fillChar; + } + + return padding; +} + +exports.ljust = function (str, width = 0, fillChar = ' ') { + str = String(str); + + return str + padding(str, width, fillChar); +}; + +exports.rjust = function (str, width = 0, fillChar = ' ') { + str = String(str); + + return padding(str, width, fillChar) + str; +}; diff --git a/node_modules/justify-text/package.json b/node_modules/justify-text/package.json new file mode 100644 index 0000000..a6e9148 --- /dev/null +++ b/node_modules/justify-text/package.json @@ -0,0 +1,17 @@ +{ + "name": "justify-text", + "version": "1.1.3", + "description": "left or right justify text", + "main": "index.js", + "types": "index.d.ts", + "scripts": { + "test": "jest", + "prepublishOnly": "npm test" + }, + "author": "Julian Nicholls", + "license": "MIT", + "repository": "https://github.com/JulianNicholls/npm-justify-text", + "devDependencies": { + "jest": "^26.4.0" + } +} diff --git a/node_modules/justify-text/test/index.test.js b/node_modules/justify-text/test/index.test.js new file mode 100644 index 0000000..fd0a7ba --- /dev/null +++ b/node_modules/justify-text/test/index.test.js @@ -0,0 +1,181 @@ +const { ljust, rjust } = require('../index'); + +describe('ljust with strings', () => { + test('empty string', () => { + const str = ljust('', 5); + + expect(str).toBe(' '); + }); + + test('empty string with no width specified', () => { + const str = ljust(''); + + expect(str).toBe(''); + }); + + test('space padding', () => { + const str = ljust('one', 5); + + expect(str).toBe('one '); + }); + + test('character padding', () => { + const str = ljust('two', 7, 'x'); + + expect(str).toBe('twoxxxx'); + }); + + test('No padding required', () => { + const str = ljust('three', 5); + + expect(str).toBe('three'); + }); + + test('No space for padding', () => { + const str = ljust('four', 3); + + expect(str).toBe('four'); + }); +}); + +describe('ljust with integers', () => { + test('space padding', () => { + const str = ljust(999, 5); + + expect(str).toBe('999 '); + }); + + test('character padding', () => { + const str = ljust(4867, 7, 'x'); + + expect(str).toBe('4867xxx'); + }); + + test('No padding required', () => { + const str = ljust(789, 3); + + expect(str).toBe('789'); + }); + + test('No space for padding', () => { + const str = ljust(7890, 3); + + expect(str).toBe('7890'); + }); +}); + +describe('ljust with non-integers', () => { + test('space padding', () => { + const str = ljust(999.8, 7); + + expect(str).toBe('999.8 '); + }); + + test('character padding', () => { + const str = ljust(4867.3, 7, 'x'); + + expect(str).toBe('4867.3x'); + }); + + test('No padding required', () => { + const str = ljust(789.5, 5); + + expect(str).toBe('789.5'); + }); + + test('No space for padding', () => { + const str = ljust(7890.123, 6); + + expect(str).toBe('7890.123'); + }); +}); + +describe('rjust with strings', () => { + test('empty string', () => { + const str = rjust('', 4); + + expect(str).toBe(' '); + }); + + test('empty string with no width specified', () => { + const str = rjust(''); + + expect(str).toBe(''); + }); + + test('space padding', () => { + const str = rjust('one', 5); + + expect(str).toBe(' one'); + }); + + test('character padding', () => { + const str = rjust('two', 7, 'x'); + + expect(str).toBe('xxxxtwo'); + }); + + test('No padding required', () => { + const str = rjust('three', 5); + + expect(str).toBe('three'); + }); + + test('No space for padding', () => { + const str = rjust('four', 3); + + expect(str).toBe('four'); + }); +}); + +describe('rjust with integers', () => { + test('space padding', () => { + const str = rjust(999, 5); + + expect(str).toBe(' 999'); + }); + + test('character padding', () => { + const str = rjust(4867, 7, 'x'); + + expect(str).toBe('xxx4867'); + }); + + test('No padding required', () => { + const str = rjust(7890, 3); + + expect(str).toBe('7890'); + }); + + test('No space for padding', () => { + const str = rjust(78901, 3); + + expect(str).toBe('78901'); + }); +}); + +describe('rjust with non-integers', () => { + test('space padding', () => { + const str = rjust(999.87, 7); + + expect(str).toBe(' 999.87'); + }); + + test('character padding', () => { + const str = rjust(486.3, 7, 'x'); + + expect(str).toBe('xx486.3'); + }); + + test('No padding required', () => { + const str = rjust(789.58, 6); + + expect(str).toBe('789.58'); + }); + + test('No space for padding', () => { + const str = rjust(7890.1234, 7); + + expect(str).toBe('7890.1234'); + }); +}); diff --git a/package-lock.json b/package-lock.json index fbb7642..fd26366 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "express-rate-limit": "^6.0.5", "html-entities": "^2.3.2", "jsdom": "^16.4.0", + "justify-text": "^1.1.3", "turndown": "^7.0.0", "url": "^0.11.0" } @@ -660,6 +661,11 @@ } } }, + "node_modules/justify-text": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/justify-text/-/justify-text-1.1.3.tgz", + "integrity": "sha512-iOZ295bkDRnbEx9a+g7zzTZQtBYS0KlsDYpVmCOc66af6ngaoJ4mXdo6ILbyW1HQYHEoIXh1wxJpoKCzzPMXSw==" + }, "node_modules/levn": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/levn/-/levn-0.3.0.tgz", @@ -1661,6 +1667,11 @@ "xml-name-validator": "^3.0.0" } }, + "justify-text": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/justify-text/-/justify-text-1.1.3.tgz", + "integrity": "sha512-iOZ295bkDRnbEx9a+g7zzTZQtBYS0KlsDYpVmCOc66af6ngaoJ4mXdo6ILbyW1HQYHEoIXh1wxJpoKCzzPMXSw==" + }, "levn": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/levn/-/levn-0.3.0.tgz", diff --git a/package.json b/package.json index b34f214..5082d47 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "express-rate-limit": "^6.0.5", "html-entities": "^2.3.2", "jsdom": "^16.4.0", + "justify-text": "^1.1.3", "turndown": "^7.0.0", "url": "^0.11.0" },