Guest User

Untitled

a guest
Sep 15th, 2017
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. /**
  2. * Check if localStorage is supported const isSupported: boolean
  3. * Check if localStorage has an Item function hasItem(key: string): boolean
  4. * Get the amount of space left in localStorage function getRemainingSpace(): number
  5. * Get the maximum amount of space in localStorage function getMaximumSpace(): number
  6. * Get the used space in localStorage function getUsedSpace(): number
  7. * Backup Assosiative Array interface Backup
  8. * Get a Backup of localStorage function getBackup(): Backup
  9. * Apply a Backup to localStorage function applyBackup(backup: Backup, fClear: boolean = true, fOverwriteExisting: boolean = true)
  10. * Dump all information of localStorage in the console function consoleInfo(fShowMaximumSize: boolean = false)
  11. */
  12. var LocalStorage;
  13. (function (LocalStorage) {
  14. /**
  15. * Flag set true if the Browser supports localStorage
  16. */
  17. LocalStorage.isSupported = (function () {
  18. try {
  19. var itemBackup = localStorage.getItem("");
  20. localStorage.removeItem("");
  21. localStorage.setItem("", itemBackup);
  22. if (itemBackup === null)
  23. localStorage.removeItem("");
  24. else
  25. localStorage.setItem("", itemBackup);
  26. return true;
  27. }
  28. catch (e) {
  29. return false;
  30. }
  31. })();
  32. /**
  33. * Check if localStorage has an Item / exists with the give key
  34. * @param key the key of the Item
  35. */
  36. function hasItem(key) {
  37. return localStorage.getItem(key) !== null;
  38. }
  39. LocalStorage.hasItem = hasItem;
  40. /**
  41. * This will return the left space in localStorage without affecting it's content
  42. * Might be slow !!!
  43. */
  44. function getRemainingSpace() {
  45. var itemBackup = localStorage.getItem("");
  46. var increase = true;
  47. var data = "1";
  48. var totalData = "";
  49. var trytotalData = "";
  50. while (true) {
  51. try {
  52. trytotalData = totalData + data;
  53. localStorage.setItem("", trytotalData);
  54. totalData = trytotalData;
  55. if (increase)
  56. data += data;
  57. }
  58. catch (e) {
  59. if (data.length < 2) {
  60. break;
  61. }
  62. increase = false;
  63. data = data.substr(data.length / 2);
  64. }
  65. }
  66. if (itemBackup === null)
  67. localStorage.removeItem("");
  68. else
  69. localStorage.setItem("", itemBackup);
  70. return totalData.length;
  71. }
  72. LocalStorage.getRemainingSpace = getRemainingSpace;
  73. /**
  74. * This function returns the maximum size of localStorage without affecting it's content
  75. * Might be slow !!!
  76. */
  77. function getMaximumSpace() {
  78. var backup = getBackup();
  79. localStorage.clear();
  80. var max = getRemainingSpace();
  81. applyBackup(backup);
  82. return max;
  83. }
  84. LocalStorage.getMaximumSpace = getMaximumSpace;
  85. /**
  86. * This will return the currently used size of localStorage
  87. */
  88. function getUsedSpace() {
  89. var sum = 0;
  90. for (var key in localStorage) {
  91. var value = localStorage.getItem(key);
  92. sum += key.length + value.length;
  93. }
  94. return sum;
  95. }
  96. LocalStorage.getUsedSpace = getUsedSpace;
  97. /**
  98. * This will return a localStorage-backup (Associative-Array key->value)
  99. */
  100. function getBackup() {
  101. var backup = {};
  102. for (var i = 0; i < localStorage.length; ++i) {
  103. var key = localStorage.key(i);
  104. var value = localStorage.getItem(key);
  105. backup[key] = value;
  106. }
  107. return backup;
  108. }
  109. LocalStorage.getBackup = getBackup;
  110. /**
  111. * This will apply a localStorage-Backup (Associative-Array key->value)
  112. * @param backup associative-array
  113. * @param fClear optional flag to clear all existing storage first. Default: true
  114. * @param fOverwriteExisting optional flag to replace existing keys. Default: true
  115. */
  116. function applyBackup(backup, fClear, fOverwriteExisting) {
  117. if (fClear === void 0) { fClear = true; }
  118. if (fOverwriteExisting === void 0) { fOverwriteExisting = true; }
  119. if (fClear == true) {
  120. localStorage.clear();
  121. }
  122. for (var key in backup) {
  123. if (fOverwriteExisting === false && backup[key] !== undefined) {
  124. continue;
  125. }
  126. var value = backup[key];
  127. localStorage.setItem(key, value);
  128. }
  129. }
  130. LocalStorage.applyBackup = applyBackup;
  131. /**
  132. * This functions dumps all keys and values of the local Storage to the console,
  133. * as well as the current size and number of items
  134. * @param fShowMaximumSize optional, flag show maximum size of localStorage. Default: false
  135. */
  136. function consoleInfo(fShowMaximumSize) {
  137. if (fShowMaximumSize === void 0) { fShowMaximumSize = false; }
  138. var amount = 0;
  139. var size = 0;
  140. for (var key in window.localStorage) {
  141. var value = localStorage.getItem(key);
  142. console.log(amount, key, value);
  143. size += key.length + value.length;
  144. amount++;
  145. }
  146. console.log("Total entries:", amount);
  147. console.log("Total size:", size);
  148. if (fShowMaximumSize === true) {
  149. var maxSize = getMaximumSpace();
  150. console.log("Total size:", maxSize);
  151. }
  152. }
  153. LocalStorage.consoleInfo = consoleInfo;
  154. })(LocalStorage || (LocalStorage = {}));
Advertisement
Add Comment
Please, Sign In to add comment