
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
localvalue-ts
Advanced tools
A small library to wrap browser's localstorage in a functional fashion.
A small layer over the browser's localstorage, fallbacks to an in-memory store if localstorage is not supported by the browser.
Built on with fp-ts
in mind, localvalue-ts
gives you a standard way to access objects stored locally.
yarn add localvalue-ts
npm install -S localvalue-ts
First create the hooks to read/write the values you just defined:
First create you codecs:
// codecs.ts
import * as t from "io-ts"
export const ThemeFlavourCodec = {
decode: ...
encode: ...
}
export const AuthTokenCodec = {
decode: ...
encode: ...
}
then you use them in your code:
// App.tsx
import { ThemeFlavourCodec, AuthTokenCodec } from "./codecs.ts"
import { createLocalStorage } from "localvalue-ts/localStorage"
import * as LV from "localvalue-ts/LocalValue"
const localStorage = createLocalStorage(
{
themeFlavour: ThemeFlavourCodec,
authToken: AuthTokenCodec,
},
{ defaultValues: { themeFlavour: "light" } },
)
const App = () => {
const myLocalValue = localStorage.themeFlavour.getValue() // LocalValue<"light" | "dark">
return pipe(
theme,
// N.B. using fold2 because with default value we will never have to deal with missing data
LV.fold2(
() => {
console.error("wrong value stored in localStorage!")
},
(themeFlavour) => {
switch (themeFlavour) {
case "light": {
return <LightThemeApp />
}
case "dark": {
return <DarkThemeApp />
}
}
},
),
)
}
export default App
A new data structure is defined for items stored in localstorage, LocalValue
. When dealing with a value stored in your localstorage there are three possibilities:
LocalValue introduces a sum type that represents the optionality/correctness dicotomy:
export interface Absent {
readonly _tag: "Absent"
}
export interface Invalid<E> {
readonly _tag: "Invalid"
readonly errors: E
}
export interface Valid<A> {
readonly _tag: "Valid"
readonly value: A
}
export type LocalValue<E, A> = Absent | Invalid<E> | Valid<A>
It also has instances for some of the most common type-classes
and you can use it in the same way you usually use your usual fp-ts
abstractions:
Given that browsers only allow you to store serialized data in string format, the only accepted codecs are of the form Codec<E, string, B>
, where E
is the shape of the decoding error, B
is the shape of the runtime error and string
is the type resulting after encoding.
If you use io-ts
you can simply create a layer to convert io-ts
codecs to Codec
compliant instances:
import { pipe } from "fp-ts/lib/function"
import * as t from "io-ts"
import * as E from "fp-ts/Either"
import { Json, JsonFromString } from "io-ts-types"
import * as LV from "localvalue-ts/LocalValue"
import { Codec } from "localvalue-ts/Codec"
const adaptIoTsCodec = <A, B>(C: t.Type<B, A>): Codec<t.Errors, A, B> => {
return {
encode: C.encode,
decode: (u: unknown) => LV.fromEither(C.decode(u)),
}
}
export const fromIoTsCodec = <A, B extends Json>(C: t.Type<A, B>) => {
const stringCodec = new t.Type<A, string>(
C.name,
C.is,
(u, c) => {
return pipe(
t.string.validate(u, c),
E.chain((jsonString) => JsonFromString.validate(jsonString, c)),
E.chain((json) => C.validate(json, c)),
)
},
(v) => {
return pipe(v, C.encode, JsonFromString.encode)
},
)
return adaptIoTsCodec(stringCodec)
}
to commit to this repository there are a few rules:
commit-msg
hook).here you can find an explanation of the release flow.
FAQs
A small library to wrap browser's localstorage in a functional fashion.
The npm package localvalue-ts receives a total of 457 weekly downloads. As such, localvalue-ts popularity was classified as not popular.
We found that localvalue-ts demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.