Skip to content

Commit

Permalink
fix: remove lodash dependency (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmywarting authored Sep 3, 2020
1 parent a376590 commit 49a640c
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 43 deletions.
1 change: 0 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
}
},
"plugins": [
"babel-plugin-lodash",
"transform-export-default-name",
"@babel/transform-flow-strip-types"
],
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
},
"dependencies": {
"ajv": "^6.12.4",
"lodash": "^4.17.20",
"slice-ansi": "^4.0.0",
"string-width": "^4.2.0"
},
Expand All @@ -21,7 +20,6 @@
"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: 3 additions & 4 deletions src/alignString.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ from 'lodash';
import stringWidth from 'string-width';

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

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

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

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

Expand Down
3 changes: 1 addition & 2 deletions src/calculateCellHeight.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ from 'lodash';
import wrapCell from './wrapCell';

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

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

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

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

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

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

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

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

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

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

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

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

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

let empty;

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

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

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

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

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

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

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

while (index--) {
if (!columns[index]) {
columns[index] = {};
}

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

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

const config = _.cloneDeep(userConfig);
const config = {...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
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ from 'lodash';
import wrapCell from './wrapCell';

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

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

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

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

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

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

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

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

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

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

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

0 comments on commit 49a640c

Please sign in to comment.