Skip to content

Commit

Permalink
fix: remove lodash dependency (closes #121)
Browse files Browse the repository at this point in the history
This reverts commit 49a640c.
  • Loading branch information
gajus committed Sep 4, 2020
1 parent 49a640c commit 8c16d60
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 32 deletions.
1 change: 1 addition & 0 deletions .babelrc
Expand Up @@ -7,6 +7,7 @@
}
},
"plugins": [
"babel-plugin-lodash",
"transform-export-default-name",
"@babel/transform-flow-strip-types"
],
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -6,6 +6,7 @@
},
"dependencies": {
"ajv": "^6.12.4",
"lodash": "^4.17.20",
"slice-ansi": "^4.0.0",
"string-width": "^4.2.0"
},
Expand All @@ -20,6 +21,7 @@
"ajv-cli": "^3.2.1",
"ajv-keywords": "^3.5.2",
"babel-plugin-istanbul": "^6.0.0",
"babel-plugin-lodash": "^3.3.4",
"babel-plugin-transform-export-default-name": "^2.0.4",
"chai": "^4.2.0",
"chalk": "^4.1.0",
Expand Down
7 changes: 4 additions & 3 deletions src/alignString.js
@@ -1,3 +1,4 @@
import _ from 'lodash';
import stringWidth from 'string-width';

