Skip to content

Commit

Permalink
GetStringOrBoolean refactor (#132)
Browse files Browse the repository at this point in the history
Co-authored-by: Richard Gibson <richard.gibson@gmail.com>
  • Loading branch information
sffc and gibson042 committed Jan 30, 2023
1 parent 23e69cf commit 654bfad
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 33 deletions.
35 changes: 19 additions & 16 deletions negotiation/diff.emu
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<emu-alg>
1. Let _candidate_ be _locale_.
1. Repeat,
1. If _availableLocales_ contains an element equal to _candidate_, return _candidate_.
1. If _availableLocales_ contains <del>an element equal to</del> _candidate_, return _candidate_.
1. Let _pos_ be the character index of the last occurrence of *"-"* (U+002D) within _candidate_. If that character does not occur, return *undefined*.
1. If _pos_ &ge; 2 and the character *"-"* occurs at index _pos_ - 2 of candidate, decrease _pos_ by 2.
1. Let _candidate_ be the substring of _candidate_ from position 0, inclusive, to position _pos_, exclusive.
Expand Down Expand Up @@ -354,29 +354,32 @@
1. Else,
1. Assert: _type_ is ~string~.
1. Set _value_ to ? ToString(_value_).
1. If _values_ is not ~empty~ and _values_ does not contain an element equal to _value_, throw a *RangeError* exception.
1. If _values_ is not ~empty~ and _values_ does not contain <del>an element equal to</del> _value_, throw a *RangeError* exception.
1. Return _value_.
</emu-alg>
</emu-clause>

<ins class="block">
<emu-clause id="sec-getstringorbooleanoption" aoid="GetStringOrBooleanOption">
<h1>GetStringOrBooleanOption ( _options_, _property_, _values_, _trueValue_, _falsyValue_, _fallback_ )</h1>

<p>
The abstract operation GetStringOrBooleanOption extracts the value of the property named _property_ from the provided _options_ object. It returns either _trueValue_, _falsyValue_, _fallback_, or one of the elements of the List _values_. The following steps are taken:
</p>

<emu-alg>
<emu-clause id="sec-getbooleanorstringnumberformatoption" type="abstract operation">
<h1>
GetBooleanOrStringNumberFormatOption (
_options_: an Object,
_property_: a property key,
_stringValues_: a List of Strings,
_fallback_: an ECMAScript language value,
): either a normal completion containing either a Boolean, String, or _fallback_, or a throw completion
</h1>
<dl class="header">
<dt>description</dt>
<dd>It extracts the value of the property named _property_ from the provided _options_ object. It returns _fallback_ if that value is *undefined*, *true* if that value is *true*, *false* if that value coerces to *false*, and otherwise coerces it to a String and returns the result if it is allowed by _stringValues_.</dd>
</dl>
<emu-alg>
1. Let _value_ be ? Get(_options_, _property_).
1. If _value_ is *undefined*, return _fallback_.
1. If _value_ is *true*, return _trueValue_.
1. Let _valueBoolean_ be ToBoolean(_value_).
1. If _valueBoolean_ is *false*, return _falsyValue_.
1. If _value_ is *true*, return *true*.
1. If ToBoolean(_value_) is *false*, return *false*.
1. Let _value_ be ? ToString(_value_).
1. NOTE: For historical reasons, the strings *"true"* and *"false"* are treated the same as the _fallback_ value.
1. If _value_ is *"true"* or *"false"*, return _fallback_.
1. If _values_ does not contain an element equal to _value_, throw a *RangeError* exception.
1. If _stringValues_ does not contain _value_, throw a *RangeError* exception.
1. Return _value_.
</emu-alg>
</emu-clause>
Expand Down
33 changes: 18 additions & 15 deletions negotiation/proposed.emu
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<emu-alg>
1. Let _candidate_ be _locale_.
1. Repeat,
1. If _availableLocales_ contains an element equal to _candidate_, return _candidate_.
1. If _availableLocales_ contains _candidate_, return _candidate_.
1. Let _pos_ be the character index of the last occurrence of *"-"* (U+002D) within _candidate_. If that character does not occur, return *undefined*.
1. If _pos_ &ge; 2 and the character *"-"* occurs at index _pos_ - 2 of candidate, decrease _pos_ by 2.
1. Let _candidate_ be the substring of _candidate_ from position 0, inclusive, to position _pos_, exclusive.
Expand Down Expand Up @@ -354,28 +354,31 @@
1. Else,
1. Assert: _type_ is ~string~.
1. Set _value_ to ? ToString(_value_).
1. If _values_ is not ~empty~ and _values_ does not contain an element equal to _value_, throw a *RangeError* exception.
1. If _values_ is not ~empty~ and _values_ does not contain _value_, throw a *RangeError* exception.
1. Return _value_.
</emu-alg>
</emu-clause>

<emu-clause id="sec-getstringorbooleanoption" aoid="GetStringOrBooleanOption">
<h1>GetStringOrBooleanOption ( _options_, _property_, _values_, _trueValue_, _falsyValue_, _fallback_ )</h1>

<p>
The abstract operation GetStringOrBooleanOption extracts the value of the property named _property_ from the provided _options_ object. It returns either _trueValue_, _falsyValue_, _fallback_, or one of the elements of the List _values_. The following steps are taken:
</p>

<emu-clause id="sec-getbooleanorstringnumberformatoption" type="abstract operation">
<h1>
GetBooleanOrStringNumberFormatOption (
_options_: an Object,
_property_: a property key,
_stringValues_: a List of Strings,
_fallback_: an ECMAScript language value,
): either a normal completion containing either a Boolean, String, or _fallback_, or a throw completion
</h1>
<dl class="header">
<dt>description</dt>
<dd>It extracts the value of the property named _property_ from the provided _options_ object. It returns _fallback_ if that value is *undefined*, *true* if that value is *true*, *false* if that value coerces to *false*, and otherwise coerces it to a String and returns the result if it is allowed by _stringValues_.</dd>
</dl>
<emu-alg>
1. Let _value_ be ? Get(_options_, _property_).
1. If _value_ is *undefined*, return _fallback_.
1. If _value_ is *true*, return _trueValue_.
1. Let _valueBoolean_ be ToBoolean(_value_).
1. If _valueBoolean_ is *false*, return _falsyValue_.
1. If _value_ is *true*, return *true*.
1. If ToBoolean(_value_) is *false*, return *false*.
1. Let _value_ be ? ToString(_value_).
1. NOTE: For historical reasons, the strings *"true"* and *"false"* are treated the same as the _fallback_ value.
1. If _value_ is *"true"* or *"false"*, return _fallback_.
1. If _values_ does not contain an element equal to _value_, throw a *RangeError* exception.
1. If _stringValues_ does not contain _value_, throw a *RangeError* exception.
1. Return _value_.
</emu-alg>
</emu-clause>
Expand Down
6 changes: 5 additions & 1 deletion numberformat/diff.emu
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@
1. If _notation_ is *"compact"*, then
1. Set _numberFormat_.[[CompactDisplay]] to _compactDisplay_.
1. <ins class="block">Set _defaultUseGrouping_ to *"min2"*.</ins>
1. Let _useGrouping_ be ? Get<ins>StringOrBoolean</ins>Option(_options_, *"useGrouping"*, <del>*"boolean"*, *undefined*, *true*</del> <ins>&laquo; *"min2"*, *"auto"*, *"always"* &raquo;, *"always"*, *false*, _defaultUseGrouping_</ins>).
1. <del class="block">Let _useGrouping_ be ? GetOption(_options_, *"useGrouping"*, ~boolean~, ~empty~, *true*).</del>
1. <ins class="block">NOTE: For historical reasons, the strings *"true"* and *"false"* are accepted and replaced with the default value.</ins>
1. <ins class="block">Let _useGrouping_ be ? GetBooleanOrStringNumberFormatOption(_options_, *"useGrouping"*, &laquo; *"min2"*, *"auto"*, *"always"*, *"true"*, *"false"* &raquo;, _defaultUseGrouping_).</ins>
1. <ins class="block">If _useGrouping_ is *"true"* or _useGrouping_ is *"false"*, set _useGrouping_ to _defaultUseGrouping_.</ins>
1. <ins class="block">If _useGrouping_ is *true*, set _useGrouping_ to *"always"*.</ins>
1. Set _numberFormat_.[[UseGrouping]] to _useGrouping_.
1. Let _signDisplay_ be ? GetOption(_options_, *"signDisplay"*, ~string~, &laquo; *"auto"*, *"never"*, *"always"*, *"exceptZero"*<ins>, *"negative"*</ins> &raquo;, *"auto"*).
1. Set _numberFormat_.[[SignDisplay]] to _signDisplay_.
Expand Down
5 changes: 4 additions & 1 deletion numberformat/proposed.emu
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@
1. If _notation_ is *"compact"*, then
1. Set _numberFormat_.[[CompactDisplay]] to _compactDisplay_.
1. Set _defaultUseGrouping_ to *"min2"*.
1. Let _useGrouping_ be ? GetStringOrBooleanOption(_options_, *"useGrouping"*, &laquo; *"min2"*, *"auto"*, *"always"* &raquo;, *"always"*, *false*, _defaultUseGrouping_).
1. NOTE: For historical reasons, the strings *"true"* and *"false"* are accepted and replaced with the default value.
1. Let _useGrouping_ be ? GetBooleanOrStringNumberFormatOption(_options_, *"useGrouping"*, &laquo; *"min2"*, *"auto"*, *"always"*, *"true"*, *"false"* &raquo;, _defaultUseGrouping_).
1. If _useGrouping_ is *"true"* or _useGrouping_ is *"false"*, set _useGrouping_ to _defaultUseGrouping_.
1. If _useGrouping_ is *true*, set _useGrouping_ to *"always"*.
1. Set _numberFormat_.[[UseGrouping]] to _useGrouping_.
1. Let _signDisplay_ be ? GetOption(_options_, *"signDisplay"*, ~string~, &laquo; *"auto"*, *"never"*, *"always"*, *"exceptZero"*, *"negative"* &raquo;, *"auto"*).
1. Set _numberFormat_.[[SignDisplay]] to _signDisplay_.
Expand Down

0 comments on commit 654bfad

Please sign in to comment.