Skip to content

Commit 170cff6

Browse files
committed
Replace negative lookbehind with replacer function for backwards compatibility with Safari.
1 parent fa9acd7 commit 170cff6

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

lib/util/normalizeValue.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@ import test from "ava"
22

33
import normalize from "./normalizeValue"
44

5-
const expected = "First line.\r\nSecond line.\r\nThird line."
5+
const expected = "\r\nFirst line.\r\nSecond line.\r\nThird line.\r\n"
66

77
test("Replaces all CR not followed by LF with CRLF", t => {
8-
const actual = normalize("First line.\rSecond line.\rThird line.")
8+
const actual = normalize("\rFirst line.\rSecond line.\rThird line.\r")
99

1010
t.is(actual, expected)
1111
})
1212

1313
test("Replaces all LF not predicessed by CR with CRLF", t => {
14-
const actual = normalize("First line.\nSecond line.\nThird line.")
14+
const actual = normalize("\nFirst line.\nSecond line.\nThird line.\n")
15+
16+
t.is(actual, expected)
17+
})
18+
19+
test("Replaces all CR or LF characters in a string", t => {
20+
const actual = normalize("\rFirst line.\nSecond line.\rThird line.\n")
1521

1622
t.is(actual, expected)
1723
})

lib/util/normalizeValue.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
* @api private
99
*/
1010
const normalizeValue = (value: unknown): string => String(value)
11-
.replace(/\r(?!\n)|(?<!\r)\n/g, "\r\n")
11+
.replace(/\r|\n/g, (match: string, i: number, str: string) => {
12+
if (
13+
(match === "\r" && str[i + 1] !== "\n")
14+
|| (match === "\n" && str[i - 1] !== "\r")
15+
) {
16+
return "\r\n"
17+
}
18+
19+
return match
20+
})
1221

1322
export default normalizeValue

0 commit comments

Comments
 (0)