UNPKG

@ionic/core

Version:
78 lines (77 loc) 2.09 kB
/*! * (C) Ionic http://ionicframework.com - MIT License */ // TODO(FW-2832): types export 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); } } export const config = /*@__PURE__*/ new Config(); export const configFromSession = (win) => { try { const configStr = win.sessionStorage.getItem(IONIC_SESSION_KEY); return configStr !== null ? JSON.parse(configStr) : {}; } catch (e) { return {}; } }; export const saveConfig = (win, c) => { try { win.sessionStorage.setItem(IONIC_SESSION_KEY, JSON.stringify(c)); } catch (e) { return; } }; export const configFromURL = (win) => { const configObj = {}; win.location.search .slice(1) .split('&') .map((entry) => entry.split('=')) .map(([key, value]) => { try { return [decodeURIComponent(key), decodeURIComponent(value)]; } catch (e) { return ['', '']; } }) .filter(([key]) => startsWith(key, IONIC_PREFIX)) .map(([key, value]) => [key.slice(IONIC_PREFIX.length), value]) .forEach(([key, value]) => { configObj[key] = value; }); return configObj; }; const startsWith = (input, search) => { return input.substr(0, search.length) === search; }; const IONIC_PREFIX = 'ionic:'; const IONIC_SESSION_KEY = 'ionic-persist-config';