@@ -120,6 +120,13 @@ Encode a string to UTF-8 bytes (strict mode)
120120
121121Throws on invalid Unicode (unpaired surrogates)
122122
123+ This is similar to the following snippet (but works on all engines):
124+ ``` js
125+ // Strict encode, requiring Unicode codepoints to be valid
126+ if (typeof string !== ' string' || ! string .isWellFormed ()) throw new TypeError ()
127+ return new TextEncoder ().encode (string)
128+ ```
129+
123130#### ` utf8fromStringLoose(string, format = 'uint8') `
124131
125132Encode a string to UTF-8 bytes (loose mode)
@@ -130,12 +137,22 @@ per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
130137_ Such replacement is a non-injective function, is irreversable and causes collisions.\
131138Prefer using strict throwing methods for cryptography applications._
132139
140+ This is similar to the following snippet (but works on all engines):
141+ ``` js
142+ // Loose encode, replacing invalid Unicode codepoints with U+FFFD
143+ if (typeof string !== ' string' ) throw new TypeError ()
144+ return new TextEncoder ().encode (string)
145+ ```
146+
133147#### ` utf8toString(arr) `
134148
135149Decode UTF-8 bytes to a string (strict mode)
136150
137151Throws on invalid UTF-8 byte sequences
138152
153+ This is similar to ` new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(arr) ` ,
154+ but works on all engines.
155+
139156#### ` utf8toStringLoose(arr) `
140157
141158Decode UTF-8 bytes to a string (loose mode)
@@ -146,6 +163,9 @@ per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
146163_ Such replacement is a non-injective function, is irreversable and causes collisions.\
147164Prefer using strict throwing methods for cryptography applications._
148165
166+ This is similar to ` new TextDecoder('utf-8', { ignoreBOM: true }).decode(arr) ` ,
167+ but works on all engines.
168+
149169### ` @exodus/bytes/utf16.js `
150170
151171UTF-16 encoding/decoding
@@ -271,8 +291,9 @@ Same as:
271291const latin1toString = createSinglebyteDecoder (' iso-8859-1' )
272292```
273293
274- Note: this is different from ` new TextDecoder('iso-8859-1') ` and ` new TextDecoder('latin1') ` , as
275- those alias to ` new TextDecoder('windows-1252') ` .
294+ > [ !NOTE]
295+ > This is different from ` new TextDecoder('iso-8859-1') ` and ` new TextDecoder('latin1') ` , as those
296+ > alias to ` new TextDecoder('windows-1252') ` .
276297
277298#### ` latin1fromString(string) `
278299
@@ -610,7 +631,8 @@ import { typedView } from '@exodus/bytes/array.js'
610631
611632Create a view of a TypedArray in the specified format (` 'uint8' ` or ` 'buffer' ` )
612633
613- Important: does not copy data, returns a view on the same underlying buffer
634+ > [ !IMPORTANT]
635+ > Does not copy data, returns a view on the same underlying buffer
614636
615637### ` @exodus/bytes/encoding.js `
616638
0 commit comments