const alignments = [
Expand Down Expand Up @@ -53,11 +54,11 @@ const alignCenter = (subject, width) => {
* @returns {string}
*/
export default (subject, containerWidth, alignment) => {
if (typeof subject !== 'string') {
if (!_.isString(subject)) {
throw new TypeError('Subject parameter value must be a string.');
}

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

Expand All @@ -69,7 +70,7 @@ export default (subject, containerWidth, alignment) => {
throw new Error('Subject parameter value width cannot be greater than the container width.');
}

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

Expand Down
3 changes: 2 additions & 1 deletion src/calculateCellHeight.js
@@ -1,3 +1,4 @@
import _ from 'lodash';
import wrapCell from './wrapCell';

/**
Expand All @@ -7,7 +8,7 @@ import wrapCell from './wrapCell';
* @returns {number}
*/
export default (value, columnWidth, useWrapWord = false) => {
if (typeof value !== 'string') {
if (!_.isString(value)) {
throw new TypeError('Value must be a string.');
}

Expand Down
7 changes: 4 additions & 3 deletions src/calculateRowHeightIndex.js
@@ -1,3 +1,4 @@
import _ from 'lodash';
import calculateCellHeight from './calculateCellHeight';

/**
Expand All @@ -16,18 +17,18 @@ export default (rows, config) => {
const cellHeightIndex = new Array(tableWidth).fill(1);

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

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

cellHeightIndex[index1] = calculateCellHeight(value, config.columns[index1].width, config.columns[index1].wrapWord);
});

rowSpanIndex.push(Math.max(...cellHeightIndex));
rowSpanIndex.push(_.max(cellHeightIndex));
});

return rowSpanIndex;
Expand Down
10 changes: 6 additions & 4 deletions src/createStream.js
@@ -1,3 +1,4 @@
import _ from 'lodash';
import alignTableData from './alignTableData';
import calculateRowHeightIndex from './calculateRowHeightIndex';
import {
Expand Down Expand Up @@ -54,7 +55,7 @@ const create = (row, columnWidthIndex, config) => {
output += body;
output += drawBorderBottom(columnWidthIndex, config.border);

output = output.trimEnd();
output = _.trimEnd(output);

process.stdout.write(output);
};
Expand Down Expand Up @@ -83,7 +84,7 @@ const append = (row, columnWidthIndex, config) => {
output += body;
output += bottom;

output = output.trimEnd();
output = _.trimEnd(output);

process.stdout.write(output);
};
Expand All @@ -95,9 +96,10 @@ const append = (row, columnWidthIndex, config) => {
export default (userConfig = {}) => {
const config = makeStreamConfig(userConfig);

const columnWidthIndex = Object.values(config.columns).map((column) => {
// @todo Use 'Object.values' when Node.js v6 support is dropped.
const columnWidthIndex = _.values(_.mapValues(config.columns, (column) => {
return column.width + column.paddingLeft + column.paddingRight;
});
}));

let empty;

Expand Down
9 changes: 5 additions & 4 deletions src/makeConfig.js
@@ -1,3 +1,4 @@
import _ from 'lodash';
import calculateMaximumColumnWidthIndex from './calculateMaximumColumnWidthIndex';
import getBorderCharacters from './getBorderCharacters';
import validateConfig from './validateConfig';
Expand All @@ -24,8 +25,8 @@ const makeBorder = (border = {}) => {
const makeColumns = (rows, columns = {}, columnDefault = {}) => {
const maximumColumnWidthIndex = calculateMaximumColumnWidthIndex(rows);

for (let index = rows[0].length; index--;) {
if (!columns[index]) {
_.times(rows[0].length, (index) => {
if (_.isUndefined(columns[index])) {
columns[index] = {};
}

Expand All @@ -37,7 +38,7 @@ const makeColumns = (rows, columns = {}, columnDefault = {}) => {
width: maximumColumnWidthIndex[index],
wrapWord: false,
}, columnDefault, columns[index]);
}
});

return columns;
};
Expand All @@ -53,7 +54,7 @@ const makeColumns = (rows, columns = {}, columnDefault = {}) => {
export default (rows, userConfig = {}) => {
validateConfig('config.json', userConfig);

const config = {...userConfig};
const config = _.cloneDeep(userConfig);

config.border = makeBorder(config.border);
config.columns = makeColumns(rows, config.columns, config.columnDefault);
Expand Down
11 changes: 5 additions & 6 deletions src/makeStreamConfig.js
@@ -1,3 +1,4 @@
import _ from 'lodash';
import getBorderCharacters from './getBorderCharacters';
import validateConfig from './validateConfig';

Expand All @@ -21,10 +22,8 @@ const makeBorder = (border = {}) => {
* @returns {object}
*/
const makeColumns = (columnCount, columns = {}, columnDefault = {}) => {
let index = columnCount;

while (index--) {
if (!columns[index]) {
_.times(columnCount, (index) => {
if (_.isUndefined(columns[index])) {
columns[index] = {};
}

Expand All @@ -35,7 +34,7 @@ const makeColumns = (columnCount, columns = {}, columnDefault = {}) => {
truncate: Infinity,
wrapWord: false,
}, columnDefault, columns[index]);
}
});

return columns;
};
Expand Down Expand Up @@ -67,7 +66,7 @@ const makeColumns = (columnCount, columns = {}, columnDefault = {}) => {
export default (userConfig = {}) => {
validateConfig('streamConfig.json', userConfig);

const config = {...userConfig};
const config = _.cloneDeep(userConfig);

if (!config.columnDefault || !config.columnDefault.width) {
throw new Error('Must provide config.columnDefault.width when creating a stream.');
Expand Down
6 changes: 3 additions & 3 deletions src/mapDataUsingRowHeightIndex.js
@@ -1,3 +1,4 @@
import _ from 'lodash';
import wrapCell from './wrapCell';

/**
Expand All @@ -10,7 +11,7 @@ export default (unmappedRows, rowHeightIndex, config) => {
const tableWidth = unmappedRows[0].length;

const mappedRows = unmappedRows.map((cells, index0) => {
const rowHeight = Array.from({length: rowHeightIndex[index0]}, () => {
const rowHeight = _.times(rowHeightIndex[index0], () => {
return new Array(tableWidth).fill('');
});

Expand All @@ -29,6 +30,5 @@ export default (unmappedRows, rowHeightIndex, config) => {
return rowHeight;
});

// flat array
return [].concat(...mappedRows);
return _.flatten(mappedRows);
};
8 changes: 4 additions & 4 deletions src/truncateTableData.js
@@ -1,6 +1,4 @@
const truncate = (string, length) => {
return string.length > length ? string.slice(0, Math.max(0, length - 3)) + '...' : string;
};
import _ from 'lodash';

/**
* @todo Make it work with ASCII content.
Expand All @@ -11,7 +9,9 @@ const truncate = (string, length) => {
export default (rows, config) => {
return rows.map((cells) => {
return cells.map((content, index) => {
return truncate(content, config.columns[index].truncate);
return _.truncate(content, {
length: config.columns[index].truncate,
});
});
});
};
3 changes: 2 additions & 1 deletion test/README/usage/expectTable.js
@@ -1,12 +1,13 @@
import {
expect,
} from 'chai';
import _ from 'lodash';

/**
* @param {string} result
* @param {string} expectedResult
* @returns {undefined}
*/
export default (result, expectedResult) => {
expect(result).to.equal(expectedResult.trim() + '\n');
expect(result).to.equal(_.trim(expectedResult) + '\n');
};
3 changes: 2 additions & 1 deletion test/README/usage/moon_mission.js
@@ -1,4 +1,5 @@
import chalk from 'chalk';
import _ from 'lodash';
import {
table,
getBorderCharacters,
Expand Down Expand Up @@ -51,7 +52,7 @@ describe('README.md usage/', () => {
],
];

const tableBorder = Object.values(getBorderCharacters('honeywell')).map((char) => {
const tableBorder = _.mapValues(getBorderCharacters('honeywell'), (char) => {
return chalk.gray(char);
});

Expand Down
5 changes: 3 additions & 2 deletions test/README/usage/predefined_border_templates.js
@@ -1,3 +1,4 @@
import _ from 'lodash';
import {
table,
getBorderCharacters,
Expand Down Expand Up @@ -71,7 +72,7 @@ describe('README.md usage/predefined_border_templates', () => {
border: getBorderCharacters('void'),
});

expectTable(output.trim() + '\n', '0A 0B 0C \n\n 1A 1B 1C \n\n 2A 2B 2C');
expectTable(_.trim(output) + '\n', '0A 0B 0C \n\n 1A 1B 1C \n\n 2A 2B 2C');
});

it('borderless', () => {
Expand All @@ -86,6 +87,6 @@ describe('README.md usage/predefined_border_templates', () => {
},
});

expectTable(output.trim() + '\n', '0A 0B 0C \n1A 1B 1C \n2A 2B 2C');
expectTable(_.trim(output) + '\n', '0A 0B 0C \n1A 1B 1C \n2A 2B 2C');
});
});

0 comments on commit 8c16d60

Please sign in to comment.