-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathappendChangeCustomLabelPosition.js
More file actions
52 lines (52 loc) · 1.89 KB
/
appendChangeCustomLabelPosition.js
File metadata and controls
52 lines (52 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Append the method changeCustomLabelPosition to the Molecule class in order
* to change the position of custom labels according to the provided option
* - 'superscript' will add a ] at the beginning of the custom label
* - 'normal' will remove a leading ] if present
* - 'auto' will set the label as superscript for non-carbon atoms and normal for carbon atoms
* @param {*} Molecule
*/
export function appendChangeCustomLabelPosition(Molecule) {
Molecule.prototype.changeCustomLabelPosition =
function changeCustomLabelPosition(customLabelPosition) {
switch (customLabelPosition) {
case 'superscript':
for (let i = 0; i < this.getAllAtoms(); i++) {
const customLabel = this.getAtomCustomLabel(i);
if (customLabel && !customLabel.startsWith(']')) {
this.setAtomCustomLabel(i, `]${customLabel}`);
}
}
break;
case 'normal':
for (let i = 0; i < this.getAllAtoms(); i++) {
const customLabel = this.getAtomCustomLabel(i);
if (customLabel?.startsWith(']')) {
this.setAtomCustomLabel(i, customLabel.slice(1));
}
}
break;
case 'auto':
for (let i = 0; i < this.getAllAtoms(); i++) {
const customLabel = this.getAtomCustomLabel(i);
if (customLabel) {
const atomLabel = this.getAtomLabel(i);
if (atomLabel === 'C') {
// normal
if (customLabel.startsWith(']')) {
this.setAtomCustomLabel(i, customLabel.slice(1));
}
} else if (!customLabel.startsWith(']')) {
this.setAtomCustomLabel(i, `]${customLabel}`);
}
}
}
break;
case undefined:
// nothing to do
break;
default:
break;
}
};
}