How to use string-replace-async - 10 common examples

To help you get started, we’ve selected a few string-replace-async examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github springload / metatemplate / src / template-formats / html / template.html.ts View on Github external
)
          ).join("");
        }
        throw Error(
          `Unknown templateUsages type ${typeof templateUsages} from aCode.variables[${key}]. aCode = ${JSON.stringify(
            aCode
          )}. templateUsages: ${JSON.stringify(templateUsages)}`
        );
      };

      // When there are attribute variable placeholders side-by-side
      // then add a space.
      // FIXME: Use a bloody HTML parser for all this code
      html = html.replace(/\}\}\{\{/g, "}} {{");

      html = await stringReplaceAsync(
        html,
        /{{(.*?)}}/gi,
        async (match, middle) => {
          const dynamicKey: DynamicKey = parseDynamicKey(middle);
          validateDynamicKey(dynamicKey);
          return draw(dynamicKey, true, dynamicKey.type as EnumOption[]);
        }
      );

      // Some attributes will be valueless eg disabled="" which is considered
      // false so we shouldn't render the attribute at all.
      html = html.replace(/[a-zA-Z][a-zA-Z0-9-]+?="\s*"/gi, "");
      // and certain boolean attributes should be converted to valueless
      html = html.replace(/([a-zA-Z][a-zA-Z0-9-]+?)="true"/gi, (match, p1) => {
        if (match.includes("value=")) {
          // TODO: compare against HTML5 boolean attributes
github springload / metatemplate / src / template-formats / html / template.html.ts View on Github external
if (!key) throw Error(`Couldn't find key in "${match}"`);

          const value = (aCode as TemplateUsageElement).variables[key];

          if (!!value) {
            const response = match
              .substring(match.indexOf(">") + 1)
              .replace("", "");
            return response;
          } else {
            return "";
          }
        }
      );

      html = await stringReplaceAsync(
        html,
        //gi,
        async match => {
          // self-closing 
          const key = getAttr(match, "key");
          if (!key) throw Error(`Couldn't find key in "${match}"`);
          return await renderByVariableElementKey(key);
        }
      );

      html = await stringReplaceAsync(
        html,
        /([\s\S]*?)<\/mt-variable>/gi,
        async (match, defaultValue) => {
          // expanded  placeholder 
          const key = getAttr(match, "key");
github springload / metatemplate / src / template-formats / html / template.html.ts View on Github external
}
      );

      // Some attributes will be valueless eg disabled="" which is considered
      // false so we shouldn't render the attribute at all.
      html = html.replace(/[a-zA-Z][a-zA-Z0-9-]+?="\s*"/gi, "");
      // and certain boolean attributes should be converted to valueless
      html = html.replace(/([a-zA-Z][a-zA-Z0-9-]+?)="true"/gi, (match, p1) => {
        if (match.includes("value=")) {
          // TODO: compare against HTML5 boolean attributes
          return match;
        }
        return p1;
      });

      html = await stringReplaceAsync(
        html,
        //gi,
        async match => {
          // FIXME: obviously regexes and HTML don't mix well
          // so if nested this will break. Replace with proper parser.
          const key = getAttr(match, "key");
          if (!key) throw Error(`Couldn't find key in "${match}"`);

          const value = (aCode as TemplateUsageElement).variables[key];

          if (!!value) {
            const response = match
              .substring(match.indexOf(">") + 1)
              .replace("", "");
            return response;
          } else {
github imachug / Kiwipedia / main / src / common / wiki-engine / wikitext.js View on Github external
export async function wikiTextToHTML(wikitext, slug) {
	let html = InstaView.convert(wikitext);

	html = await stringReplaceAsync(html, /ARTICLENAMEGOESHERE(.*?)(['"])/g, async (all, article, quote) => {
		let wiki;

		if(article.indexOf(":") == -1) {
			// Local link
			wiki = slug;
		} else {
			// Interwiki
			article = article.replace(/^:/, "");

			wiki = article.substr(0, article.indexOf(":"));
			article = article.substr(article.indexOf(":") + 1);

			wiki = toSlug(wiki.replace("/", "MYAWESOMECONSTANT")).replace(toSlug("MYAWESOMECONSTANT"), "/");
		}

		article = toSlug(article);
github springload / metatemplate / src / template-formats / html / template.html.ts View on Github external
}
        }
      );

      html = await stringReplaceAsync(
        html,
        //gi,
        async match => {
          // self-closing 
          const key = getAttr(match, "key");
          if (!key) throw Error(`Couldn't find key in "${match}"`);
          return await renderByVariableElementKey(key);
        }
      );

      html = await stringReplaceAsync(
        html,
        /([\s\S]*?)<\/mt-variable>/gi,
        async (match, defaultValue) => {
          // expanded  placeholder 
          const key = getAttr(match, "key");
          if (!key) throw Error(`Couldn't find key in "${match}"`);
          return await renderByVariableElementKey(key, defaultValue);
        }
      );

      return html;
    };
github embark-framework / embark / src / lib / modules / specialconfigs / index.js View on Github external
replaceWithENSAddress(cmd, cb) {
    const self = this;
    let regex = /\'[a-zA-Z0-9.]+\.eth\'/g;
    return stringReplaceAsync.seq(cmd, regex, (ensDomain) => {
      ensDomain = ensDomain.slice(1, ensDomain.length - 1);
      return (new Promise((resolve, reject) => {
        self.events.request("ens:resolve", ensDomain, (err, address) => {
          if(err) {
            return reject(new Error(err));
          }
          address = `'${address}'`;
          return resolve(address);
        });
      }));
    }).then((address) => {
      cb(null, address);
    }).catch(cb);
  }
github analog-nico / pxl-for-emails / lib / PxlForEmails.js View on Github external
return BPromise.try(() => {

            let fullMetadata = getMetadataForOpenTracking(metadata)

            return stringReplaceAsync.seq(
                htmlEmail,
                this.options.openTracking.regexLinks,
                BPromise.coroutine(function *(match, p1, link, p3) {

                    if (this.options.openTracking.applyToFirstCandidateOnly && numCandidate > 0) {
                        return match
                    }

                    let applyInstruction = this.options.openTracking.shouldApply(link)
                    if (!applyInstruction) {
                        return match
                    }

                    if (!openPxl) {

                        if (applyInstruction.metadata) {
github embark-framework / embark / packages / plugins / embark-specialconfigs / src / listConfigs.js View on Github external
const replaceWithAddresses = (cmd) => {
      let regex = /\$\w+\[?\d?\]?/g;
      return stringReplaceAsync.seq(cmd, regex, (match, index) => {
        return (new Promise((resolve, reject) => {
          if (match.startsWith('$accounts')) {
            let accountIndex = cmd.substring(index + 10, index + 12);
            accountIndex = parseInt(accountIndex, 10);
            return this.events.request('blockchain:getAccounts', (err, accounts) => {
              if (err) {
                return reject('Error getting accounts: ' + err.message || err);
              }
              if (!accounts[accountIndex]) {
                return reject(__('No corresponding account at index %d', accountIndex));
              }
              resolve(accounts[accountIndex]);
            });
          }

          let referedContractName = match.slice(1);
github embark-framework / embark / packages / plugins / specialconfigs / src / listConfigs.js View on Github external
const replaceWithAddresses = (cmd) => {
      let regex = /\$\w+\[?\d?\]?/g;
      return stringReplaceAsync.seq(cmd, regex, (match, index) => {
        return (new Promise((resolve, reject) => {
          if (match.startsWith('$accounts')) {
            let accountIndex = cmd.substring(index + 10, index + 12);
            accountIndex = parseInt(accountIndex, 10);
            return this.events.request('blockchain:getAccounts', (err, accounts) => {
              if (err) {
                return reject('Error getting accounts: ' + err.message || err);
              }
              if (!accounts[accountIndex]) {
                return reject(__('No corresponding account at index %d', accountIndex));
              }
              resolve(accounts[accountIndex]);
            });
          }

          let referedContractName = match.slice(1);
github embark-framework / embark / packages / plugins / embark-specialconfigs / src / listConfigs.js View on Github external
const replaceWithENSAddress = (cmd) => {
      let regex = /\'[a-zA-Z0-9.]+\.eth\'/g;
      return stringReplaceAsync.seq(cmd, regex, (ensDomain) => {
        ensDomain = ensDomain.slice(1, ensDomain.length - 1);
        return (new Promise((resolve, reject) => {
          this.events.request("ens:resolve", ensDomain, (err, address) => {
            if(err) {
              return reject(new Error(err));
            }
            address = `'${address}'`;
            return resolve(address);
          });
        }));
      });
    };

string-replace-async

Asynchronous String.prototype.replace()

MIT
Latest version published 3 years ago

Package Health Score

47 / 100
Full package analysis

Popular string-replace-async functions