⚡ Generate and append a unique ID to each item
This commit is contained in:
@@ -13,6 +13,7 @@ import {
|
||||
layout as defaultLayout,
|
||||
} from '@/utils/defaults';
|
||||
import ErrorHandler from '@/utils/ErrorHandler';
|
||||
import { applyItemId } from '@/utils/MiscHelpers';
|
||||
import conf from '../../public/conf.yml';
|
||||
|
||||
export default class ConfigAccumulator {
|
||||
@@ -57,18 +58,24 @@ export default class ConfigAccumulator {
|
||||
|
||||
/* Sections */
|
||||
sections() {
|
||||
let sections = [];
|
||||
// If the user has stored sections in local storage, return those
|
||||
const localSections = localStorage[localStorageKeys.CONF_SECTIONS];
|
||||
if (localSections) {
|
||||
try {
|
||||
const json = JSON.parse(localSections);
|
||||
if (json.length >= 1) return json;
|
||||
if (json.length >= 1) sections = json;
|
||||
} catch (e) {
|
||||
ErrorHandler('Malformed section data in local storage');
|
||||
}
|
||||
}
|
||||
// If the function hasn't yet returned, then return the config file sections
|
||||
return this.conf ? this.conf.sections || [] : [];
|
||||
// If sections were not set from local data, then use config file instead
|
||||
if (sections.length === 0) {
|
||||
sections = this.conf ? this.conf.sections || [] : [];
|
||||
}
|
||||
// Apply a unique ID to each item
|
||||
sections = applyItemId(sections);
|
||||
return sections;
|
||||
}
|
||||
|
||||
/* Complete config */
|
||||
|
||||
@@ -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