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AAC,CAAC,CAAC,CAAC,KAAK,CAAC;EACnB,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,WAAW,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;EAC/C,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;EACjC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE;EACtC,KAAK,CAAC;AACR","file":"Replaceable.js","sourcesContent":["import { Transform } from 'stream'\nimport { checkRule } from './lib'\nimport cleanStack from '@artdeco/clean-stack'\n\n/**\n * @typedef {import('.').Rule} Rule\n */\n\nexport default class Replaceable extends Transform {\n /**\n * Replaceable class that extends Transform and pushes data when it's done replacing each incoming chunk. If the replacement is passed as a function, it will work in the same way as the replacer for `string.replace` method (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace), taking the `match` as the first argument, and matched `p1`, `p2`, _etc_ parameters as following arguments. The replacer can also be an async function.\n * @constructor\n * @param {Rule|Rule[]} rules A single replacement rule, or multiple rules.\n * @example\n *\n * // markdown __ to html emphasise implementation\n * const stream = replaceStream({\n * re: /__(\\S+)__/g,\n * replacement(match, p1) {\n * return `<em>${p1}</em>`\n * },\n * })\n */\n constructor(rules) {\n super()\n const re = Array.isArray(rules) ? rules : [rules]\n const fr = re.filter(checkRule)\n this.rules = fr\n }\n\n /**\n * Stop executing further after the current rule.\n */\n brake() {\n this._broke = true\n }\n\n async reduce(chunk) {\n /** @type {string} */\n const s = await this.rules.reduce(async (acc, { re, replacement }) => {\n /** @type {string} */\n let string = await acc\n if (this._broke) return string\n\n if (typeof replacement == 'string') {\n string = string.replace(re, replacement)\n } else {\n /** @type {function} */\n const R = replacement.bind(this)\n const promises = []\n let commonError\n const t = string.replace(re, (match, ...args) => {\n commonError = new Error()\n try {\n if (this._broke) return match\n const p = R(match, ...args)\n if (p instanceof Promise) {\n promises.push(p)\n }\n return p\n } catch (e) { // hide stack for sync stack traces\n hideStack(commonError, e)\n }\n })\n if (promises.length) {\n try { // hide stack only for when throw happens before awaits\n const data = await Promise.all(promises)\n string = string.replace(re, () => data.shift())\n } catch (e) {\n hideStack(commonError, e)\n }\n } else {\n string = t\n }\n }\n return string\n }, `${chunk}`)\n\n return s\n }\n async _transform(chunk, _, next) {\n try {\n const s = await this.reduce(chunk)\n this.push(s)\n next()\n } catch (e) {\n const s = cleanStack(e.stack)\n e.stack = s\n next(e)\n }\n }\n}\n\nconst hideStack = (commonError, thrownError) => {\n if (!(thrownError instanceof Error)) throw thrownError\n const [, , commonLine] = commonError.stack.split('\\n', 3)\n const i = thrownError.stack.indexOf(commonLine)\n if (i == -1) throw thrownError\n const stack = thrownError.stack.substr(0, i - 1)\n const li = stack.lastIndexOf('\\n')\n thrownError.stack = stack.substr(0, li)\n throw thrownError\n}"]}
\No newline at end of file