mirror of
https://gitee.com/zhangsken/opencode.git
synced 2026-10-16 10:30:01 +08:00
refactor(util): reuse BOM-stripped text (#46078)
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import { expect, test } from "bun:test"
|
||||
import { Bom } from "./bom.js"
|
||||
|
||||
test.each([
|
||||
{ prefix: "", bom: false, expected: undefined },
|
||||
{ prefix: "", bom: true, expected: "\uFEFF" },
|
||||
{ prefix: "\uFEFF", bom: false, expected: "" },
|
||||
{ prefix: "\uFEFF", bom: true, expected: undefined },
|
||||
{ prefix: "\uFEFF\uFEFF", bom: false, expected: "" },
|
||||
{ prefix: "\uFEFF\uFEFF", bom: true, expected: "\uFEFF" },
|
||||
])("syncBytes(%j)", (row) => {
|
||||
const encoder = new TextEncoder()
|
||||
const text = "a\uFEFF\u00e9"
|
||||
const input = encoder.encode(row.prefix + text)
|
||||
|
||||
expect(Bom.syncBytes(input, row.bom)).toEqual({
|
||||
text,
|
||||
bytes: row.expected === undefined ? undefined : encoder.encode(row.expected + text),
|
||||
})
|
||||
expect(input).toEqual(encoder.encode(row.prefix + text))
|
||||
})
|
||||
@@ -27,7 +27,7 @@ export function decodeBytes(content: Uint8Array) {
|
||||
export function syncBytes(content: Uint8Array, bom: boolean) {
|
||||
const decoded = decode(content)
|
||||
const current = split(decoded)
|
||||
const canonical = join(current.text, bom)
|
||||
const canonical = bom ? value + current.text : current.text
|
||||
return { text: current.text, bytes: decoded === canonical ? undefined : new TextEncoder().encode(canonical) }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user