killa
Version:
State management for Vanilla and React
40 lines (39 loc) • 1.07 kB
JavaScript
import { IS_WINDOW_DEFINED, IS_DOCUMENT_DEFINED } from "killa/constants";
const noop = () => {
};
const [addDocumentEvent, removeDocumentEvent] = IS_DOCUMENT_DEFINED && document.addEventListener ? [
document.addEventListener.bind(window),
document.removeEventListener.bind(window)
] : [noop, noop];
const [addWindowEvent, removeWindowEvent] = IS_WINDOW_DEFINED && window.addEventListener ? [
window.addEventListener.bind(window),
window.removeEventListener.bind(window)
] : [noop, noop];
const serialize = (value) => JSON.stringify(value);
const deserialize = (value) => {
if (value === null)
return null;
return JSON.parse(value);
};
const merge = (object, objectToMerge) => {
return {
...object,
...objectToMerge
};
};
const messageError = console.error;
const encoded = (str) => btoa(encodeURIComponent(str));
const decoded = (str) => decodeURIComponent(atob(str));
export {
addDocumentEvent,
addWindowEvent,
decoded,
deserialize,
encoded,
merge,
messageError,
noop,
removeDocumentEvent,
removeWindowEvent,
serialize
};