Reflect.toStringSpec proposal

# Sergey R (2 years ago)

I want to propose a new language feature — Reflect.toStringSpec.

Here is a repo chicoxyzzy/proposal-reflect-tostringspec

Some context: airbnb/babel-plugin-dynamic-import-node/pull/2

# Allen Wirfs-Brock (2 years ago)

On Dec 13, 2016, at 4:51 PM, Sergey R <chi187 at> wrote:

I want to propose a new language feature — Reflect.toStringSpec. Here is a repo chicoxyzzy/proposal-reflect-tostringspec, chicoxyzzy/proposal-reflect-tostringspec Some context: airbnb/babel-plugin-dynamic-import-node/pull/2, airbnb/babel-plugin-dynamic-import-node/pull/2_______________________________________________

Doesn’t this do exactly what you want:

// JS level equivalent of ES spec ToString abstract operation <> 

function ToString(v) {
   if (typeof v === “symbol”) throw TypeError(“Can’t convert a symbol to a string”);
   return String(v);  //for non-symbols String(v) primitively returns ToString(v)

This seems like something that can reasonably be coded as part of a polyfill

# Bergi (2 years ago)

Sergey R schrieb:

I want to propose a new language feature — Reflect.toStringSpec.

Reflect.toString or Reflect.toStringSpec?

Here is a repo chicoxyzzy/proposal-reflect-tostringspec

| Rationale | | There is no exact way to call spec's ToString in JS. | However it may be necessary for polyfills.

It's not that hard to emulate, is it? There are other internal operations would be more important.

Regardless, the Reflect namespace is supposed to only contain object tools that would be the default for proxy traps. No ToString in there.

| Current solution is to do something like: | | function toStringSpec(target) { | return target == null ? target : String(Object(target)); | }

That looks horribly wrong. It doesn't do the least what ToString does in ES6.

The spec says it should do

function ToString(x) {
     if (typeof x != "symbol) return String(x);
     else throw new TypeError("…");

This would be a pretty standard approach, also being used e.g. in ljharb/es-abstract/blob/035153777213981e0e24f6cf007ffbd279384130/es6.js#L129-L135

And if you don't care about Symbols, you can easily just use String.

Kind , Bergi

# Jordan Harband (2 years ago)

Here you go: require('es-abstract').ToString

# Jordan Harband (2 years ago)

This was also pointed out to me elsewhere:

const ToStringSpec = x => `${x}`;