UNPKG

radashi-helper

Version:
33 lines (27 loc) 1.79 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _chunk5R2KEZLQcjs = require('./chunk-5R2KEZLQ.cjs'); // src/util/dedent.ts function dedent(text, ...values) { if (_chunk5R2KEZLQcjs.isArray.call(void 0, text)) { if (values.length > 0) { return dedent( text.reduce((acc, input, i) => { let value = String(_nullishCoalesce(values[i], () => ( ""))); const indent2 = value.includes("\n") && _optionalChain([input, 'access', _ => _.match, 'call', _2 => _2(/[ \t]*(?=[^\n]*$)/), 'optionalAccess', _3 => _3[0]]); if (indent2) { value = value.replace(/\n(?=[^\n]*?\S)/g, "\n" + indent2); } return acc + input + value; }, "") ); } text = text[0]; } const indent = _nullishCoalesce(values[0], () => ( detectIndent(text))); const output = indent ? text.replace(new RegExp(`^${indent}`, "gm"), "") : text; return output.replace(/^[ \t]*\n|\n[ \t]*$/g, ""); } function detectIndent(text) { return _optionalChain([text, 'access', _4 => _4.match, 'call', _5 => _5(/^[ \t]*(?=\S)/m), 'optionalAccess', _6 => _6[0]]); } exports.dedent = dedent;