Skip to content

Commit 09878c7

Browse files
committed
doc: improve some docs
1 parent d30e288 commit 09878c7

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ Encode a string to UTF-8 bytes (strict mode)
120120

121121
Throws 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

125132
Encode 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.\
131138
Prefer 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

135149
Decode UTF-8 bytes to a string (strict mode)
136150

137151
Throws 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

141158
Decode 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.\
147164
Prefer 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

151171
UTF-16 encoding/decoding
@@ -271,8 +291,9 @@ Same as:
271291
const 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

611632
Create 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

array.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export type OutputFormat = 'uint8' | 'buffer';
5050
/**
5151
* Create a view of a TypedArray in the specified format (`'uint8'` or `'buffer'`)
5252
*
53-
* Important: does not copy data, returns a view on the same underlying buffer
53+
* > [!IMPORTANT]
54+
* > Does not copy data, returns a view on the same underlying buffer
5455
*
5556
* @param arr - The input TypedArray
5657
* @param format - The desired output format (`'uint8'` or `'buffer'`)

single-byte.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ export function createSinglebyteEncoder(
9595
* const latin1toString = createSinglebyteDecoder('iso-8859-1')
9696
* ```
9797
*
98-
* Note: this is different from `new TextDecoder('iso-8859-1')` and `new TextDecoder('latin1')`, as
99-
* those alias to `new TextDecoder('windows-1252')`.
98+
* > [!NOTE]
99+
* > This is different from `new TextDecoder('iso-8859-1')` and `new TextDecoder('latin1')`, as those
100+
* > alias to `new TextDecoder('windows-1252')`.
100101
*
101102
* @param arr - The bytes to decode
102103
* @returns The decoded string

utf8.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
2323
*
2424
* Throws on invalid Unicode (unpaired surrogates)
2525
*
26+
* This is similar to the following snippet (but works on all engines):
27+
* ```js
28+
* // Strict encode, requiring Unicode codepoints to be valid
29+
* if (typeof string !== 'string' || !string.isWellFormed()) throw new TypeError()
30+
* return new TextEncoder().encode(string)
31+
* ```
32+
*
2633
* @param string - The string to encode
2734
* @param format - Output format (default: 'uint8')
2835
* @returns The encoded bytes
@@ -40,6 +47,13 @@ export function utf8fromString(string: string, format?: OutputFormat): Uint8Arra
4047
* _Such replacement is a non-injective function, is irreversable and causes collisions.\
4148
* Prefer using strict throwing methods for cryptography applications._
4249
*
50+
* This is similar to the following snippet (but works on all engines):
51+
* ```js
52+
* // Loose encode, replacing invalid Unicode codepoints with U+FFFD
53+
* if (typeof string !== 'string') throw new TypeError()
54+
* return new TextEncoder().encode(string)
55+
* ```
56+
*
4357
* @param string - The string to encode
4458
* @param format - Output format (default: 'uint8')
4559
* @returns The encoded bytes
@@ -56,6 +70,9 @@ export function utf8fromStringLoose(
5670
*
5771
* Throws on invalid UTF-8 byte sequences
5872
*
73+
* This is similar to `new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(arr)`,
74+
* but works on all engines.
75+
*
5976
* @param arr - The bytes to decode
6077
* @returns The decoded string
6178
*/
@@ -70,6 +87,9 @@ export function utf8toString(arr: Uint8Array): string;
7087
* _Such replacement is a non-injective function, is irreversable and causes collisions.\
7188
* Prefer using strict throwing methods for cryptography applications._
7289
*
90+
* This is similar to `new TextDecoder('utf-8', { ignoreBOM: true }).decode(arr)`,
91+
* but works on all engines.
92+
*
7393
* @param arr - The bytes to decode
7494
* @returns The decoded string
7595
*/

0 commit comments

Comments
 (0)