UNPKG

@larva.io/webcomponents

Version:

Fentrica SmartUnits WebComponents package

74 lines (72 loc) 2.1 kB
/*! * (C) Fentrica http://fentrica.com - Seee LICENSE.md */ class Config { constructor() { this.m = new Map(); } reset(configObj) { this.m = new Map(Object.entries(configObj)); } get(key, fallback) { const value = this.m.get(key); return value !== undefined ? value : fallback; } getBoolean(key, fallback = false) { const val = this.m.get(key); if (val === undefined) { return fallback; } if (typeof val === 'string') { return val === 'true'; } return !!val; } getNumber(key, fallback) { const val = parseFloat(this.m.get(key)); return isNaN(val) ? (fallback !== undefined ? fallback : NaN) : val; } set(key, value) { this.m.set(key, value); } } const config = /*@__PURE__*/ new Config(); const configFromSession = (win) => { try { const configStr = win.sessionStorage.getItem(LARVA_SESSION_KEY); return configStr !== null ? JSON.parse(configStr) : {}; } catch (e) { return {}; } }; const saveConfig = (win, c) => { try { win.sessionStorage.setItem(LARVA_SESSION_KEY, JSON.stringify(c)); } catch (e) { return; } }; const configFromURL = (win) => { const configObj = {}; win.location.search .slice(1) .split('&') .map(entry => entry.split('=')) .map(([key, value]) => [decodeURIComponent(key), decodeURIComponent(value)]) .filter(([key]) => startsWith(key, LARVA_PREFIX)) .map(([key, value]) => [key.slice(LARVA_PREFIX.length), value]) .forEach(([key, value]) => { configObj[key] = value; }); return configObj; }; const startsWith = (input, search) => { return input.substr(0, search.length) === search; }; const LARVA_PREFIX = 'larva:'; const LARVA_SESSION_KEY = 'larva-persist-config'; export { configFromURL as a, config as b, configFromSession as c, saveConfig as s }; //# sourceMappingURL=config-Bt4QT_oL.js.map //# sourceMappingURL=config-Bt4QT_oL.js.map