I'm not a huge fan of this idea, but just as a reference point, here is a
routine to convert a string to using smart quotes:

```js
// Change straight quotes to curly and double hyphens to em-dashes etc.
export function smarten(a: string) {
  if (!a) return a;

  a = a.replace(/(^|[-\u2014\s(\["])'/g, "$1\u2018"); // opening singles
  a = a.replace(/'/g, "\u2019"); // closing singles & apostrophes
  a = a.replace(/(^|[-\u2014/\[(\u2018\s])"/g, "$1\u201c"); // opening
doubles
  a = a.replace(/"/g, "\u201d"); // closing doubles
  a = a.replace(/\s*--\s*/g, "\u2014"); // em-dashes
  a = a.replace(/\.\.\./g, "\u2026"); // ellipsis
  a = a.replace(/ - /g, "\u2013"); // en-dashes
  a = a.replace(/\s+\?/g, "?"); // Remove Indian-style spaces before
question mark
  a = a.replace(/\s+\/\s+/, "/"); // Spaces around slashes.

  return a;
}
```

On Mon, May 21, 2018 at 10:19 AM Isiah Meadows <[email protected]>
wrote:

> I was using HTML primitive escaping as a concrete example, but there's
> others. Most use cases in my experience are essentially escaping for
> various reasons, but it's also useful for simple extensible templating
> where you control the expansion, but not the source. Here's a concrete
> example of what this could do:
>
> ```js
> // Old
> export function format(message, args, prettify = inspect) {
>     return message.replace(/\{(.+?)\}/g, (m, prop) =>
>         hasOwn.call(args, prop) ? prettify(args[prop], {depth: 5}) : m
>     )
> }
>
> // New
> export function format(message, args, prettify = inspect) {
>     return message.replace(Object.keys(args).reduce((acc, k) => ({
>       ...acc, [`{${k}}`]: prettify(args[k], {depth: 5})
>     }), {})
> }
> ```
>
> (I presume you're aware that this does have some language precedent in
> Ruby's `String#gsub(regex, hash)`.)
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to