Class: Dommy::TextEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/text_codec.rb

Overview

‘TextEncoder` — encodes a String into UTF-8 bytes. Per spec, only “utf-8” encoding is supported.

Spec: encoding.spec.whatwg.org/#textencoder

Instance Method Summary collapse

Instance Method Details

#__js_call__(method, args) ⇒ Object



24
25
26
27
28
29
# File 'lib/dommy/text_codec.rb', line 24

def __js_call__(method, args)
  case method
  when "encode"
    encode(args[0])
  end
end

#__js_get__(key) ⇒ Object



20
21
22
# File 'lib/dommy/text_codec.rb', line 20

def __js_get__(key)
  key == "encoding" ? encoding : nil
end

#encode(input = "") ⇒ Object

encode(string) → Array<Integer> (bytes). Browsers return a Uint8Array; Dommy returns the equivalent Ruby byte array since there is no typed-array layer.



16
17
18
# File 'lib/dommy/text_codec.rb', line 16

def encode(input = "")
  input.to_s.encode(Encoding::UTF_8).bytes
end

#encodingObject



9
10
11
# File 'lib/dommy/text_codec.rb', line 9

def encoding
  "utf-8"
end