Namespace: Encode

Encode

Functions to encode and decode strings

Methods

(static) decodeCodePoints(data) → {string}

Decode an array of Unicode code points into a string

Parameters:
Name Type Description
data Array.<number>

An array of Unicode code points

Since:
  • 0.0.1
Returns:
Type
string
Example
decodeCodePoints([0x0068, 0x0065, 0x006c, 0x006c, 0x006f])
// => "hello"
decodeCodePoints([0x1f601, 0x1f436, 0x1f355])
// => "πŸ˜πŸΆπŸ•"

(static) decodeUTF8(data) → {string}

Decodes an array of UTF-8 bytes. From https://gist.github.com/joni/3760795#gistcomment-1299119.

Parameters:
Name Type Description
data Array.<number>

Array of UTF-8 encoded bytes

Since:
  • 0.0.1
Returns:

Decoded string

Type
string
Example
decodeUTF8([0x68, 0x65, 0x6c, 0x6c, 0x6f])
// => "hello"
decodeUTF8([0xf0, 0x9f, 0x98, 0x80, 0xf0, 0x9f, 0x98, 0x81])
// => "πŸ˜€πŸ˜"

(static) decodeUTF16(codes) → {string}

Converts an array of UTF-16 char codes into a string

Parameters:
Name Type Description
codes Array.<number>
Since:
  • 0.0.1
Returns:
Type
string
Example
decodeUTF16([0x0068, 0x0065, 0x006c, 0x006c, 0x006f])
// => "hello"
decodeUTF16([0xd83d, 0xde01, 0xd83d, 0xdc36, 0xd83c, 0xdf55])
// => "πŸ˜πŸΆπŸ•"

(static) encodeCodePoints(subject) → {Array.<number>}

Split a string into an array of its numeric Unicode code points

Parameters:
Name Type Description
subject string

A string to encode

Since:
  • 0.0.1
Returns:
Type
Array.<number>
Example
encodeCodePoints("hello")
// => [0x0068, 0x0065, 0x006c, 0x006c, 0x006f]
encodeCodePoints("πŸ˜πŸΆπŸ•")
// => [0x1f601, 0x1f436, 0x1f355]
encodeCodePoints("ΰ€…ΰ€¨ΰ₯ΰ€šΰ₯ΰ€›ΰ₯‡ΰ€¦")
// => [0x905, 0x928, 0x941, 0x91a, 0x94d, 0x91b, 0x947, 0x926]

(static) encodeUTF8(subject) → {Array.<number>}

Encodes a string into an array of UTF-8 code unit values. From https://stackoverflow.com/a/18729931

Parameters:
Name Type Description
subject string

String to encode

Since:
  • 0.0.1
Returns:

An array of UTF-8 byte values

Type
Array.<number>
Example
encodeUTF8("hello")
// => [0x68, 0x65, 0x6c, 0x6c, 0x6f]
encodeUTF8("πŸ˜€πŸ˜")
// => [0xf0, 0x9f, 0x98, 0x80, 0xf0, 0x9f, 0x98, 0x81]