Skip to content

Commit b3ff949

Browse files
authored
Merge pull request #338 from homero304/TextDecoder-and-TextEncoder
TextDecoder and TextEncoder
2 parents 74afc79 + 53a2f13 commit b3ff949

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

4-binary/02-text-decoder/article.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
# TextDecoder and TextEncoder
1+
# TextDecoder y TextEncoder
22

3-
What if the binary data is actually a string? For instance, we received a file with textual data.
3+
¿Qué pasa si los datos binarios son en realidad un string? Por ejemplo, recibimos un archivo con datos textuales.
44

5-
The build-in [TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder) object allows to read the value into an actual JavaScript string, given the buffer and the encoding.
5+
El objeto [TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder) nos permite leer el texto de un conjunto de datos binarios y convertirlo en un dato de tipo string de JavaScript, dados el búfer y la codificación.
66

7-
We first need to create it:
7+
Primero necesitamos crearlo:
88
```js
99
let decoder = new TextDecoder([label], [options]);
1010
```
1111

12-
- **`label`** -- the encoding, `utf-8` by default, but `big5`, `windows-1251` and many other are also supported.
13-
- **`options`** -- optional object:
14-
- **`fatal`** -- boolean, if `true` then throw an exception for invalid (non-decodable) characters, otherwise (default) replace them with character `\uFFFD`.
15-
- **`ignoreBOM`** -- boolean, if `true` then ignore BOM (an optional byte-order unicode mark), rarely needed.
12+
- **`label`** -- la codificación, `utf-8` por defecto, pero `big5`, `windows-1251` y muchos otros también son soportados.
13+
- **`options`** -- objeto opcional:
14+
- **`fatal`** -- booleano, si es `true` arroja una excepción por caracteres inválidos (no-decodificable), de otra manera (por defecto) son reemplazados con el carácter `\uFFFD`.
15+
- **`ignoreBOM`** -- booleano, si es `true` entonces ignora BOM (una marca Unicode de orden de bytes opcional), raramente es necesario.
1616

17-
...And then decode:
17+
...Y luego decodificar:
1818

1919
```js
2020
let str = decoder.decode([input], [options]);
2121
```
2222

23-
- **`input`** -- `BufferSource` to decode.
24-
- **`options`** -- optional object:
25-
- **`stream`** -- true for decoding streams, when `decoder` is called repeatedly with incoming chunks of data. In that case a multi-byte character may occasionally split between chunks. This options tells `TextDecoder` to memorize "unfinished" characters and decode them when the next chunk comes.
23+
- **`input`** -- `BufferSource` para decodificar.
24+
- **`options`** -- objeto opcional:
25+
- **`stream`** -- true para decodificación de secuencias, cuando el `decoder` es usado repetidamente para fragmentos de datos entrantes. En ese caso, un carácter de varios bytes puede ocasionalmente dividirse entre fragmentos. Esta opción le dice al `TextDecoder` que memorice caracteres "incompletos" y que los decodifique cuando venga el siguiente fragmento.
2626

27-
For instance:
27+
Por ejemplo:
2828

2929
```js run
30-
let uint8Array = new Uint8Array([72, 101, 108, 108, 111]);
30+
let uint8Array = new Uint8Array([72, 111, 108, 97]);
3131

32-
alert( new TextDecoder().decode(uint8Array) ); // Hello
32+
alert( new TextDecoder().decode(uint8Array) ); // Hola
3333
```
3434

3535

@@ -39,38 +39,38 @@ let uint8Array = new Uint8Array([228, 189, 160, 229, 165, 189]);
3939
alert( new TextDecoder().decode(uint8Array) ); // 你好
4040
```
4141

42-
We can decode a part of the buffer by creating a subarray view for it:
42+
Podemos decodificar una parte del búfer al crear una vista de sub arreglo para ello:
4343

4444

4545
```js run
46-
let uint8Array = new Uint8Array([0, 72, 101, 108, 108, 111, 0]);
46+
let uint8Array = new Uint8Array([0, 72, 111, 108, 97, 0]);
4747

48-
// the string is in the middle
49-
// create a new view over it, without copying anything
48+
// El string esta en medio
49+
// crear una nueva vista sobre el string, sin copiar nada
5050
let binaryString = uint8Array.subarray(1, -1);
5151

52-
alert( new TextDecoder().decode(binaryString) ); // Hello
52+
alert( new TextDecoder().decode(binaryString) ); // Hola
5353
```
5454

5555
## TextEncoder
5656

57-
[TextEncoder](https://encoding.spec.whatwg.org/#interface-textencoder) does the reverse thing -- converts a string into bytes.
57+
[TextEncoder](https://encoding.spec.whatwg.org/#interface-textencoder) hace lo contrario: convierte un string en bytes.
5858

59-
The syntax is:
59+
La sintaxis es:
6060

6161
```js
6262
let encoder = new TextEncoder();
6363
```
6464

65-
The only encoding it supports is "utf-8".
65+
La única codificación que soporta es "utf-8".
6666

67-
It has two methods:
68-
- **`encode(str)`** -- returns `Uint8Array` from a string.
69-
- **`encodeInto(str, destination)`** -- encodes `str` into `destination` that must be `Uint8Array`.
67+
Tiene dos métodos:
68+
- **`encode(str)`** -- regresa un dato de tipo `Uint8Array` de un string.
69+
- **`encodeInto(str, destination)`** -- codifica un `str` en `destination`, este último debe ser de tipo `Uint8Array`.
7070

7171
```js run
7272
let encoder = new TextEncoder();
7373

74-
let uint8Array = encoder.encode("Hello");
75-
alert(uint8Array); // 72,101,108,108,111
74+
let uint8Array = encoder.encode("Hola");
75+
alert(uint8Array); // 72,111,108,97
7676
```

0 commit comments

Comments
 (0)