@soratani-code/samtools 1.5.2 → 1.5.4
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 +5 -0
- package/lib/tools.js +48 -1
- package/package.json +1 -1
package/lib/tools.d.ts
CHANGED
|
@@ -16,3 +16,8 @@ export declare function mergeTemplateFromOptions(template: string, options: {
|
|
|
16
16
|
label: string;
|
|
17
17
|
value: any;
|
|
18
18
|
}[]): string;
|
|
19
|
+
export declare function parseOperatorsTokens(expr: string | null | undefined): string[];
|
|
20
|
+
export declare function highlightMatchesToHtml(text: string | null | undefined, terms: string[] | null | undefined, options?: {
|
|
21
|
+
tag?: string;
|
|
22
|
+
className?: string;
|
|
23
|
+
}): string;
|
package/lib/tools.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.mergeTemplateFromOptions = exports.replaceTemplateFromOptions = exports.mergeTemplateToSource = exports.parseTemplateToOptions = exports.insertArray = exports.deepDelete = exports.isDeepEqual = void 0;
|
|
3
|
+
exports.highlightMatchesToHtml = exports.parseOperatorsTokens = exports.mergeTemplateFromOptions = 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) {
|
|
@@ -148,3 +148,50 @@ function mergeTemplateFromOptions(template, options) {
|
|
|
148
148
|
}, (0, lodash_1.cloneDeep)(template));
|
|
149
149
|
}
|
|
150
150
|
exports.mergeTemplateFromOptions = mergeTemplateFromOptions;
|
|
151
|
+
function parseOperatorsTokens(expr) {
|
|
152
|
+
if (expr == null)
|
|
153
|
+
return [];
|
|
154
|
+
return expr
|
|
155
|
+
.split(/[+\-*/()]/)
|
|
156
|
+
.map((s) => s.trim())
|
|
157
|
+
.filter(Boolean);
|
|
158
|
+
}
|
|
159
|
+
exports.parseOperatorsTokens = parseOperatorsTokens;
|
|
160
|
+
function escapeHtml(s) {
|
|
161
|
+
return s
|
|
162
|
+
.replace(/&/g, '&')
|
|
163
|
+
.replace(/</g, '<')
|
|
164
|
+
.replace(/>/g, '>')
|
|
165
|
+
.replace(/"/g, '"')
|
|
166
|
+
.replace(/'/g, ''');
|
|
167
|
+
}
|
|
168
|
+
function highlightMatchesToHtml(text, terms, options) {
|
|
169
|
+
if (text == null)
|
|
170
|
+
return '';
|
|
171
|
+
const tlist = (terms || []).filter(Boolean);
|
|
172
|
+
if (!tlist.length)
|
|
173
|
+
return escapeHtml(text);
|
|
174
|
+
const tag = (options === null || options === void 0 ? void 0 : options.tag) || 'mark';
|
|
175
|
+
const className = (options === null || options === void 0 ? void 0 : options.className) || 'samtools-highlight';
|
|
176
|
+
const unique = Array.from(new Set(tlist)).sort((a, b) => b.length - a.length);
|
|
177
|
+
const esc = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
178
|
+
const alt = unique.map(esc).join('|');
|
|
179
|
+
const re = new RegExp(`(^|[^\\p{Script=Han}\\w])(${alt})(?=$|[^\\p{Script=Han}\\w])`, 'gu');
|
|
180
|
+
let lastIndex = 0;
|
|
181
|
+
let out = '';
|
|
182
|
+
let m;
|
|
183
|
+
while ((m = re.exec(text)) !== null) {
|
|
184
|
+
const idx = m.index;
|
|
185
|
+
const prefix = m[1] || '';
|
|
186
|
+
const matched = m[2];
|
|
187
|
+
if (idx > lastIndex)
|
|
188
|
+
out += escapeHtml(text.slice(lastIndex, idx));
|
|
189
|
+
out += escapeHtml(prefix);
|
|
190
|
+
out += `<${tag} class="${escapeHtml(className)}">${escapeHtml(matched)}</${tag}>`;
|
|
191
|
+
lastIndex = idx + prefix.length + matched.length;
|
|
192
|
+
}
|
|
193
|
+
if (lastIndex < text.length)
|
|
194
|
+
out += escapeHtml(text.slice(lastIndex));
|
|
195
|
+
return out;
|
|
196
|
+
}
|
|
197
|
+
exports.highlightMatchesToHtml = highlightMatchesToHtml;
|