Skip to content

Commit ebaab25

Browse files
committed
refactor: cleanup utf16.node.js
1 parent 47cf6a2 commit ebaab25

1 file changed

Lines changed: 2 additions & 3 deletions

File tree

utf16.node.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { E_STRICT } from './fallback/utf16.js'
33

44
if (Buffer.TYPED_ARRAY_SUPPORT) throw new Error('Unexpected Buffer polyfill')
55

6+
const { TextDecoder } = globalThis
67
const { isWellFormed, toWellFormed } = String.prototype
78
const to8 = (a) => new Uint8Array(a.buffer, a.byteOffset, a.byteLength)
89

@@ -62,18 +63,16 @@ function decodeNode(input, loose = false, format = 'uint16') {
6263
}
6364

6465
function decodeDecoder(input, loose = false, format = 'uint16') {
65-
let encoding
6666
if (format === 'uint16') {
6767
if (!(input instanceof Uint16Array)) throw new TypeError('Expected an Uint16Array')
68-
encoding = isLE ? 'utf-16le' : 'utf-16be'
6968
} else if (format === 'uint8-le' || format === 'uint8-be') {
7069
if (!(input instanceof Uint8Array)) throw new TypeError('Expected an Uint8Array')
7170
if (input.byteLength % 2 !== 0) throw new TypeError('Expected even number of bytes')
72-
encoding = format === 'uint8-le' ? 'utf-16le' : 'utf-16be'
7371
} else {
7472
throw new TypeError('Unknown format')
7573
}
7674

75+
const encoding = format === 'uint8-le' || format === 'uint16' && isLE ? 'utf-16le' : 'utf-16be'
7776
return new TextDecoder(encoding, { ignoreBOM: true, fatal: !loose }).decode(input) // TODO: cache decoder?
7877
}
7978

0 commit comments

Comments
 (0)