Skip to content

Commit 3dcc64b

Browse files
committed
doc: improve documentation
1 parent 3762945 commit 3dcc64b

9 files changed

Lines changed: 375 additions & 114 deletions

File tree

README.md

Lines changed: 150 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -99,31 +99,68 @@ See [the list of encodings](https://encoding.spec.whatwg.org/#names-and-labels).
9999

100100
### `@exodus/bytes/utf8.js`
101101

102+
UTF-8 encoding/decoding
103+
102104
```js
103105
import { utf8fromString, utf8toString } from '@exodus/bytes/utf8.js'
104106

105107
// loose
106108
import { utf8fromStringLoose, utf8toStringLoose } from '@exodus/bytes/utf8.js'
107109
```
108110

109-
##### `utf8fromString(str, format = 'uint8')`
110-
##### `utf8fromStringLoose(str, format = 'uint8')`
111-
##### `utf8toString(arr)`
112-
##### `utf8toStringLoose(arr)`
111+
_These methods by design encode/decode BOM (codepoint `U+FEFF` Byte Order Mark) as-is._\
112+
_If you need BOM handling or detection, use `@exodus/bytes/encoding.js`_
113+
114+
#### `utf8fromString(string, format = 'uint8')`
115+
116+
Encode a string to UTF-8 bytes (strict mode)
117+
118+
Throws on invalid Unicode (unpaired surrogates)
119+
120+
#### `utf8fromStringLoose(string, format = 'uint8')`
121+
122+
Encode a string to UTF-8 bytes (loose mode)
123+
124+
Replaces invalid Unicode (unpaired surrogates) with replacement codepoints `U+FFFD`
125+
per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
126+
127+
_Such replacement is a non-injective function, is irreversable and causes collisions.\
128+
Prefer using strict throwing methods for cryptography applications._
129+
130+
#### `utf8toString(arr)`
131+
132+
Decode UTF-8 bytes to a string (strict mode)
133+
134+
Throws on invalid UTF-8 byte sequences
135+
136+
#### `utf8toStringLoose(arr)`
137+
138+
Decode UTF-8 bytes to a string (loose mode)
139+
140+
Replaces invalid UTF-8 byte sequences with replacement codepoints `U+FFFD`
141+
per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
142+
143+
_Such replacement is a non-injective function, is irreversable and causes collisions.\
144+
Prefer using strict throwing methods for cryptography applications._
113145

114146
### `@exodus/bytes/utf16.js`
115147

148+
UTF-16 encoding/decoding
149+
150+
_These methods by design encode/decode BOM (codepoint `U+FEFF` Byte Order Mark) as-is._
151+
_If you need BOM handling or detection, use `@exodus/bytes/encoding.js`_
152+
116153
```js
117154
import { utf16fromString, utf16toString } from '@exodus/bytes/utf16.js'
118155

119156
// loose
120157
import { utf16fromStringLoose, utf16toStringLoose } from '@exodus/bytes/utf16.js'
121158
```
122159

123-
##### `utf16fromString(str, format = 'uint16')`
124-
##### `utf16fromStringLoose(str, format = 'uint16')`
125-
##### `utf16toString(arr, 'uint16')`
126-
##### `utf16toStringLoose(arr, 'uint16')`
160+
#### `utf16fromString(string, format = 'uint16')`
161+
#### `utf16fromStringLoose(string, format = 'uint16')`
162+
#### `utf16toString(arr, 'uint16')`
163+
#### `utf16toStringLoose(arr, 'uint16')`
127164

128165
### `@exodus/bytes/single-byte.js`
129166

@@ -174,13 +211,13 @@ corresponding [unicode.org encodings](https://unicode.org/Public/MAPPINGS/VENDOR
174211
they encode/decode all the old valid (non-replacement) strings / byte sequences identically, but can also support
175212
a wider range of inputs.
176213

177-
##### `createSinglebyteDecoder(encoding, loose = false)`
214+
#### `createSinglebyteDecoder(encoding, loose = false)`
178215

179216
Create a decoder for a supported one-byte `encoding`, given its lowercased name `encoding`.
180217

181218
Returns a function `decode(arr)` that decodes bytes to a string.
182219

183-
##### `createSinglebyteEncoder(encoding, { mode = 'fatal' })`
220+
#### `createSinglebyteEncoder(encoding, { mode = 'fatal' })`
184221

185222
Create an encoder for a supported one-byte `encoding`, given its lowercased name `encoding`.
186223

@@ -191,7 +228,7 @@ not be encoded in the target encoding.
191228

192229
In `'replacement'` mode, all unmapped codepoints and unpaired surrogates will be replaced with `U+3F` (codepoint for '?').
193230

194-
##### `latin1toString(arr)`
231+
#### `latin1toString(arr)`
195232

196233
Decode `iso-8859-1` bytes to a string.
197234

@@ -205,7 +242,7 @@ const latin1toString = createSinglebyteDecoder('iso-8859-1')
205242
Note: this is different from `new TextDecoder('iso-8859-1')` and `new TextDecoder('latin1')`, as
206243
those alias to `new TextDecoder('windows-1252')`.
207244

208-
##### `latin1fromString(string)`
245+
#### `latin1fromString(string)`
209246

210247
Encode a string to `iso-8859-1` bytes.
211248

@@ -216,7 +253,7 @@ Same as:
216253
const latin1fromString = createSinglebyteEncoder('iso-8859-1', { mode: 'fatal' })
217254
```
218255

219-
##### `windows1252toString(arr)`
256+
#### `windows1252toString(arr)`
220257

221258
Decode `windows-1252` bytes to a string.
222259

@@ -227,7 +264,7 @@ Same as:
227264
const windows1252toString = createSinglebyteDecoder('windows-1252')
228265
```
229266

230-
##### `windows1252fromString(string)`
267+
#### `windows1252fromString(string)`
231268

232269
Encode a string to `windows-1252` bytes.
233270

@@ -253,7 +290,7 @@ Decode the legacy multi-byte encodings according to the [Encoding standard](http
253290
Supports all legacy multi-byte encodings listed in the standard:
254291
`gbk`, `gb18030`, `big5`, `euc-jp`, `iso-2022-jp`, `shift_jis`, `euc-kr`.
255292

256-
##### `createMultibyteDecoder(encoding, loose = false)`
293+
#### `createMultibyteDecoder(encoding, loose = false)`
257294

258295
Create a decoder for a supported legacy multi-byte `encoding`, given its lowercased name `encoding`.
259296

@@ -267,67 +304,98 @@ That function will have state while `stream = true` is used.
267304
import { fromBigInt, toBigInt } from '@exodus/bytes/bigint.js'
268305
```
269306

270-
##### `fromBigInt(bigint, { length, format = 'uint8' })`
271-
##### `toBigInt(arr)`
307+
#### `fromBigInt(bigint, { length, format = 'uint8' })`
308+
#### `toBigInt(arr)`
272309

273310
### `@exodus/bytes/hex.js`
274311

275-
Implements Base16 from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648) (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
312+
Implements Base16 from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
313+
(no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
276314

277315
```js
278316
import { fromHex, toHex } from '@exodus/bytes/hex.js'
279317
```
280318

281-
##### `fromHex(string)`
282-
##### `toHex(arr)`
319+
#### `fromHex(string, format = 'uint8')`
320+
321+
Decode a hex string to bytes
322+
323+
Unlike `Buffer.from()`, throws on invalid input
324+
325+
#### `toHex(arr)`
326+
327+
Encode a `Uint8Array` to a lowercase hex string
283328

284329
### `@exodus/bytes/base64.js`
285330

286-
Implements Base64 from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648) (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
331+
Implements Base64 from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
332+
(no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
287333

288334
```js
289335
import { fromBase64, toBase64 } from '@exodus/bytes/base64.js'
290336
import { fromBase64url, toBase64url } from '@exodus/bytes/base64.js'
291337
import { fromBase64any } from '@exodus/bytes/base64.js'
292338
```
293339

294-
##### `fromBase64(str, { format = 'uint8', padding = 'both' })`
295-
##### `fromBase64url(str, { format = 'uint8', padding = false })`
296-
##### `fromBase64any(str, { format = 'uint8', padding = 'both' })`
297-
##### `toBase64(arr, { padding = true })`
298-
##### `toBase64url(arr, { padding = false })`
340+
#### `fromBase64(string, { format = 'uint8', padding = 'both' })`
341+
342+
Decode a base64 string to bytes
343+
344+
Operates in strict mode for last chunk, does not allow whitespace
345+
346+
#### `fromBase64url(string, { format = 'uint8', padding = false })`
347+
348+
Decode a base64url string to bytes
349+
350+
Operates in strict mode for last chunk, does not allow whitespace
351+
352+
#### `fromBase64any(string, { format = 'uint8', padding = 'both' })`
353+
354+
Decode either base64 or base64url string to bytes
355+
356+
Automatically detects the variant based on characters present
357+
358+
#### `toBase64(arr, { padding = true })`
359+
360+
Encode a `Uint8Array` to a base64 string (RFC 4648)
361+
362+
#### `toBase64url(arr, { padding = false })`
363+
364+
Encode a `Uint8Array` to a base64url string (RFC 4648)
299365

300366
### `@exodus/bytes/base32.js`
301367

302-
Implements Base32 from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648) (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
368+
Implements Base32 from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
369+
(no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
303370

304371
```js
305372
import { fromBase32, toBase32 } from '@exodus/bytes/base32.js'
306373
import { fromBase32hex, toBase32hex } from '@exodus/bytes/base32.js'
307374
```
308375

309-
##### `fromBase32(str, { format = 'uint8', padding = 'both' })`
310-
##### `fromBase32hex(str, { format = 'uint8', padding = 'both' })`
311-
##### `toBase32(arr, { padding = false })`
312-
##### `toBase32hex(arr, { padding = false })`
376+
#### `fromBase32(string, { format = 'uint8', padding = 'both' })`
377+
#### `fromBase32hex(string, { format = 'uint8', padding = 'both' })`
378+
#### `toBase32(arr, { padding = false })`
379+
#### `toBase32hex(arr, { padding = false })`
313380

314381
### `@exodus/bytes/bech32.js`
315382

316-
Implements [BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#specification) and [BIP-0350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki#specification).
383+
Implements [BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#specification)
384+
and [BIP-0350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki#specification).
317385

318386
```js
319387
import { fromBech32, toBech32 } from '@exodus/bytes/bech32.js'
320388
import { fromBech32m, toBech32m } from '@exodus/bytes/bech32.js'
321389
import { getPrefix } from '@exodus/bytes/bech32.js'
322390
```
323391

324-
##### `getPrefix(str, limit = 90)`
392+
#### `getPrefix(string, limit = 90)`
325393

326-
##### `fromBech32(str, limit = 90)`
327-
##### `toBech32(prefix, bytes, limit = 90)`
394+
#### `fromBech32(string, limit = 90)`
395+
#### `toBech32(prefix, bytes, limit = 90)`
328396

329-
##### `fromBech32m(str, limit = 90)`
330-
##### `toBech32m(prefix, bytes, limit = 90)`
397+
#### `fromBech32m(string, limit = 90)`
398+
#### `toBech32m(prefix, bytes, limit = 90)`
331399

332400
### `@exodus/bytes/base58.js`
333401

@@ -336,11 +404,11 @@ import { fromBase58, toBase58 } from '@exodus/bytes/base58.js'
336404
import { fromBase58xrp, toBase58xrp } from '@exodus/bytes/base58.js'
337405
```
338406

339-
##### `fromBase58(str, format = 'uint8')`
340-
##### `toBase58(arr)`
407+
#### `fromBase58(string, format = 'uint8')`
408+
#### `toBase58(arr)`
341409

342-
##### `fromBase58xrp(str, format = 'uint8')`
343-
##### `toBase58xrp(arr)`
410+
#### `fromBase58xrp(string, format = 'uint8')`
411+
#### `toBase58xrp(arr)`
344412

345413
### `@exodus/bytes/base58check.js`
346414

@@ -352,11 +420,11 @@ import { makeBase58check } from '@exodus/bytes/base58check.js'
352420

353421
On non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed.
354422

355-
##### `async fromBase58check(str, format = 'uint8')`
356-
##### `async toBase58check(arr)`
357-
##### `fromBase58checkSync(str, format = 'uint8')`
358-
##### `toBase58checkSync(arr)`
359-
##### `makeBase58check(hashAlgo, hashAlgoSync)`
423+
#### `async fromBase58check(string, format = 'uint8')`
424+
#### `async toBase58check(arr)`
425+
#### `fromBase58checkSync(string, format = 'uint8')`
426+
#### `toBase58checkSync(arr)`
427+
#### `makeBase58check(hashAlgo, hashAlgoSync)`
360428

361429
### `@exodus/bytes/wif.js`
362430

@@ -367,10 +435,24 @@ import { fromWifStringSync, toWifStringSync } from '@exodus/bytes/wif.js'
367435

368436
On non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed.
369437

370-
##### `async fromWifString(string, version)`
371-
##### `fromWifStringSync(string, version)`
372-
##### `async toWifString({ version, privateKey, compressed })`
373-
##### `toWifStringSync({ version, privateKey, compressed })`
438+
#### `async fromWifString(string, version)`
439+
#### `fromWifStringSync(string, version)`
440+
#### `async toWifString({ version, privateKey, compressed })`
441+
#### `toWifStringSync({ version, privateKey, compressed })`
442+
443+
### `@exodus/bytes/array.js`
444+
445+
TypedArray utils and conversions.
446+
447+
```js
448+
import { typedView } from '@exodus/bytes/array.js'
449+
```
450+
451+
#### `typedView(arr, format = 'uint8')`
452+
453+
Create a view of a TypedArray in the specified format (`'uint8'` or `'buffer'`)
454+
455+
Important: does not copy data, returns a view on the same underlying buffer
374456

375457
### `@exodus/bytes/encoding.js`
376458

@@ -393,29 +475,37 @@ some [hooks](https://encoding.spec.whatwg.org/#specification-hooks) (see below).
393475

394476
[TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder) implementation/polyfill.
395477

478+
Decode bytes to strings according to [WHATWG Encoding](https://encoding.spec.whatwg.org) specification.
479+
396480
#### `new TextEncoder()`
397481

398482
[TextEncoder](https://encoding.spec.whatwg.org/#interface-textencoder) implementation/polyfill.
399483

484+
Encode strings to UTF-8 bytes according to [WHATWG Encoding](https://encoding.spec.whatwg.org) specification.
485+
400486
#### `new TextDecoderStream(label = 'utf-8', { fatal = false, ignoreBOM = false })`
401487

402488
[TextDecoderStream](https://encoding.spec.whatwg.org/#interface-textdecoderstream) implementation/polyfill.
403489

490+
A [Streams](https://streams.spec.whatwg.org/) wrapper for `TextDecoder`.
491+
404492
Requires [Streams](https://streams.spec.whatwg.org/) to be either supported by the platform or
405493
[polyfilled](https://npmjs.com/package/web-streams-polyfill).
406494

407495
#### `new TextEncoderStream()`
408496

409497
[TextEncoderStream](https://encoding.spec.whatwg.org/#interface-textencoderstream) implementation/polyfill.
410498

499+
A [Streams](https://streams.spec.whatwg.org/) wrapper for `TextEncoder`.
500+
411501
Requires [Streams](https://streams.spec.whatwg.org/) to be either supported by the platform or
412502
[polyfilled](https://npmjs.com/package/web-streams-polyfill).
413503

414504
#### `labelToName(label)`
415505

416506
Implements [get an encoding from a string `label`](https://encoding.spec.whatwg.org/#concept-encoding-get).
417507

418-
Converts an encoding [label](https://encoding.spec.whatwg.org/#names-and-labels) to its name,
508+
Convert an encoding [label](https://encoding.spec.whatwg.org/#names-and-labels) to its name,
419509
as a case-sensitive string.
420510

421511
If an encoding with that label does not exist, returns `null`.
@@ -424,7 +514,7 @@ All encoding names are also valid labels for corresponding encodings.
424514

425515
#### `normalizeEncoding(label)`
426516

427-
Converts an encoding [label](https://encoding.spec.whatwg.org/#names-and-labels) to its name,
517+
Convert an encoding [label](https://encoding.spec.whatwg.org/#names-and-labels) to its name,
428518
as an ASCII-lowercased string.
429519

430520
If an encoding with that label does not exist, returns `null`.
@@ -447,10 +537,10 @@ All encoding names are also valid labels for corresponding encodings.
447537
Implements [BOM sniff](https://encoding.spec.whatwg.org/#bom-sniff) legacy hook.
448538
449539
Given a `TypedArray` or an `ArrayBuffer` instance `input`, returns either of:
450-
* `'utf-8'`, if `input` starts with UTF-8 byte order mark.
451-
* `'utf-16le'`, if `input` starts with UTF-16LE byte order mark.
452-
* `'utf-16be'`, if `input` starts with UTF-16BE byte order mark.
453-
* `null` otherwise.
540+
- `'utf-8'`, if `input` starts with UTF-8 byte order mark.
541+
- `'utf-16le'`, if `input` starts with UTF-16LE byte order mark.
542+
- `'utf-16be'`, if `input` starts with UTF-16BE byte order mark.
543+
- `null` otherwise.
454544
455545
#### `legacyHookDecode(input, fallbackEncoding = 'utf-8')`
456546
@@ -463,10 +553,10 @@ decodes the `input` using that encoding, skipping BOM if it was present.
463553
464554
Notes:
465555
466-
* BOM-sniffed encoding takes precedence over `fallbackEncoding` option per spec.
467-
Use with care.
468-
* Always operates in non-fatal [mode](https://encoding.spec.whatwg.org/#textdecoder-error-mode),
469-
aka replacement. It can convert different byte sequences to equal strings.
556+
- BOM-sniffed encoding takes precedence over `fallbackEncoding` option per spec.
557+
Use with care.
558+
- Always operates in non-fatal [mode](https://encoding.spec.whatwg.org/#textdecoder-error-mode),
559+
aka replacement. It can convert different byte sequences to equal strings.
470560
471561
This method is similar to the following code, except that it doesn't support encoding labels and
472562
only expects lowercased encoding name:

0 commit comments

Comments
 (0)