Prevent the attribute Items from getting invalid keys

This commit is contained in:
Oliver 2026-04-25 13:55:01 -06:00
parent 3a7ffa4332
commit bda5d05969
4 changed files with 56 additions and 5 deletions

View file

@ -1,6 +1,6 @@
/**
* A helper method that converts an arbitrary string into a format that can be
* used as an object key easily.
* A helper method that converts an arbitrary string into a format
* that can be used as an object key easily.
*
* @param {string} text The text to convert
* @returns The converted ID
@ -9,5 +9,26 @@ export function toID(text) {
return text
.toLowerCase()
.replace(/\s+/g, `_`)
.replace(/\W/g, ``);
.replace(/\W/g, ``)
.replace(/(^_|_$)/);
};
/**
* A helper method that reports if an arbitrary string is considered a
* valid ID for use in the system
*
* @param {string} text The text to check
* @returns Whether or not the text is a valid ID
*/
export function isValidID(text) {
return !(
// any uppercase characters
text.match(/[A-Z]/)
// any non-word characters
|| text.match(/\W/)
// any whitespace characters
|| text.match(/\s/)
);
};