Skip to content

Commit eb0de06

Browse files
authored
size: add utf16.browser impl (#62)
1 parent d6f2dba commit eb0de06

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
"/single-byte.node.js",
125125
"/utf16.js",
126126
"/utf16.d.ts",
127+
"/utf16.browser.js",
127128
"/utf16.native.js",
128129
"/utf16.node.js",
129130
"/utf8.js",
@@ -205,6 +206,7 @@
205206
"types": "./utf16.d.ts",
206207
"node": "./utf16.node.js",
207208
"react-native": "./utf16.native.js",
209+
"browser": "./utf16.browser.js",
208210
"default": "./utf16.js"
209211
},
210212
"./utf8.js": {
@@ -222,6 +224,7 @@
222224
}
223225
},
224226
"browser": {
227+
"./utf16.js": "./utf16.browser.js",
225228
"./fallback/utf8.auto.js": "./fallback/utf8.auto.browser.js"
226229
},
227230
"react-native": {

tests/utf16.noenc.test.cjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
delete globalThis.TextEncoder
2-
if (!globalThis.Deno) delete globalThis.TextDecoder // Deno path requires it
3-
require('./utf16.test.js')
1+
if (process.env.EXODUS_TEST_IS_BROWSER || globalThis.Deno) {
2+
require('node:test').test.skip('Under browsers and Deno, TextDecoder is required')
3+
} else {
4+
delete globalThis.TextEncoder
5+
delete globalThis.TextDecoder
6+
require('./utf16.test.js')
7+
}

utf16.browser.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// We trust browsers to always have correct TextDecoder for utf-16le/utf-16be with ignoreBOM without streaming
2+
3+
import { encodeApi, decodeApiDecoders } from './fallback/utf16.js'
4+
5+
export const utf16fromString = (str, format = 'uint16') => encodeApi(str, false, format)
6+
export const utf16fromStringLoose = (str, format = 'uint16') => encodeApi(str, true, format)
7+
export const utf16toString = (arr, format = 'uint16') => decodeApiDecoders(arr, false, format)
8+
export const utf16toStringLoose = (arr, format = 'uint16') => decodeApiDecoders(arr, true, format)

0 commit comments

Comments
 (0)