Skip to content

Commit

Permalink
feat: implemet normalizeWhitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
2wheeh committed Mar 11, 2024
1 parent a976835 commit 0b69f96
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ui/src/lib/utils/common/normalizeWhitespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function normalizeWhitespace(str: string) {
// replace newline and tab with a space: https://stackoverflow.com/questions/10805125
let newText = str.replace(/\r?\n|\r?\t/g, ' ');
// normalize multiple spaces: https://stackoverflow.com/questions/1981349
newText = newText.replace(/\s\s+/g, ' ');

newText = newText.trimStart();

return newText;
}
12 changes: 12 additions & 0 deletions ui/src/lib/utils/common/normarlizeWhitespace.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { normalizeWhitespace } from '@/lib/utils/common/normalizeWhitespace';

describe('normalizeWhitespace', () => {
test.each([
[' a b c ', 'a b c '],
[' a\nb\n\n\n\nc ', 'a b c '],
[' a\tb\t\t\t\tc ', 'a b c '],
[' \n\n\t a\n\t\n b\t\n\tc ', 'a b c '],
])('should normalize whitespace', (input, expected) => {
expect(normalizeWhitespace(input)).toBe(expected);
});
});

0 comments on commit 0b69f96

Please sign in to comment.