UNPKG

@gecut/utilities

Version:

The ultimate utility toolkit from Gecut Company, crafted with TypeScript for optimal speed and efficiency. Designed to boost productivity with a suite of fast and optimized tools.

38 lines 1.47 kB
import { constantCase } from 'change-case'; export class GecutEnvVM { constructor(appName, variables) { this.appName = appName; this.variables = variables; this.variablesName = Object.fromEntries(Object.keys(variables).map((name) => [ name, constantCase(appName) + '.' + constantCase(name.toString()), ])); } get(variableName) { const defaultValue = this.variables[variableName]; const value = window.localStorage.getItem(this.variablesName[variableName]); if (defaultValue == null) throw new Error('key not found'); else if (value == null) return defaultValue; else if (typeof defaultValue === 'string') return String(value); else if (typeof defaultValue === 'number') return Number(value); else if (typeof defaultValue === 'boolean') return Boolean(value); return defaultValue; } set(variableName, value) { const key = this.variablesName[variableName]; if (typeof value === 'function') { const oldValue = this.get(variableName); return window.localStorage.setItem(key, String(value(oldValue))); } return window.localStorage.setItem(key, String(value)); } remove(variableName) { return window.localStorage.removeItem(this.variablesName[variableName]); } } //# sourceMappingURL=envvm.js.map