forked from ExodusOSS/bytes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti-byte.js
More file actions
19 lines (17 loc) · 708 Bytes
/
Copy pathmulti-byte.js
File metadata and controls
19 lines (17 loc) · 708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { assertUint8 } from './assert.js'
import { multibyteDecoder, multibyteEncoder } from './fallback/multi-byte.js'
export function createMultibyteDecoder(encoding, loose = false) {
const jsDecoder = multibyteDecoder(encoding, loose) // asserts
let streaming = false
return (arr, stream = false) => {
assertUint8(arr)
if (!streaming && arr.byteLength === 0) return ''
streaming = stream
return jsDecoder(arr, stream)
}
}
export function createMultibyteEncoder(encoding, { mode = 'fatal' } = {}) {
// TODO: replacement, truncate (replacement will need varying length)
if (mode !== 'fatal') throw new Error('Unsupported mode')
return multibyteEncoder(encoding) // asserts
}