reshuffle-eidr-connector
Version:
Reshuffle connectors for eidr.org
76 lines • 3 kB
JavaScript
;
exports.__esModule = true;
exports.parseJsonWithValue = void 0;
// Defines the property path of the json to convert to object
// Define the fields separated by a period
// Can use wildcard notation - however, the last segment must be a field name
// i.e. '*.DisplayName' is ok, 'AssociatedOrg.*.DisplayName' is ok
// 'MetaDataObject.*' is NOT ok
var jsonFormatWithValueRules = [
'ExtraObjectMetadata.EpisodeInfo.SequenceInfo.DistributionNumber',
'ExtraObjectMetadata.EpisodeInfo.SequenceInfo.md:DistributionNumber',
'ExtraObjectMetadata.EpisodeInfo.SequenceInfo.HouseSequence',
'ExtraObjectMetadata.EpisodeInfo.SequenceInfo.md:HouseSequence',
'ExtraObjectMetadata.CompilationInfo.CompilationClass',
'ExtraObjectMetadata.CompilationInfo.md:CompilationClass',
// '*.SequenceInfo.DistributionNumber', // Example with wildcard
];
// Recursion to convert primitive values in specified path
// of the JSON to an object
function parseJsonWithOneRule(jsonToParse, fields) {
if (!(fields === null || fields === void 0 ? void 0 : fields.length) || !jsonToParse) {
return;
}
fields = fields.slice();
var currentField = fields[0];
if (fields.length === 1) {
if (Array.isArray(jsonToParse[currentField])) {
jsonToParse[currentField].forEach(function (arrItem, i) {
if (typeof arrItem !== 'object') {
jsonToParse[i] = {
value: arrItem
};
}
});
}
else if (typeof jsonToParse[currentField] !== 'object' &&
jsonToParse[currentField]) {
jsonToParse[currentField] = {
value: jsonToParse[currentField]
};
}
return;
}
var subFields = fields.slice(1, fields.length);
// If wildcard syntax, check the branches all the way down
if (currentField === '*') {
if (typeof jsonToParse === 'object') {
Object.keys(jsonToParse).forEach(function (value) {
parseJsonWithOneRule(jsonToParse[value], fields);
parseJsonWithOneRule(jsonToParse[value], subFields);
});
}
return;
}
if (jsonToParse[currentField]) {
if (Array.isArray(jsonToParse[currentField])) {
jsonToParse[currentField].forEach(function (arrItem) {
return parseJsonWithOneRule(arrItem, subFields);
});
}
else {
parseJsonWithOneRule(jsonToParse[currentField], subFields);
}
}
}
function parseJsonWithValue(json) {
if (!json || typeof json !== 'object') {
return json;
}
jsonFormatWithValueRules.forEach(function (rule) {
return parseJsonWithOneRule(json, rule.split('.'));
});
return json;
}
exports.parseJsonWithValue = parseJsonWithValue;
//# sourceMappingURL=jsonPopulateValue.js.map