UNPKG

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