{"id":18722018,"url":"https://github.com/realign/r-localstorage","last_synced_at":"2025-11-11T04:30:16.126Z","repository":{"id":124607906,"uuid":"163912254","full_name":"ReAlign/r-localstorage","owner":"ReAlign","description":"localstorage","archived":false,"fork":false,"pushed_at":"2020-04-09T09:34:14.000Z","size":22,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-28T17:48:52.914Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ReAlign.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-03T02:16:46.000Z","updated_at":"2019-04-19T03:26:43.000Z","dependencies_parsed_at":null,"dependency_job_id":"e7782ea7-02e3-49a3-a58e-3bbc41f9d2aa","html_url":"https://github.com/ReAlign/r-localstorage","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/ReAlign%2Fr-localstorage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ReAlign%2Fr-localstorage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ReAlign%2Fr-localstorage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ReAlign%2Fr-localstorage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ReAlign","download_url":"https://codeload.github.com/ReAlign/r-localstorage/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239587243,"owners_count":19663892,"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-11-07T13:39:22.550Z","updated_at":"2025-11-11T04:30:16.071Z","avatar_url":"https://github.com/ReAlign.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LocalStorage\n\n[![NPM version][npm-image]][npm-url]\n\n[npm-url]: https://www.npmjs.com/package/@realign-zone/local-storage\n[npm-image]: https://img.shields.io/npm/v/@realign-zone/local-storage.svg?style=for-the-badge\n\n## Introduction\n\n\u003e 让使用 localStorage 变得更简单\n\n* [x] 原生 `localStorage` 只能存储字符串\n* [x] 对于复杂数据结构，在存储之前，必须先进行序列化\n* [x] 对于复杂数据结构，在读取的时候，又需要反序列化\n* [x] 直接操作数据，可能会对数据造成覆盖，中间最好有一层检测机制，保证数据的准确性\n* [x] 对于 `查询当前剩余容量` `获取key列表` 等一些快捷操作，也做了封装\n* [x] 记录添加/更新时间\n* [x] 设置时效，过期自动清除\n\n针对上述，对 `localStorage` 进行一次封装，默认对数据进行序列化、反序列化、覆盖提醒、快捷处理等操作，用户只需关心自己要保存、获取的数据的一系列操作。\n\n## Usage\n\n### ES6\n\n```js\nimport LS from '@realign-zone/local-storage';\n```\n\n### UMD\n\n```js\n// download files\nuse @realign-zone/local-storage/dist/local-storage.umd.min.js\n```\n\n### Browser\n\n```html\n\u003cscript src=\"/local-storage.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n    LocalStorage.get('xxx');\n\u003c/script\u003e\n```\n\n***\n\n```js\n$VALUE = {\n    createTime: timestamp,\n    value\n};\n```\n\n### API\n\n#### set(key, value, opts)\n\n\u003e 存储数据，返回 set 的对象\n\n\n| args | description | note |\n| --- | --- | --- |\n| key | 存储数据的key |  |\n| value | 存储数据的实际值 |  |\n| opts | .cover: 是否覆盖已存在的数据 |  |\n|  | .expiry: 有效期【时间戳/时长描述】 | 时间戳：有效截止时间点，eg：1546786492336\u003cbr\u003e时长描述：数据存活时长，eg：1ms，2s，3m，4h，5d、6w，7mo，8y |\n\n```js\n// Example\nLS.set('author', {name: 'realign', age: 16}, {expiry: '3y'});\n\nreturn { key, val: $VALUE };\n```\n\n#### get(key)\n\n\u003e 获取数据，返回 对应值（会保持原来的数据类型）\n\n```js\n// Example\nLS.get('name');\nreturn $VALUE\n```\n\n#### has(key)\n\n\u003e 判断是否存在此 key，返回 boolean\n\n```js\n// Example\nLS.has('name'); // true\nLS.has('my'); // false\n```\n\n#### remove(key)\n\n\u003e 删除 key 对应的这条数据，返回 boolean，该条数据是否仍存在\n\n* 已成功删除 true（传入不存在的key，仍会返回 true）\n* 未成功删除 false\n\n```js\n// Example\nLS.remove('name'); // true\n```\n\n#### clear\n\n\u003e 清空当前域下 localstorage 数据\n\n```js\n// Example\nLS.clear(); // true\n```\n\n#### getKeyList\n\n\u003e 获取 localstorage 所有 key\n\n```js\n// Example\nLS.getKeyList(); // ['name', 'age']\n```\n\n#### getAll\n\n\u003e 获取 localstorage 所有 数据\n\n```js\n// Example\nLS.getAll(); // { 'name': $VALUE, 'age': $VALUE }\n```\n\n#### getSurplusCapacityKb\n\n\u003e 获取 localstorage 剩余容量（kb）\n\n```js\n// Example\nLS.getSurplusCapacityKb(); // 5119.998046875\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frealign%2Fr-localstorage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frealign%2Fr-localstorage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frealign%2Fr-localstorage/lists"}