@soratani-code/samtools 1.3.21 → 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 +8 -1
- package/lib/tools.js +43 -19
- package/package.json +1 -1
package/lib/tools.d.ts
CHANGED
|
@@ -1,5 +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):
|
|
4
|
+
export declare function parseTemplateToOptions(source: string, template: string): {
|
|
5
|
+
text: string;
|
|
6
|
+
options: any;
|
|
7
|
+
};
|
|
5
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.mergeTemplateToSource = exports.parseTemplateToOptions = 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) {
|
|
@@ -72,39 +72,54 @@ function insertArray(arr, index, item) {
|
|
|
72
72
|
}
|
|
73
73
|
exports.insertArray = insertArray;
|
|
74
74
|
function parseTemplateToOptions(source, template) {
|
|
75
|
-
var _a;
|
|
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();
|
|
76
80
|
const valueTokens = [];
|
|
77
81
|
const valueRe = /\$\{\s*([^}]*)\s*\}/g;
|
|
78
82
|
let m;
|
|
79
|
-
while ((m = valueRe.exec(template
|
|
83
|
+
while ((m = valueRe.exec(template)) !== null) {
|
|
80
84
|
valueTokens.push(m[1]);
|
|
81
85
|
}
|
|
82
|
-
const
|
|
83
|
-
? source.split(/\s*[
|
|
86
|
+
const rawLabelPieces = source
|
|
87
|
+
? source.split(/\s*[+\-−]\s*/).map((s) => cleanLabelPiece(s)).filter(Boolean)
|
|
84
88
|
: [];
|
|
85
|
-
const
|
|
86
|
-
const
|
|
87
|
-
const opsFromLabel = source ? (source.match(/[+-]/g) || []) : [];
|
|
88
|
-
const opsFromValue = template ? (template.match(/[+-]/g) || []) : [];
|
|
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])) : [];
|
|
89
91
|
const ops = opsFromLabel.length ? opsFromLabel : opsFromValue;
|
|
90
|
-
const
|
|
91
|
-
if (
|
|
92
|
+
const parseToken = (tok) => {
|
|
93
|
+
if (tok == null)
|
|
92
94
|
return null;
|
|
93
|
-
const t =
|
|
95
|
+
const t = String(tok).trim();
|
|
94
96
|
if (t === '' || t.toLowerCase() === 'null')
|
|
95
97
|
return null;
|
|
96
98
|
const n = Number(t);
|
|
97
99
|
return Number.isFinite(n) ? n : null;
|
|
98
100
|
};
|
|
99
|
-
const len = Math.max(
|
|
100
|
-
const
|
|
101
|
+
const len = Math.max(valueTokens.length, rawLabelPieces.length);
|
|
102
|
+
const options = [];
|
|
101
103
|
for (let i = 0; i < len; i++) {
|
|
102
|
-
const label = (_a =
|
|
103
|
-
const raw = valueTokens[i];
|
|
104
|
-
const value =
|
|
105
|
-
|
|
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 });
|
|
106
108
|
}
|
|
107
|
-
|
|
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
|
+
};
|
|
108
123
|
}
|
|
109
124
|
exports.parseTemplateToOptions = parseTemplateToOptions;
|
|
110
125
|
function mergeTemplateToSource(source, template) {
|
|
@@ -130,3 +145,12 @@ function mergeTemplateToSource(source, template) {
|
|
|
130
145
|
return replaced;
|
|
131
146
|
}
|
|
132
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;
|