OpenUI.Runtime.Value (OpenUI v0.1.1)

Copy Markdown View Source

The value semantics of OpenUI Lang (language.md, sections 3.1 and 3.2).

The spec defines coercion, loose equality and number formatting by ECMAScript, so this module reproduces ToNumber, IsLooselyEqual and Number::toString on Elixir terms. Two differences come from the BEAM:

  • there is no NaN or Infinity, so a string that ToNumber would read as Infinity ("Infinity", "1e400") counts as 0, like NaN does;
  • arrays and objects have no identity, so loose equality compares them by structure.

Numbers are integers when they hold an integral value within 2^53, and floats otherwise, which keeps encoded JSON identical to JavaScript's.

Summary

Functions

Applies a binary operator to two already evaluated operands.

IsLooselyEqual (==).

Turns an integral float into an integer and an integer beyond 2^53 into a float.

StringToNumber: the number a string reads as, or :nan. Leading and trailing whitespace is ignored and the empty string is 0.

ToString, as String(value) gives it.

The length of a string as JavaScript counts it, in UTF-16 code units. Bytes that are not valid UTF-8, as in text cut mid-character, count one each.

ToNumber, with 0 in place of NaN.

ToBoolean.

Applies ! or unary -.

Functions

binary_op(binary, left, right)

@spec binary_op(String.t(), term(), term()) :: term()

Applies a binary operator to two already evaluated operands.

loose_equal?(left, right)

@spec loose_equal?(term(), term()) :: boolean()

IsLooselyEqual (==).

normalize_number(number)

@spec normalize_number(number()) :: number()

Turns an integral float into an integer and an integer beyond 2^53 into a float.

parse_number(string)

@spec parse_number(String.t()) :: number() | :nan

StringToNumber: the number a string reads as, or :nan. Leading and trailing whitespace is ignored and the empty string is 0.

stringify(string)

@spec stringify(term()) :: String.t()

ToString, as String(value) gives it.

text_length(string)

@spec text_length(String.t()) :: non_neg_integer()

The length of a string as JavaScript counts it, in UTF-16 code units. Bytes that are not valid UTF-8, as in text cut mid-character, count one each.

to_number(number)

@spec to_number(term()) :: number()

ToNumber, with 0 in place of NaN.

truthy?(value)

@spec truthy?(term()) :: boolean()

ToBoolean.

unary_op(binary, value)

@spec unary_op(String.t(), term()) :: term()

Applies ! or unary -.