UNPKG

@benev/nubs

Version:
40 lines 982 B
export function jsonStorage(storage) { return { getItem(key) { return jsonGet(storage, key); }, setItem(key, data) { return jsonSet(storage, key, data); }, }; } export function jsonStorageProxy(storage) { return new Proxy({}, { get(t, key) { return jsonGet(storage, key); }, set(t, key, data) { return jsonSet(storage, key, data); }, }); } function jsonGet(storage, key) { const text = storage.getItem(key); try { return text ? JSON.parse(text) : undefined; } catch (error) { console.warn("json storage 'get' error", error); } } function jsonSet(storage, key, data) { const dataIsUnset = data === undefined || data === null; const text = dataIsUnset ? "" : JSON.stringify(data); storage.setItem(key, text); return true; } //# sourceMappingURL=json-storage.js.map