Skip to content

Instantly share code, notes, and snippets.

@refracta
Last active May 14, 2022 10:35
Show Gist options
  • Save refracta/a732045ddb96efb6dbf353d05aacfed8 to your computer and use it in GitHub Desktop.
Save refracta/a732045ddb96efb6dbf353d05aacfed8 to your computer and use it in GitHub Desktop.
Object Proxy of localStorage
let ls = new Proxy(localStorage, {
get: (o1, p1) => o1[p1] ? new Proxy(JSON.parse(o1[p1]), {
get: (o2, p2) => typeof o2[p2] === 'function' ? function () { let r = o2[p2].apply(o2, arguments); ls[p1] = o2; return r; } : o2[p2],
set: (o2, p2, v2) => { o2[p2] = v2; ls[p1] = o2 }
}) : null,
set: (o, p, v) => o[p] = JSON.stringify(v)
});
/*
let ls = new Proxy(localStorage,
{
get: (o, p) => {
let level = 1;
let map = {['o' + level]: o, ['p' + level]: p};
function recur(o, p) {
}
recur(o, p);
}
set: (o, p, v) => o[p] = JSON.stringify(v)
}
);
*/
// Primitive number , string , boolean , null , undefined , symbol, bigint
// Only depth 1
// TODO: depth N
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment