Repository navigation
Expand file tree
/
Copy pathbom.ts
More file actions
46 lines (36 loc) · 1.48 KB
/
Copy pathbom.ts
File metadata and controls
46 lines (36 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
export * as Bom from "./bom.js"
import { Effect } from "effect"
import type { FSUtil } from "./fs-util.js"
const code = 0xfeff
const value = String.fromCharCode(code)
export function split(text: string) {
const stripped = text.replace(/^\uFEFF+/, "")
return { bom: stripped.length !== text.length, text: stripped }
}
export function join(text: string, bom: boolean) {
const stripped = split(text).text
return bom ? value + stripped : stripped
}
export function has(content: Uint8Array) {
return content[0] === 0xef && content[1] === 0xbb && content[2] === 0xbf
}
export function decodeBytes(content: Uint8Array) {
return split(decode(content))
}
export function syncBytes(content: Uint8Array, bom: boolean) {
const decoded = decode(content)
const current = split(decoded)
const canonical = bom ? value + current.text : current.text
return { text: current.text, bytes: decoded === canonical ? undefined : new TextEncoder().encode(canonical) }
}
export const readFile = Effect.fn("Bom.readFile")(function* (fs: FSUtil.Interface, filepath: string) {
return decodeBytes(yield* fs.readFile(filepath))
})
export const syncFile = Effect.fn("Bom.syncFile")(function* (fs: FSUtil.Interface, filepath: string, bom: boolean) {
const synced = syncBytes(yield* fs.readFile(filepath), bom)
if (synced.bytes) yield* fs.writeWithDirs(filepath, synced.bytes)
return synced.text
})
function decode(content: Uint8Array) {
return new TextDecoder("utf-8", { ignoreBOM: true }).decode(content)
}