molstar
Version:
A comprehensive macromolecular library.
51 lines • 1.86 kB
JavaScript
/*
* Copyright (c) 2018 Mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { MSymbol, isSymbol } from './symbol';
export function symbol(args, type, description) {
return MSymbol('', args, type, description);
}
export function normalizeTable(table) {
_normalizeTable('', '', table);
}
export function symbolList(table) {
var list = [];
_symbolList(table, list);
return list;
}
function formatKey(key) {
var regex = /([a-z])([A-Z])([a-z]|$)/g;
// do this twice because 'xXxX'
return key.replace(regex, function (s, a, b, c) { return a + "-" + b.toLocaleLowerCase() + c; }).replace(regex, function (s, a, b, c) { return a + "-" + b.toLocaleLowerCase() + c; });
}
function _normalizeTable(namespace, key, obj) {
if (isSymbol(obj)) {
obj.info.namespace = namespace;
obj.info.name = obj.info.name || formatKey(key);
obj.id = obj.info.namespace + "." + obj.info.name;
return;
}
var currentNs = "" + (obj['@namespace'] || formatKey(key));
var newNs = namespace ? namespace + "." + currentNs : currentNs;
for (var _i = 0, _a = Object.keys(obj); _i < _a.length; _i++) {
var childKey = _a[_i];
if (typeof obj[childKey] !== 'object' && !isSymbol(obj[childKey]))
continue;
_normalizeTable(newNs, childKey, obj[childKey]);
}
}
function _symbolList(obj, list) {
if (isSymbol(obj)) {
list.push(obj);
return;
}
for (var _i = 0, _a = Object.keys(obj); _i < _a.length; _i++) {
var childKey = _a[_i];
if (typeof obj[childKey] !== 'object' && !isSymbol(obj[childKey]))
continue;
_symbolList(obj[childKey], list);
}
}
//# sourceMappingURL=helpers.js.map