1 | import { isTab, deNaTab, endsBracs, afterNaTab } from '@spare/util';
|
2 | import { RN, TB } from '@spare/enum-chars';
|
3 |
|
4 | const 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 |
|
14 | const afterNonTab = tx => tx.substring(deNaTab(tx));
|
15 |
|
16 | function 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 |
|
21 | function 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 |
|
26 | const trim = Function.prototype.call.bind(String.prototype.trim);
|
27 |
|
28 | const wL = (tx = '') => {
|
29 | console.log(tx);
|
30 | };
|
31 |
|
32 | const 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 |
|
44 | export { afterNonTab, indexNonTab, narrow, narrowExclude, tag, trim, wL };
|