Skip to content

Commit 7a4347e

Browse files
committed
refactor: a bit more compact
1 parent 315687b commit 7a4347e

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

fallback/_utils.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const { Buffer, TextEncoder, TextDecoder } = globalThis
22
const haveNativeBuffer = Buffer && !Buffer.TYPED_ARRAY_SUPPORT
33
export const nativeBuffer = haveNativeBuffer ? Buffer : null
4-
export const isHermes = Boolean(globalThis.HermesInternal)
5-
export const isDeno = Boolean(globalThis.Deno)
6-
export const isLE = new Uint8Array(Uint16Array.of(258).buffer)[0] === 2
4+
export const isHermes = !!globalThis.HermesInternal
5+
export const isDeno = !!globalThis.Deno
6+
export const isLE = /* @__PURE__ */ (() => new Uint8Array(Uint16Array.of(258).buffer)[0] === 2)()
77

88
// We consider Node.js TextDecoder/TextEncoder native
99
let isNative = (x) => x && (haveNativeBuffer || `${x}`.includes('[native code]'))
@@ -17,16 +17,19 @@ export const nativeDecoder = isNative(TextDecoder)
1717
// Actually windows-1252, compatible with ascii and latin1 decoding
1818
// Beware that on non-latin1, i.e. on windows-1252, this is broken in ~all Node.js versions released
1919
// in 2025 due to a regression, so we call it Latin1 as it's usable only for that
20-
let nativeDecoderLatin1impl = null
21-
if (nativeDecoder) {
20+
const getNativeLain1 = () => {
2221
// Not all barebone engines with TextDecoder support something except utf-8, detect
23-
try {
24-
nativeDecoderLatin1impl = new TextDecoder('latin1', { ignoreBOM: true })
25-
} catch {}
22+
if (!nativeDecoder) {
23+
try {
24+
return new TextDecoder('latin1', { ignoreBOM: true })
25+
} catch {}
26+
}
27+
28+
return null
2629
}
2730

28-
export const nativeDecoderLatin1 = nativeDecoderLatin1impl
29-
export const canDecoders = Boolean(nativeDecoderLatin1impl)
31+
export const nativeDecoderLatin1 = /* @__PURE__ */ getNativeLain1()
32+
export const canDecoders = !!nativeDecoderLatin1
3033

3134
// Block Firefox < 146 specifically from using native hex/base64, as it's very slow there
3235
// Refs: https://bugzilla.mozilla.org/show_bug.cgi?id=1994067 (and linked issues), fixed in 146
@@ -51,7 +54,7 @@ function shouldSkipBuiltins() {
5154
return false // eslint-disable-line no-unreachable
5255
}
5356

54-
export const skipWeb = shouldSkipBuiltins()
57+
export const skipWeb = /* @__PURE__ */ shouldSkipBuiltins()
5558

5659
function decodePartAddition(a, start, end, m) {
5760
let o = ''

fallback/encoding.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ export class TextDecoder {
5353
const enc = normalizeEncoding(encoding)
5454
if (!enc || enc === 'replacement') throw new RangeError(E_ENCODING)
5555
define(this, 'encoding', enc)
56-
define(this, 'fatal', Boolean(options.fatal))
57-
define(this, 'ignoreBOM', Boolean(options.ignoreBOM))
56+
define(this, 'fatal', !!options.fatal)
57+
define(this, 'ignoreBOM', !!options.ignoreBOM)
5858
this.#unicode = enc === 'utf-8' || enc === 'utf-16le' || enc === 'utf-16be'
5959
this.#multibyte = !this.#unicode && isMultibyte(enc)
6060
this.#canBOM = this.#unicode && !this.ignoreBOM
@@ -66,7 +66,7 @@ export class TextDecoder {
6666

6767
decode(input, options = {}) {
6868
if (typeof options !== 'object') throw new TypeError(E_OPTIONS)
69-
const stream = Boolean(options.stream)
69+
const stream = !!options.stream
7070
let u = input === undefined ? new Uint8Array() : fromSource(input)
7171
const empty = u.length === 0 // also can't be streaming after next line
7272
if (empty && stream) return '' // no state change

utf8.node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if (Buffer.TYPED_ARRAY_SUPPORT) throw new Error('Unexpected Buffer polyfill')
99
let decoderFatal
1010
const decoderLoose = new TextDecoder('utf-8', { ignoreBOM: true })
1111
const { isWellFormed } = String.prototype
12-
const isDeno = Boolean(globalThis.Deno)
12+
const isDeno = !!globalThis.Deno
1313

1414
try {
1515
decoderFatal = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true })

0 commit comments

Comments
 (0)