{"id":28237010,"url":"https://github.com/bytebodger/local-storage","last_synced_at":"2025-11-01T08:04:20.492Z","repository":{"id":40748596,"uuid":"237694596","full_name":"bytebodger/local-storage","owner":"bytebodger","description":"A small utility class for storing and retrieving complex values from localStorage","archived":false,"fork":false,"pushed_at":"2023-01-07T05:38:48.000Z","size":1406,"stargazers_count":4,"open_issues_count":12,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-19T00:17:33.378Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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":"2020-02-01T23:50:39.000Z","updated_at":"2024-11-11T12:30:31.000Z","dependencies_parsed_at":"2023-02-06T13:02:41.702Z","dependency_job_id":null,"html_url":"https://github.com/bytebodger/local-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%2Flocal-storage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytebodger%2Flocal-storage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytebodger%2Flocal-storage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytebodger%2Flocal-storage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bytebodger","download_url":"https://codeload.github.com/bytebodger/local-storage/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytebodger%2Flocal-storage/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259101151,"owners_count":22805216,"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-05-19T00:17:15.101Z","updated_at":"2025-11-01T08:04:20.396Z","avatar_url":"https://github.com/bytebodger.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# local-storage\n\nThis is a small utility class for storing and retrieving complex values from localStorage. By default, localStorage 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 localStorage 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 local object to store the values temporarily. This will provide some semblance of localStorage-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 { local } from '@toolz/local-storage';\n\nlocal.setItem('theAnswer', 42); // sets the value 42 in localStorage\nlocal.getItem('theAnswer'); // returns the number 42\n```\n\n## Methods\n\n### .clear()\n\n`.clear()` empties all values from localStorage.\n\n```javascript\nconst API = {\n   arguments: {},\n   returns: void,\n}\n```\n\n**Examples:**\n\n```javascript\nlocal.setItem('one', 1);\nlocal.setItem('two', 2);\n\nlocal.clear();\n\nlocal.getItem('one'); // returns NULL\nlocal.getItem('two', 22); // return 22\n```\n\n### .getItem()\n\n`.getItem()` retrieves an item from localStorage 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\nlocal.setItem('foo', [1, 2, 3]);\nlocal.setItem('firstName', 'Joe');\nlocal.setItem('address', {street: '101 Main', city: 'fooville'});\n\nlocal.getItem('foo'); // returns [1, 2, 3]\nlocal.getItem('firstName'); // returns 'Joe'\nlocal.getItem('address'); // returns {street: '101 Main', city: 'fooville'}\nlocal.getItem('notSet'); // returns NULL\nlocal.getItem('anotherNotSet', 3.14); // returns 3.14\n```\n\n### .removeItem()\n\n`.removeItem()` unsets an item from localStorage. 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\nlocal.setItem('foo', [1, 2, 3]);\nlocal.setItem('firstName', 'Joe');\n\nlocal.removeItem('foo');\nlocal.removeItem('firstName');\n\nlocal.getItem('foo'); // returns NULL\nlocal.getItem('firstName', 'Mary'); // return 'Mary'\n```\n\n### .setItem()\n\n`.setItem()` sets an item into localStorage. 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\nlocal.setItem('foo', [1, 2, 3]);\nlocal.setItem('firstName', 'Joe');\n\nlocal.getItem('foo'); // returns [1, 2, 3]\nlocal.getItem('firstName', 'Mary'); // return 'Joe'\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbytebodger%2Flocal-storage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbytebodger%2Flocal-storage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbytebodger%2Flocal-storage/lists"}