[Ur] Utility objectifyUr function in JavaScript
Mark Clements
mark.clements at ki.se
Mon Oct 28 15:35:09 EDT 2019
Dear all,
I found that the following function is very useful for converting Ur/Web
data structures within JavaScript. This may be useful to others --
although has it been implemented elsewhere?
Kindly, Mark.
----
var urNewKeyMap = {typ : "type"};
function objectifyUr(urValue, newKeyMap) {
function convert(ur) {
// case: null value
if (ur == null) {
return null;
}
// case: atom
else if (typeof(ur) != 'object') {
return ur;
}
// case: JavaScript array (not used)
else if (Array.isArray(ur)) {
return ur.map(convert);
}
// case: Ur/Web list
else if (Object.keys(ur)[0] == '_1') {
var acc = [];
var ll = ur; // ?
for (; ll !== null; ll=ll._2) {
acc.push(convert(ll._1));
}
return acc;
}
// case: Some list
else if (Object.keys(ur)[0] == 'v') {
return convert(ur.v);
}
// case: Some ??
else if (Object.keys(ur)[0] == 'n' && Object.keys(ur)[1] == 'v') {
return convert(ur.v);
}
// case: Ur/Web struct
else {
return Object.keys(ur).reduce(function(result, key) {
var newKey = key.substr(1,1).toLowerCase()+key.substr(2);
var y = convert(ur[key]);
if (Object.keys(newKeyMap).includes(newKey)) {
newKey = newKeyMap[newKey];
}
if (y != null) {
result[newKey] = convert(ur[key]);
}
return result;
}, {});
}
}
return convert(urValue, newKeyMap);
}
När du skickar e-post till Karolinska Institutet (KI) innebär detta att KI kommer att behandla dina personuppgifter. Här finns information om hur KI behandlar personuppgifter<https://ki.se/medarbetare/integritetsskyddspolicy>.
Sending email to Karolinska Institutet (KI) will result in KI processing your personal data. You can read more about KI’s processing of personal data here<https://ki.se/en/staff/data-protection-policy>.
More information about the Ur
mailing list