Skip to content

Instantly share code, notes, and snippets.

@VitorLuizC
Last active March 30, 2020 14:17
Show Gist options
  • Save VitorLuizC/e171238de79bc4d60c375f9612887a4b to your computer and use it in GitHub Desktop.
Save VitorLuizC/e171238de79bc4d60c375f9612887a4b to your computer and use it in GitHub Desktop.
const KEY = 'isMenuVisible';
const getIsMenuVisible = () => {
const json = window.localStorage.getItem(KEY);
if (json) {
try {
const value = JSON.parse(json);
if (typeof value === 'boolean') {
return value;
}
} catch (_) {}
}
return false;
};
import * as Option from 'fp-ts/es6/Option';
import { pipe } from 'fp-ts/es6/pipeable';
import { constant } from 'fp-ts/es6/function';
const isBoolean = (value: unknown): value is boolean =>
typeof value === 'boolean';
const KEY = 'isMenuVisible';
const getIsMenuVisible = () =>
pipe(
window.localStorage.getItem(KEY),
Option.fromNullable,
Option.chain(value => Option.tryCatch(() => JSON.parse(value))),
Option.chain(Option.fromPredicate(isBoolean)),
Option.getOrElse(constant(false))
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment