Skip to content

Instantly share code, notes, and snippets.

@DarrenSem
Created November 26, 2022 22:26
Show Gist options
  • Save DarrenSem/8d4c8c6fc655742c5214abf4fd29241e to your computer and use it in GitHub Desktop.
Save DarrenSem/8d4c8c6fc655742c5214abf4fd29241e to your computer and use it in GitHub Desktop.
store.js ALL-IN-ONE function: () = dump Storage, (keyOrNS, value OTHER THAN undefined) = return value after setting it, (keyOrNS) = return value - UNDEFINED if missing, (null, keyOrNS) = removeItem(keyOrNS) then return keyOrNS
// store.js ALL-IN-ONE function: () = dump Storage, (keyOrNS, value OTHER THAN undefined) = return value after setting it, (keyOrNS) = return value - UNDEFINED if missing, (null, keyOrNS) = removeItem(keyOrNS) then return keyOrNS
// let store=(k,d,s=localStorage,z=s.getItem(k))=>null==k?null==d?s:(s.removeItem(d),d):void 0===d?null==z?void 0:JSON.parse(z):(s.setItem(k,JSON.stringify(d,0,"\t")),d);
let store = (
keyOrNS,
data,
storage = [localStorage, sessionStorage][0],
_z = storage.getItem(keyOrNS)
) => {
return (
keyOrNS == null ? ( // if key is missing (undefined or null)...
data == null // ...AND data is missing (undefined or null)
? storage // then return Storage object itself
: ( storage.removeItem(data), data ) // if ONLY key is missing, remove 2nd arg = keyOrNS
)
: data === undefined ? ( // if key was provided BUT data is missing (and not null)
_z == null
? undefined // return undefined if keyOrNS has no value (AKA .getItem returned null)
: JSON.parse(_z) // return JSON.parse(value) -- which can return null, but NEVER returns undefined
)
: (
storage.setItem( keyOrNS, JSON.stringify(data, 0, "\t") ),
data // return the raw value (after storing the JSON.stringify version of it)
)
);
};
let assert=(...a)=>!!a.reduce((a,b,c,d,e=Array.isArray(b)?b:[b])=>("function"==typeof e[0]?e[0]():e[0])?a:console.assert(0,...e),a.length);
// let assert = (...tests) => !!tests.reduce(
// (pass, func, i, ar, messages = Array.isArray(func) ? func : [func]) => (
// typeof messages[0] === "function" ? messages[0]() : messages[0]
// ) ? pass : console.assert(0, ...messages)
// , tests.length
// );
let store_tests = assert(
() => store(2, 3.0) === 3,
[() => store(2.0) === 3, "key of 2.0 auto-converts to same key as 2"],
[() => store("2") === 3, "key of '2' auto-converts to same key as 2"],
() => store("2") == "3",
[() => store("2") !== "3", "strict equal check reveals value as 3 number not '3' string"],
[() => store(null, "2.0") === "2.0", "removeItem(keyOrNameSpace = 2nd arg of '2.0')"],
[() => store("2.0") === undefined, "key of '2.0' is different (no value); DOES NOT auto-convert key to 2"],
[() => store("2.0", null) === null, "JSON.stringify() any value, so null is valid (but undefined is not)"],
[() => store("2") === 3, "confirm value for key of '2' was not changed by key '2.0' being set"],
[() => store("2.0") === null, "confirm returned value for key of '2.0' is null (which is valid)"],
[() => store("missing") === undefined, "but return value for 'missing' key is undefined (invalid)"],
[(_z) => (_z = store(), console.log(_z), _z instanceof Storage), "missing keyOrNS + 'data' = dump Storage"],
[(_z) => (_z = store(null), console.log(_z), _z instanceof Storage), "null keyOrNS = dump"],
[() => store(2, undefined) === 3, "undefined is not a valid value - because JSON.parse(undefined) fails"],
[() => store(2, null) === null, "JSON.stringify() any value, so null is valid (but undefined is not)"],
[() => store("2.0", 4) === 4, "change value for key of '2.0' to 4"],
[() => store("2") === null, "confirm value for key of '2' is now null"],
[(_z) => (_z = store(null, null), console.log(_z), _z instanceof Storage), "null keyOrNS + 'data' = dump"],
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment