table 6.2.0 → 6.3.0

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 (84) hide show
  1. package/README.md +1 -1
  2. package/dist/src/alignString.js +64 -0
  3. package/dist/src/alignTableData.js +21 -0
  4. package/dist/src/calculateCellHeight.js +17 -0
  5. package/dist/src/calculateCellWidthIndex.js +17 -0
  6. package/dist/src/calculateMaximumColumnWidthIndex.js +25 -0
  7. package/dist/src/calculateRowHeightIndex.js +30 -0
  8. package/dist/src/createStream.js +74 -0
  9. package/dist/src/drawBorder.js +55 -0
  10. package/dist/src/drawHorizontalContent.js +19 -0
  11. package/dist/src/drawRow.js +17 -0
  12. package/dist/src/drawTable.js +38 -0
  13. package/dist/src/getBorderCharacters.js +83 -0
  14. package/dist/{index.js.flow → src/index.d.ts} +2 -6
  15. package/dist/src/index.js +24 -0
  16. package/dist/src/makeConfig.js +59 -0
  17. package/dist/src/makeStreamConfig.js +60 -0
  18. package/dist/src/mapDataUsingRowHeightIndex.js +27 -0
  19. package/dist/src/padTableData.js +11 -0
  20. package/dist/src/stringifyTableData.js +8 -0
  21. package/dist/src/table.js +28 -0
  22. package/dist/src/truncateTableData.js +20 -0
  23. package/dist/src/types/api.d.ts +99 -0
  24. package/dist/src/types/api.js +3 -0
  25. package/dist/src/types/internal.js +3 -0
  26. package/dist/src/validateConfig.js +27 -0
  27. package/dist/src/validateTableData.js +29 -0
  28. package/dist/{validators.js → src/validators.js} +0 -0
  29. package/dist/src/wrapCell.js +33 -0
  30. package/dist/src/wrapString.js +25 -0
  31. package/dist/src/wrapWord.js +29 -0
  32. package/package.json +33 -25
  33. package/dist/alignString.js +0 -107
  34. package/dist/alignString.js.flow +0 -97
  35. package/dist/alignTableData.js +0 -34
  36. package/dist/alignTableData.js.flow +0 -21
  37. package/dist/calculateCellHeight.js +0 -37
  38. package/dist/calculateCellHeight.js.flow +0 -24
  39. package/dist/calculateCellWidthIndex.js +0 -27
  40. package/dist/calculateCellWidthIndex.js.flow +0 -17
  41. package/dist/calculateMaximumColumnWidthIndex.js +0 -36
  42. package/dist/calculateMaximumColumnWidthIndex.js.flow +0 -27
  43. package/dist/calculateRowHeightIndex.js +0 -45
  44. package/dist/calculateRowHeightIndex.js.flow +0 -36
  45. package/dist/createStream.js +0 -124
  46. package/dist/createStream.js.flow +0 -125
  47. package/dist/drawBorder.js +0 -115
  48. package/dist/drawBorder.js.flow +0 -89
  49. package/dist/drawHorizontalContent.js +0 -41
  50. package/dist/drawHorizontalContent.js.flow +0 -31
  51. package/dist/drawRow.js +0 -41
  52. package/dist/drawRow.js.flow +0 -29
  53. package/dist/drawTable.js +0 -60
  54. package/dist/drawTable.js.flow +0 -64
  55. package/dist/getBorderCharacters.js +0 -118
  56. package/dist/getBorderCharacters.js.flow +0 -120
  57. package/dist/index.js +0 -31
  58. package/dist/makeConfig.js +0 -100
  59. package/dist/makeConfig.js.flow +0 -85
  60. package/dist/makeStreamConfig.js +0 -107
  61. package/dist/makeStreamConfig.js.flow +0 -92
  62. package/dist/mapDataUsingRowHeightIndex.js +0 -41
  63. package/dist/mapDataUsingRowHeightIndex.js.flow +0 -34
  64. package/dist/padTableData.js +0 -23
  65. package/dist/padTableData.js.flow +0 -14
  66. package/dist/schemas/config.json +0 -26
  67. package/dist/schemas/shared.json +0 -98
  68. package/dist/schemas/streamConfig.json +0 -23
  69. package/dist/stringifyTableData.js +0 -21
  70. package/dist/stringifyTableData.js.flow +0 -11
  71. package/dist/table.js +0 -121
  72. package/dist/table.js.flow +0 -108
  73. package/dist/truncateTableData.js +0 -30
  74. package/dist/truncateTableData.js.flow +0 -18
  75. package/dist/validateConfig.js +0 -40
  76. package/dist/validateConfig.js.flow +0 -27
  77. package/dist/validateTableData.js +0 -54
  78. package/dist/validateTableData.js.flow +0 -44
  79. package/dist/wrapCell.js +0 -47
  80. package/dist/wrapCell.js.flow +0 -35
  81. package/dist/wrapString.js +0 -39
  82. package/dist/wrapString.js.flow +0 -29
  83. package/dist/wrapWord.js +0 -46
  84. package/dist/wrapWord.js.flow +0 -39
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = (rows, config) => {
4
+ return rows.map((cells) => {
5
+ return cells.map((value, index1) => {
6
+ const column = config.columns[index1];
7
+ return ' '.repeat(column.paddingLeft) + value + ' '.repeat(column.paddingRight);
8
+ });
9
+ });
10
+ };
11
+ //# sourceMappingURL=padTableData.js.map
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = (rows) => {
4
+ return rows.map((cells) => {
5
+ return cells.map(String);
6
+ });
7
+ };
8
+ //# sourceMappingURL=stringifyTableData.js.map
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const alignTableData_1 = __importDefault(require("./alignTableData"));
7
+ const calculateCellWidthIndex_1 = __importDefault(require("./calculateCellWidthIndex"));
8
+ const calculateRowHeightIndex_1 = __importDefault(require("./calculateRowHeightIndex"));
9
+ const drawTable_1 = __importDefault(require("./drawTable"));
10
+ const makeConfig_1 = __importDefault(require("./makeConfig"));
11
+ const mapDataUsingRowHeightIndex_1 = __importDefault(require("./mapDataUsingRowHeightIndex"));
12
+ const padTableData_1 = __importDefault(require("./padTableData"));
13
+ const stringifyTableData_1 = __importDefault(require("./stringifyTableData"));
14
+ const truncateTableData_1 = __importDefault(require("./truncateTableData"));
15
+ const validateTableData_1 = __importDefault(require("./validateTableData"));
16
+ exports.default = (data, userConfig = {}) => {
17
+ validateTableData_1.default(data);
18
+ let rows = stringifyTableData_1.default(data);
19
+ const config = makeConfig_1.default(rows, userConfig);
20
+ rows = truncateTableData_1.default(rows, config);
21
+ const rowHeightIndex = calculateRowHeightIndex_1.default(rows, config);
22
+ rows = mapDataUsingRowHeightIndex_1.default(rows, rowHeightIndex, config);
23
+ rows = alignTableData_1.default(rows, config);
24
+ rows = padTableData_1.default(rows, config);
25
+ const cellWidthIndex = calculateCellWidthIndex_1.default(rows[0]);
26
+ return drawTable_1.default(rows, cellWidthIndex, rowHeightIndex, config);
27
+ };
28
+ //# sourceMappingURL=table.js.map
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const lodash_truncate_1 = __importDefault(require("lodash.truncate"));
7
+ /**
8
+ * @todo Make it work with ASCII content.
9
+ */
10
+ exports.default = (rows, config) => {
11
+ return rows.map((cells) => {
12
+ return cells.map((content, index) => {
13
+ return lodash_truncate_1.default(content, {
14
+ length: config.columns[index].truncate,
15
+ omission: '…',
16
+ });
17
+ });
18
+ });
19
+ };
20
+ //# sourceMappingURL=truncateTableData.js.map
@@ -0,0 +1,99 @@
1
+ export declare type DrawLinePredicate = (index: number, size: number) => boolean;
2
+ export declare type DrawVerticalLine = DrawLinePredicate;
3
+ export declare type DrawHorizontalLine = DrawLinePredicate;
4
+ export declare type BorderUserConfig = {
5
+ readonly topLeft?: string;
6
+ readonly topRight?: string;
7
+ readonly topBody?: string;
8
+ readonly topJoin?: string;
9
+ readonly bottomLeft?: string;
10
+ readonly bottomRight?: string;
11
+ readonly bottomBody?: string;
12
+ readonly bottomJoin?: string;
13
+ readonly bodyRight?: string;
14
+ readonly bodyLeft?: string;
15
+ readonly bodyJoin?: string;
16
+ readonly joinLeft?: string;
17
+ readonly joinRight?: string;
18
+ readonly joinBody?: string;
19
+ readonly joinJoin?: string;
20
+ };
21
+ export declare type ColumnUserConfig = {
22
+ /**
23
+ * Cell content horizontal alignment (default: left)
24
+ */
25
+ readonly alignment?: 'center' | 'left' | 'right';
26
+ /**
27
+ * Column width (default: auto calculation based on the cell content)
28
+ */
29
+ readonly width?: number;
30
+ /**
31
+ * Number of characters are which the content will be truncated (default: Infinity)
32
+ */
33
+ readonly truncate?: number;
34
+ /**
35
+ * Cell content padding width left (default: 1)
36
+ */
37
+ readonly paddingLeft?: number;
38
+ /**
39
+ * Cell content padding width right (default: 1)
40
+ */
41
+ readonly paddingRight?: number;
42
+ /**
43
+ * If true, the text is broken at the nearest space or one of the special characters: "\|/_.,;-"
44
+ */
45
+ readonly wrapWord?: boolean;
46
+ };
47
+ export declare type BaseUserConfig = {
48
+ /**
49
+ * Custom border
50
+ */
51
+ readonly border?: BorderUserConfig;
52
+ /**
53
+ * Default values for all columns. Column specific settings overwrite the default values.
54
+ */
55
+ readonly columnDefault?: ColumnUserConfig;
56
+ /**
57
+ * Column specific configuration.
58
+ */
59
+ readonly columns?: Indexable<ColumnUserConfig>;
60
+ /**
61
+ * Used to tell whether to draw a vertical line.
62
+ * This callback is called for each non-content line of the table.
63
+ * The default behavior is to always return true.
64
+ */
65
+ readonly drawVerticalLine?: DrawVerticalLine;
66
+ };
67
+ export declare type TableUserConfig = BaseUserConfig & {
68
+ /**
69
+ * Used to tell whether to draw a horizontal line.
70
+ * This callback is called for each non-content line of the table.
71
+ * The default behavior is to always return true.
72
+ */
73
+ readonly drawHorizontalLine?: DrawHorizontalLine;
74
+ /**
75
+ * Horizontal lines inside the table are not drawn.
76
+ */
77
+ readonly singleLine?: boolean;
78
+ };
79
+ export declare type StreamUserConfig = BaseUserConfig & {
80
+ /**
81
+ * The number of columns
82
+ */
83
+ readonly columnCount: number;
84
+ /**
85
+ * Default values for all columns. Column specific settings overwrite the default values.
86
+ */
87
+ readonly columnDefault: ColumnUserConfig & {
88
+ /**
89
+ * The default width for each column
90
+ */
91
+ readonly width: number;
92
+ };
93
+ };
94
+ export declare type WritableStream = {
95
+ readonly write: (rows: string[]) => void;
96
+ };
97
+ export declare type Indexable<T> = {
98
+ readonly [index: number]: T;
99
+ };
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=api.js.map
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=internal.js.map
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
7
+ // @ts-expect-error
8
+ // eslint-disable-next-line import/no-unresolved
9
+ const validators_1 = __importDefault(require("./validators"));
10
+ exports.default = (schemaId, config) => {
11
+ const validate = validators_1.default[schemaId];
12
+ if (!validate(config)) {
13
+ const errors = validate.errors.map((error) => {
14
+ return {
15
+ message: error.message,
16
+ params: error.params,
17
+ schemaPath: error.schemaPath,
18
+ };
19
+ });
20
+ /* eslint-disable no-console */
21
+ console.log('config', config);
22
+ console.log('errors', errors);
23
+ /* eslint-enable no-console */
24
+ throw new Error('Invalid config.');
25
+ }
26
+ };
27
+ //# sourceMappingURL=validateConfig.js.map
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = (rows) => {
4
+ if (!Array.isArray(rows)) {
5
+ throw new TypeError('Table data must be an array.');
6
+ }
7
+ if (rows.length === 0) {
8
+ throw new Error('Table must define at least one row.');
9
+ }
10
+ if (rows[0].length === 0) {
11
+ throw new Error('Table must define at least one column.');
12
+ }
13
+ const columnNumber = rows[0].length;
14
+ for (const row of rows) {
15
+ if (!Array.isArray(row)) {
16
+ throw new TypeError('Table row data must be an array.');
17
+ }
18
+ if (row.length !== columnNumber) {
19
+ throw new Error('Table must have a consistent number of cells.');
20
+ }
21
+ for (const cell of row) {
22
+ // eslint-disable-next-line no-control-regex
23
+ if (/[\u0001-\u0006\u0008\u0009\u000B-\u001A]/.test(cell)) {
24
+ throw new Error('Table data must not contain control characters.');
25
+ }
26
+ }
27
+ }
28
+ };
29
+ //# sourceMappingURL=validateTableData.js.map
File without changes
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const wrapString_1 = __importDefault(require("./wrapString"));
7
+ const wrapWord_1 = __importDefault(require("./wrapWord"));
8
+ /**
9
+ * Wrap a single cell value into a list of lines
10
+ *
11
+ * Always wraps on newlines, for the remainder uses either word or string wrapping
12
+ * depending on user configuration.
13
+ *
14
+ */
15
+ exports.default = (cellValue, columnWidth, useWrapWord) => {
16
+ // First split on literal newlines
17
+ const cellLines = cellValue.split('\n');
18
+ // Then iterate over the list and word-wrap every remaining line if necessary.
19
+ for (let lineNr = 0; lineNr < cellLines.length;) {
20
+ let lineChunks;
21
+ if (useWrapWord) {
22
+ lineChunks = wrapWord_1.default(cellLines[lineNr], columnWidth);
23
+ }
24
+ else {
25
+ lineChunks = wrapString_1.default(cellLines[lineNr], columnWidth);
26
+ }
27
+ // Replace our original array element with whatever the wrapping returned
28
+ cellLines.splice(lineNr, 1, ...lineChunks);
29
+ lineNr += lineChunks.length;
30
+ }
31
+ return cellLines;
32
+ };
33
+ //# sourceMappingURL=wrapCell.js.map
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const slice_ansi_1 = __importDefault(require("slice-ansi"));
7
+ const string_width_1 = __importDefault(require("string-width"));
8
+ /**
9
+ * Creates an array of strings split into groups the length of size.
10
+ * This function works with strings that contain ASCII characters.
11
+ *
12
+ * wrapText is different from would-be "chunk" implementation
13
+ * in that whitespace characters that occur on a chunk size limit are trimmed.
14
+ *
15
+ */
16
+ exports.default = (subject, size) => {
17
+ let subjectSlice = subject;
18
+ const chunks = [];
19
+ do {
20
+ chunks.push(slice_ansi_1.default(subjectSlice, 0, size));
21
+ subjectSlice = slice_ansi_1.default(subjectSlice, size).trim();
22
+ } while (string_width_1.default(subjectSlice));
23
+ return chunks;
24
+ };
25
+ //# sourceMappingURL=wrapString.js.map
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const slice_ansi_1 = __importDefault(require("slice-ansi"));
7
+ const string_width_1 = __importDefault(require("string-width"));
8
+ exports.default = (input, size) => {
9
+ let subject = input;
10
+ const chunks = [];
11
+ // https://regex101.com/r/gY5kZ1/1
12
+ const re = new RegExp('(^.{1,' + String(size) + '}(\\s+|$))|(^.{1,' + String(size - 1) + '}(\\\\|/|_|\\.|,|;|-))');
13
+ do {
14
+ let chunk;
15
+ chunk = re.exec(subject);
16
+ if (chunk) {
17
+ chunk = chunk[0];
18
+ subject = slice_ansi_1.default(subject, string_width_1.default(chunk));
19
+ chunk = chunk.trim();
20
+ }
21
+ else {
22
+ chunk = slice_ansi_1.default(subject, 0, size);
23
+ subject = slice_ansi_1.default(subject, size);
24
+ }
25
+ chunks.push(chunk);
26
+ } while (string_width_1.default(subject));
27
+ return chunks;
28
+ };
29
+ //# sourceMappingURL=wrapWord.js.map
package/package.json CHANGED
@@ -17,32 +17,34 @@
17
17
  },
18
18
  "description": "Formats data into a string table.",
19
19
  "devDependencies": {
20
- "@babel/cli": "^7.12.10",
21
- "@babel/core": "^7.12.10",
22
- "@babel/plugin-transform-flow-strip-types": "^7.12.10",
23
- "@babel/preset-env": "^7.12.11",
24
- "@babel/register": "^7.12.10",
20
+ "@types/chai": "^4.2.16",
21
+ "@types/lodash.clonedeep": "^4.5.6",
22
+ "@types/lodash.flatten": "^4.4.6",
23
+ "@types/lodash.mapvalues": "^4.6.6",
24
+ "@types/lodash.truncate": "^4.4.6",
25
+ "@types/mocha": "^8.2.2",
26
+ "@types/node": "^14.14.37",
27
+ "@types/sinon": "^10.0.0",
28
+ "@types/slice-ansi": "^4.0.0",
25
29
  "ajv-cli": "^5.0.0",
26
30
  "ajv-keywords": "^5.0.0",
27
- "babel-plugin-istanbul": "^6.0.0",
28
- "babel-plugin-transform-export-default-name": "^2.1.0",
29
31
  "chai": "^4.2.0",
30
32
  "chalk": "^4.1.0",
31
33
  "coveralls": "^3.1.0",
32
34
  "eslint": "^7.16.0",
33
35
  "eslint-config-canonical": "^25.0.0",
34
- "flow-bin": "^0.141.0",
35
- "flow-copy-source": "^2.0.9",
36
36
  "gitdown": "^3.1.3",
37
37
  "husky": "^4.3.6",
38
38
  "js-beautify": "^1.13.0",
39
39
  "lodash.mapvalues": "^4.6.0",
40
40
  "lodash.random": "^3.2.0",
41
41
  "lodash.sample": "^4.2.1",
42
+ "mkdirp": "^1.0.4",
42
43
  "mocha": "^8.2.1",
43
44
  "nyc": "^15.1.0",
44
45
  "semantic-release": "^17.3.1",
45
- "sinon": "^9.2.2"
46
+ "sinon": "^9.2.2",
47
+ "typescript": "4.2.4"
46
48
  },
47
49
  "engines": {
48
50
  "node": ">=10.0.0"
@@ -50,7 +52,7 @@
50
52
  "husky": {
51
53
  "hooks": {
52
54
  "post-commit": "npm run create-readme && git add README.md && git commit -m 'docs: generate docs' --no-verify",
53
- "pre-commit": "npm run lint && npm run test && npm run build"
55
+ "pre-commit": "npm run build && npm run lint && npm run test"
54
56
  }
55
57
  },
56
58
  "keywords": [
@@ -61,31 +63,37 @@
61
63
  "ansi"
62
64
  ],
63
65
  "license": "BSD-3-Clause",
64
- "main": "./dist/index.js",
66
+ "main": "./dist/src/index.js",
67
+ "files": [
68
+ "dist/src/**/*.js",
69
+ "dist/src/types/api.d.ts",
70
+ "dist/src/index.d.ts"
71
+ ],
65
72
  "name": "table",
66
73
  "nyc": {
67
- "include": [
68
- "src/**/*.js"
74
+ "extensions": [
75
+ ".ts"
76
+ ],
77
+ "exclude": [
78
+ "dist/src/validators.js",
79
+ "test/**/*.ts"
69
80
  ],
70
- "instrument": false,
71
81
  "reporter": [
72
82
  "text-lcov"
73
- ],
74
- "require": [
75
- "@babel/register"
76
- ],
77
- "sourceMap": false
83
+ ]
78
84
  },
79
85
  "repository": {
80
86
  "type": "git",
81
87
  "url": "https://github.com/gajus/table"
82
88
  },
83
89
  "scripts": {
84
- "build": "rm -fr ./dist && NODE_ENV=production babel ./src --out-dir ./dist --copy-files && npm run create-validators && flow-copy-source src dist",
90
+ "compile": "tsc",
91
+ "prebuild": "rm -fr ./dist && mkdirp dist/src",
92
+ "build": "npm run create-validators && npm run compile",
85
93
  "create-readme": "gitdown ./.README/README.md --output-file ./README.md",
86
- "create-validators": "ajv compile --all-errors --inline-refs=false -s src/schemas/config -s src/schemas/streamConfig -r src/schemas/shared -c ajv-keywords/dist/keywords/typeof -o | js-beautify > dist/validators.js",
87
- "lint": "npm run build && eslint ./src ./test && flow",
88
- "test": "mocha --require @babel/register"
94
+ "create-validators": "ajv compile --all-errors --inline-refs=false -s src/schemas/config -s src/schemas/streamConfig -r src/schemas/shared -c ajv-keywords/dist/keywords/typeof -o | js-beautify > dist/src/validators.js",
95
+ "lint": "eslint ./src ./test",
96
+ "test": "mocha ./dist/test/**/*.js"
89
97
  },
90
- "version": "6.2.0"
98
+ "version": "6.3.0"
91
99
  }
@@ -1,107 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _isNumberObject = _interopRequireDefault(require("is-number-object"));
9
-
10
- var _isString = _interopRequireDefault(require("is-string"));
11
-
12
- var _stringWidth = _interopRequireDefault(require("string-width"));
13
-
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
- const alignments = ['left', 'right', 'center'];
17
- /**
18
- * @param {string} subject
19
- * @param {number} width
20
- * @returns {string}
21
- */
22
-
23
- const alignLeft = (subject, width) => {
24
- return subject + ' '.repeat(width);
25
- };
26
- /**
27
- * @param {string} subject
28
- * @param {number} width
29
- * @returns {string}
30
- */
31
-
32
-
33
- const alignRight = (subject, width) => {
34
- return ' '.repeat(width) + subject;
35
- };
36
- /**
37
- * @param {string} subject
38
- * @param {number} width
39
- * @returns {string}
40
- */
41
-
42
-
43
- const alignCenter = (subject, width) => {
44
- let halfWidth;
45
- halfWidth = width / 2;
46
-
47
- if (width % 2 === 0) {
48
- return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth);
49
- } else {
50
- halfWidth = Math.floor(halfWidth);
51
- return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth + 1);
52
- }
53
- };
54
- /**
55
- * Pads a string to the left and/or right to position the subject
56
- * text in a desired alignment within a container.
57
- *
58
- * @param {string} subject
59
- * @param {number} containerWidth
60
- * @param {string} alignment One of the valid options (left, right, center).
61
- * @returns {string}
62
- */
63
-
64
-
65
- const alignString = (subject, containerWidth, alignment) => {
66
- if (!(0, _isString.default)(subject)) {
67
- throw new TypeError('Subject parameter value must be a string.');
68
- }
69
-
70
- if (!(0, _isNumberObject.default)(containerWidth)) {
71
- throw new TypeError('Container width parameter value must be a number.');
72
- }
73
-
74
- const subjectWidth = (0, _stringWidth.default)(subject);
75
-
76
- if (subjectWidth > containerWidth) {
77
- // console.log('subjectWidth', subjectWidth, 'containerWidth', containerWidth, 'subject', subject);
78
- throw new Error('Subject parameter value width cannot be greater than the container width.');
79
- }
80
-
81
- if (!(0, _isString.default)(alignment)) {
82
- throw new TypeError('Alignment parameter value must be a string.');
83
- }
84
-
85
- if (!alignments.includes(alignment)) {
86
- throw new Error('Alignment parameter value must be a known alignment parameter value (left, right, center).');
87
- }
88
-
89
- if (subjectWidth === 0) {
90
- return ' '.repeat(containerWidth);
91
- }
92
-
93
- const availableWidth = containerWidth - subjectWidth;
94
-
95
- if (alignment === 'left') {
96
- return alignLeft(subject, availableWidth);
97
- }
98
-
99
- if (alignment === 'right') {
100
- return alignRight(subject, availableWidth);
101
- }
102
-
103
- return alignCenter(subject, availableWidth);
104
- };
105
-
106
- var _default = alignString;
107
- exports.default = _default;
@@ -1,97 +0,0 @@
1
- import isNumber from 'is-number-object';
2
- import isString from 'is-string';
3
- import stringWidth from 'string-width';
4
-
5
- const alignments = [
6
- 'left',
7
- 'right',
8
- 'center',
9
- ];
10
-
11
- /**
12
- * @param {string} subject
13
- * @param {number} width
14
- * @returns {string}
15
- */
16
- const alignLeft = (subject, width) => {
17
- return subject + ' '.repeat(width);
18
- };
19
-
20
- /**
21
- * @param {string} subject
22
- * @param {number} width
23
- * @returns {string}
24
- */
25
- const alignRight = (subject, width) => {
26
- return ' '.repeat(width) + subject;
27
- };
28
-
29
- /**
30
- * @param {string} subject
31
- * @param {number} width
32
- * @returns {string}
33
- */
34
- const alignCenter = (subject, width) => {
35
- let halfWidth;
36
-
37
- halfWidth = width / 2;
38
-
39
- if (width % 2 === 0) {
40
- return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth);
41
- } else {
42
- halfWidth = Math.floor(halfWidth);
43
-
44
- return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth + 1);
45
- }
46
- };
47
-
48
- /**
49
- * Pads a string to the left and/or right to position the subject
50
- * text in a desired alignment within a container.
51
- *
52
- * @param {string} subject
53
- * @param {number} containerWidth
54
- * @param {string} alignment One of the valid options (left, right, center).
55
- * @returns {string}
56
- */
57
- export default (subject, containerWidth, alignment) => {
58
- if (!isString(subject)) {
59
- throw new TypeError('Subject parameter value must be a string.');
60
- }
61
-
62
- if (!isNumber(containerWidth)) {
63
- throw new TypeError('Container width parameter value must be a number.');
64
- }
65
-
66
- const subjectWidth = stringWidth(subject);
67
-
68
- if (subjectWidth > containerWidth) {
69
- // console.log('subjectWidth', subjectWidth, 'containerWidth', containerWidth, 'subject', subject);
70
-
71
- throw new Error('Subject parameter value width cannot be greater than the container width.');
72
- }
73
-
74
- if (!isString(alignment)) {
75
- throw new TypeError('Alignment parameter value must be a string.');
76
- }
77
-
78
- if (!alignments.includes(alignment)) {
79
- throw new Error('Alignment parameter value must be a known alignment parameter value (left, right, center).');
80
- }
81
-
82
- if (subjectWidth === 0) {
83
- return ' '.repeat(containerWidth);
84
- }
85
-
86
- const availableWidth = containerWidth - subjectWidth;
87
-
88
- if (alignment === 'left') {
89
- return alignLeft(subject, availableWidth);
90
- }
91
-
92
- if (alignment === 'right') {
93
- return alignRight(subject, availableWidth);
94
- }
95
-
96
- return alignCenter(subject, availableWidth);
97
- };
@@ -1,34 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _stringWidth = _interopRequireDefault(require("string-width"));
9
-
10
- var _alignString = _interopRequireDefault(require("./alignString"));
11
-
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
-
14
- /**
15
- * @param {table~row[]} rows
16
- * @param {object} config
17
- * @returns {table~row[]}
18
- */
19
- const alignTableData = (rows, config) => {
20
- return rows.map(cells => {
21
- return cells.map((value, index1) => {
22
- const column = config.columns[index1];
23
-
24
- if ((0, _stringWidth.default)(value) === column.width) {
25
- return value;
26
- } else {
27
- return (0, _alignString.default)(value, column.width, column.alignment);
28
- }
29
- });
30
- });
31
- };
32
-
33
- var _default = alignTableData;
34
- exports.default = _default;