Skip to content

Commit

Permalink
Reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Nov 4, 2024
1 parent 5330019 commit 5be192a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ browser-compat: javascript.builtins.Uint8Array.fromBase64

The **`Uint8Array.fromBase64()`** static method creates a new {{jsxref("Uint8Array")}} object from a [base64](/en-US/docs/Glossary/Base64)-encoded string.

For the general concepts around base64 strings, see the [base64](/en-US/docs/Glossary/Base64) glossary entry. This method should be preferred over {{domxref("Window.atob()")}} because it results in a byte array, which is easier to work with than a string containing raw bytes. If you already have an array buffer allocated and you want to populate it, use the instance method {{jsxref("Uint8Array.prototype.setFromBase64()")}} instead.
This method should be preferred over {{domxref("Window.atob()")}} because it results in a byte array, which is easier to work with than a string containing raw bytes, unless your decoded binary data is actually intended to be ASCII text. If you already have an array buffer allocated and you want to populate it, use the instance method {{jsxref("Uint8Array.prototype.setFromBase64()")}} instead.

## Syntax

Expand All @@ -21,7 +21,7 @@ Uint8Array.fromBase64(string, options)
### Parameters

- `string`
- : A base64-encoded string to convert to a `Uint8Array`. The string must only contain characters in the base64 alphabet, which includes A–Z, a–z, 0–9, and two special characters, which are either `+` and `/` (if using `alphabet: "base64"` in `options`) or `-` and `_` (if using `alphabet: "base64url"` in `options`). Any ASCII white space characters within the string are ignored.
- : A base64-encoded string encoding bytes to convert to a `Uint8Array`. The string must only contain characters in the base64 alphabet, which includes A–Z, a–z, 0–9, and two special characters, which are either `+` and `/` (if using `alphabet: "base64"` in `options`) or `-` and `_` (if using `alphabet: "base64url"` in `options`). It may have padding `=` characters at the end. Any ASCII white space characters within the string are ignored.
- `options` {{optional_inline}}
- : An object customizing the base64 string interpretation process. It can contain the following properties:
- `alphabet` {{optional_inline}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ Uint8Array.fromHex(string)
### Parameters

- `string`
- : A hexadecimal string to convert to a `Uint8Array`. The string must only contain characters in the hexadecimal alphabet, which includes 0–9 and A–F (case-insensitive). It must have an even number of characters. Unlike {{jsxref("Uint8Array.fromBase64()")}}, the input cannot contain whitespace.

- : A hexadecimal string encoding bytes to convert to a `Uint8Array`. The string must:

- Have an even number of characters because two characters encode one byte.
- Only contain characters in the hexadecimal alphabet, which includes 0–9 and A–F (case-insensitive).
- Not contain whitespace (unlike {{jsxref("Uint8Array.prototype.setFromBase64()")}}).

### Return value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The **`Uint8Array`** typed array represents an array of 8-bit unsigned integers.
`Uint8Array` is currently the only `TypedArray` subclass that has additional methods compared to other typed arrays. Because of its nature as a generic byte array, it is the most suitable for working with arbitrary binary data. It supports two sets of methods for the creation, serialization, and modification of `Uint8Array` data to/from hex strings and base64 strings.

- {{jsxref("Uint8Array.fromBase64()")}}, {{jsxref("Uint8Array.prototype.toBase64()")}}, and {{jsxref("Uint8Array.prototype.setFromBase64()")}} for working with [base64](/en-US/docs/Glossary/Base64) strings, where 3 bytes are encoded by 4 characters that are either 0–9, A–Z, a–z, "+", and "/" (or "-" and "\_", if using URL-safe base64).
- {{jsxref("Uint8Array.fromHex()")}}, {{jsxref("Uint8Array.prototype.toHex()")}}, and {{jsxref("Uint8Array.prototype.setFromHex()")}} for working with hex strings, where every byte is encoded by two characters that's either 0–9 or A–F (case-insensitive).
- {{jsxref("Uint8Array.fromHex()")}}, {{jsxref("Uint8Array.prototype.toHex()")}}, and {{jsxref("Uint8Array.prototype.setFromHex()")}} for working with hex strings, where every byte is encoded by two characters, each one being either 0–9 or A–F (case-insensitive).

## Constructor

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ browser-compat: javascript.builtins.Uint8Array.setFromBase64

The **`setFromBase64()`** method of {{jsxref("Uint8Array")}} instances populates this `Uint8Array` object with bytes from a [base64](/en-US/docs/Glossary/Base64)-encoded string, returning an object indicating how many bytes were read and written.

For the general concepts around base64 strings, see the [base64](/en-US/docs/Glossary/Base64) glossary entry. This method is most suitable for populating a pre-allocated array buffer. If you just want to create a new `Uint8Array` object from a base64-encoded string, use the static method {{jsxref("Uint8Array.fromBase64()")}} instead.
This method is most suitable for populating a pre-allocated array buffer. If you just want to create a new `Uint8Array` object from a base64-encoded string, use the static method {{jsxref("Uint8Array.fromBase64()")}} instead.

## Syntax

Expand All @@ -21,7 +21,7 @@ setFromBase64(string, options)
### Parameters

- `string`
- : A base64-encoded string to convert to a `Uint8Array`. It has the same requirements as the [`string` parameter of `Uint8Array.fromBase64()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/fromBase64#string). Note that the string is only read up to the point where the array is filled, so any invalid base64 syntax after that point is ignored.
- : A base64-encoded string encoding bytes to write into a `Uint8Array`. It has the same requirements as the [`string` parameter of `Uint8Array.fromBase64()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/fromBase64#string). Note that the string is only read up to the point where the array is filled, so any invalid base64 syntax after that point is ignored.
- `options` {{optional_inline}}
- : An object customizing the base64 string interpretation process. It has the same requirements as the [`options` parameter of `Uint8Array.fromBase64()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/fromBase64#options).

Expand All @@ -30,7 +30,7 @@ setFromBase64(string, options)
An object containing the following properties:

- `read`
- : The number of base64 characters read from the input string. It is either the length of the input string (including padding), if the decoded data fits into the array, or up to the last complete 4-character chunk that fits into the array. Chunks will never be split (because the remaining bits cannot be partially "put back" into the base64 without completely re-encoding it); if the next chunk cannot fit into the remainder of the array, it will be entirely unread, resulting in the last one or two bytes of the array not being written.
- : The number of base64 characters read from the input string. If the decoded data fits into the array, this is the length of the input string (including padding); otherwise, it is the length up to the last complete 4-character chunk that fits into the array. Chunks will never be split (because the remaining bits cannot be partially "put back" into the base64 without completely re-encoding it); if the next chunk cannot fit into the remainder of the array, it will be entirely unread, resulting in the last one or two bytes of the array not being written.
- `written`
- : The number of bytes written to the `Uint8Array`. Will never be greater than this `Uint8Array`'s {{jsxref("TypedArray/byteLength", "byteLength")}}.

Expand Down Expand Up @@ -70,7 +70,7 @@ console.log(uint8Array);
// Uint8Array(8) [60, 98, 62, 77, 68, 78, 0, 0]
```

Note how the last two bytes of the array are not written, because to decode the next two bytes, we need to read three more base64 characters, which represent 18 bits, resulting in 2 trailing bits being discarded. Therefore, we can only write 2 chunks, or 6 bytes, to the array.
Note how the last two bytes of the array are not written. To decode these two bytes, we need to read at least three more base64 characters, which represent 18 bits. These can't fit into the remaining two bytes of the array, so we can only write 2 chunks, or 6 bytes.

### Setting data at a specific offset

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@ setFromHex(string)
### Parameters

- `string`
- : A hexadecimal string to convert to a `Uint8Array`. The string must only contain characters in the hexadecimal alphabet, which includes 0–9 and A–F (case-insensitive). It must have an even number of characters. Unlike {{jsxref("Uint8Array.prototype.setFromBase64()")}}, the input cannot contain whitespace. Note that the string is only read up to the point where the array is filled, so any invalid hex syntax after that point is ignored.

- : A hexadecimal string encoding bytes to write into a `Uint8Array`. The string must:

- Have an even number of characters because two characters encode one byte.
- Only contain characters in the hexadecimal alphabet, which includes 0–9 and A–F (case-insensitive).
- Not contain whitespace (unlike {{jsxref("Uint8Array.prototype.setFromBase64()")}}).

Note that the string is only read up to the point where the array is filled, so any invalid hex syntax after that point is ignored.

### Return value

An object containing the following properties:

- `read`
- : The number of hex characters read from the input string. It is either the length of the input string, if the decoded data fits into the array, or a multiple of 2 up to the last 2-character chunk that fits into the array.
- : The number of hex characters read from the input string. If the decoded data fits into the array, it is the length of the input string: otherwise, it is the number of complete hex characters that fit into the array.
- `written`
- : The number of bytes written to the `Uint8Array`. Will never be greater than this `Uint8Array`'s {{jsxref("TypedArray/byteLength", "byteLength")}}.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ browser-compat: javascript.builtins.Uint8Array.toBase64

The **`toBase64()`** method of {{jsxref("Uint8Array")}} instances returns a [base64](/en-US/docs/Glossary/Base64)-encoded string based on the data in this `Uint8Array` object.

For the general concepts around base64 strings, see the [base64](/en-US/docs/Glossary/Base64) glossary entry. This method should be preferred over {{domxref("Window.btoa()")}}, especially if you already have a `Uint8Array` holding the object, because you don't need to convert it to a string first.
This method should be preferred over {{domxref("Window.btoa()")}}, especially if you already have a `Uint8Array` holding the object, because you don't need to convert it to a string first.

## Syntax

Expand Down

0 comments on commit 5be192a

Please sign in to comment.