From 9c83735dc1fdf89cc37645b128b42b3b7b4873d3 Mon Sep 17 00:00:00 2001 From: onikilamat <67230019+onikilamat@users.noreply.github.com> Date: Mon, 30 Nov 2026 13:23:34 +0300 Subject: [PATCH] TextDecoder and TextEncoder --- 4-binary/02-text-decoder/article.md | 44 ++++++++++++++--------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/4-binary/02-text-decoder/article.md b/4-binary/02-text-decoder/article.md index 9287279ec..7ce3c9e2e 100644 --- a/4-binary/02-text-decoder/article.md +++ b/4-binary/02-text-decoder/article.md @@ -1,30 +1,30 @@ -# TextDecoder and TextEncoder +# TextDecoder ve TextEncoder -What if the binary data is actually a string? For instance, we received a file with textual data. +Ya ikili (binary) veri aslında bir string ise? Örneğin, metin içeren bir dosya aldık. -The build-in [TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder) object allows to read the value into an an actual JavaScript string, given the buffer and the encoding. +Yerleşik [TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder) nesnesi, buffer ve kodlama (encoding) verildiğinde değeri gerçek bir JavaScript string'ine okumamızı sağlar. -We first need to create it: +Önce onu oluşturmamız gerekir: ```js let decoder = new TextDecoder([label], [options]); ``` -- **`label`** -- the encoding, `utf-8` by default, but `big5`, `windows-1251` and many other are also supported. -- **`options`** -- optional object: - - **`fatal`** -- boolean, if `true` then throw an exception for invalid (non-decodable) characters, otherwise (default) replace them with character `\uFFFD`. - - **`ignoreBOM`** -- boolean, if `true` then ignore BOM (an optional byte-order unicode mark), rarely needed. +- **`label`** -- kodlama; varsayılan olarak `utf-8`, ancak `big5`, `windows-1251` ve daha birçoğu da desteklenir. +- **`options`** -- isteğe bağlı nesne: + - **`fatal`** -- boolean; `true` ise geçersiz (çözülemeyen) karakterler için hata fırlatır, aksi halde (varsayılan) bu karakterleri `\uFFFD` karakteriyle değiştirir. + - **`ignoreBOM`** -- boolean; `true` ise BOM'u (isteğe bağlı unicode bayt sırası işareti) yok sayar, nadiren gerekir. -...And then decode: +...Ardından çözümleyelim (decode): ```js let str = decoder.decode([input], [options]); ``` -- **`input`** -- `BufferSource` to decode. -- **`options`** -- optional object: - - **`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. +- **`input`** -- çözümlenecek `BufferSource`. +- **`options`** -- isteğe bağlı nesne: + - **`stream`** -- akışları (stream) çözümlerken, yani `decoder` gelen veri parçalarıyla tekrar tekrar çağrıldığında `true` verilir. Bu durumda çok baytlı bir karakter bazen parçalar arasında bölünebilir. Bu seçenek, `TextDecoder`'a "tamamlanmamış" karakterleri hatırlamasını ve bir sonraki parça geldiğinde çözümlemesini söyler. -For instance: +Örneğin: ```js run let uint8Array = new Uint8Array([72, 101, 108, 108, 111]); @@ -39,14 +39,14 @@ let uint8Array = new Uint8Array([228, 189, 160, 229, 165, 189]); alert( new TextDecoder().decode(uint8Array) ); // 你好 ``` -We can decode a part of the buffer by creating a subarray view for it: +Buffer'ın bir kısmını, o kısım için bir alt dizi (subarray) görünümü oluşturarak çözümleyebiliriz: ```js run let uint8Array = new Uint8Array([0, 72, 101, 108, 108, 111, 0]); -// the string is in the middle -// create a new view over it, without copying anything +// string ortada yer alıyor +// hiçbir şey kopyalamadan onun üzerinde yeni bir görünüm oluştur let binaryString = uint8Array.subarray(1, -1); alert( new TextDecoder().decode(binaryString) ); // Hello @@ -54,19 +54,19 @@ alert( new TextDecoder().decode(binaryString) ); // Hello ## TextEncoder -[TextEncoder](https://encoding.spec.whatwg.org/#interface-textencoder) does the reverse thing -- converts a string into bytes. +[TextEncoder](https://encoding.spec.whatwg.org/#interface-textencoder) tersini yapar -- bir string'i baytlara dönüştürür. -The syntax is: +Sözdizimi şöyledir: ```js run let encoder = new TextEncoder(); ``` -The only encoding it supports is "utf-8". +Desteklediği tek kodlama "utf-8"'dir. -It has two methods: -- **`encode(str)`** -- returns `Uint8Array` from a string. -- **`encodeInto(str, destination)`** -- encodes `str` into `destination` that must be `Uint8Array`. +İki metodu vardır: +- **`encode(str)`** -- bir string'den `Uint8Array` döndürür. +- **`encodeInto(str, destination)`** -- `str`'yi, `Uint8Array` olması gereken `destination` içine kodlar. ```js run let encoder = new TextEncoder();