Comparing version 0.6.1 to 0.7.0
export * as react from './react'; | ||
export * as solid from './solid'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.react = void 0; | ||
exports.solid = exports.react = void 0; | ||
exports.react = require("./react"); | ||
exports.solid = require("./solid"); |
@@ -1,7 +0,5 @@ | ||
import { StoreSeparateConfig } from '..'; | ||
/** Basic signature for React setState function */ | ||
export declare type SetState<T> = (value: T) => void; | ||
/** Basic signature for React useState function */ | ||
import { SetState, Options } from './common'; | ||
/** Basic signature for useState function */ | ||
export declare type UseState = <T>(value: T) => [T, SetState<T>]; | ||
export declare type Options<O extends Record<string, any>> = Omit<StoreSeparateConfig<O>, 'checkGets' | 'checkDefaults' | 'parse' | 'set' | 'stringify'>; | ||
export { Options, SetState } from './common'; | ||
/** | ||
@@ -27,2 +25,2 @@ * Store multiple separate values in state that are automatically updated | ||
*/ | ||
export declare function storeStateful<O extends Record<string, any> = Record<string, any>>(defaults: O, useState: UseState, configuration?: Options<O>): O; | ||
export declare function useStateProxy<O extends Record<string, any> = Record<string, any>>(defaults: O, useState: UseState, configuration?: Options<O>): O; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.storeStateful = void 0; | ||
const __1 = require(".."); | ||
function keyInObject(key, object) { | ||
if (!(key in object)) { | ||
throw new TypeError(`${key} was not passed in defaults object`); | ||
} | ||
} | ||
exports.useStateProxy = void 0; | ||
const common_1 = require("./common"); | ||
/** | ||
@@ -30,34 +25,17 @@ * Store multiple separate values in state that are automatically updated | ||
*/ | ||
function storeStateful(defaults, useState, configuration = {}) { | ||
/** Current values */ | ||
const object = {}; | ||
/** setState functions */ | ||
const stateFunctions = {}; | ||
// Iterate over keys of defaults object | ||
for (const key of Object.keys(defaults).sort()) { | ||
// Save current value and setState method in separate objects | ||
; | ||
[object[key], stateFunctions[key]] = useState(defaults[key]); | ||
} | ||
/** State proxy object */ | ||
const state = (0, __1.storeSeparate)(object, Object.assign(Object.assign({}, configuration), { checkGets: false, checkDefaults: false, | ||
// Call useState for relevant key on set | ||
set(key, value) { | ||
keyInObject(key, object); | ||
stateFunctions[key](value); | ||
object[key] = value; | ||
}, | ||
// Should never be called due to config | ||
get: () => null, | ||
// Don't parse anything since raw object is stored | ||
parse: value => value, | ||
// Stringify and reparse if it's an object to remove the proxy while storing | ||
// Fixes React not rerendering on array/object changes | ||
stringify(value) { | ||
if (typeof value === 'object') | ||
return JSON.parse(JSON.stringify(value)); | ||
return value; | ||
} })); | ||
return state; | ||
function useStateProxy(defaults, useState, configuration = {}) { | ||
return (0, common_1.storeReactlikeState)(defaults, { | ||
setDefaults(defaults) { | ||
const current = {}; | ||
const stateFunctions = {}; | ||
// Iterate over keys of defaults object | ||
for (const key of Object.keys(defaults).sort()) { | ||
// Save current value and setState method in separate objects | ||
; | ||
[current[key], stateFunctions[key]] = useState(defaults[key]); | ||
} | ||
return { current, stateFunctions }; | ||
}, | ||
}, configuration); | ||
} | ||
exports.storeStateful = storeStateful; | ||
exports.useStateProxy = useStateProxy; |
import type { Keys } from './types'; | ||
export { default as Validations } from './validations'; | ||
export * as Factories from './factories'; | ||
/** | ||
@@ -19,2 +18,8 @@ * Configuration options used between both storeObject and storeSeparate | ||
/** | ||
* Whether to modify values on the proxied object or leave it as-is. | ||
* Doesn't do anything if passed with `partial` for `storeObject` | ||
* @default true | ||
*/ | ||
mutateProxiedObject?: boolean; | ||
/** | ||
* Called whenever a key should be set | ||
@@ -21,0 +26,0 @@ * @param value The value being set |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.storeSeparate = exports.storeObject = exports.Factories = exports.Validations = void 0; | ||
exports.storeSeparate = exports.storeObject = exports.Validations = void 0; | ||
var validations_1 = require("./validations"); | ||
Object.defineProperty(exports, "Validations", { enumerable: true, get: function () { return validations_1.default; } }); | ||
exports.Factories = require("./factories"); | ||
const setObj = (target, newObj) => Object.entries(newObj).forEach(([k, v]) => (target[k] = v)); | ||
/** | ||
* Fill in default values for CommonConfig | ||
*/ | ||
const commonDefaults = ({ checkGets, checkDefaults, set, get, parse, stringify, }) => ({ | ||
const commonDefaults = ({ checkGets, checkDefaults, mutateProxiedObject, set, get, parse, stringify, }) => ({ | ||
checkGets: checkGets !== null && checkGets !== void 0 ? checkGets : true, | ||
checkDefaults: checkDefaults !== null && checkDefaults !== void 0 ? checkDefaults : true, | ||
mutateProxiedObject: mutateProxiedObject !== null && mutateProxiedObject !== void 0 ? mutateProxiedObject : true, | ||
set: set !== null && set !== void 0 ? set : ((key, value) => (localStorage[key] = value)), | ||
@@ -180,3 +179,3 @@ get: get !== null && get !== void 0 ? get : (value => { var _a; return (_a = localStorage[value]) !== null && _a !== void 0 ? _a : null; }), | ||
function storeObject(lsKey, defaults, configuration = {}) { | ||
const { checkGets, checkDefaults, partial, set, get, validate, modify, parse, stringify, } = defaultStoreObjectConfig(configuration); | ||
const { checkGets, checkDefaults, mutateProxiedObject, partial, set, get, validate, modify, parse, stringify, } = defaultStoreObjectConfig(configuration); | ||
/** Call validOrThrow with relevant parameters by default */ | ||
@@ -224,3 +223,5 @@ const vot = (value, action = 'set') => validOrThrow(validate, modify, value, action, lsKey); | ||
set(target, key, value) { | ||
const setResult = Reflect.set(target, key, value); | ||
const setResult = mutateProxiedObject | ||
? Reflect.set(target, key, value) | ||
: true; | ||
if (partial) { | ||
@@ -237,13 +238,20 @@ const validModified = vot(target); | ||
if (checkGets) { | ||
let newVal = target[key]; | ||
if (partial) { | ||
target[key] = vot(filterWanted(parse(get(lsKey)), false), 'get')[key]; | ||
newVal = target[key] = vot(filterWanted(parse(get(lsKey)), false), 'get')[key]; | ||
vot(target, 'get'); | ||
} | ||
else { | ||
target[key] = (_a = checkParse(get(lsKey))[key]) !== null && _a !== void 0 ? _a : defaults[key]; | ||
newVal = (_a = checkParse(get(lsKey))[key]) !== null && _a !== void 0 ? _a : defaults[key]; | ||
} | ||
if (shouldObjectProxy(target[key])) { | ||
if (shouldObjectProxy(newVal)) { | ||
// Return a Proxy to the object to catch sets | ||
return nestedProxyHandler(target, key, target[key], this.set); | ||
return nestedProxyHandler(target, key, newVal, this.set); | ||
} | ||
if (mutateProxiedObject) { | ||
target[key] = newVal; | ||
} | ||
else { | ||
return newVal; | ||
} | ||
} | ||
@@ -357,3 +365,3 @@ return Reflect.get(target, key); | ||
function storeSeparate(defaults, configuration = {}) { | ||
const { id, checkGets, checkDefaults, set, get, validate, modify, parse, stringify, } = defaultStoreSeparateConfig(configuration); | ||
const { id, checkGets, checkDefaults, mutateProxiedObject, set, get, validate, modify, parse, stringify, } = defaultStoreSeparateConfig(configuration); | ||
const object = Object.assign({}, defaults); | ||
@@ -379,14 +387,26 @@ /** Call validOrThrow with relevant parameters by default */ | ||
set(addId(key, id), stringify(modified)); | ||
return Reflect.set(target, key, modified); | ||
if (mutateProxiedObject) { | ||
return Reflect.set(target, key, modified); | ||
} | ||
else { | ||
return true; | ||
} | ||
}, | ||
get(target, key) { | ||
let newVal = target[key]; | ||
if (checkGets) { | ||
const valueUnparsed = get(addId(key, id)); | ||
const value = valueUnparsed ? parse(valueUnparsed) : defaults[key]; | ||
target[key] = vot(key, value, 'get'); | ||
const value = valueUnparsed !== null ? parse(valueUnparsed) : defaults[key]; | ||
newVal = vot(key, value, 'get'); | ||
} | ||
if (shouldObjectProxy(target[key])) { | ||
if (shouldObjectProxy(newVal)) { | ||
// Return a Proxy to the object to catch sets | ||
return nestedProxyHandler(target, key, target[key], this.set); | ||
return nestedProxyHandler(target, key, newVal, this.set); | ||
} | ||
if (mutateProxiedObject) { | ||
target[key] = newVal; | ||
} | ||
else { | ||
return newVal; | ||
} | ||
return Reflect.get(target, key); | ||
@@ -393,0 +413,0 @@ }, |
{ | ||
"$schema": "https://json.schemastore.org/package", | ||
"name": "ls-proxy", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"description": "Wrapper around localStorage (and other stores) to easily store JSON objects", | ||
@@ -6,0 +6,0 @@ "repository": "https://gitlab.com/MysteryBlokHed/ls-proxy", |
@@ -146,3 +146,4 @@ # ls-proxy [![Build Badge]](https://gitlab.com/MysteryBlokHed/ls-proxy/-/pipelines) [![NPM Badge]](https://www.npmjs.com/package/ls-proxy) [![License Badge]](#license) | ||
Examples are located in [`examples`](https://gitlab.com/MysteryBlokHed/ls-proxy/-/tree/main/examples). | ||
Some examples are located in [`examples`](https://gitlab.com/MysteryBlokHed/ls-proxy/-/tree/main/examples), | ||
but many others are available in the JSDoc for their respective functions. | ||
@@ -167,3 +168,7 @@ ## storeObject vs storeSeparate | ||
- React State (`ls-proxy/factories/react`) | ||
- React State for functional components (`useStateProxy` in `ls-proxy/factories/react`) | ||
- Allows state to be used in a way more similar to that of class components | ||
in the context of a functional component | ||
- SolidJS Signals (`createSignalProxy` in `ls-proxy/factories/solid`) | ||
- Achieves a similar effect as the React factory | ||
@@ -170,0 +175,0 @@ See [For other stores](#for-other-stores) for a brief explanation about how to set one up. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62468
18
1052
308