@datagrok/helm 2.1.24 → 2.1.25

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@datagrok/helm",
3
3
  "friendlyName": "Helm",
4
- "version": "2.1.24",
4
+ "version": "2.1.25",
5
5
  "author": {
6
6
  "name": "Oleksandra Serhiienko",
7
7
  "email": "oserhiienko@datagrok.ai"
@@ -46,14 +46,16 @@ export function parseHelm(s: string): string[] {
46
46
  for (const monomer of ss) {
47
47
  if (!monomer || monomer === '') continue;
48
48
  if (monomer.startsWith('[') && monomer.includes(']')) {
49
- const element = monomer.substring(1, monomer.indexOf(']'));
49
+ const closingIndex = findClosing(monomer, 1, '[', ']');
50
+ const element = monomer.substring(1, closingIndex);
50
51
  monomers.push(element);
51
- const residue = monomer.substring(monomer.indexOf(']') + 1);
52
+ const residue = monomer.substring(closingIndex + 1);
52
53
  ss.push(residue);
53
54
  } else if (monomer.includes('[') && monomer.endsWith(']')) {
54
- const element = monomer.substring(monomer.lastIndexOf('[') + 1, monomer.length - 1);
55
+ const openingIndex = findOpening(monomer, monomer.length - 2, '[', ']');
56
+ const element = monomer.substring(openingIndex + 1, monomer.length - 1);
55
57
  monomers.push(element);
56
- const residue = monomer.substring(0, monomer.lastIndexOf('['));
58
+ const residue = monomer.substring(0, openingIndex);
57
59
  ss.push(residue);
58
60
  } else if (monomer.includes('(') && monomer.includes(')')) {
59
61
  // here we only want to split the string at first '(' and last ')'
@@ -64,9 +66,8 @@ export function parseHelm(s: string): string[] {
64
66
  const elements = [firstPiece, secondPiece, thirdPiece];
65
67
  for (const el of elements)
66
68
  ss.push(el);
67
- } else {
69
+ } else
68
70
  monomers.push(monomer);
69
- }
70
71
  }
71
72
  }
72
73
  }
@@ -111,6 +112,40 @@ export function findMonomers(monomerSymbolList: string[]): Set<string> {
111
112
  return new Set(monomerSymbolList.filter((val) => !monomerNames.includes(val)));
112
113
  }
113
114
 
115
+ function findClosing(s: string, start: number, open: string, close: string) {
116
+ let i = start;
117
+ let count = 0;
118
+ while (i < s.length) {
119
+ if (s[i] === open)
120
+ count++;
121
+
122
+ if (s[i] === close) {
123
+ if (count === 0)
124
+ return i;
125
+ count--;
126
+ }
127
+ i++;
128
+ }
129
+ return -1;
130
+ }
131
+
132
+ function findOpening(s: string, start: number, open: string, close: string) {
133
+ let i = start;
134
+ let count = 0;
135
+ while (i >= 0) {
136
+ if (s[i] === close)
137
+ count++;
138
+
139
+ if (s[i] === open) {
140
+ if (count === 0)
141
+ return i;
142
+ count--;
143
+ }
144
+ i--;
145
+ }
146
+ return -1;
147
+ }
148
+
114
149
  function split(s: string, sep: string) {
115
150
  const ret = [];
116
151
  let frag = '';
@@ -136,19 +171,18 @@ function split(s: string, sep: string) {
136
171
  if (c == '\"') {
137
172
  if (!(i > 0 && s.substring(i - 1, i) == '\\'))
138
173
  quote = quote == 0 ? 1 : 0;
139
- } else if (c == '[') {
174
+ } else if (c == '[')
140
175
  ++bracket;
141
- } else if (c == ']') {
176
+ else if (c == ']')
142
177
  --bracket;
143
- } else if (c == '(') {
178
+ else if (c == '(')
144
179
  ++parentheses;
145
- } else if (c == ')') {
180
+ else if (c == ')')
146
181
  --parentheses;
147
- } else if (c == '{') {
182
+ else if (c == '{')
148
183
  ++braces;
149
- } else if (c == '}') {
184
+ else if (c == '}')
150
185
  --braces;
151
- }
152
186
  }
153
187
  }
154
188
  ret.push(frag);