{"id":13826003,"url":"https://github.com/bcomnes/local-storage-proxy","last_synced_at":"2025-04-22T21:45:45.933Z","repository":{"id":35093626,"uuid":"205712104","full_name":"bcomnes/local-storage-proxy","owner":"bcomnes","description":"Work with window.localStorage as if it were an object","archived":false,"fork":false,"pushed_at":"2023-10-24T14:09:11.000Z","size":120,"stargazers_count":20,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T14:13:30.519Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/local-storage-proxy","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/bcomnes.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":["bcomnes"],"custom":["https://bret.io"]}},"created_at":"2019-09-01T17:49:18.000Z","updated_at":"2024-12-28T20:55:04.000Z","dependencies_parsed_at":"2023-01-15T13:41:07.467Z","dependency_job_id":"20fde7d6-f93e-4a0d-ab47-1c014cbf9c61","html_url":"https://github.com/bcomnes/local-storage-proxy","commit_stats":{"total_commits":87,"total_committers":3,"mean_commits":29.0,"dds":0.4482758620689655,"last_synced_commit":"578940a64b341a445df1afcf6911605300110b3d"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Flocal-storage-proxy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Flocal-storage-proxy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Flocal-storage-proxy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Flocal-storage-proxy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bcomnes","download_url":"https://codeload.github.com/bcomnes/local-storage-proxy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250330580,"owners_count":21412999,"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":"2024-08-04T09:01:30.622Z","updated_at":"2025-04-22T21:45:45.908Z","avatar_url":"https://github.com/bcomnes.png","language":"JavaScript","funding_links":["https://github.com/sponsors/bcomnes","https://bret.io"],"categories":["JavaScript"],"sub_categories":[],"readme":"# local-storage-proxy\n\nWork with window.localStorage as if it were an object\n\n```\nnpm install local-storage-proxy\n```\n\n## Usage\n\n``` js\nimport localStorageProxy from 'https://unpkg.com/local-storage-proxy@^2?module'\n\nwindow.localStorage.clear()\n\nconst state = localStorageProxy('namespace', {\n  defaults: {\n    some: [], // Doesn't override saved state\n    defaults: null\n  },\n  lspReset: false\n})\n\nconsole.log(state.some) // []\nstate.some.push('foo')\nconsole.log(state.some) // [ 'foo' ]\n\nstate.addEventListener('update', ev =\u003e {\n  console.log('state was updated, or localStorage was updated on another tab')\n})\n\nconsole.log(window.localStorage.getItem('namespace')) // {\"some\":[\"foo\"],\"defaults\":null}\n```\n\n## API\n\n### `lsp = localStorageProxy(namespace, [opts])`\n\nCreate a local storage proxy attatched to a root `namespace`.  All gets and sets will JSON.stringify to this key.  Returns nested proxies that update the key when you set the value.  Performance may be limited, but its great for small peices of data with limited writes.\n\n`lsp` is also an instance of an [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) and will emit an event under the `update` namespace. This is triggered whenever you set a value on the local storage proxy, or when the [`storage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event) event fires (e.g. when localStorage updates in another tab). The `storage` event is only listened for in the browser since there is no concept of a separate page or tab in node.js.\n\n`opts` include:\n\n```js\n{\n  defaults: {}, // Default keys to set.  Overridden by any existing local storage state\n  lspReset: false,\n  storageEventListener: true\n}\n```\n\nAdditions to the `opts.defaults` object can be safely added without overwriting existing data, and can also be assumed to be available even after the user has instantiated default and new data to the same namepace.\n\nDefault values are stored in localStorage. New default values can be added at any time, however if you change a default, you may need to updaate `lspReset`.\n\nAn `lspReset` key, set to false by default, can be used to force clear out local storage on all clients reloading if the value is different than the time they last loaded state.  It can be any JSON serializable object.  Strings work well.  This will need to be changed whenever change an existing default.\n\n`storageEventListener` is a boolean that determines if the [`storage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event) event is listened for, (only in the browser). Since there is no fined grained way to listen for a single namespace on this event, you might want to turn this off if localStorage is updated frequently by other routines which will cause local storage proxy to emit many `update` events.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcomnes%2Flocal-storage-proxy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbcomnes%2Flocal-storage-proxy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcomnes%2Flocal-storage-proxy/lists"}