@soratani-code/samtools 1.3.20 → 1.3.21
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 +2 -0
- package/lib/tools.js +60 -1
- package/package.json +1 -1
package/lib/tools.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
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): Record<string, any>[];
|
|
5
|
+
export declare function mergeTemplateToSource(source: string, template: string): 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.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,62 @@ function insertArray(arr, index, item) {
|
|
|
71
71
|
return clone;
|
|
72
72
|
}
|
|
73
73
|
exports.insertArray = insertArray;
|
|
74
|
+
function parseTemplateToOptions(source, template) {
|
|
75
|
+
var _a;
|
|
76
|
+
const valueTokens = [];
|
|
77
|
+
const valueRe = /\$\{\s*([^}]*)\s*\}/g;
|
|
78
|
+
let m;
|
|
79
|
+
while ((m = valueRe.exec(template !== null && template !== void 0 ? template : '')) !== null) {
|
|
80
|
+
valueTokens.push(m[1]);
|
|
81
|
+
}
|
|
82
|
+
const rawLabels = source
|
|
83
|
+
? source.split(/\s*[+-]\s*/).map(s => s.trim()).filter(Boolean)
|
|
84
|
+
: [];
|
|
85
|
+
const cleanLabel = (s) => s.replace(/[()()]/g, '').trim();
|
|
86
|
+
const labels = rawLabels.map(cleanLabel);
|
|
87
|
+
const opsFromLabel = source ? (source.match(/[+-]/g) || []) : [];
|
|
88
|
+
const opsFromValue = template ? (template.match(/[+-]/g) || []) : [];
|
|
89
|
+
const ops = opsFromLabel.length ? opsFromLabel : opsFromValue;
|
|
90
|
+
const parseValue = (token) => {
|
|
91
|
+
if (token == null)
|
|
92
|
+
return null;
|
|
93
|
+
const t = token.trim();
|
|
94
|
+
if (t === '' || t.toLowerCase() === 'null')
|
|
95
|
+
return null;
|
|
96
|
+
const n = Number(t);
|
|
97
|
+
return Number.isFinite(n) ? n : null;
|
|
98
|
+
};
|
|
99
|
+
const len = Math.max(labels.length, valueTokens.length);
|
|
100
|
+
const result = [];
|
|
101
|
+
for (let i = 0; i < len; i++) {
|
|
102
|
+
const label = (_a = labels[i]) !== null && _a !== void 0 ? _a : '';
|
|
103
|
+
const raw = valueTokens[i];
|
|
104
|
+
const value = parseValue(raw);
|
|
105
|
+
result.push({ label, value });
|
|
106
|
+
}
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
exports.parseTemplateToOptions = parseTemplateToOptions;
|
|
110
|
+
function mergeTemplateToSource(source, template) {
|
|
111
|
+
if (typeof source !== 'string' || typeof template !== 'string')
|
|
112
|
+
return template;
|
|
113
|
+
const regex = /\/\s*(?:\(\s*)?([^()\/-]+?)\s*-\s*([^()\/]+?)(?:\s*\))?/g;
|
|
114
|
+
const leftOperands = [];
|
|
115
|
+
let m;
|
|
116
|
+
while ((m = regex.exec(source)) !== null) {
|
|
117
|
+
leftOperands.push(m[1].trim());
|
|
118
|
+
}
|
|
119
|
+
if (!leftOperands.length) {
|
|
120
|
+
return template;
|
|
121
|
+
}
|
|
122
|
+
let idx = 0;
|
|
123
|
+
const replaced = template.replace(/\$\{null\}|\bnull\b/g, (match) => {
|
|
124
|
+
if (idx >= leftOperands.length) {
|
|
125
|
+
return match;
|
|
126
|
+
}
|
|
127
|
+
const val = leftOperands[idx++];
|
|
128
|
+
return val;
|
|
129
|
+
});
|
|
130
|
+
return replaced;
|
|
131
|
+
}
|
|
132
|
+
exports.mergeTemplateToSource = mergeTemplateToSource;
|