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

ls-proxy

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ls-proxy - npm Package Compare versions

Comparing version 0.3.1 to 0.4.0

23

lib/index.d.ts
export { default as Validations } from './validations';
/** Configuration for storeObject */
/**
* Configuration for StoreObjectConfig
* @template O The stored object
*/
export interface StoreObjectConfig<O extends Record<string, any>> {

@@ -14,4 +17,4 @@ /**

*
* It's important to note that passing this option effectively enables a key validation of sorts;
* any keys that were not passed are ignored and not passed to validate or modify (if these methods are defined)
* It's important to note that passing this option effectively enables key validation:
* any keys that were not passed are ignored and not passed to validate or modify
* @default false

@@ -21,2 +24,14 @@ */

/**
* Called whenever a key should be set
* @param value The value being set
* @default localStorage.setItem
*/
set?(key: string, value: string): void;
/**
* Called whenever a key should be retrieved
* @param value The value being set
* @returns The key's value
*/
get?(key: string): string | null;
/**
* Validate an object before setting it in localStorage or reading it.

@@ -57,2 +72,3 @@ * Can confirm/deny if the object is valid, along with an optional error message if it is not

* @param configuration Config options
* @template O The stored object
*

@@ -155,2 +171,3 @@ * @example

* @param configuration Config options
* @template O The stored object
*

@@ -157,0 +174,0 @@ * @example

38

lib/index.js

@@ -6,6 +6,12 @@ "use strict";

Object.defineProperty(exports, "Validations", { enumerable: true, get: function () { return validations_1.default; } });
const defaultStoreObjectConfig = ({ checkGets, partial, validate, modify, parse, stringify, }) => {
/**
* Fill in default values for StoreObjectConfig
* @template O The stored object
*/
const defaultStoreObjectConfig = ({ checkGets, partial, set, get, validate, modify, parse, stringify, }) => {
return {
checkGets: checkGets !== null && checkGets !== void 0 ? checkGets : true,
partial: partial !== null && partial !== void 0 ? partial : false,
set: set !== null && set !== void 0 ? set : ((key, value) => (localStorage[key] = value)),
get: get !== null && get !== void 0 ? get : (value => { var _a; return (_a = localStorage[value]) !== null && _a !== void 0 ? _a : null; }),
validate: validate !== null && validate !== void 0 ? validate : (() => true),

@@ -24,3 +30,7 @@ modify: modify !== null && modify !== void 0 ? modify : (value => value),

[Object, Array].includes(object.constructor);
/** Proxy handler for deeply nested objects on the main object */
/**
* Proxy handler for deeply nested objects on the main object
* @template P The parent object
* @template N The child of the parent
*/
const nestedProxyHandler = (parent, parentKey, nested, parentSet) => {

@@ -52,2 +62,3 @@ return new Proxy(nested, {

* @param configuration Config options
* @template O The stored object
*

@@ -131,3 +142,3 @@ * @example

function storeObject(lsKey, defaults, configuration = {}) {
const { checkGets, partial, validate, modify, parse, stringify } = defaultStoreObjectConfig(configuration);
const { checkGets, partial, set, get, validate, modify, parse, stringify } = defaultStoreObjectConfig(configuration);
/** Call validOrThrow with relevant parameters by default */

@@ -157,13 +168,14 @@ const vot = (value, action = 'set') => validOrThrow(validate, modify, value, action, lsKey);

// Update localStorage value or read existing values
if (!localStorage[lsKey]) {
localStorage[lsKey] = checkStringify(defaults);
const value = get(lsKey);
if (!value) {
set(lsKey, checkStringify(defaults));
}
else if (partial) {
const current = parse(localStorage[lsKey]);
const current = parse(value);
object = filterWanted(current);
const validModified = vot(object);
localStorage[lsKey] = stringify(Object.assign(Object.assign({}, current), validModified));
set(lsKey, stringify(Object.assign(Object.assign({}, current), validModified)));
}
else {
object = checkParse(localStorage[lsKey]);
object = checkParse(value);
}

@@ -176,6 +188,6 @@ /** Proxy handler for the main object */

const validModified = vot(target);
localStorage[lsKey] = stringify(Object.assign(Object.assign({}, parse(localStorage[lsKey])), validModified));
set(lsKey, stringify(Object.assign(Object.assign({}, parse(get(lsKey))), validModified)));
}
else
localStorage[lsKey] = checkStringify(target);
set(lsKey, checkStringify(target));
return setResult;

@@ -187,7 +199,7 @@ },

if (partial) {
target[key] = vot(filterWanted(parse(localStorage[lsKey]), false), 'get')[key];
target[key] = vot(filterWanted(parse(get(lsKey)), false), 'get')[key];
vot(target, 'get');
}
else {
target[key] = (_a = checkParse(localStorage[lsKey])[key]) !== null && _a !== void 0 ? _a : defaults[key];
target[key] = (_a = checkParse(get(lsKey))[key]) !== null && _a !== void 0 ? _a : defaults[key];
}

@@ -213,2 +225,3 @@ if (shouldObjectProxy(target[key])) {

* @param lsKey The key in localStorage
* @template O The stored object
* @returns The object if valid

@@ -250,2 +263,3 @@ */

* @param configuration Config options
* @template O The stored object
*

@@ -252,0 +266,0 @@ * @example

@@ -0,1 +1,2 @@

/** Get the keys of an object as strings */
export declare type Keys<O extends Record<string, any>> = keyof O & string;

@@ -7,2 +7,6 @@ import type { Keys } from './types';

*
* @param value The unknown value to validate types of
* @param requiredKeys The **only** keys that should be present
* @template O The stored object
*
* @example

@@ -22,3 +26,3 @@ * ```typescript

*/
const keys: <O extends Record<string, any>>(value: Readonly<any>, requiredKeys: readonly string[]) => boolean;
const keys: <O extends Record<string, any>>(value: Readonly<any>, requiredKeys: readonly Keys<O>[]) => boolean;
/**

@@ -29,2 +33,3 @@ * Validate that the types passed for an object are expected

* @param typesMap A map of expected keys for an object to expected types, checked like `typeof value[key] === typesMap[key]`
* @template O The stored object
* @example

@@ -31,0 +36,0 @@ * ```typescript

@@ -9,2 +9,6 @@ "use strict";

*
* @param value The unknown value to validate types of
* @param requiredKeys The **only** keys that should be present
* @template O The stored object
*
* @example

@@ -31,2 +35,3 @@ * ```typescript

* @param typesMap A map of expected keys for an object to expected types, checked like `typeof value[key] === typesMap[key]`
* @template O The stored object
* @example

@@ -33,0 +38,0 @@ * ```typescript

{
"$schema": "https://json.schemastore.org/package",
"name": "ls-proxy",
"version": "0.3.1",
"version": "0.4.0",
"description": "Wrapper around localStorage to easily store JSON objects",

@@ -23,3 +23,3 @@ "repository": "https://gitlab.com/MysteryBlokHed/ls-proxy",

"doc": "typedoc",
"release": "yarn build --mode production && git add . && git commit -m \":bookmark: Release <version>\" --edit"
"release": "yarn build && webpack --mode production && git add . && git commit -m \":bookmark: Release <version>\" --edit && git rm ./*.min.user.js"
},

@@ -26,0 +26,0 @@ "jest": {

@@ -96,2 +96,9 @@ # ls-proxy [![Build Badge]](https://gitlab.com/MysteryBlokHed/ls-proxy/-/pipelines) [![NPM Badge]](https://www.npmjs.com/package/ls-proxy) [![License Badge]](#license)

Each release tag also has a minified version of the script available,
which can be used by changing the file extension to `.min.user.js`:
```javascript
// @require https://gitlab.com/MysteryBlokHed/ls-proxy/-/raw/v0.1.0/ls-proxy.min.user.js
```
Functions are available on the global `LSProxy` object:

@@ -98,0 +105,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