UNPKG

1.51 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var util = require('@spare/util');
6var enumChars = require('@spare/enum-chars');
7
8const indexNonTab = tx => {
9 let i = 0;
10
11 for (let {
12 length
13 } = tx; i < length; i++) if (!util.isTab(tx.charAt(i))) return i;
14
15 return i;
16};
17
18const afterNonTab = tx => tx.substring(util.deNaTab(tx));
19
20function narrow(tx, lb, rb) {
21 const [li, ri] = [tx.indexOf(lb), tx.lastIndexOf(rb)];
22 return li > 0 && ri > 0 ? tx.slice(li, ri + rb.length) : tx;
23}
24
25function narrowExclude(tx, lb, rb) {
26 const [li, ri] = [tx.indexOf(lb), tx.lastIndexOf(rb)];
27 return li && ri ? tx.slice(li + lb.length, ri) : tx;
28}
29
30const trim = Function.prototype.call.bind(String.prototype.trim);
31
32const wL = (tx = '') => {
33 console.log(tx);
34};
35
36const tag = (label, item) => {
37 const i = util.deNaTab(label);
38 let [key, text] = [util.endsBracs(label) ? label : `${label.substring(0, i)}[${label.substring(i)}]`, `${item}`];
39
40 if (text.includes('\n')) {
41 const t = ' '.repeat(i);
42 text = (text.endsWith('}') || text.endsWith(']')) && !text.endsWith(']]') ? util.afterNaTab(text.split(enumChars.RN).map(x => t + x).join(enumChars.RN)) : ['', ...text.split(enumChars.RN).map(x => t + enumChars.TB + x), t].join(enumChars.RN);
43 }
44
45 return `${key} (${text})`;
46};
47
48exports.afterNonTab = afterNonTab;
49exports.indexNonTab = indexNonTab;
50exports.narrow = narrow;
51exports.narrowExclude = narrowExclude;
52exports.tag = tag;
53exports.trim = trim;
54exports.wL = wL;