Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 112x 112x 106x 112x 105x 105x 112x 98x 98x 1x 1x 98x 98x 98x 98x 98x 98x 98x 98x 98x 78x 78x 98x 161x 161x 1x 1x 161x 1x 1x 1x 1x 160x 1x 1x 1x 159x 9x 158x 77x 77x 17x 17x 77x 60x 60x 60x 77x 149x 72x 72x 72x 161x 26x 98x 1x 1x 1x 153x 153x 153x 153x 143x 73x 73x | import { subscriptToAscii } from '@/helpers/science';
import { isCAS } from '@/utils/typeGuards/common';
/**
* The kind of thing a single search term looks like. Used purely to colorize the
* search box — it does not change search behavior.
* @category Utils
* @group Search
*/
export type SearchTermType = 'string' | 'cas' | 'smiles' | 'formula';
/** All 118 element symbols, for validating that a token is a chemical formula. */
const ELEMENT_SYMBOLS: ReadonlySet<string> = new Set([
'H',
'He',
'Li',
'Be',
'B',
'C',
'N',
'O',
'F',
'Ne',
'Na',
'Mg',
'Al',
'Si',
'P',
'S',
'Cl',
'Ar',
'K',
'Ca',
'Sc',
'Ti',
'V',
'Cr',
'Mn',
'Fe',
'Co',
'Ni',
'Cu',
'Zn',
'Ga',
'Ge',
'As',
'Se',
'Br',
'Kr',
'Rb',
'Sr',
'Y',
'Zr',
'Nb',
'Mo',
'Tc',
'Ru',
'Rh',
'Pd',
'Ag',
'Cd',
'In',
'Sn',
'Sb',
'Te',
'I',
'Xe',
'Cs',
'Ba',
'La',
'Ce',
'Pr',
'Nd',
'Pm',
'Sm',
'Eu',
'Gd',
'Tb',
'Dy',
'Ho',
'Er',
'Tm',
'Yb',
'Lu',
'Hf',
'Ta',
'W',
'Re',
'Os',
'Ir',
'Pt',
'Au',
'Hg',
'Tl',
'Pb',
'Bi',
'Po',
'At',
'Rn',
'Fr',
'Ra',
'Ac',
'Th',
'Pa',
'U',
'Np',
'Pu',
'Am',
'Cm',
'Bk',
'Cf',
'Es',
'Fm',
'Md',
'No',
'Lr',
'Rf',
'Db',
'Sg',
'Bh',
'Hs',
'Mt',
'Ds',
'Rg',
'Cn',
'Nh',
'Fl',
'Mc',
'Lv',
'Ts',
'Og',
]);
/**
* Detects structural characters that appear in SMILES notation but never in a
* plain chemical formula: bond orders (`=`, `#`), chirality (`@`), stereo bonds
* (`\`), bracket atoms (`[nH]`, `[Na+]`), and lowercase aromatic organic atoms
* (`b c n o p s`).
*
* @param value - The trimmed, space-free term.
* @returns True when the term carries a SMILES-only signal.
* @source
*/
function hasSmilesSignal(value: string): boolean {
if (/[=#@\\]/.test(value)) return true;
// A bracket atom, e.g. "[nH]" or "[Na+]".
if (/\[[^\]]+\]/.test(value)) return true;
// A lowercase aromatic organic atom standing on its own (not the second letter
// of a two-letter element symbol like the "a" in "Na").
if (/(?:^|[^A-Za-z])[bcnops](?![a-z])/.test(value)) return true;
return false;
}
/**
* Validates that a token is a well-formed chemical formula: a sequence of real
* element symbols with optional counts, balanced `()`/`[]` groups, hydrate/salt
* separators (`·`, `*`, `.`), and an optional trailing charge. Lowercase letters
* are only accepted as the second character of a two-letter element symbol, so
* ordinary words (`acetone`, `benzene`) are rejected.
*
* @param value - The trimmed, space-free term.
* @returns True when the whole token parses as a formula with ≥1 element.
* @source
*/
function isChemicalFormula(value: string): boolean {
// A display-formatted formula uses subscript digits (Na₆O₁₈P₆); normalize them to ASCII
// so it's recognized the same as the plain-digit form (Na6O18P6).
value = subscriptToAscii(value);
if (!/^[A-Za-z0-9()[\]+\-·•∙⋅.*/xn]+$/.test(value)) return false;
let i = 0;
let elementCount = 0;
let depth = 0;
const skipDigits = (): void => {
while (i < value.length && /\d/.test(value[i])) i++;
};
while (i < value.length) {
const char = value[i];
if (char === '(' || char === '[') {
depth++;
i++;
} else if (char === ')' || char === ']') {
if (depth === 0) return false;
depth--;
i++;
skipDigits();
} else if (/[·•∙⋅.*]/.test(char)) {
// Hydrate/salt separator, optionally followed by a coefficient (e.g. "·2").
i++;
while (i < value.length && /[\dxn/]/.test(value[i])) i++;
} else if (/[0-9+-]/.test(char)) {
i++;
} else if (/[A-Z]/.test(char)) {
const two = value.slice(i, i + 2);
if (/^[A-Z][a-z]$/.test(two) && ELEMENT_SYMBOLS.has(two)) {
elementCount++;
i += 2;
} else if (ELEMENT_SYMBOLS.has(char)) {
elementCount++;
i += 1;
} else {
return false;
}
skipDigits();
} else {
// A lowercase letter here isn't part of an element symbol → not a formula.
return false;
}
}
return depth === 0 && elementCount >= 1;
}
/**
* Classifies a single search term as a plain string, a CAS number, a SMILES
* string, or a chemical formula. Detection is heuristic and intended only to
* colorize the search box — it never changes what is searched. Phrases (anything
* containing whitespace) are always plain strings, since CAS/SMILES/formula
* tokens are contiguous.
*
* Ambiguous short tokens that read as valid formulas (e.g. "CCO", which is also
* SMILES for ethanol) are reported as `"formula"`; SMILES is only chosen when a
* SMILES-only signal is present (bonds, brackets, or aromatic lowercase atoms).
*
* @category Utils
* @group Search
* @param term - A single search term (one leaf of the query).
* @returns The detected term type.
* @example
* ```ts
* detectTermType("7647-14-5"); // "cas"
* detectTermType("NaOH"); // "formula"
* detectTermType("KMnO4"); // "formula"
* detectTermType("O=C=O"); // "smiles"
* detectTermType("c1ccccc1"); // "smiles"
* detectTermType("sodium hydroxide"); // "string"
* detectTermType("acetone"); // "string"
* ```
* @source
*/
export function detectTermType(term: string): SearchTermType {
const value = term.trim();
if (value === '' || /\s/.test(value)) return 'string';
if (isCAS(value)) return 'cas';
if (hasSmilesSignal(value)) return 'smiles';
if (isChemicalFormula(value)) return 'formula';
return 'string';
}
|