{"id":27535652,"url":"https://github.com/bytebodger/session-storage","last_synced_at":"2025-04-18T19:09:35.904Z","repository":{"id":57167239,"uuid":"349271377","full_name":"bytebodger/session-storage","owner":"bytebodger","description":"A small utility class for storing and retrieving complex values from sessionStorage","archived":false,"fork":false,"pushed_at":"2021-03-19T03:24:06.000Z","size":83,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-03-26T08:04:20.275Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bytebodger.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-19T01:59:32.000Z","updated_at":"2021-03-19T03:24:08.000Z","dependencies_parsed_at":"2022-08-30T15:21:56.860Z","dependency_job_id":null,"html_url":"https://github.com/bytebodger/session-storage","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytebodger%2Fsession-storage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytebodger%2Fsession-storage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytebodger%2Fsession-storage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytebodger%2Fsession-storage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bytebodger","download_url":"https://codeload.github.com/bytebodger/session-storage/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249323545,"owners_count":21251178,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-04-18T19:09:35.333Z","updated_at":"2025-04-18T19:09:35.893Z","avatar_url":"https://github.com/bytebodger.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# session-storage\n\nThis is a small utility class for storing and retrieving complex values from sessionStorage. By default, sessionStorage only saves strings. But by using `JSON.stringify()` and `JSON.parse()`, we can save objects, arrays, nulls, Booleans, numbers, and strings. And when using the `getItem()` method, it will retrieve those values in their native type.\n\nThis package also fails gracefully (and silently) in those instances where _there is no sessionStorage available_ in the client. One example where this can happen is when a user's browser is in Incognito Mode. In such cases, this package will use a simple session object to store the values temporarily. This will provide some semblance of sessionStorage-like behavior, even if those values will not be present in the next session.\n\nDue to the limitations of `JSON.stringify()` and `JSON.parse()`, the integrity of retrieved values cannot be maintained for certain complex values. Specifically, _functions_ will not survive the `JSON.stringify()/JSON.parse()` process.\n\n## Usage\n\n```javascript\nimport { session } from '@toolz/session-storage';\n\nsession.setItem('theAnswer', 42); // sets the value 42 in sessionStorage\nsession.getItem('theAnswer'); // returns the number 42\n```\n\n## Methods\n\n### .clear()\n\n`.clear()` empties all values from sessionStorage.\n\n```javascript\nconst API = {\n   arguments: {},\n   returns: void,\n}\n```\n\n**Examples:**\n\n```javascript\nsession.setItem('one', 1);\nsession.setItem('two', 2);\n\nsession.clear();\n\nsession.getItem('one'); // returns NULL\nsession.getItem('two', 22); // return 22\n```\n\n### .getItem()\n\n`.getItem()` retrieves an item from sessionStorage in its native data type. If it doesn't exist and no default value is provided, it returns `NULL`. If a default value is provided and the item doesn't exist, it sets the default value as the item and returns that value.\n\n```javascript\nconst API = {\n   arguments: {\n      itemName: {\n         required,\n         format: 'populated string',\n      },\n      defaultValue: {\n         optional,\n         format: any,\n      },\n   },\n   returns: any,\n}\n```\n\n**Examples:**\n\n```javascript\nsession.setItem('foo', [1, 2, 3]);\nsession.setItem('firstName', 'Joe');\nsession.setItem('address', {street: '101 Main', city: 'fooville'});\n\nsession.getItem('foo'); // returns [1, 2, 3]\nsession.getItem('firstName'); // returns 'Joe'\nsession.getItem('address'); // returns {street: '101 Main', city: 'fooville'}\nsession.getItem('notSet'); // returns NULL\nsession.getItem('anotherNotSet', 3.14); // returns 3.14\n```\n\n### .removeItem()\n\n`.removeItem()` unsets an item from sessionStorage. If the item didn't previously exist, the method throws no error.\n\n```javascript\nconst API = {\n   arguments: {\n      itemName: {\n         required,\n         format: 'populated string',\n      },\n   },\n   returns: true,\n}\n```\n\n**Examples:**\n\n```javascript\nsession.setItem('foo', [1, 2, 3]);\nsession.setItem('firstName', 'Joe');\n\nsession.removeItem('foo');\nsession.removeItem('firstName');\n\nsession.getItem('foo'); // returns NULL\nsession.getItem('firstName', 'Mary'); // return 'Mary'\n```\n\n### .setItem()\n\n`.setItem()` sets an item into sessionStorage. If the item already existed, it will overwrite the previous one. If the item did not exist, it will create a new item.\n\n```javascript\nconst API = {\n   arguments: {\n      itemName: {\n         required,\n         format: 'populated string',\n      },\n      itemValue: {\n         optional,\n         format: any,\n      },\n   },\n   returns: any,\n}\n```\n\n**Examples:**\n\n```javascript\nsession.setItem('foo', [1, 2, 3]);\nsession.setItem('firstName', 'Joe');\n\nsession.getItem('foo'); // returns [1, 2, 3]\nsession.getItem('firstName', 'Mary'); // return 'Joe'\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbytebodger%2Fsession-storage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbytebodger%2Fsession-storage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbytebodger%2Fsession-storage/lists"}