Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(utils): improve stripWhitespace utility #9668

Merged
merged 1 commit into from Aug 13, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/utils/src/lang.js
Expand Up @@ -29,7 +29,8 @@ const WHITESPACE_REPLACEMENTS = [
[/{\n{2,}/g, '{\n'], // strip start 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{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) {
Expand Down
10 changes: 5 additions & 5 deletions packages/utils/test/lang.test.js
Expand Up @@ -36,12 +36,12 @@ describe('util: lang', () => {
})

test('should strip white spaces in given argument', () => {
expect(stripWhitespace('foo')).toEqual('foo')
expect(stripWhitespace('foo\t\r\f\n')).toEqual('foo\n')
expect(stripWhitespace('foo{\n\n\n')).toEqual('foo{\n')
expect(stripWhitespace('\n\n\n\f\r\f}')).toEqual('\n\f\r\f}')
expect(stripWhitespace('foo\t\r \f\nbar')).toEqual('foo\nbar')
expect(stripWhitespace('foo{\n\n\nbar')).toEqual('foo{\nbar')
expect(stripWhitespace('foo\n\n\n\t \r\f}')).toEqual('foo\n\t \r\f}')
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', () => {
Expand Down