@soratani-code/samtools 1.5.3 → 1.5.5

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 CHANGED
@@ -17,3 +17,9 @@ export declare function mergeTemplateFromOptions(template: string, options: {
17
17
  value: any;
18
18
  }[]): string;
19
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;
24
+ export declare function insertBetweenImmutable<D = any>(arr: D[], filler: (index: number) => D): D[];
25
+ export declare function interchangeById<T extends Record<string, any> = any>(arr: T[] | (string | number)[], idA: string | number, idB: string | number, idKey?: string): T[] | (string | number)[];
package/lib/tools.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseOperatorsTokens = exports.mergeTemplateFromOptions = exports.replaceTemplateFromOptions = exports.mergeTemplateToSource = exports.parseTemplateToOptions = exports.insertArray = exports.deepDelete = exports.isDeepEqual = void 0;
3
+ exports.interchangeById = exports.insertBetweenImmutable = 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) {
@@ -157,3 +157,72 @@ function parseOperatorsTokens(expr) {
157
157
  .filter(Boolean);
158
158
  }
159
159
  exports.parseOperatorsTokens = parseOperatorsTokens;
160
+ function escapeHtml(s) {
161
+ return s
162
+ .replace(/&/g, '&amp;')
163
+ .replace(/</g, '&lt;')
164
+ .replace(/>/g, '&gt;')
165
+ .replace(/"/g, '&quot;')
166
+ .replace(/'/g, '&#39;');
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;
198
+ function insertBetweenImmutable(arr, filler) {
199
+ const n = arr.length;
200
+ if (n === 0)
201
+ return [];
202
+ if (n === 1)
203
+ return [arr[0]];
204
+ const out = [];
205
+ for (let i = 0; i < n; i++) {
206
+ out.push(arr[i]);
207
+ if (i !== n - 1)
208
+ out.push(filler(i));
209
+ }
210
+ return out;
211
+ }
212
+ exports.insertBetweenImmutable = insertBetweenImmutable;
213
+ function interchangeById(arr, idA, idB, idKey = 'id') {
214
+ const isPrimitive = typeof arr[0] !== 'object' || arr[0] === null;
215
+ const findIndex = (id) => isPrimitive
216
+ ? arr.indexOf(id)
217
+ : arr.findIndex((x) => x[idKey] === id);
218
+ const i = findIndex(idA);
219
+ const j = findIndex(idB);
220
+ if (i === -1 || j === -1 || i === j)
221
+ return arr.slice();
222
+ const copy = arr.slice();
223
+ const tmp = copy[i];
224
+ copy[i] = copy[j];
225
+ copy[j] = tmp;
226
+ return copy;
227
+ }
228
+ exports.interchangeById = interchangeById;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soratani-code/samtools",
3
- "version": "1.5.3",
3
+ "version": "1.5.5",
4
4
  "description": "",
5
5
  "typings": "lib/index.d.ts",
6
6
  "main": "lib/index.js",