table 3.8.2 → 4.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/README.md +35 -16
  2. package/dist/alignString.js +19 -842
  3. package/dist/alignTableData.js +4 -226
  4. package/dist/calculateCellHeight.js +6 -343
  5. package/dist/calculateCellWidthIndex.js +2 -112
  6. package/dist/calculateMaximumColumnWidthIndex.js +5 -299
  7. package/dist/calculateRowHeightIndex.js +8 -333
  8. package/dist/createStream.js +19 -814
  9. package/dist/drawBorder.js +11 -292
  10. package/dist/drawRow.js +1 -69
  11. package/dist/drawTable.js +7 -474
  12. package/dist/getBorderCharacters.js +1 -315
  13. package/dist/index.js +4 -28
  14. package/dist/makeConfig.js +15 -544
  15. package/dist/makeStreamConfig.js +13 -587
  16. package/dist/mapDataUsingRowHeightIndex.js +9 -349
  17. package/dist/padTableData.js +5 -166
  18. package/dist/schemas/config.json +2 -2
  19. package/dist/schemas/streamConfig.json +2 -2
  20. package/dist/stringifyTableData.js +4 -116
  21. package/dist/table.js +5 -203
  22. package/dist/truncateTableData.js +3 -147
  23. package/dist/validateConfig.js +20 -23
  24. package/dist/validateStreamConfig.js +17 -19
  25. package/dist/validateTableData.js +11 -528
  26. package/dist/wrapString.js +3 -135
  27. package/dist/wrapWord.js +4 -257
  28. package/package.json +27 -24
  29. package/test/README/usage/basic.js +0 -25
  30. package/test/README/usage/cell_content_alignment.js +0 -45
  31. package/test/README/usage/column_width.js +0 -33
  32. package/test/README/usage/custom_border.js +0 -50
  33. package/test/README/usage/draw_horizontal_line.js +0 -41
  34. package/test/README/usage/expectTable.js +0 -13
  35. package/test/README/usage/moon_mission.js +0 -67
  36. package/test/README/usage/padding_cell_content.js +0 -39
  37. package/test/README/usage/predefined_border_templates.js +0 -91
  38. package/test/README/usage/streaming.js +0 -56
  39. package/test/README/usage/text_truncating.js +0 -32
  40. package/test/README/usage/text_wrapping.js +0 -67
  41. package/test/alignString.js +0 -102
  42. package/test/calculateCellHeight.js +0 -44
  43. package/test/calculateCellWidthIndex.js +0 -20
  44. package/test/calculateMaximumColumnWidthIndex.js +0 -59
  45. package/test/calculateRowHeightIndex.js +0 -78
  46. package/test/config.js +0 -43
  47. package/test/configSamples.js +0 -153
  48. package/test/createStream.js +0 -41
  49. package/test/drawBorder.js +0 -71
  50. package/test/makeConfig.js +0 -75
  51. package/test/mapDataUsingRowHeightIndex.js +0 -119
  52. package/test/streamConfig.js +0 -43
  53. package/test/streamConfigSamples.js +0 -151
  54. package/test/validateTableData.js +0 -65
  55. package/test/wrapString.js +0 -45
  56. package/test/wrapWord.js +0 -32
