Skip to content

Commit

Permalink
feat: optimize text truncation (#148)
Browse files Browse the repository at this point in the history
Co-authored-by: Nam Hoang Le <[email protected]>
  • Loading branch information
nam-hle and nhle-mgmtp authored Apr 19, 2021
1 parent 7c047ed commit 9a344ce
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .README/usage/text_truncation.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ console.log(output);
║ t amet, consectetur ║
║ adipiscing elit. Pha ║
║ sellus pulvinar nibh ║
║ sed mauris conva...
║ sed mauris convall…
╚══════════════════════╝
```
1 change: 1 addition & 0 deletions src/truncateTableData.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default (rows, config) => {
return cells.map((content, index) => {
return truncate(content, {
length: config.columns[index].truncate,
omission: '…',
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/README/usage/text_truncating.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('README.md usage/', () => {
║ t amet, consectetur ║
║ adipiscing elit. Pha ║
║ sellus pulvinar nibh ║
║ sed mauris conva...
║ sed mauris convall…
╚══════════════════════╝
`);
});
Expand Down
47 changes: 44 additions & 3 deletions test/truncateTableData.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('truncateTableData', () => {

expect(truncateTableData(rows, makeConfig(rows, {columnDefault: {
truncate: 20,
}}))).to.deep.equal([['a'.repeat(17) + '...']]);
}}))).to.deep.equal([['a'.repeat(19) + '']]);
});
});

Expand All @@ -39,7 +39,7 @@ describe('truncateTableData', () => {
truncate: 30,
},
},
}))).to.deep.equal([['a'.repeat(27) + '...']]);
}))).to.deep.equal([['a'.repeat(29) + '']]);
});
});
});
Expand All @@ -57,7 +57,48 @@ describe('truncateTableData', () => {
truncate: 30,
},
},
}))).to.deep.equal([['a'.repeat(27) + '...', 'b'.repeat(17) + '...'], ['c'.repeat(27) + '...', 'd'.repeat(17) + '...']]);
}))).to.deep.equal([
['a'.repeat(29) + '…', 'b'.repeat(19) + '…'],
['c'.repeat(29) + '…', 'd'.repeat(19) + '…']]);
});
});

context('edge cases', () => {
context('truncate = 0', () => {
it('returns ellipsis only', () => {
const rows = [['a'.repeat(100)]];
expect(truncateTableData(rows, makeConfig(rows, {
columns: {0: {truncate: 0}},
}))).to.deep.equal([['…']]);
});
});

context('truncate = 1', () => {
it('returns ellipsis only', () => {
const rows = [['a'.repeat(100)]];
expect(truncateTableData(rows, makeConfig(rows, {
columns: {0: {truncate: 1}},

}))).to.deep.equal([['…']]);
});
});

context('truncate = 2', () => {
it('returns 2-length string with ellipsis', () => {
const rows = [['a'.repeat(100)]];
expect(truncateTableData(rows, makeConfig(rows, {
columns: {0: {truncate: 2}},
}))).to.deep.equal([['a…']]);
});
});

context('empty string', () => {
it('returns empty string', () => {
const rows = [['']];
expect(truncateTableData(rows, makeConfig(rows, {
columns: {0: {truncate: 100}},
}))).to.deep.equal([['']]);
});
});
});
});

0 comments on commit 9a344ce

Please sign in to comment.