@visulima/string
Version:
Functions for manipulating strings.
123 lines (118 loc) • 4.71 kB
JavaScript
;
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
const constants = require('./packem_shared/constants-nihvIvk5.cjs');
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const hop = Object.prototype.hasOwnProperty;
const has = /* @__PURE__ */ __name((object, property) => hop.call(object, property), "has");
const extend = /* @__PURE__ */ __name((target, source) => {
for (const property in source) {
if (has(source, property)) {
target[property] = source[property];
}
}
return target;
}, "extend");
const internalOutdentArray = /* @__PURE__ */ __name((strings, firstInterpolatedValueSetsIndentationLevel, options) => {
if (strings.length === 0) {
return [];
}
let indentationLevel = 0;
const match = constants.RE_DETECT_INDENTATION.exec(strings[0]);
if (match) {
indentationLevel = match[1].length;
}
const reSource = `(\\r\\n|\\r|\\n).{0,${indentationLevel}}`;
const reMatchIndent = new RegExp(reSource, "g");
if (firstInterpolatedValueSetsIndentationLevel) {
strings = strings.slice(1);
}
const { newline, trimLeadingNewline, trimTrailingNewline } = options;
const normalizeNewlines = typeof newline === "string";
const l = strings.length;
const outdentedStrings = Array.from({ length: l });
for (let index = 0; index < l; index++) {
let v = strings[index];
v = v.replace(reMatchIndent, "$1");
if (index === 0 && trimLeadingNewline) {
v = v.replace(constants.RE_LEADING_NEWLINE, "");
}
if (index === l - 1 && trimTrailingNewline) {
v = v.replace(constants.RE_TRAILING_NEWLINE, "");
}
if (normalizeNewlines) {
v = v.replaceAll(constants.RE_MATCH_NEWLINES, newline);
}
outdentedStrings[index] = v;
}
return outdentedStrings;
}, "internalOutdentArray");
const concatStringsAndValues = /* @__PURE__ */ __name((strings, values) => {
const chunks = Array.from({ length: strings.length + values.length });
let index = 0;
const l = strings.length;
for (; index < l - 1; index++) {
chunks[index * 2] = strings[index];
chunks[index * 2 + 1] = String(values[index]);
}
chunks[index * 2] = strings[index];
return chunks.join("");
}, "concatStringsAndValues");
const isTemplateStringsArray = /* @__PURE__ */ __name((v) => has(v, "raw") && has(v, "length"), "isTemplateStringsArray");
const templateCache = /* @__PURE__ */ new WeakMap();
const createInstance = /* @__PURE__ */ __name((options) => {
const enableCache = options.cache !== false;
const cache = enableCache ? options.cacheStore ?? templateCache : null;
const hasNewlineOption = options.newline !== void 0;
function outdent2(stringsOrOptions, ...values) {
if (isTemplateStringsArray(stringsOrOptions)) {
const strings = stringsOrOptions;
const valuesLength = values.length;
if (strings.length === 1 && strings[0] === "") {
return "";
}
if (valuesLength === 0) {
if (enableCache && cache && !hasNewlineOption) {
const cached = cache.get(strings);
if (cached) {
return cached[0];
}
}
const result = internalOutdentArray(strings, false, options);
if (enableCache && cache) {
cache.set(strings, result);
}
return result[0];
}
const firstValueIsOutdent = values[0] === outdent2 || values[0] === defaultOutdent;
const firstInterpolatedValueSetsIndentationLevel = firstValueIsOutdent && constants.RE_ONLY_WHITESPACE_WITH_AT_LEAST_ONE_NEWLINE.test(strings[0]) && constants.RE_STARTS_WITH_NEWLINE_OR_IS_EMPTY.test(strings[1]);
let renderedArray;
if (enableCache && cache && !hasNewlineOption) {
renderedArray = cache.get(strings);
}
if (!renderedArray) {
renderedArray = internalOutdentArray(strings, firstInterpolatedValueSetsIndentationLevel, options);
if (enableCache && cache) {
cache.set(strings, renderedArray);
}
}
return concatStringsAndValues(renderedArray, firstInterpolatedValueSetsIndentationLevel ? values.slice(1) : values);
}
return createInstance(extend(extend({}, options), stringsOrOptions));
}
__name(outdent2, "outdent");
return extend(outdent2, {
string(string_) {
if (string_ === "" || !string_) {
return "";
}
return internalOutdentArray([string_], false, options)[0];
}
});
}, "createInstance");
const defaultOutdent = createInstance({
trimLeadingNewline: true,
trimTrailingNewline: true
});
const outdent = defaultOutdent;
exports.outdent = outdent;