Re: Overload str.replace to take a Map?
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 Meadowswrote: > 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 es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Re: Proposal: Phase-Invariant Einno Soliton Templates
I am not an academic troll: i am actually underemployed at the monent; i enjoy the labor very much, as well as the people i work with. -- Abdul S. ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Overload str.replace to take a Map?
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)`.) - Isiah Meadows m...@isiahmeadows.com www.isiahmeadows.com On Sun, May 20, 2018 at 11:57 PM, Jordan Harbandwrote: > Something that escapes HTML wouldn't belong in the language, it would > belong in browsers (the HTML spec, probably). This list is for > language-level proposals, so I don't think this is the right list to > suggest it. > > Are there use cases for things in this thread that aren't browser-specific? > > On Sun, May 20, 2018 at 8:49 PM, Isiah Meadows > wrote: > >> Next challenge: how does it compare to these two? >> >> ```js >> // Simplified version >> function simpleEscape(text) { >> return text.replace(/<(?:\/?script)?||>||/gu, m => { >> switch (m) { >> case '<': return '[lt]', >> case '': return '[lt]', >> case '>': return '[gt]', >> case '': return '[gt]', >> case '