Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

localvalue-ts

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

localvalue-ts - npm Package Compare versions

Comparing version 1.1.0-beta.7 to 2.0.0-beta.1

12

localStorage.d.ts

@@ -11,5 +11,5 @@ import { Codec, errorType, runtimeType } from "./Codec";

}
export declare const getLocalValue: <E, A>(t: string, codec: Codec<E, string, A>, options?: LocalValueOptions<A> | undefined) => LocalValue<E, A>;
export declare const setLocalValue: <E, A>(t: string, codec: Codec<E, string, A>, v: A, options?: LocalValueOptions<A> | undefined) => void;
export declare const removeLocalValue: (t: string, options?: LocalValueOptions<any> | undefined) => void;
export declare const getLocalValue: <E, A>(t: string, codec: Codec<E, string, A>, options?: LocalValueOptions<A> | undefined) => IO<LocalValue<E, A>>;
export declare const setLocalValue: <E, A>(t: string, codec: Codec<E, string, A>, v: A, options?: LocalValueOptions<A> | undefined) => IO<void>;
export declare const removeLocalValue: (t: string, options?: LocalValueOptions<any> | undefined) => IO<void>;
export declare type StorageDef<K extends string> = {

@@ -19,5 +19,5 @@ [k in K]: Codec<any, string, any>;

export declare type LocalValueModifiers<K> = {
getValue: () => LocalValue<errorType<K>, runtimeType<K>>;
setValue: (v: runtimeType<K>) => void;
removeValue: () => void;
get: IO<LocalValue<errorType<K>, runtimeType<K>>>;
set: (v: runtimeType<K>) => IO<void>;
remove: IO<void>;
};

@@ -24,0 +24,0 @@ export declare type StorageInstance<S> = S extends StorageDef<infer K> ? {

@@ -28,2 +28,5 @@ "use strict";

var LV = __importStar(require("./LocalValue"));
// -------------------------------------------------------------------------------------
// localStorage
// -------------------------------------------------------------------------------------
var memoryStore = new localStorageProxy_1.MemoryStorageProxy();

@@ -36,6 +39,8 @@ var getStore = function (useMemory) {

var store = getStore((_a = options === null || options === void 0 ? void 0 : options.useMemorySore) !== null && _a !== void 0 ? _a : false);
var storedValue = function_1.pipe(O.fromNullable(store.getItem(t)), LV.fromOption, LV.chain(function (v) { return codec.decode(v); }));
return (options === null || options === void 0 ? void 0 : options.defaultValue) && !LV.isValid(storedValue)
? LV.valid(options.defaultValue)
: storedValue;
return function () {
var storedValue = function_1.pipe(O.fromNullable(store.getItem(t)), LV.fromOption, LV.chain(function (v) { return codec.decode(v); }));
return (options === null || options === void 0 ? void 0 : options.defaultValue) && !LV.isValid(storedValue)
? LV.valid(options.defaultValue)
: storedValue;
};
};

@@ -46,3 +51,3 @@ exports.getLocalValue = getLocalValue;

var store = getStore((_a = options === null || options === void 0 ? void 0 : options.useMemorySore) !== null && _a !== void 0 ? _a : false);
store.setItem(t, codec.encode(v));
return function () { return store.setItem(t, codec.encode(v)); };
};

@@ -53,3 +58,3 @@ exports.setLocalValue = setLocalValue;

var store = getStore((_a = options === null || options === void 0 ? void 0 : options.useMemorySore) !== null && _a !== void 0 ? _a : false);
store.removeItem(t);
return function () { return store.removeItem(t); };
};

@@ -65,7 +70,7 @@ exports.removeLocalValue = removeLocalValue;

