From 0145578493a123ee0ff0e9adc4921582d456d366 Mon Sep 17 00:00:00 2001 From: mrazauskas <72159681+mrazauskas@users.noreply.github.com> Date: Fri, 13 Aug 2021 14:22:16 +0300 Subject: [PATCH] chore(utils): improve `stripWhitespace` utility (#9668) --- packages/utils/src/lang.js | 3 ++- packages/utils/test/lang.test.js | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/utils/src/lang.js b/packages/utils/src/lang.js index e0cdd6275f..79c5d20e80 100644 --- a/packages/utils/src/lang.js +++ b/packages/utils/src/lang.js @@ -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) { diff --git a/packages/utils/test/lang.test.js b/packages/utils/test/lang.test.js index 1f39ca530d..eb83324cf0 100644 --- a/packages/utils/test/lang.test.js +++ b/packages/utils/test/lang.test.js @@ -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', () => {