Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/on-pull-request.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test successful build of base-util
name: Run tests and test successful build of base-util

on: pull_request

Expand All @@ -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
19 changes: 14 additions & 5 deletions src/StringUtil/StringUtil.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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', () => {
Expand Down
14 changes: 6 additions & 8 deletions src/StringUtil/StringUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down