return function_1.pipe(storage, R.mapWithIndex(function (k, c) { return ({
getValue: function () { return exports.getLocalValue(k, c, storageOptionsToValueOptions(k, o)); },
setValue: function (v) { return exports.setLocalValue(k, c, v, o); },
removeValue: function () { return exports.removeLocalValue(k, o); },
get: exports.getLocalValue(k, c, storageOptionsToValueOptions(k, o)),
set: function (v) { return exports.setLocalValue(k, c, v, o); },
remove: exports.removeLocalValue(k, o),
}); }));
};
exports.createLocalStorage = createLocalStorage;

@@ -8,20 +8,1 @@ export declare const storeChangedCustomEvent = "storeChangedCustomEvent";

}
export declare const isLocalStorageEvent: (e: any) => e is LocalStorageChangedEvent;
interface IProxyStorage {
getItem(key: string): string | null;
setItem(Key: string, value: string): void;
removeItem(key: string): void;
}
export declare class LocalStorageProxy implements IProxyStorage {
getItem(key: string): string | null;
setItem(key: string, value: string): void;
removeItem(key: string): void;
}
export declare class MemoryStorageProxy implements IProxyStorage {
private _memoryStorage;
getItem(key: string): string | null;
setItem(key: string, value: string): void;
removeItem(key: string): void;
}
export declare const localStorageProxy: IProxyStorage;
export {};

@@ -32,2 +32,3 @@ "use strict";

};
/** @internal */
var isLocalStorageEvent = function (e) {

@@ -41,2 +42,3 @@ var _a;

};
/** @internal */
var LocalStorageProxy = /** @class */ (function () {

@@ -59,2 +61,3 @@ function LocalStorageProxy() {

exports.LocalStorageProxy = LocalStorageProxy;
/** @internal */
var MemoryStorageProxy = /** @class */ (function () {

@@ -79,4 +82,5 @@ function MemoryStorageProxy() {

exports.MemoryStorageProxy = MemoryStorageProxy;
/** @internal */
exports.localStorageProxy = localStorageAvailable()
? new LocalStorageProxy()
: new MemoryStorageProxy();

@@ -8,4 +8,2 @@ import { Kind2, URIS2 } from "fp-ts/HKT";

import { Foldable2 } from "fp-ts/Foldable";
import { Either } from "./Either";
import { Option } from "./Option";
export interface Absent {

@@ -12,0 +10,0 @@ readonly _tag: "Absent";

{
"name": "localvalue-ts",
"version": "1.1.0-beta.7",
"version": "2.0.0-beta.1",
"description": "A small library to wrap browser's localstorage in a functional fashion.",

@@ -45,9 +45,6 @@ "main": "index.js",

},
"peerDependencies": {
"io-ts": "^2.2.16"
},
"dependencies": {
"fp-ts": "^2.9.5",
"io-ts": "^2.2.16",
"monocle-ts": "2.3.5",
"monocle-ts": "^2.3.5",
"newtype-ts": "^0.3.4"

@@ -54,0 +51,0 @@ },

@@ -34,7 +34,12 @@ ![release](https://github.com/fido-id/localvalue-ts/actions/workflows/release.yml/badge.svg)

import * as t from "io-ts"
import { fromIoTsCodec } from "localvalue-ts/io-ts"
const ThemeFlavourC = t.union([t.literal("dark"), t.literal("light")])
export const ThemeFlavourCodec = {
decode: ...
encode: ...
}
export const ThemeFlavour = fromIoTsCodec(ThemeFlavourC)
export const AuthTokenCodec = {
decode: ...
encode: ...
}
```

@@ -46,15 +51,21 @@

// App.tsx
import { ThemeFlavour } from "./codecs.ts"
import { getLocalValue } from "localvalue-ts/localStorage"
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 = getLocalValue("themeFlavour", ThemeFlavour, {
defaultValue: "light",
})
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(
// N.B. using fold2 because with default value we will never have to deal with missing data
() => {

@@ -114,2 +125,39 @@ console.error("wrong value stored in localStorage!")

If you use `io-ts` you can simply create a layer to convert `io-ts` codecs to `Codec` compliant instances:
```ts
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"
import { Codec } from "./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)
}
```
## contributing

@@ -116,0 +164,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc