Skip to content

Commit

Permalink
style:
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Sep 26, 2017
1 parent 8d367c8 commit 94cb88b
Show file tree
Hide file tree
Showing 25 changed files with 374 additions and 387 deletions.
8 changes: 4 additions & 4 deletions src/alignString.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,23 @@ const alignCenter = (subject, width) => {
*/
export default (subject, containerWidth, alignment) => {
if (!_.isString(subject)) {
throw new Error('Subject parameter value must be a string.');
throw new TypeError('Subject parameter value must be a string.');
}

if (!_.isNumber(containerWidth)) {
throw new Error('Container width parameter value must be a number.');
throw new TypeError('Container width parameter value must be a number.');
}

const subjectWidth = stringWidth(subject);

if (subjectWidth > containerWidth) {
// console.log('subjectWidth', subjectWidth, 'containerWidth', containerWidth, 'subject', subject);
// console.log('subjectWidth', subjectWidth, 'containerWidth', containerWidth, 'subject', subject);

throw new Error('Subject parameter value width cannot be greater than the container width.');
}

if (!_.isString(alignment)) {
throw new Error('Alignment parameter value must be a string.');
throw new TypeError('Alignment parameter value must be a string.');
}

if (alignments.indexOf(alignment) === -1) {
Expand Down
4 changes: 2 additions & 2 deletions src/calculateCellHeight.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import wrapWord from './wrapWord';
*/
export default (value, columnWidth, useWrapWord = false) => {
if (!_.isString(value)) {
throw new Error('Value must be a string.');
throw new TypeError('Value must be a string.');
}

if (!_.isInteger(columnWidth)) {
throw new Error('Column width must be an integer.');
throw new TypeError('Column width must be an integer.');
}

if (columnWidth < 1) {
Expand Down
4 changes: 2 additions & 2 deletions src/calculateRowHeightIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export default (rows, config) => {

_.forEach(cells, (value, index1) => {
if (!_.isNumber(config.columns[index1].width)) {
throw new Error('column[index].width must be a number.');
throw new TypeError('column[index].width must be a number.');
}

if (!_.isBoolean(config.columns[index1].wrapWord)) {
throw new Error('column[index].wrapWord must be a boolean.');
throw new TypeError('column[index].wrapWord must be a boolean.');
}

cellHeightIndex[index1] = calculateCellHeight(value, config.columns[index1].width, config.columns[index1].wrapWord);
Expand Down
2 changes: 1 addition & 1 deletion src/createStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const append = (row, columnWidthIndex, config) => {

let output;

output = '\r\x1b[K';
output = '\r\u001B[K';

output += drawBorderJoin(columnWidthIndex, config.border);
output += body;
Expand Down
12 changes: 6 additions & 6 deletions src/drawTable.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import _ from 'lodash';
import {
drawBorderTop,
drawBorderJoin,
drawBorderBottom
drawBorderTop,
drawBorderJoin,
drawBorderBottom
} from './drawBorder';
import drawRow from './drawRow';

Expand All @@ -15,9 +15,9 @@ import drawRow from './drawRow';
* @returns {string}
*/
export default (rows, border, columnSizeIndex, rowSpanIndex, drawHorizontalLine) => {
let output,
realRowIndex,
rowHeight;
let output;
let realRowIndex;
let rowHeight;

const rowCount = rows.length;

Expand Down
2 changes: 1 addition & 1 deletion src/makeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default (rows, userConfig = {}) => {
config.columns = makeColumns(rows, config.columns, config.columnDefault);

if (!config.drawHorizontalLine) {
/**
/**
* @returns {boolean}
*/
config.drawHorizontalLine = () => {
Expand Down
6 changes: 3 additions & 3 deletions src/validateTableData.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
export default (rows) => {
if (!Array.isArray(rows)) {
throw new Error('Table data must be an array.');
throw new TypeError('Table data must be an array.');
}

if (rows.length === 0) {
Expand All @@ -27,7 +27,7 @@ export default (rows) => {

for (const cells of rows) {
if (!Array.isArray(cells)) {
throw new Error('Table row data must be an array.');
throw new TypeError('Table row data must be an array.');
}

if (cells.length !== columnNumber) {
Expand All @@ -37,7 +37,7 @@ export default (rows) => {
// @todo Make an exception for newline characters.
// @see https://github.com/gajus/table/issues/9
for (const cell of cells) {
if (/[\x01-\x1A]/.test(cell)) {
if (/[\u0001-\u001A]/.test(cell)) {
throw new Error('Table data must not contain control characters.');
}
}
Expand Down
12 changes: 6 additions & 6 deletions test/README/usage/cell_content_alignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import expectTable from './expectTable';
describe('README.md usage/', () => {
it('cell_content_alignment', () => {
const data = [
['0A', '0B', '0C'],
['1A', '1B', '1C'],
['2A', '2B', '2C']
['0A', '0B', '0C'],
['1A', '1B', '1C'],
['2A', '2B', '2C']
];

const config = {
Expand All @@ -30,9 +30,9 @@ describe('README.md usage/', () => {

const output = table(data, config);

// console.log(output);
// console.log(output);

/* eslint-disable no-restricted-syntax */
/* eslint-disable no-restricted-syntax */
expectTable(output, `
╔════════════╤════════════╤════════════╗
║ 0A │ 0B │ 0C ║
Expand All @@ -42,6 +42,6 @@ describe('README.md usage/', () => {
║ 2A │ 2B │ 2C ║
╚════════════╧════════════╧════════════╝
`);
/* eslint-enable no-restricted-syntax */
/* eslint-enable no-restricted-syntax */
});
});
4 changes: 2 additions & 2 deletions test/README/usage/streaming.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
createStream
createStream
} from './../../../src';
import expectTable from './expectTable';

Expand Down Expand Up @@ -50,7 +50,7 @@ describe('README.md usage/', () => {

const output = resetProcessStdoudWrite();

expectTable(output + '\n', '╔════╤════╤════╗\n║ 0A │ 0B │ 0C ║\n╚════╧════╧════╝\r\u001b[K╟────┼────┼────╢\n║ 1A │ 1B │ 1C ║\n╚════╧════╧════╝\r\u001b[K╟────┼────┼────╢\n║ 2A │ 2B │ 2C ║\n╚════╧════╧════╝');
expectTable(output + '\n', '╔════╤════╤════╗\n║ 0A │ 0B │ 0C ║\n╚════╧════╧════╝\r\u001B[K╟────┼────┼────╢\n║ 1A │ 1B │ 1C ║\n╚════╧════╧════╝\r\u001B[K╟────┼────┼────╢\n║ 2A │ 2B │ 2C ║\n╚════╧════╧════╝');
});
});
});
2 changes: 1 addition & 1 deletion test/alignString.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable max-nested-callbacks */

import {
expect
expect
} from 'chai';
import chalk from 'chalk';
import alignString from './../src/alignString';
Expand Down
2 changes: 1 addition & 1 deletion test/calculateCellHeight.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable max-nested-callbacks */

import {
expect
expect
} from 'chai';
import calculateCellHeight from './../src/calculateCellHeight';

Expand Down
2 changes: 1 addition & 1 deletion test/calculateCellWidthIndex.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
expect
expect
} from 'chai';
import calculateCellWidthIndex from './../src/calculateCellWidthIndex';

Expand Down
2 changes: 1 addition & 1 deletion test/calculateMaximumColumnWidthIndex.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
expect
expect
} from 'chai';
import chalk from 'chalk';
import calculateMaximumColumnWidthIndex from './../src/calculateMaximumColumnWidthIndex';
Expand Down
2 changes: 1 addition & 1 deletion test/calculateRowHeightIndex.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable max-nested-callbacks */

import {
expect
expect
} from 'chai';
import calculateRowHeightIndex from './../src/calculateRowHeightIndex';

Expand Down
40 changes: 15 additions & 25 deletions test/config.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,33 @@
import {
expect
expect
} from 'chai';
import configSamples from './configSamples';
import validateConfig from '../dist/validateConfig';
import configSchema from '../src/schemas/config.json';
import Ajv from 'ajv';
import ajvKeywords from 'ajv-keywords';
import validateConfig from '../dist/validateConfig';
import configSchema from '../src/schemas/config.json';
import configSamples from './configSamples';

describe('config.json schema', () => {
var validate;
let validate;

before(() => {
var ajv = new Ajv({allErrors: true});
const ajv = new Ajv({allErrors: true});

ajvKeywords(ajv, 'typeof');
validate = ajv.compile(configSchema);
});

it('should pass validation of valid config samples', () => {
configSamples.valid.forEach((sample, i) => {
testValid(sample, validate);
testValid(sample, validateConfig);
});

function testValid(sample, validate) {
var valid = validate(sample);
if (!valid) console.log(validate.errors);
expect(valid).to.equal(true);
it('passes validation of valid config samples', () => {
for (const sample of configSamples.valid) {
expect(validate(sample)).to.equal(true);
expect(validateConfig(sample)).to.equal(true);
}
});

it('should fail validation of invalid config samples', () => {
configSamples.invalid.forEach((sample, i) => {
testInvalid(sample, validate);
testInvalid(sample, validateConfig);
});

function testInvalid(sample, validate) {
var valid = validate(sample);
expect(valid).to.equal(false);
it('fails validation of invalid config samples', () => {
for (const sample of configSamples.invalid) {
expect(validate(sample)).to.equal(false);
expect(validateConfig(sample)).to.equal(false);
}
});
});
Loading

0 comments on commit 94cb88b

Please sign in to comment.