@@ -1,91 +0,0 @@
1
- import _ from 'lodash';
2
- import table, {
3
- getBorderCharacters
4
- } from './../../../src';
5
- import expectTable from './expectTable';
6
-
7
- describe('README.md usage/predefined_border_templates', () => {
8
- let data;
9
-
10
- before(() => {
11
- data = [
12
- ['0A', '0B', '0C'],
13
- ['1A', '1B', '1C'],
14
- ['2A', '2B', '2C']
15
- ];
16
- });
17
-
18
- it('honeywell', () => {
19
- const output = table(data, {
20
- border: getBorderCharacters('honeywell')
21
- });
22
-
23
- // eslint-disable-next-line no-restricted-syntax
24
- expectTable(output, `
25
- ╔════╤════╤════╗
26
- ║ 0A │ 0B │ 0C ║
27
- ╟────┼────┼────╢
28
- ║ 1A │ 1B │ 1C ║
29
- ╟────┼────┼────╢
30
- ║ 2A │ 2B │ 2C ║
31
- ╚════╧════╧════╝
32
- `);
33
- });
34
-
35
- it('norc', () => {
36
- const output = table(data, {
37
- border: getBorderCharacters('norc')
38
- });
39
-
40
- // eslint-disable-next-line no-restricted-syntax
41
- expectTable(output, `
42
- ┌────┬────┬────┐
43
- │ 0A │ 0B │ 0C │
44
- ├────┼────┼────┤
45
- │ 1A │ 1B │ 1C │
46
- ├────┼────┼────┤
47
- │ 2A │ 2B │ 2C │
48
- └────┴────┴────┘
49
- `);
50
- });
51
-
52
- it('ramac', () => {
53
- const output = table(data, {
54
- border: getBorderCharacters('ramac')
55
- });
56
-
57
- // eslint-disable-next-line no-restricted-syntax
58
- expectTable(output, `
59
- +----+----+----+
60
- | 0A | 0B | 0C |
61
- |----|----|----|
62
- | 1A | 1B | 1C |
63
- |----|----|----|
64
- | 2A | 2B | 2C |
65
- +----+----+----+
66
- `);
67
- });
68
-
69
- it('void', () => {
70
- const output = table(data, {
71
- border: getBorderCharacters('void')
72
- });
73
-
74
- expectTable(_.trim(output) + '\n', '0A 0B 0C \n\n 1A 1B 1C \n\n 2A 2B 2C');
75
- });
76
-
77
- it('borderless', () => {
78
- const output = table(data, {
79
- border: getBorderCharacters('void'),
80
- columnDefault: {
81
- paddingLeft: 0,
82
- paddingRight: 1
83
- },
84
- drawHorizontalLine: () => {
85
- return false;
86
- }
87
- });
88
-
89
- expectTable(_.trim(output) + '\n', '0A 0B 0C \n1A 1B 1C \n2A 2B 2C');
90
- });
91
- });
@@ -1,56 +0,0 @@
1
- import {
2
- createStream
3
- } from './../../../src';
4
- import expectTable from './expectTable';
5
-
6
- describe('README.md usage/', () => {
7
- describe('process.stdout.write', () => {
8
- let processStdoutWriteBuffer;
9
-
10
- /**
11
- * @var {Function} Reference to the original process.stdout.write function.
12
- */
13
- const processStdoutWrite = process.stdout.write;
14
-
15
- /**
16
- * @returns {undefined}
17
- */
18
- const overwriteProcessStdoutWrite = () => {
19
- processStdoutWriteBuffer = '';
20
-
21
- process.stdout.write = (text) => {
22
- processStdoutWriteBuffer += text;
23
- };
24
- };
25
-
26
- /**
27
- * @returns {string}
28
- */
29
- const resetProcessStdoudWrite = () => {
30
- process.stdout.write = processStdoutWrite;
31
-
32
- return processStdoutWriteBuffer;
33
- };
34
-
35
- it('streaming', () => {
36
- const config = {
37
- columnCount: 3,
38
- columnDefault: {
39
- width: 2
40
- }
41
- };
42
-
43
- const stream = createStream(config);
44
-
45
- overwriteProcessStdoutWrite();
46
-
47
- stream.write(['0A', '0B', '0C']);
48
- stream.write(['1A', '1B', '1C']);
49
- stream.write(['2A', '2B', '2C']);
50
-
51
- const output = resetProcessStdoudWrite();
52
-
53
- expectTable(output + '\n', '╔════╤════╤════╗\n║ 0A │ 0B │ 0C ║\n╚════╧════╧════╝\r\u001b[K╟────┼────┼────╢\n║ 1A │ 1B │ 1C ║\n╚════╧════╧════╝\r\u001b[K╟────┼────┼────╢\n║ 2A │ 2B │ 2C ║\n╚════╧════╧════╝');
54
- });
55
- });
56
- });
@@ -1,32 +0,0 @@
1
- import table from './../../../src';
2
- import expectTable from './expectTable';
3
-
4
- describe('README.md usage/', () => {
5
- it('text_truncating', () => {
6
- const data = [
7
- ['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pulvinar nibh sed mauris convallis dapibus. Nunc venenatis tempus nulla sit amet viverra.']
8
- ];
9
-
10
- const config = {
11
- columns: {
12
- 0: {
13
- truncate: 100,
14
- width: 20
15
- }
16
- }
17
- };
18
-
19
- const output = table(data, config);
20
-
21
- // eslint-disable-next-line no-restricted-syntax
22
- expectTable(output, `
23
- ╔══════════════════════╗
24
- ║ Lorem ipsum dolor si ║
25
- ║ t amet, consectetur ║
26
- ║ adipiscing elit. Pha ║
27
- ║ sellus pulvinar nibh ║
28
- ║ sed mauris conva... ║
29
- ╚══════════════════════╝
30
- `);
31
- });
32
- });
@@ -1,67 +0,0 @@
1
- import table from './../../../src';
2
- import expectTable from './expectTable';
3
-
4
- describe('README.md usage/', () => {
5
- it('text_wrapping (no wrap word)', () => {
6
- const data = [
7
- ['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pulvinar nibh sed mauris convallis dapibus. Nunc venenatis tempus nulla sit amet viverra.']
8
- ];
9
-
10
- const config = {
11
- columns: {
12
- 0: {
13
- width: 20
14
- }
15
- }
16
- };
17
-
18
- const output = table(data, config);
19
-
20
- // eslint-disable-next-line no-restricted-syntax
21
- expectTable(output, `
22
- ╔══════════════════════╗
23
- ║ Lorem ipsum dolor si ║
24
- ║ t amet, consectetur ║
25
- ║ adipiscing elit. Pha ║
26
- ║ sellus pulvinar nibh ║
27
- ║ sed mauris convallis ║
28
- ║ dapibus. Nunc venena ║
29
- ║ tis tempus nulla sit ║
30
- ║ amet viverra. ║
31
- ╚══════════════════════╝
32
- `);
33
- });
34
-
35
- it('text_wrapping (wrap word)', () => {
36
- const data = [
37
- ['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pulvinar nibh sed mauris convallis dapibus. Nunc venenatis tempus nulla sit amet viverra.']
38
- ];
39
-
40
- const config = {
41
- columns: {
42
- 0: {
43
- width: 20,
44
- wrapWord: true
45
- }
46
- }
47
- };
48
-
49
- const output = table(data, config);
50
-
51
- // eslint-disable-next-line no-restricted-syntax
52
- expectTable(output, `
53
- ╔══════════════════════╗
54
- ║ Lorem ipsum dolor ║
55
- ║ sit amet, ║
56
- ║ consectetur ║
57
- ║ adipiscing elit. ║
58
- ║ Phasellus pulvinar ║
59
- ║ nibh sed mauris ║
60
- ║ convallis dapibus. ║
61
- ║ Nunc venenatis ║
62
- ║ tempus nulla sit ║
63
- ║ amet viverra. ║
64
- ╚══════════════════════╝
65
- `);
66
- });
67
- });
@@ -1,102 +0,0 @@
1
- /* eslint-disable max-nested-callbacks */
2
-
3
- import {
4
- expect
5
- } from 'chai';
6
- import chalk from 'chalk';
7
- import alignString from './../src/alignString';
8
-
9
- describe('alignString', () => {
10
- context('subject parameter value is not a string', () => {
11
- it('throws an error', () => {
12
- expect(() => {
13
- alignString();
14
- }).to.throw(Error, 'Subject parameter value must be a string.');
15
- });
16
- });
17
- context('container width parameter value is not a string', () => {
18
- it('throws an error', () => {
19
- expect(() => {
20
- alignString('');
21
- }).to.throw(Error, 'Container width parameter value must be a number.');
22
- });
23
- });
24
- context('subject parameter value width is greater than the container width', () => {
25
- it('throws an error', () => {
26
- expect(() => {
27
- alignString('aa', 1, 'left');
28
- }).to.throw(Error, 'Subject parameter value width cannot be greater than the container width.');
29
- });
30
- });
31
- context('container alignment parameter value is not a string', () => {
32
- it('throws an error', () => {
33
- expect(() => {
34
- alignString('', 1);
35
- }).to.throw(Error, 'Alignment parameter value must be a string.');
36
- });
37
- });
38
- context('container alignment parameter value is not a known alignment parameter value', () => {
39
- it('throws an error', () => {
40
- expect(() => {
41
- alignString('', 1, 'foo');
42
- }).to.throw(Error, 'Alignment parameter value must be a known alignment parameter value (left, right, center).');
43
- });
44
- });
45
- context('subject parameter value', () => {
46
- context('0 width', () => {
47
- it('produces a string consisting of container width number of whitespace characters', () => {
48
- expect(alignString('', 5, 'left')).to.equal(' ', 'left');
49
- expect(alignString('', 5, 'center')).to.equal(' ', 'center');
50
- expect(alignString('', 5, 'right')).to.equal(' ', 'right');
51
- });
52
- });
53
- context('plain text', () => {
54
- context('alignment', () => {
55
- context('left', () => {
56
- it('pads the string on the right side using a whitespace character', () => {
57
- expect(alignString('aa', 6, 'left')).to.equal('aa ');
58
- });
59
- });
60
- context('right', () => {
61
- it('pads the string on the left side using a whitespace character', () => {
62
- expect(alignString('aa', 6, 'right')).to.equal(' aa');
63
- });
64
- });
65
- context('center', () => {
66
- it('pads the string on both sides using a whitespace character', () => {
67
- expect(alignString('aa', 6, 'center')).to.equal(' aa ');
68
- });
69
- context('uneven number of available with', () => {
70
- it('floors the available width; adds extra space to the end of the string', () => {
71
- expect(alignString('aa', 7, 'center')).to.equal(' aa ');
72
- });
73
- });
74
- });
75
- });
76
- });
77
- context('text containing ANSI escape codes', () => {
78
- context('alignment', () => {
79
- context('left', () => {
80
- it('pads the string on the right side using a whitespace character', () => {
81
- expect(alignString(chalk.red('aa'), 6, 'left')).to.equal(chalk.red('aa') + ' ');
82
- });
83
- });
84
- context('right', () => {
85
- it('pads the string on the left side using a whitespace character', () => {
86
- expect(alignString(chalk.red('aa'), 6, 'right')).to.equal(' ' + chalk.red('aa'));
87
- });
88
- });
89
- context('center', () => {
90
- it('pads the string on both sides using a whitespace character', () => {
91
- expect(alignString(chalk.red('aa'), 6, 'center')).to.equal(' ' + chalk.red('aa') + ' ');
92
- });
93
- context('uneven number of available with', () => {
94
- it('floors the available width; adds extra space to the end of the string', () => {
95
- expect(alignString(chalk.red('aa'), 7, 'center')).to.equal(' ' + chalk.red('aa') + ' ');
96
- });
97
- });
98
- });
99
- });
100
- });
101
- });
102
- });
@@ -1,44 +0,0 @@
1
- /* eslint-disable max-nested-callbacks */
2
-
3
- import {
4
- expect
5
- } from 'chai';
6
- import calculateCellHeight from './../src/calculateCellHeight';
7
-
8
- describe('calculateCellHeight', () => {
9
- describe('value', () => {
10
- context('is not a string', () => {
11
- it('throws an error', () => {
12
- expect(() => {
13
- calculateCellHeight(null);
14
- }).to.throw(Error, 'Value must be a string.');
15
- });
16
- });
17
- });
18
- describe('context width', () => {
19
- context('is not an integer', () => {
20
- it('throws an error', () => {
21
- expect(() => {
22
- calculateCellHeight('foo', null);
23
- }).to.throw(Error, 'Column width must be an integer.');
24
- });
25
- });
26
- context('is 0', () => {
27
- it('throws an error', () => {
28
- expect(() => {
29
- calculateCellHeight('foo', 0);
30
- }).to.throw(Error, 'Column width must be greater than 0.');
31
- });
32
- });
33
- context('is lesser than the column width', () => {
34
- it('has height 1', () => {
35
- expect(calculateCellHeight('foo', 10)).to.equal(1);
36
- });
37
- });
38
- context('is 2 and half times greater than the column width', () => {
39
- it('has height 3', () => {
40
- expect(calculateCellHeight('aabbc', 2)).to.equal(3);
41
- });
42
- });
43
- });
44
- });
@@ -1,20 +0,0 @@
1
- import {
2
- expect
3
- } from 'chai';
4
- import calculateCellWidthIndex from './../src/calculateCellWidthIndex';
5
-
6
- describe('calculateCellWidthIndex', () => {
7
- context('all cells have different width', () => {
8
- it('describes each cell contents width', () => {
9
- const cellWidthIndex = calculateCellWidthIndex([
10
- 'a',
11
- 'aaa',
12
- 'aaaaaa'
13
- ]);
14
-
15
- expect(cellWidthIndex[0]).to.equal(1, 'first column');
16
- expect(cellWidthIndex[1]).to.equal(3, 'second column');
17
- expect(cellWidthIndex[2]).to.equal(6, 'third column');
18
- });
19
- });
20
- });
@@ -1,59 +0,0 @@
1
- import {
2
- expect
3
- } from 'chai';
4
- import chalk from 'chalk';
5
- import calculateMaximumColumnWidthIndex from './../src/calculateMaximumColumnWidthIndex';
6
-
7
- describe('calculateMaximumColumnWidthIndex', () => {
8
- it('throws an error when attempting to calculate maximum column value index for an empty data set', () => {
9
- expect(() => {
10
- calculateMaximumColumnWidthIndex([]);
11
- }).to.throw(Error, 'Dataset must have at least one row.');
12
- });
13
- it('calculates the maximum column value index', () => {
14
- const maximumColumnValueIndex = calculateMaximumColumnWidthIndex([
15
- [
16
- '',
17
- 'a',
18
- 'b',
19
- 'c'
20
- ],
21
- [
22
- '',
23
- 'a',
24
- 'bbbbbbbbbb',
25
- 'c'
26
- ],
27
- [
28
- '',
29
- '',
30
- 'b',
31
- 'ccccc'
32
- ]
33
- ]);
34
-
35
- expect(maximumColumnValueIndex).to.deep.equal([0, 1, 10, 5]);
36
- });
37
- context('cell values contain ANSI codes', () => {
38
- it('uses visual width of the string', () => {
39
- const maximumColumnValueIndex = calculateMaximumColumnWidthIndex([
40
- [
41
- chalk.red('aaaaa')
42
- ]
43
- ]);
44
-
45
- expect(maximumColumnValueIndex[0]).to.equal(5);
46
- });
47
- });
48
- context('cell values contain fullwidth characters', () => {
49
- it('uses visual width of the string', () => {
50
- const maximumColumnValueIndex = calculateMaximumColumnWidthIndex([
51
- [
52
- chalk.red('古')
53
- ]
54
- ]);
55
-
56
- expect(maximumColumnValueIndex[0]).to.equal(2);
57
- });
58
- });
59
- });
@@ -1,78 +0,0 @@
1
- /* eslint-disable max-nested-callbacks */
2
-
3
- import {
4
- expect
5
- } from 'chai';
6
- import calculateRowHeightIndex from './../src/calculateRowHeightIndex';
7
-
8
- describe('calculateRowHeightIndex', () => {
9
- context('single column', () => {
10
- context('cell content width is lesser than column width', () => {
11
- it('is equal to 1', () => {
12
- const data = [
13
- [
14
- 'aaa'
15
- ]
16
- ];
17
-
18
- const config = {
19
- columns: {
20
- 0: {
21
- width: 10,
22
- wrapWord: false
23
- }
24
- }
25
- };
26
-
27
- const rowSpanIndex = calculateRowHeightIndex(data, config);
28
-
29
- expect(rowSpanIndex[0]).to.equal(1);
30
- });
31
- });
32
- context('cell content width is twice the size of the column width', () => {
33
- it('is equal to 2', () => {
34
- const data = [
35
- [
36
- 'aaabbb'
37
- ]
38
- ];
39
-
40
- const config = {
41
- columns: {
42
- 0: {
43
- width: 3,
44
- wrapWord: false
45
- }
46
- }
47
- };
48
-
49
- const rowSpanIndex = calculateRowHeightIndex(data, config);
50
-
51
- expect(rowSpanIndex[0]).to.equal(2);
52
- });
53
- });
54
- });
55
- context('multiple columns', () => {
56
- context('multiple cell content width is greater than the column width', () => {
57
- it('uses the largest height', () => {
58
- const data = [
59
- ['aaabbb'],
60
- ['aaabbb']
61
- ];
62
-
63
- const config = {
64
- columns: {
65
- 0: {
66
- width: 2,
67
- wrapWord: false
68
- }
69
- }
70
- };
71
-
72
- const rowSpanIndex = calculateRowHeightIndex(data, config);
73
-
74
- expect(rowSpanIndex[0]).to.equal(3);
75
- });
76
- });
77
- });
78
- });
package/test/config.js DELETED
@@ -1,43 +0,0 @@
1
- import {
2
- expect
3
- } from 'chai';
4
- import configSamples from './configSamples';
5
- import validateConfig from '../dist/validateConfig';
6
- import configSchema from '../src/schemas/config.json';
7
- import Ajv from 'ajv';
8
- import ajvKeywords from 'ajv-keywords';
9
-
10
- describe('config.json schema', () => {
11
- var validate;
12
-
13
- before(() => {
14
- var ajv = new Ajv({allErrors: true});
15
- ajvKeywords(ajv, 'typeof');
16
- validate = ajv.compile(configSchema);
17
- });
18
-
19
- it('should pass validation of valid config samples', () => {
20
- configSamples.valid.forEach((sample, i) => {
21
- testValid(sample, validate);
22
- testValid(sample, validateConfig);
23
- });
24
-
25
- function testValid(sample, validate) {
26
- var valid = validate(sample);
27
- if (!valid) console.log(validate.errors);
28
- expect(valid).to.equal(true);
29
- }
30
- });
31
-
32
- it('should fail validation of invalid config samples', () => {
33
- configSamples.invalid.forEach((sample, i) => {
34
- testInvalid(sample, validate);
35
- testInvalid(sample, validateConfig);
36
- });
37
-
38
- function testInvalid(sample, validate) {
39
- var valid = validate(sample);
40
- expect(valid).to.equal(false);
41
- }
42
- });
43
- });