Skip to content

Commit

Permalink
review: Less greedily remove space
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwildvarian committed Nov 30, 2023
1 parent 156719c commit c26e9d6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
24 changes: 12 additions & 12 deletions src/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@ describe('Builder', () => {
' <file source-language="de" datatype="plaintext" original="ng2.template">\n' +
' <body>\n' +
' <trans-unit id="ID1" datatype="html">\n' +
' <source> source val \u00a0 x </source>\n' +
' <source> source val \u00a0 x </source>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
Expand All @@ -1465,7 +1465,7 @@ describe('Builder', () => {
' <file source-language="de" datatype="plaintext" original="ng2.template">\n' +
' <body>\n' +
' <trans-unit id="ID1" datatype="html">\n' +
' <source> source val\u00a0x </source>\n' +
' <source> source val \u00a0 x </source>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
Expand All @@ -1474,8 +1474,8 @@ describe('Builder', () => {
' <file source-language="de" target-language="fr-ch" datatype="plaintext" original="ng2.template">\n' +
' <body>\n' +
' <trans-unit id="ID1" datatype="html">\n' +
' <source> source val\u00a0x </source>\n' +
' <target state="new"> source val\u00a0x </target>\n' +
' <source> source val \u00a0 x </source>\n' +
' <target state="new"> source val \u00a0 x </target>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
Expand All @@ -1491,7 +1491,7 @@ describe('Builder', () => {
' <file source-language="de" datatype="plaintext" original="ng2.template">\n' +
' <body>\n' +
' <trans-unit id="ID1" datatype="html">\n' +
' <source> source val \u00a0 x </source>\n' +
' <source> source val \u00a0 x </source>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
Expand All @@ -1512,7 +1512,7 @@ describe('Builder', () => {
' <file source-language="de" datatype="plaintext" original="ng2.template">\n' +
' <body>\n' +
' <trans-unit id="ID1" datatype="html">\n' +
' <source>source val\u00a0x</source>\n' +
' <source>source val \u00a0 x</source>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
Expand All @@ -1521,8 +1521,8 @@ describe('Builder', () => {
' <file source-language="de" target-language="fr-ch" datatype="plaintext" original="ng2.template">\n' +
' <body>\n' +
' <trans-unit id="ID1" datatype="html">\n' +
' <source>source val\u00a0x</source>\n' +
' <target state="new">source val\u00a0x</target>\n' +
' <source>source val \u00a0 x</source>\n' +
' <target state="new">source val \u00a0 x</target>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
Expand Down Expand Up @@ -1633,7 +1633,7 @@ describe('Builder', () => {
' <file source-language="de" datatype="plaintext" original="ng2.template">\n' +
' <body>\n' +
' <trans-unit id="ID1" datatype="html">\n' +
' <source> source val \u00a0 x </source>\n' +
' <source> source val \u00a0 x </source>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
Expand All @@ -1653,7 +1653,7 @@ describe('Builder', () => {
' <file source-language="de" datatype="plaintext" original="ng2.template">\n' +
' <body>\n' +
' <trans-unit id="ID1" datatype="html">\n' +
' <source> source val\u00a0x </source>\n' +
' <source> source val \u00a0 x </source>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
Expand All @@ -1662,8 +1662,8 @@ describe('Builder', () => {
' <file source-language="de" target-language="fr-ch" datatype="plaintext" original="ng2.template">\n' +
' <body>\n' +
' <trans-unit id="ID1" datatype="html">\n' +
' <source> source val\u00a0x </source>\n' +
' <target state="new"> source val\u00a0x </target>\n' +
' <source> source val \u00a0 x </source>\n' +
' <target state="new"> source val \u00a0 x </target>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
Expand Down
11 changes: 4 additions & 7 deletions src/stringUtils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Converts sequences of space, tabs and "section breaks" like linefeeds and
// carriage returns to a single space, but leaving other space characters like
// no-break-space (U+00A0) intact.
// Refer to https://developer.mozilla.org/en-US/docs/Web/CSS/white-space-collapse#collapsing_of_white_space
export function doCollapseWhitespace<T extends string | undefined>(destSourceText: T): T {
let result = destSourceText
?.replace(/\t+/g, ' ') // Convert tabs to spaces
?.replace(/(\r\n|\n|\r)+/g, ' ') // Collapse "sequences of segment breaks" to a single space (no need to preserve line-breaks)
?.replace(/ *([^\S ]+) */g, '$1') // Remove space before or after a "space-like" character that is not a space (e.g. no-break space).
?.replace(/ {2,}/g, ' '); // Collapse sequences of spaces to a single space

return result as T;
return destSourceText?.replace(/[\n\r\t ]+/g, ' ') as T;
}

0 comments on commit c26e9d6

Please sign in to comment.