diff --git a/.github/workflows/on-pull-request.yml b/.github/workflows/on-pull-request.yml index 9d6a7bf50..dedd9f42d 100644 --- a/.github/workflows/on-pull-request.yml +++ b/.github/workflows/on-pull-request.yml @@ -1,4 +1,4 @@ -name: Test successful build of base-util +name: Run tests and test successful build of base-util on: pull_request @@ -25,7 +25,7 @@ jobs: ${{ runner.OS }}- - name: Install dependencies ⏬ - run: npm install + run: npm ci - name: Build artifacts 🏗️ - run: npm run build + run: npm run build # This runs the tests as well diff --git a/src/StringUtil/StringUtil.spec.ts b/src/StringUtil/StringUtil.spec.ts index d828f7c58..3d34dcea1 100644 --- a/src/StringUtil/StringUtil.spec.ts +++ b/src/StringUtil/StringUtil.spec.ts @@ -77,17 +77,17 @@ describe('StringUtil', () => { }); it('splits hard on the width length and wraps the string into given format if its length' + - 'is greater as provided line width', () => { - const inputString = 'MyLengthIsGreaterAs20'; + 'is greater than the provided line width', () => { + const inputString = 'MyLengthIsGreaterThan20'; const width = 10; const spaceReplacer = '\n'; const outputString = StringUtil.stringDivider(inputString, width, spaceReplacer); - const expectedString = 'MyLengthIsG-\nreaterAs20'; + const expectedString = 'MyLengthIs-\nGreaterTha-\nn20'; expect(outputString).toBe(expectedString); }); - it('splits on whitespace and wraps the string into given format if its length is greater as' + - 'provided line width', () => { + it('splits on whitespace and wraps the string into given format if its length is greater than' + + 'the provided line width', () => { const inputString = 'I should be split on whitespace'; const width = 11; const spaceReplacer = '\n'; @@ -104,6 +104,15 @@ describe('StringUtil', () => { const expectedString = 'abc-\ndef-\nghi-\njkl-\nmno-\npqr'; expect(outputString).toBe(expectedString); }); + + it('splits before whitespace if needed', () => { + const inputString = 'abcdef ghijkl mnopqr'; + const width = 5; + const spaceReplacer = '\n'; + const outputString = StringUtil.stringDivider(inputString, width, spaceReplacer); + const expectedString = 'abcde-\nf\nghijk-\nl\nmnopq-\nr'; + expect(outputString).toBe(expectedString); + }); }); describe('#stripHTMLTags', () => { diff --git a/src/StringUtil/StringUtil.ts b/src/StringUtil/StringUtil.ts index 1b3ff4119..65f4eaa8f 100644 --- a/src/StringUtil/StringUtil.ts +++ b/src/StringUtil/StringUtil.ts @@ -62,18 +62,18 @@ class StringUtil { * @return {string} The 'wrapped' string. */ static stringDivider(str: string, width: number, spaceReplacer: string): string { - - let startIndex = 0; - let stopIndex = width; - if (str.length > width) { let p = width; let left; let right; + + // we start right *after* the last character that could appear in a line and go to the left + // and check if there is whitespace or - somewhere while (p > 0 && (str[p] !== ' ' && str[p] !== '-')) { p--; } if (p > 0) { + // split on whitespace or - if (str.substring(p, p + 1) === '-') { left = str.substring(0, p + 1); } else { @@ -85,10 +85,8 @@ class StringUtil { } else { // no whitespace or - found, // splitting hard on the width length - left = str.substring(startIndex, stopIndex + 1) + '-'; - right = str.substring(stopIndex + 1); - startIndex = stopIndex; - stopIndex += width; + left = str.substring(0, width) + '-'; + right = str.substring(width); return left + spaceReplacer + StringUtil.stringDivider( right, width, spaceReplacer); }