chore(utils): improve stripWhitespace utility (#9668)

This commit is contained in:
mrazauskas 2021-08-13 14:22:16 +03:00 committed by GitHub
parent 17bbb21b5e
commit 0145578493
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 6 deletions

View File

@ -29,7 +29,8 @@ const WHITESPACE_REPLACEMENTS = [
[/{\n{2,}/g, '{\n'], // strip start padding from blocks [/{\n{2,}/g, '{\n'], // strip start padding from blocks
[/\n{2,}([ \t\f\r]*})/g, '\n$1'], // strip end padding from blocks [/\n{2,}([ \t\f\r]*})/g, '\n$1'], // strip end padding from blocks
[/\n{3,}/g, '\n\n'], // strip multiple blank lines (1 allowed) [/\n{3,}/g, '\n\n'], // strip multiple blank lines (1 allowed)
[/\n{2,}$/g, '\n'] // strip blank lines EOF (0 allowed) [/^\n+/, ''], // strip blank lines at the beginning of a string
[/\n{2,}$/, '\n'] // strip blank lines at the end of a string
] ]
export const stripWhitespace = function stripWhitespace (string) { export const stripWhitespace = function stripWhitespace (string) {

View File

@ -36,12 +36,12 @@ describe('util: lang', () => {
}) })
test('should strip white spaces in given argument', () => { test('should strip white spaces in given argument', () => {
expect(stripWhitespace('foo')).toEqual('foo') expect(stripWhitespace('foo\t\r \f\nbar')).toEqual('foo\nbar')
expect(stripWhitespace('foo\t\r\f\n')).toEqual('foo\n') expect(stripWhitespace('foo{\n\n\nbar')).toEqual('foo{\nbar')
expect(stripWhitespace('foo{\n\n\n')).toEqual('foo{\n') expect(stripWhitespace('foo\n\n\n\t \r\f}')).toEqual('foo\n\t \r\f}')
expect(stripWhitespace('\n\n\n\f\r\f}')).toEqual('\n\f\r\f}')
expect(stripWhitespace('foo\n\n\nbar')).toEqual('foo\n\nbar') expect(stripWhitespace('foo\n\n\nbar')).toEqual('foo\n\nbar')
expect(stripWhitespace('foo\n\n\n')).toEqual('foo\n') expect(stripWhitespace('\n\nfoo\n')).toEqual('foo\n')
expect(stripWhitespace('foo\n\n')).toEqual('foo\n')
}) })
test('should encode html', () => { test('should encode html', () => {