Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function resolveNameForTitleMap({ schema, properties, value }) {
if (schema.titleMap) {
// Here we add a field side by side with the value
// to keep the title associated to the value
const info = schema.titleMap.find(titleMap => titleMap.value === value);
const parentKey = schema.key.slice();
const key = parentKey.pop();
const nameKey = `$${key}_name`;
const parentValue = getValue(properties, { key: parentKey });
if (info) {
parentValue[nameKey] = info.name;
} else {
delete parentValue[nameKey];
}
}
}
return;
}
// Here we add a field side by side with the value
// to keep the title associated to the value
const valueIsArray = Array.isArray(value);
const uniformValue = valueIsArray ? value : [value];
const names = uniformValue
.map(nextValue => schema.titleMap.find(titleMap => titleMap.value === nextValue))
.map(entry => entry && entry.name);
const parentKey = schema.key.slice();
const key = parentKey.pop();
const nameKey = `$${key}_name`;
const parentValue = getValue(properties, { key: parentKey });
if (names.some(name => name !== undefined)) {
parentValue[nameKey] = valueIsArray ? names : names[0];
} else {
delete parentValue[nameKey];
}
}