@soratani-code/samtools 1.3.21 → 1.5.1

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
@@ -1,5 +1,16 @@
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>[];
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;
13
+ export declare function mergeTemplateFromOptions(template: string, options: {
14
+ label: string;
15
+ value: any;
16
+ }[]): 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.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) {
@@ -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 !== null && template !== void 0 ? template : '')) !== null) {
83
+ while ((m = valueRe.exec(template)) !== null) {
80
84
  valueTokens.push(m[1]);
81
85
  }
82
- const rawLabels = source
83
- ? source.split(/\s*[+-]\s*/).map(s => s.trim()).filter(Boolean)
86
+ const rawLabelPieces = source
87
+ ? source.split(/\s*[+\-−]\s*/).map((s) => cleanLabelPiece(s)).filter(Boolean)
84
88
  : [];
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 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 parseValue = (token) => {
91
- if (token == null)
92
+ const parseToken = (tok) => {
93
+ if (tok == null)
92
94
  return null;
93
- const t = token.trim();
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(labels.length, valueTokens.length);
100
- const result = [];
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 = labels[i]) !== null && _a !== void 0 ? _a : '';
103
- const raw = valueTokens[i];
104
- const value = parseValue(raw);
105
- result.push({ label, value });
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
- return result;
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,21 @@ 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;
157
+ function mergeTemplateFromOptions(template, options) {
158
+ return (0, lodash_1.reduce)(options, (tpl, opt) => {
159
+ const has = template.includes('${' + opt.value + '}');
160
+ if (has)
161
+ return tpl.replace('${' + opt.value + '}', opt.label);
162
+ return tpl;
163
+ }, (0, lodash_1.cloneDeep)(template));
164
+ }
165
+ exports.mergeTemplateFromOptions = mergeTemplateFromOptions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soratani-code/samtools",
3
- "version": "1.3.21",
3
+ "version": "1.5.1",
4
4
  "description": "",
5
5
  "typings": "lib/index.d.ts",
6
6
  "main": "lib/index.js",