Skip to content

Commit 373785c

Browse files
authored
fix: Regexp error in empty column
1 parent 9158458 commit 373785c

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

src/wrapWord.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const calculateStringLengths = (input: string, size: number): Array<[Length:numb
77
const chunks: Array<[number, number]> = [];
88

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

1212
do {
1313
let chunk: string;
@@ -38,7 +38,7 @@ export const wrapWord = (input: string, size: number): string[] => {
3838
const result: string[] = [];
3939

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

4444
startIndex += length + offset;

test/wrapWord.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ describe('wrapWord', () => {
2323
expect(wrapWord(stringToRed('aaaaa'), 2)).to.deep.equal(arrayToRed(['aa', 'aa', 'a']));
2424
});
2525
});
26+
context('empty string', () => {
27+
it('should return empty string as well', () => {
28+
expect(wrapWord('', 0)).to.deep.equal(['']);
29+
expect(wrapWord('', 1)).to.deep.equal(['']);
30+
expect(wrapWord('', 2)).to.deep.equal(['']);
31+
expect(wrapWord('', 3)).to.deep.equal(['']);
32+
});
33+
});
2634
context('a long word with a special character', () => {
2735
it('cuts the word at the special character', () => {
2836
expect(wrapWord('aaa\\bbb', 5)).to.deep.equal(['aaa\\', 'bbb']);

0 commit comments

Comments
 (0)