Skip to content

Instantly share code, notes, and snippets.

@brunnerh
Last active April 30, 2022 12:06
Show Gist options
  • Save brunnerh/14b8585978399d47a69a788379a93805 to your computer and use it in GitHub Desktop.
Save brunnerh/14b8585978399d47a69a788379a93805 to your computer and use it in GitHub Desktop.
Function for enumerating storage contents.
/**
* Turns a storage into an array of key/value tuples.
* (Same format as the result of {@link Object.entries}.)
* @param {Storage} storage A storage instance (e.g. {@link Window.localStorage}).
* @returns {[string, string][]} Key/value tuple array of entries.
*/
function entriesFromStorage(storage) {
return new Array(storage.length).fill(0)
.map((e, i) => storage.key(i))
.map(key => [key, storage.getItem(key)]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment