refactor: prevent attribute changing twice

This commit is contained in:
Ayo 2023-11-18 23:38:06 +01:00
parent d54ff6fa94
commit a3845b7e7c

View file

@ -154,27 +154,26 @@ export class WebComponent extends HTMLElement {
* @returns * @returns
*/ */
#handler = (setter) => ({ #handler = (setter) => ({
set(obj, prop, newval) { set(obj, prop, newValue) {
const oldval = obj[prop]; const oldValue = obj[prop];
console.log(">>> old value:", oldval, typeof oldval);
obj[prop] = newval; obj[prop] = newValue;
/** /**
* Converts camelCaps string into kebab-case * Converts camelCaps string into kebab-case
* @param {string} str * @param {string} str
* @returns {string} * @returns {string}
*/ */
const kebabize = (str) => const getKebab = (str) =>
str.replace( str.replace(
/[A-Z]+(?![a-z])|[A-Z]/g, /[A-Z]+(?![a-z])|[A-Z]/g,
($, ofs) => (ofs ? "-" : "") + $.toLowerCase() ($, ofs) => (ofs ? "-" : "") + $.toLowerCase()
); );
if (JSON.stringify(oldval) !== newval) { if (oldValue != newValue) {
console.log(oldval, newval); console.log("value changed", oldValue, newValue);
const kebab = kebabize(prop); const kebab = getKebab(prop);
setter(kebab, newval); setter(kebab, newValue);
} }
return true; return true;