⚡ Generate and append a unique ID to each item
This commit is contained in:
@@ -25,3 +25,23 @@ export const sanitize = (string) => {
|
||||
const reg = /[&<>"'/]/ig;
|
||||
return string.replace(reg, (match) => (map[match]));
|
||||
};
|
||||
|
||||
/* Based on section title, item name and index, return a string value for ID */
|
||||
const makeItemId = (sectionStr, itemStr, index) => {
|
||||
const charSum = sectionStr.split('').map((a) => a.charCodeAt(0)).reduce((x, y) => x + y);
|
||||
const itemTitleStr = itemStr.replace(/\s+/g, '-').replace(/[^a-zA-Z ]/g, '').toLowerCase();
|
||||
return `${index}_${charSum}_${itemTitleStr}`;
|
||||
};
|
||||
|
||||
/* Given an array of sections, apply a unique ID to each item, and return modified array */
|
||||
export const applyItemId = (inputSections) => {
|
||||
const sections = inputSections || [];
|
||||
sections.forEach((sec, secIdx) => {
|
||||
if (sec.items) {
|
||||
sec.items.forEach((item, itemIdx) => {
|
||||
sections[secIdx].items[itemIdx].id = makeItemId(sec.name, item.title, itemIdx);
|
||||
});
|
||||
}
|
||||
});
|
||||
return sections;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user