Comparing version 0.3.0 to 0.3.1
@@ -16,2 +16,27 @@ "use strict"; | ||
}; | ||
const shouldObjectProxy = (object) => | ||
// Check that the target isn't falsey (primarily in case it's null, since typeof null === 'object') | ||
object && | ||
// Check type | ||
typeof object === 'object' && | ||
// 'object' type includes some unwanted types, so check constructor | ||
[Object, Array].includes(object.constructor); | ||
/** Proxy handler for deeply nested objects on the main object */ | ||
const nestedProxyHandler = (parent, parentKey, nested, parentSet) => { | ||
return new Proxy(nested, { | ||
set(target, key, value) { | ||
const setResult = Reflect.set(target, key, value); | ||
// Trigger set trap of original object, updating localStorage | ||
parentSet(parent, parentKey, target, parent); | ||
return setResult; | ||
}, | ||
get(target, key) { | ||
if (shouldObjectProxy(target[key])) { | ||
// Return a Proxy to the object to catch sets | ||
return nestedProxyHandler(target, key, Reflect.get(target, key), this.set); | ||
} | ||
return Reflect.get(target, key); | ||
}, | ||
}); | ||
}; | ||
/** | ||
@@ -141,26 +166,2 @@ * Store a stringified JSON object in localStorage. | ||
} | ||
/** Proxy handler for deeply nested objects on the main object */ | ||
const nestedProxyHandler = (parent, parentKey, nested, parentSet) => { | ||
return new Proxy(nested, { | ||
set(target, key, value) { | ||
const setResult = Reflect.set(target, key, value); | ||
// Trigger set trap of original object, updating localStorage | ||
parentSet(parent, parentKey, target, parent); | ||
return setResult; | ||
}, | ||
get(target, key) { | ||
if ( | ||
// Check that the target isn't falsey (primarily in case it's null, since typeof null === 'object') | ||
target[key] && | ||
// Check type | ||
typeof target[key] === 'object' && | ||
// 'object' type includes arrays and other things, so check the constructor | ||
target[key].constructor === Object) { | ||
// Return a Proxy to the object to catch sets | ||
return nestedProxyHandler(target, key, Reflect.get(target, key), this.set); | ||
} | ||
return Reflect.get(target, key); | ||
}, | ||
}); | ||
}; | ||
/** Proxy handler for the main object */ | ||
@@ -188,9 +189,3 @@ const proxyHandler = { | ||
} | ||
if ( | ||
// Check that the target isn't falsey (primarily in case it's null, since typeof null === 'object') | ||
target[key] && | ||
// Check type | ||
typeof target[key] === 'object' && | ||
// 'object' type includes arrays and other things, so check the constructor | ||
target[key].constructor === Object) { | ||
if (shouldObjectProxy(target[key])) { | ||
// Return a Proxy to the object to catch sets | ||
@@ -197,0 +192,0 @@ return nestedProxyHandler(target, key, target[key], this.set); |
{ | ||
"$schema": "https://json.schemastore.org/package", | ||
"name": "ls-proxy", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Wrapper around localStorage to easily store JSON objects", | ||
@@ -6,0 +6,0 @@ "repository": "https://gitlab.com/MysteryBlokHed/ls-proxy", |
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
25557
554