@soratani-code/samtools 1.3.20 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/tools.d.ts +9 -0
- package/lib/tools.js +84 -1
- package/package.json +1 -1
package/lib/tools.d.ts
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
1
|
export declare function isDeepEqual(a: any, b: any): any;
|
|
2
2
|
export declare function deepDelete(data: any, path: string): any;
|
|
3
3
|
export declare function insertArray(arr: any[], index: number, item: any): any[];
|
|
4
|
+
export declare function parseTemplateToOptions(source: string, template: string): {
|
|
5
|
+
text: string;
|
|
6
|
+
options: any;
|
|
7
|
+
};
|
|
8
|
+
export declare function mergeTemplateToSource(source: string, template: string): string;
|
|
9
|
+
export declare function replaceTemplateFromOptions(template: string, options: {
|
|
10
|
+
label: string;
|
|
11
|
+
value: any;
|
|
12
|
+
}[]): string;
|
package/lib/tools.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.insertArray = exports.deepDelete = exports.isDeepEqual = void 0;
|
|
3
|
+
exports.replaceTemplateFromOptions = exports.mergeTemplateToSource = exports.parseTemplateToOptions = exports.insertArray = exports.deepDelete = exports.isDeepEqual = void 0;
|
|
4
4
|
const lodash_1 = require("lodash");
|
|
5
5
|
const type_1 = require("./type");
|
|
6
6
|
function isDeepEqual(a, b) {
|
|
@@ -71,3 +71,86 @@ function insertArray(arr, index, item) {
|
|
|
71
71
|
return clone;
|
|
72
72
|
}
|
|
73
73
|
exports.insertArray = insertArray;
|
|
74
|
+
function parseTemplateToOptions(source, template) {
|
|
75
|
+
var _a, _b;
|
|
76
|
+
if (!template)
|
|
77
|
+
return { text: template || '', options: [] };
|
|
78
|
+
const normalizeOp = (ch) => (ch === '−' ? '-' : ch);
|
|
79
|
+
const cleanLabelPiece = (s) => (s || '').replace(/[()()]/g, '').trim();
|
|
80
|
+
const valueTokens = [];
|
|
81
|
+
const valueRe = /\$\{\s*([^}]*)\s*\}/g;
|
|
82
|
+
let m;
|
|
83
|
+
while ((m = valueRe.exec(template)) !== null) {
|
|
84
|
+
valueTokens.push(m[1]);
|
|
85
|
+
}
|
|
86
|
+
const rawLabelPieces = source
|
|
87
|
+
? source.split(/\s*[+\-−]\s*/).map((s) => cleanLabelPiece(s)).filter(Boolean)
|
|
88
|
+
: [];
|
|
89
|
+
const opsFromLabel = source ? Array.from(source.matchAll(/[+\-−]/g)).map(x => normalizeOp(x[0])) : [];
|
|
90
|
+
const opsFromValue = template ? Array.from(template.matchAll(/[+\-−]/g)).map(x => normalizeOp(x[0])) : [];
|
|
91
|
+
const ops = opsFromLabel.length ? opsFromLabel : opsFromValue;
|
|
92
|
+
const parseToken = (tok) => {
|
|
93
|
+
if (tok == null)
|
|
94
|
+
return null;
|
|
95
|
+
const t = String(tok).trim();
|
|
96
|
+
if (t === '' || t.toLowerCase() === 'null')
|
|
97
|
+
return null;
|
|
98
|
+
const n = Number(t);
|
|
99
|
+
return Number.isFinite(n) ? n : null;
|
|
100
|
+
};
|
|
101
|
+
const len = Math.max(valueTokens.length, rawLabelPieces.length);
|
|
102
|
+
const options = [];
|
|
103
|
+
for (let i = 0; i < len; i++) {
|
|
104
|
+
const label = (_a = rawLabelPieces[i]) !== null && _a !== void 0 ? _a : '';
|
|
105
|
+
const raw = (_b = valueTokens[i]) !== null && _b !== void 0 ? _b : '';
|
|
106
|
+
const value = parseToken(raw);
|
|
107
|
+
options.push({ label, raw, value });
|
|
108
|
+
}
|
|
109
|
+
let idx = 0;
|
|
110
|
+
const text = template.replace(/\$\{\s*([^}]*)\s*\}/g, (match, inner) => {
|
|
111
|
+
const token = inner == null ? '' : String(inner).trim();
|
|
112
|
+
const labelForThis = options[idx] ? (options[idx].label || '') : '';
|
|
113
|
+
idx += 1;
|
|
114
|
+
if (token === '' || token.toLowerCase() === 'null') {
|
|
115
|
+
return '${' + labelForThis + '}';
|
|
116
|
+
}
|
|
117
|
+
return match;
|
|
118
|
+
});
|
|
119
|
+
return {
|
|
120
|
+
text,
|
|
121
|
+
options,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
exports.parseTemplateToOptions = parseTemplateToOptions;
|
|
125
|
+
function mergeTemplateToSource(source, template) {
|
|
126
|
+
if (typeof source !== 'string' || typeof template !== 'string')
|
|
127
|
+
return template;
|
|
128
|
+
const regex = /\/\s*(?:\(\s*)?([^()\/-]+?)\s*-\s*([^()\/]+?)(?:\s*\))?/g;
|
|
129
|
+
const leftOperands = [];
|
|
130
|
+
let m;
|
|
131
|
+
while ((m = regex.exec(source)) !== null) {
|
|
132
|
+
leftOperands.push(m[1].trim());
|
|
133
|
+
}
|
|
134
|
+
if (!leftOperands.length) {
|
|
135
|
+
return template;
|
|
136
|
+
}
|
|
137
|
+
let idx = 0;
|
|
138
|
+
const replaced = template.replace(/\$\{null\}|\bnull\b/g, (match) => {
|
|
139
|
+
if (idx >= leftOperands.length) {
|
|
140
|
+
return match;
|
|
141
|
+
}
|
|
142
|
+
const val = leftOperands[idx++];
|
|
143
|
+
return val;
|
|
144
|
+
});
|
|
145
|
+
return replaced;
|
|
146
|
+
}
|
|
147
|
+
exports.mergeTemplateToSource = mergeTemplateToSource;
|
|
148
|
+
function replaceTemplateFromOptions(template, options) {
|
|
149
|
+
return (0, lodash_1.reduce)(options, (tpl, opt) => {
|
|
150
|
+
const has = template.includes('${' + opt.value + '}');
|
|
151
|
+
if (has)
|
|
152
|
+
tpl;
|
|
153
|
+
return tpl.replace('${' + opt.label + '}', '${' + opt.value + '}');
|
|
154
|
+
}, (0, lodash_1.cloneDeep)(template));
|
|
155
|
+
}
|
|
156
|
+
exports.replaceTemplateFromOptions = replaceTemplateFromOptions;
|