Stage 1 Draft / July 1, 2022

String.cooked

New API

This adds a builtin static method to String

1 Properties of the String Constructor 1

1.1 String.cooked ( template, ...substitutions )

The String.cooked function may be called with a variable number of arguments. The first argument is template and the remainder of the arguments form the List substitutions. The following steps are taken:

  1. Return ? InterpolateTemplateStringsArray(template, substitutions, cooked).
Note

The cooked function implements behaviour suitable for a tag function of a Tagged Template (13.3.11). When called as such, the first argument will be a well formed template object and the rest parameter will contain the substitution values. In practice, it is more useful as a function which custom tag functions delegate to as a final step.

1.2 InterpolateTemplateStringsArray ( template, substitutions, prep )

The abstract operation InterpolateTemplateStringsArray takes arguments template (an ECMAScript language value), substitutions (a List of ECMAScript language values), and prep (cooked or raw) and returns either a normal completion containing a String or a throw completion. It performs the following steps when called:

  1. Let numberOfSubstitutions be the number of elements in substitutions.
  2. Let cooked be ? ToObject(template).
  3. If prep is cooked, let t be cooked; else let t be ? ToObject(? Get(cooked, "raw")).
  4. Let literalSegments be ? LengthOfArrayLike(t).
  5. If literalSegments ≤ 0, return the empty String.
  6. Let stringElements be a new empty List.
  7. Let nextIndex be 0.
  8. Repeat,
    1. Let nextKey be ! ToString(𝔽(nextIndex)).
    2. Let nextVal be ? Get(t, nextKey).
    3. If Type(nextVal) is Undefined, throw a TypeError exception.
    4. Let nextSeg be ? ToString(nextVal).
    5. Append the code unit elements of nextSeg to the end of stringElements.
    6. If nextIndex + 1 = literalSegments, then
      1. Return the String value whose code units are the elements in the List stringElements. If stringElements has no elements, the empty String is returned.
    7. If nextIndex < numberOfSubstitutions, let next be substitutions[nextIndex].
    8. Else, let next be the empty String.
    9. Let nextSub be ? ToString(next).
    10. Append the code unit elements of nextSub to the end of stringElements.
    11. Set nextIndex to nextIndex + 1.
Editor's Note

This implements a generic abstract operation that can be reused by String.raw. When making the pull request into ecma262, we should update String.raw as well.

A Copyright & Software License

Copyright Notice

© 2022 Darien Maillet Valentine

Software License

All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.