UNPKG

tequery

Version:

text transform query

105 lines 4.41 kB
"use strict"; /* eslint-disable @typescript-eslint/no-explicit-any */ Object.defineProperty(exports, "__esModule", { value: true }); exports._cq = exports.cq = exports.jsonf = exports.json = exports._shiftr = exports.shiftr = exports._shiftl = exports.shiftl = exports._pack = exports.pack = exports.ln = exports.lineNum = exports._count = exports.count = exports.len = void 0; const textCurry = (func) => (...params) => (text) => func(text, ...params); const len = (text) => text.length; exports.len = len; const count = (text, regexStr) => (text.match(new RegExp(regexStr, 'g')) || []).length; exports.count = count; exports._count = textCurry(exports.count); const lineNum = (text) => text.split('\n').length; exports.lineNum = lineNum; exports.ln = exports.lineNum; const pack = (text, n = 1) => text.replace(new RegExp(`\n{${n},}`, 'g'), '\n'.repeat(n)); exports.pack = pack; exports._pack = textCurry(exports.pack); const shiftl = (text, to = '\t', n = 1) => text.split(to).slice(n).join(to); exports.shiftl = shiftl; exports._shiftl = textCurry(exports.shiftl); const shiftr = (text, to = '\t', n = 1) => text.split(to).slice(0, -n).join(to); exports.shiftr = shiftr; exports._shiftr = textCurry(exports.shiftr); exports.json = JSON.stringify; const jsonf = (v) => JSON.stringify(v, null, '\t'); exports.jsonf = jsonf; /** cell query */ const OP_SHIFT = ['<', '>']; const OP_PICK = ['_', '.']; const OPS = [OP_SHIFT, OP_PICK].flat(); const isOpShift = (v) => OP_SHIFT.includes(v); const isOpPick = (v) => OP_PICK.includes(v); // const isOp = (v: string): v is Op => OPS.includes(v) const defaultSpChars = ',st'; const toCqOption = (option) => { var _a, _b, _c, _d, _e; return { spChars: (_a = option.spChars) !== null && _a !== void 0 ? _a : defaultSpChars, opsShift: typeof option.opsShift === 'string' ? (_b = option.opsShift) === null || _b === void 0 ? void 0 : _b.split('').filter(isOpShift) : (_c = option.opsShift) !== null && _c !== void 0 ? _c : [], opsPick: typeof option.opsPick === 'string' ? (_d = option.opsPick) === null || _d === void 0 ? void 0 : _d.split('').filter(isOpPick) : (_e = option.opsPick) !== null && _e !== void 0 ? _e : [], }; }; const emptyOr = (v, defaultValue) => Boolean(v) && typeof v === 'string' ? v : defaultValue; const cqOptionParse = (option) => { if (typeof option !== 'string') return toCqOption(option); const m = option.match(new RegExp(`^(.*?)([${OPS.join('')}]*)$`)); const spChars = emptyOr(m === null || m === void 0 ? void 0 : m[1], defaultSpChars); const opsShift = emptyOr(m === null || m === void 0 ? void 0 : m[2], '').split('').filter(isOpShift); const opsPick = emptyOr(m === null || m === void 0 ? void 0 : m[2], '').split('').filter(isOpPick); return { spChars, opsPick, opsShift }; }; const cq = (text, option) => { var _a, _b; const { spChars, opsShift, opsPick } = cqOptionParse(option); const splits = spChars .split('') .map((v) => ({ ',': ',', s: ' ', t: '\\t' }[v])) .filter(Boolean) .join(''); // sps 追加 const spRe = new RegExp(`[${splits}]`, 'g'); const sps = ((_a = text.match(spRe)) === null || _a === void 0 ? void 0 : _a.map((v) => v)) || []; const cells = text.split(spRe); opsShift.forEach((op) => { switch (op) { case '<': { cells.shift(); sps.shift(); break; } case '>': { cells.pop(); sps.pop(); break; } } }); const pickCells = []; const pickSps = []; opsPick.forEach((op) => { switch (op) { case '.': { pickCells.push(cells.shift()); pickSps.push(sps.shift()); break; } case '_': { cells.shift(); sps.shift(); break; } } }); const [resCells, resSps] = opsPick.length === 0 ? [cells, sps] : [pickCells, pickSps]; // if (resCells.length === 0) return '' const tail = (_b = resCells.pop()) !== null && _b !== void 0 ? _b : ''; return resCells.map((v, i) => `${v}${resSps[i]}`).join('') + tail; }; exports.cq = cq; exports._cq = textCurry(exports.cq); //# sourceMappingURL=funcs.js.map