Skip to content

Commit

Permalink
fix: Regexp error in empty column
Browse files Browse the repository at this point in the history
  • Loading branch information
nam-hle committed Dec 1, 2021
1 parent 9158458 commit 373785c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/wrapWord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const calculateStringLengths = (input: string, size: number): Array<[Length:numb
const chunks: Array<[number, number]> = [];

// https://regex101.com/r/gY5kZ1/1
const re = new RegExp('(^.{1,' + String(size) + '}(\\s+|$))|(^.{1,' + String(size - 1) + '}(\\\\|/|_|\\.|,|;|-))');
const re = new RegExp('(^.{1,' + String(Math.max(size, 1)) + '}(\\s+|$))|(^.{1,' + String(Math.max(2, size - 1) - 1) + '}(\\\\|/|_|\\.|,|;|-))');

do {
let chunk: string;
Expand Down Expand Up @@ -38,7 +38,7 @@ export const wrapWord = (input: string, size: number): string[] => {
const result: string[] = [];

let startIndex = 0;
calculateStringLengths(input, size).forEach(([length, offset]) => {
calculateStringLengths(input, Math.max(size, 1)).forEach(([length, offset]) => {
result.push(slice(input, startIndex, startIndex + length));

startIndex += length + offset;
Expand Down
8 changes: 8 additions & 0 deletions test/wrapWord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ describe('wrapWord', () => {
expect(wrapWord(stringToRed('aaaaa'), 2)).to.deep.equal(arrayToRed(['aa', 'aa', 'a']));
});
});
context('empty string', () => {
it('should return empty string as well', () => {
expect(wrapWord('', 0)).to.deep.equal(['']);
expect(wrapWord('', 1)).to.deep.equal(['']);
expect(wrapWord('', 2)).to.deep.equal(['']);
expect(wrapWord('', 3)).to.deep.equal(['']);
});
});
context('a long word with a special character', () => {
it('cuts the word at the special character', () => {
expect(wrapWord('aaa\\bbb', 5)).to.deep.equal(['aaa\\', 'bbb']);
Expand Down

0 comments on commit 373785c

Please sign in to comment.