-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: unescapeAll multiple escaped characters after each other
fix: unescapeAll multiple escaped characters after each other fix: unescapeAll multiple escaped characters after each other
- Loading branch information
Showing
5 changed files
with
90 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,69 @@ | ||
import { parse } from "@formatjs/icu-messageformat-parser"; | ||
import { escapeIcuAll } from "./escapeIcuAll"; | ||
|
||
function escapesAndIsValid(input: string) { | ||
const result = escapeIcuAll(input); | ||
parse(`{value, plural, other {${result}}}`); | ||
return result; | ||
} | ||
|
||
describe("escape icu variant", () => { | ||
it("handles parameter", () => { | ||
expect(escapeIcuAll("this {is} variant")).toEqual("this '{'is'}' variant"); | ||
expect(escapesAndIsValid("this {is} variant")).toEqual( | ||
"this '{'is'}' variant" | ||
); | ||
}); | ||
|
||
it("handles already escaped parameter", () => { | ||
expect(escapeIcuAll("this '{is}' variant")).toEqual( | ||
"this '''{'is'}'' variant" | ||
expect(escapesAndIsValid("this '{is}' variant")).toEqual( | ||
"this '''{'is'}''' variant" | ||
); | ||
}); | ||
|
||
it("handles text with apostrophe", () => { | ||
expect(escapeIcuAll("apostrophe ' is here")).toEqual( | ||
expect(escapesAndIsValid("apostrophe ' is here")).toEqual( | ||
"apostrophe ' is here" | ||
); | ||
}); | ||
|
||
it("escapes hash", () => { | ||
expect(escapeIcuAll("hash # is here")).toEqual("hash '#' is here"); | ||
expect(escapesAndIsValid("hash # is here")).toEqual("hash '#' is here"); | ||
}); | ||
|
||
it("handles double quotes", () => { | ||
expect(escapeIcuAll("this is '' not {param} escaped")).toEqual( | ||
expect(escapesAndIsValid("this is '' not {param} escaped")).toEqual( | ||
"this is '''' not '{'param'}' escaped" | ||
); | ||
}); | ||
|
||
it("handles triple quotes", () => { | ||
expect(escapeIcuAll("this is ''' actually #' escaped")).toEqual( | ||
"this is ''''' actually '#'' escaped" | ||
expect(escapesAndIsValid("unescaped ''' unescaped #' unescaped")).toEqual( | ||
"unescaped ''''' unescaped '#''' unescaped" | ||
); | ||
}); | ||
|
||
it("takes hash as escape character", () => { | ||
expect(escapeIcuAll("should be '# }' escaped")).toEqual( | ||
"should be '''#' '}'' escaped" | ||
expect(escapesAndIsValid("should be '# }' escaped")).toEqual( | ||
"should be '''#' '}''' escaped" | ||
); | ||
}); | ||
|
||
it("escapes dangling escape at the end", () => { | ||
expect(escapeIcuAll("test '")).toEqual("test ''"); | ||
}); | ||
|
||
it("doesn't take tags escapes into consideration", () => { | ||
expect(escapeIcuAll("'<'")).toEqual("'<''"); | ||
expect(escapesAndIsValid("test '")).toEqual("test ''"); | ||
}); | ||
|
||
it("handles tough escape sequence", () => { | ||
expect(escapeIcuAll("' ' '{ '' ' '' '")).toEqual( | ||
expect(escapesAndIsValid("' ' '{ '' ' '' '")).toEqual( | ||
"' ' '''{' '''' ' '''' ''" | ||
); | ||
}); | ||
|
||
it("handles case with two escapes inside escape sequence", () => { | ||
expect(escapesAndIsValid("{}")).toEqual("'{}'"); | ||
}); | ||
|
||
// tags won't work with icu parser | ||
it("doesn't take tags escapes into consideration", () => { | ||
expect(escapeIcuAll("'<'")).toEqual("'<''"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,83 @@ | ||
import { escapeIcuVariant } from "./escapeIcuVariant"; | ||
import { parse } from "@formatjs/icu-messageformat-parser"; | ||
|
||
function escapesAndIsValid(input: string) { | ||
const result = escapeIcuVariant(input); | ||
parse(`{value, plural, other {${result}}}`); | ||
return result; | ||
} | ||
|
||
describe("escape icu variant", () => { | ||
it("handles parameter", () => { | ||
expect(escapeIcuVariant("this {is} variant")).toEqual( | ||
expect(escapesAndIsValid("this {is} variant")).toEqual( | ||
"this '{is}' variant" | ||
); | ||
}); | ||
|
||
it("handles already escaped parameter", () => { | ||
expect(escapeIcuVariant("this '{is}' variant")).toEqual( | ||
expect(escapesAndIsValid("this '{is}' variant")).toEqual( | ||
"this '{is}' variant" | ||
); | ||
}); | ||
|
||
it("handles text with apostrophe", () => { | ||
expect(escapeIcuVariant("apostrophe ' is here")).toEqual( | ||
expect(escapesAndIsValid("apostrophe ' is here")).toEqual( | ||
"apostrophe ' is here" | ||
); | ||
}); | ||
|
||
it("doesn't escape hash", () => { | ||
expect(escapeIcuVariant("hash # is here")).toEqual("hash # is here"); | ||
expect(escapesAndIsValid("hash # is here")).toEqual("hash # is here"); | ||
}); | ||
|
||
it("escapes shortest necessary distance", () => { | ||
expect(escapeIcuVariant("hash {param} is here {param2} and here")).toEqual( | ||
expect(escapesAndIsValid("hash {param} is here {param2} and here")).toEqual( | ||
"hash '{param} is here {param2}' and here" | ||
); | ||
}); | ||
|
||
it("handles double quotes", () => { | ||
expect(escapeIcuVariant("this is '' not {param} escaped")).toEqual( | ||
expect(escapesAndIsValid("this is '' not {param} escaped")).toEqual( | ||
"this is '' not '{param}' escaped" | ||
); | ||
}); | ||
|
||
it("handles triple quotes", () => { | ||
expect(escapeIcuVariant("this is ''' actually #' escaped")).toEqual( | ||
expect(escapesAndIsValid("this is ''' actually #' escaped")).toEqual( | ||
"this is ''' actually #' escaped" | ||
); | ||
}); | ||
|
||
it("takes hash as escape character", () => { | ||
expect(escapeIcuVariant("should be '# }' escaped")).toEqual( | ||
expect(escapesAndIsValid("should be '# }' escaped")).toEqual( | ||
"should be '# }' escaped" | ||
); | ||
}); | ||
|
||
it("escapes dangling escape at the end", () => { | ||
expect(escapeIcuVariant("test '")).toEqual("test ''"); | ||
}); | ||
|
||
it("doesn't take tags escapes into consideration", () => { | ||
expect(escapeIcuVariant("'<'")).toEqual("'<''"); | ||
expect(escapesAndIsValid("test '")).toEqual("test ''"); | ||
}); | ||
|
||
it("handles apostrophes in escape sequence correctly", () => { | ||
expect(escapeIcuVariant("'{ '' }'")).toEqual("'{ '' }'"); | ||
expect(escapesAndIsValid("'{ '' }'")).toEqual("'{ '' }'"); | ||
}); | ||
|
||
it("escapes stuff correctly when apostrophes as parameter", () => { | ||
expect(escapeIcuVariant("{ '' }")).toEqual("'{ '' }'"); | ||
expect(escapesAndIsValid("{ '' }")).toEqual("'{ '' }'"); | ||
}); | ||
|
||
it("escapes stuff correctly when apostrophe as parameter", () => { | ||
expect(escapeIcuVariant("{ ' }")).toEqual("'{' ' '}'"); | ||
expect(escapesAndIsValid("{ ' }")).toEqual("'{' ' '}'"); | ||
}); | ||
|
||
it("handles apostrophes and dangling escape sequence at the end", () => { | ||
expect(escapeIcuVariant("# položka seznamu '{ '' }")).toEqual( | ||
expect(escapesAndIsValid("# položka seznamu '{ '' }")).toEqual( | ||
"# položka seznamu '{ '' }'" | ||
); | ||
}); | ||
|
||
// is not valid for icu parser, because that one considers tags escapable | ||
it("doesn't take tags escapes into consideration", () => { | ||
expect(escapeIcuVariant("'<'")).toEqual("'<''"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters