UNPKG

replace-quotes

Version:
90 lines (88 loc) 3.62 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { backtick: () => backtick, default: () => replaceQuotes, double: () => double, single: () => single }); module.exports = __toCommonJS(index_exports); var import_zod = require("zod"); var import_node_inspect_extracted = require("node-inspect-extracted"); var matchSchema = import_zod.z.union([ import_zod.z.string(), import_zod.z.tuple([import_zod.z.string(), import_zod.z.string()]), import_zod.z.tuple([import_zod.z.string(), import_zod.z.string(), import_zod.z.boolean()]) ]); var replaceSchema = import_zod.z.union([import_zod.z.string(), import_zod.z.tuple([import_zod.z.string(), import_zod.z.string()])]); var matchString = "string | [string, string] | [string, string, boolean]"; var replaceString = "string | [string, string]"; var double = String.fromCharCode(34); var single = String.fromCharCode(39); var backtick = String.fromCharCode(96); function replaceQuotes(...quotes) { if (quotes.length < 2) throw new Error("At least two arguments are required"); quotes.forEach((quote, index) => { const isLast = index + 1 === quotes.length; const result = (isLast ? replaceSchema : matchSchema).safeParse(quote); if (!result.success) { throw new Error( "Invalid argument at index " + index + "\nExpected: " + (isLast ? replaceString : matchString) + "\nGot: " + (0, import_node_inspect_extracted.inspect)(quote) ); } }); const sourceQuotesPairs = quotes.map((f) => Array.isArray(f) ? f : [f, f]); const [targetStartQuote, targetEndQuote] = sourceQuotesPairs.pop(); const getLiteralsRegex = new RegExp( sourceQuotesPairs.map( // to match string literal ([start, end]) => "(" + start + "(?:[^" + end + "\\\\]|\\\\.)*" + end + ")" ).concat( // to match regex literal "/(?:[^/\\\\]|\\\\.)*/", "\\[Function:? .*?\\]", "\\[class .*?\\]", "Symbol\\(.*?\\)" ).join("|"), "g" ); const getUnescapedTargetEndQuotesRegex = new RegExp( "(?<!\\\\)(?:\\\\\\\\)*" + targetEndQuote, "g" ); return (text) => text.replaceAll(getLiteralsRegex, (...match) => { const groups = match.slice(1, sourceQuotesPairs.length + 1); if (!groups.some(Boolean)) return match[0]; const [matchedSourceStartQuote, matchedSourceEndQuote, onlyForContext] = sourceQuotesPairs[groups.findIndex(Boolean)]; if (onlyForContext) return match[0]; let newString = match[0].slice(1, -1).replaceAll( new RegExp("\\\\" + matchedSourceEndQuote, "g"), matchedSourceEndQuote ).replaceAll(getUnescapedTargetEndQuotesRegex, (m) => "\\" + m); return targetStartQuote + newString + targetEndQuote; }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { backtick, double, single });