@coin-voyage/paykit
Version:
Seamless crypto payments. Onboard users from any chain, any coin into your app with one click.
60 lines • 1.76 kB
JavaScript
/**
* LocalStorage helper functions to save and retrieve data on a per-app basis
* e.g recently connected wallets, transactions, etc.
*/
//import { randomUUID as randomID } from 'crypto';
const randomID = () => {
// TODO: use randomUUID when it's supported in all browsers
// https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID
// Until then, use this fallback
return (Date.now().toString(36) +
Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15));
};
export const save = (storageKey, data) => {
try {
localStorage.setItem(storageKey, JSON.stringify(data));
return get(storageKey);
}
catch (_e) {
return [];
}
};
export const get = (storageKey) => {
try {
const data = localStorage.getItem(storageKey);
if (data) {
return JSON.parse(data);
}
return [];
}
catch (_e) {
// error parsing data, reset
save(storageKey, []);
return [];
}
};
export const add = (storageKey, item) => {
const data = get(storageKey);
const newData = [
{
...item,
ckStoreKey: randomID(), // unique key to identify the item so we can remove it later
timestamp: new Date(),
},
...data,
];
save(storageKey, newData);
return get(storageKey);
};
export const remove = (storageKey, item) => {
const data = get(storageKey);
const newData = data.filter((i) => i.ckStoreKey !== item.ckStoreKey);
save(storageKey, newData);
return get(storageKey);
};
export const clear = (storageKey) => {
save(storageKey, []);
return get(storageKey);
};
//# sourceMappingURL=localstorage.js.map