项目作者: r37r0m0d3l

项目描述 :
Universal localStorage fallback.
高级语言: JavaScript
项目地址: git://github.com/r37r0m0d3l/fallback-local-storage.git
创建时间: 2016-03-20T13:50:24Z
项目社区:https://github.com/r37r0m0d3l/fallback-local-storage

开源协议:MIT License

下载


Local Storage Fallback

Universal localStorage fallback.
Saves your time by fixing Private Mode error writing in web browser and
auto serialization that not included in localStorage by default.

npm
downloads
stars
types
build
lgtm

Install

  1. npm i fallback-local-storage

Include

Require CommonJS.

  1. const FallbackLocalStorage = require("fallback-local-storage");

Import as ECMAScript module.

  1. import FallbackLocalStorage from "fallback-local-storage";

CDN.

  1. <script src="https://unpkg.com/fallback-local-storage"></script>

Creating an instance

Creating instance with little check.

  1. let appStorage;
  2. if (FallbackLocalStorage.getStorage().includes("localStorage")) {
  3. // Here we don't have any problems
  4. // with writing to `window.localStorage`
  5. appStorage = globalThis.localStorage;
  6. } else {
  7. // Looks like we have some troubles.
  8. // Browser has disable `window.localStorage` support.
  9. // Or browser is in `Private Mode`
  10. // which disables localStorage completely.
  11. appStorage = new FallbackLocalStorage();
  12. }

Recommended way of using instance.

  1. globalThis.appStorage = appStorage;
  2. self.appStorage = appStorage;

Not recommended. Only if you have troubles with Private Mode and other libraries.

  1. globalThis.localStorage = new FallbackLocalStorage();
  2. self.localStorage = new FallbackLocalStorage();
  1. // Toggle debug information output.
  2. const DEBUG = false;
  3. // Allow iteration over instance.
  4. // Disable if you don't want be compatible with localStorage.
  5. const ITERABLE = true;
  6. // Serialize data before save and retrieve. VERY RECOMMENDED.
  7. const AUTO_SERIALIZE = true;
  8. // Custom serializer for values.
  9. const CUSTOM_SERIALIZER = null;
  10. const appStorage = new FallbackLocalStorage(
  11. DEBUG, ITERABLE, AUTO_SERIALIZE, CUSTOM_SERIALIZER
  12. );

API

All basic methods of localStorage are included.

  1. appStorage.setItem("hash", { name: "John" });
  2. appStorage.getItem("isTurnedOn", false);
  3. appStorage.removeItem("hash");
  4. appStorage.hasItem("hash");
  5. appStorage.keys();
  6. appStorage.values();
  7. appStorage.entries();
  8. appStorage.forEach();
  9. appStorage.toString();
  10. appStorage.toJSON();
  11. appStorage.length;
  12. appStorage.clear();

static getStorage (): Array

Return list of available storage

  1. FallbackLocalStorage.getStorage();
  2. // ["localStorage", "sessionStorage", "fallbackStorage"]

Examples

  1. const appStorage = new FallbackLocalStorage();
  2. // FallbackLocalStorage. Start using [localStorage].
  3. const array = ["ONE", "TWO", "THREE"];
  4. appStorage.setItem("array", array);
  5. // If auto-serialize is disabled
  6. // Value for key "array" will be converted to string: "ONE,TWO,THREE"
  7. // If auto-serialize is enabled
  8. // ["ONE", "TWO", "THREE"]
  9. const object = { "1": "ONE", "2": "TWO", "3": "THREE" };
  10. appStorage.setItem("object", object);
  11. // If auto-serialize is disabled
  12. // Value for key "object" will be converted to string: "[object Object]"
  13. // If auto-serialize is enabled
  14. // { 1: "ONE", 2: "TWO", 3: "THREE" }
  15. const map = new Map([[1, "ONE"], [2, "TWO"], [3, "THREE"]]);
  16. appStorage.setItem("map", map);
  17. // If auto-serialize is disabled
  18. // Value for key "map" will be converted to string: "[object Map]"
  19. // If auto-serialize is enabled
  20. // { 1: "ONE", 2: "TWO", 3: "THREE" }
  21. const set = new Set(["ONE", "TWO", "THREE"]);
  22. appStorage.setItem("set", set);
  23. // If auto-serialize is disabled
  24. // Value for key "set" will be converted to string: "[object Set]"
  25. // If auto-serialize is enabled
  26. // ["ONE", "TWO", "THREE"]

See also

My other projects