not a huge fan either, but here are 2 real-world non-frontend examples: the first is an excerpt from a backend-tool to automagically convert documentation from developers.github.com into swagger-docs [1]. its used to auto-generate nodejs-clients [2] for github’s ~300 apis, rather than doing it manually. tc39 may-or-may-not have similar use-case in its ongoing effort to make their online-specs machine-readable.
```js ... htmlToDescription = function (options) { /* * this function will format options.html to swagger markdown-description */ return options.html // format \n .replace((/\n\n+/g), '\n') .replace((/\n<li>/g), '\n\n<li>') .replace((/\n<(?:p|pre)>/g), '\n\n') .replace((/\n([^\n])/g), ' $1') .replace((/\n /g), '\n') // format header accept .replace((/( header:)<\/p>\n(<code>)/g), '$1 $2') // format <a> .replace((/<a href="\//g), '<a href="https://developer.github.com/') .replace((/<a href="#/g), '<a href="' + options.url + '#') .replace((/<a href="(.*?)".*?>(.*?)<\/a>/g), '[$2]($1)') // format <xxx> .replace((/<code>(.*?)<\/code>/g), '```$1```') .replace((/<li>(.*?)<\/li>/g), ' - $1') .replace((/<strong>(.*?)<\/strong>/g), '**$1**') .replace((/<[^<>]*?>/g), '') // format whitespace .replace((/ {1,}/g), ' ') .split('\n') .map(function (element) { return element.trim(); }) .filter(local.echo) .map(function (element) { return element + '\n'; }); }; ``` this second example [3] cannot be solved by the discussed proposal in its current form. it uses a list of regexp's to merge sections of one document into another document. i was basically too lazy to create/maintain a complicated mustached-based template for README.md, so instead used an existing, real-world README.md as a “template”, with a list of regexp-rules to customize it for any given npm-package. ```js /* * this excerpt from * https://github.com/kaizhu256/node-utility2/blob/2018.1.13/lib.utility2.js#L3326 * will inplace-merge pieces of document dataFrom -> document dataTo * * * document dataFrom + document dataTo -> inplace-merged document dataTo * * # name1 # name2 # name1 * <description1> <description1> <description1> * * # live web demo #live web demo # live web demo * - none - [foo.com](foo.com) - none * * ... ... ... */ // search-and-replace - customize dataTo [ // customize name and description (/.*?\n.*?\n/), // customize cdn-download (/\n# cdn download\n[\S\s]*?\n\n\n\n/), // customize live web demo (/\n# live web demo\n[\S\s]*?\n\n\n\n/), // customize todo (/\n#### todo\n[\S\s]*?\n\n\n\n/), // customize quickstart-example-js new RegExp('\\n {8}local\\.global\\.local = local;\\n' + '[^`]*?\\n {4}\\/\\/ run browser js\\-env code - init-test\\n'), new RegExp('\\n {8}local\\.testRunBrowser = function \\(event\\) \\{\\n' + '[^`]*?^ {12}if \\(!event \\|\\| \\(event &&\\n', 'm'), (/\n {12}\/\/ custom-case\n[^`]*?\n {12}\}\n/), // customize quickstart-html-style (/\n<\/style>\\n\\\n<style>\\n\\\n[^`]*?\\n\\\n<\/style>\\n\\\n/), // customize quickstart-html-body (/\nutility2-comment -->(?:\\n\\\n){4}[^`]*?^<!-- utility2-comment\\n\\\n/m), // customize build script (/\n# internal build script\n[\S\s]*?^- build_ci\.sh\n/m), (/\nshBuildCiAfter\(\) \{\(set -e\n[^`]*?\n\)\}\n/), (/\nshBuildCiBefore\(\) \{\(set -e\n[^`]*?\n\)\}\n/) ].forEach(function (rgx) { // handle large string-replace options.dataFrom.replace(rgx, function (match0) { options.dataTo.replace(rgx, function (match1) { options.dataTo = options.dataTo.split(match1); options.dataTo[0] += match0; options.dataTo[0] += options.dataTo.splice(1, 1)[0]; options.dataTo = options.dataTo.join(match1); }); }); }); ``` [1] chained-replace excerpt from tool to transform documentation from developers.github.com https://github.com/kaizhu256/node-swgg-github-all/blob/2018.2.2/test.js#L86 <https://github.com/kaizhu256/node-swgg-github-all/blob/2018.2.2/test.js#L86> [2] list of auto-generated nodejs-clients for github’s apis https://www.npmjs.com/package/swgg-github-activity <https://www.npmjs.com/package/swgg-github-activity> https://www.npmjs.com/package/swgg-github-all <https://www.npmjs.com/package/swgg-github-all> https://www.npmjs.com/package/swgg-github-apps <https://www.npmjs.com/package/swgg-github-apps> https://www.npmjs.com/package/swgg-github-gists <https://www.npmjs.com/package/swgg-github-gists> https://www.npmjs.com/package/swgg-github-git <https://www.npmjs.com/package/swgg-github-git> https://www.npmjs.com/package/swgg-github-issues <https://www.npmjs.com/package/swgg-github-issues> https://www.npmjs.com/package/swgg-github-migration <https://www.npmjs.com/package/swgg-github-migration> https://www.npmjs.com/package/swgg-github-misc <https://www.npmjs.com/package/swgg-github-misc> https://www.npmjs.com/package/swgg-github-projects <https://www.npmjs.com/package/swgg-github-projects> https://www.npmjs.com/package/swgg-github-pulls <https://www.npmjs.com/package/swgg-github-pulls> https://www.npmjs.com/package/swgg-github-reactions <https://www.npmjs.com/package/swgg-github-reactions> https://www.npmjs.com/package/swgg-github-scim <https://www.npmjs.com/package/swgg-github-scim> https://www.npmjs.com/package/swgg-github-search <https://www.npmjs.com/package/swgg-github-search> https://www.npmjs.com/package/swgg-github-teams <https://www.npmjs.com/package/swgg-github-teams> https://www.npmjs.com/package/swgg-github-users <https://www.npmjs.com/package/swgg-github-users> [3] inplace-merge documents using list of regexp's https://github.com/kaizhu256/node-utility2/blob/2018.1.13/lib.utility2.js#L3326 <https://github.com/kaizhu256/node-utility2/blob/2018.1.13/lib.utility2.js#L3326> kai zhu kaizhu...@gmail.com > On 21 May 2018, at 1:01 PM, Bob Myers <r...@gol.com> wrote: > > 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 <isiahmead...@gmail.com > <mailto:isiahmead...@gmail.com>> 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 > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss