UNPKG

dataset-config

Version:

Parse HTML data attributes into a structured object with automatic type conversion.

3 lines (2 loc) 1.22 kB
var datasetConfig=function(){"use strict";const e={prefix:"",parseFunction:!0,excludeKeys:[]};class t{#e;#t;#s;constructor(t,s={}){this.#e=t,this.#t={...e,...s},this.#s=new Set(this.#t.excludeKeys.map(this.#i))}parse(){const e={},t=this.#t.prefix.length;for(const s in this.#e.dataset)if(this.#t.prefix){if(!s.startsWith(this.#t.prefix))continue;const i=this.#r(s.slice(t)),r=i.split(".")[0];if(this.#s.has(r))continue;this.#n(e,i,this.#o(this.#e.dataset[s]))}else{const t=this.#i(s).split(".")[0];if(this.#s.has(t))continue;this.#n(e,s,this.#o(this.#e.dataset[s]))}return e}#n(e,t,s){const i=this.#i(t).split(".");let r=e;for(const[e,t]of i.entries())e===i.length-1?r[t]=s:(r[t]&&"object"==typeof r[t]||(r[t]={}),r=r[t])}#o(e){if(this.#t.parseFunction&&"function"==typeof window[e])return window[e];if("true"===e)return!0;if("false"===e)return!1;if(!isNaN(e)&&""!==e.trim())return Number(e);if(e.startsWith("{")&&e.endsWith("}")||e.startsWith("[")&&e.endsWith("]"))try{return JSON.parse(e)}catch(t){return e}return e}#i(e){return e.replace(/-([a-z])/g,((e,t)=>t.toUpperCase()))}#r(e){return e.charAt(0).toLowerCase()+e.slice(1)}}return(e,s)=>new t(e,s).parse()}(); //# sourceMappingURL=dataset-config.browser.min.js.map