lockr.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. (function(root, factory) {
  2. if (typeof exports !== 'undefined') {
  3. if (typeof module !== 'undefined' && module.exports) {
  4. exports = module.exports = factory(root, exports);
  5. }
  6. } else if (typeof define === 'function' && define.amd) {
  7. define(['exports'], function(exports) {
  8. root.Lockr = factory(root, exports);
  9. });
  10. } else {
  11. root.Lockr = factory(root, {});
  12. }
  13. }(this, function(root, Lockr) {
  14. 'use strict';
  15. if (!Array.prototype.indexOf) {
  16. Array.prototype.indexOf = function(elt /*, from*/)
  17. {
  18. var len = this.length >>> 0;
  19. var from = Number(arguments[1]) || 0;
  20. from = (from < 0)
  21. ? Math.ceil(from)
  22. : Math.floor(from);
  23. if (from < 0)
  24. from += len;
  25. for (; from < len; from++)
  26. {
  27. if (from in this &&
  28. this[from] === elt)
  29. return from;
  30. }
  31. return -1;
  32. };
  33. }
  34. Lockr.prefix = "";
  35. Lockr._getPrefixedKey = function(key, options) {
  36. options = options || {};
  37. if (options.noPrefix) {
  38. return key;
  39. } else {
  40. return this.prefix + key;
  41. }
  42. };
  43. Lockr.set = function (key, value, options) {
  44. var query_key = this._getPrefixedKey(key, options);
  45. try {
  46. localStorage.setItem(query_key, JSON.stringify({"data": value}));
  47. } catch (e) {
  48. if (console) console.warn("Lockr didn't successfully save the '{"+ key +": "+ value +"}' pair, because the localStorage is full.");
  49. }
  50. };
  51. Lockr.get = function (key, missing, options) {
  52. var query_key = this._getPrefixedKey(key, options),
  53. value;
  54. try {
  55. value = JSON.parse(localStorage.getItem(query_key));
  56. } catch (e) {
  57. if(localStorage[query_key]) {
  58. value = {data: localStorage.getItem(query_key)};
  59. } else{
  60. value = null;
  61. }
  62. }
  63. if(!value) {
  64. return missing;
  65. }
  66. else if (typeof value === 'object' && typeof value.data !== 'undefined') {
  67. return value.data;
  68. }
  69. };
  70. Lockr.sadd = function(key, value, options) {
  71. var query_key = this._getPrefixedKey(key, options),
  72. json;
  73. var values = Lockr.smembers(key);
  74. if (values.indexOf(value) > -1) {
  75. return null;
  76. }
  77. try {
  78. values.push(value);
  79. json = JSON.stringify({"data": values});
  80. localStorage.setItem(query_key, json);
  81. } catch (e) {
  82. console.log(e);
  83. if (console) console.warn("Lockr didn't successfully add the "+ value +" to "+ key +" set, because the localStorage is full.");
  84. }
  85. };
  86. Lockr.smembers = function(key, options) {
  87. var query_key = this._getPrefixedKey(key, options),
  88. value;
  89. try {
  90. value = JSON.parse(localStorage.getItem(query_key));
  91. } catch (e) {
  92. value = null;
  93. }
  94. return (value && value.data) ? value.data : [];
  95. };
  96. Lockr.sismember = function(key, value, options) {
  97. return Lockr.smembers(key).indexOf(value) > -1;
  98. };
  99. Lockr.keys = function() {
  100. var keys = [];
  101. var allKeys = Object.keys(localStorage);
  102. if (Lockr.prefix.length === 0) {
  103. return allKeys;
  104. }
  105. allKeys.forEach(function (key) {
  106. if (key.indexOf(Lockr.prefix) !== -1) {
  107. keys.push(key.replace(Lockr.prefix, ''));
  108. }
  109. });
  110. return keys;
  111. };
  112. Lockr.getAll = function (includeKeys) {
  113. var keys = Lockr.keys();
  114. if (includeKeys) {
  115. return keys.reduce(function (accum, key) {
  116. var tempObj = {};
  117. tempObj[key] = Lockr.get(key);
  118. accum.push(tempObj);
  119. return accum;
  120. }, []);
  121. }
  122. return keys.map(function (key) {
  123. return Lockr.get(key);
  124. });
  125. };
  126. Lockr.srem = function(key, value, options) {
  127. var query_key = this._getPrefixedKey(key, options),
  128. json,
  129. index;
  130. var values = Lockr.smembers(key, value);
  131. index = values.indexOf(value);
  132. if (index > -1)
  133. values.splice(index, 1);
  134. json = JSON.stringify({"data": values});
  135. try {
  136. localStorage.setItem(query_key, json);
  137. } catch (e) {
  138. if (console) console.warn("Lockr couldn't remove the "+ value +" from the set "+ key);
  139. }
  140. };
  141. Lockr.rm = function (key) {
  142. var queryKey = this._getPrefixedKey(key);
  143. localStorage.removeItem(queryKey);
  144. };
  145. Lockr.flush = function () {
  146. if (Lockr.prefix.length) {
  147. Lockr.keys().forEach(function(key) {
  148. localStorage.removeItem(Lockr._getPrefixedKey(key));
  149. });
  150. } else {
  151. localStorage.clear();
  152. }
  153. };
  154. return Lockr;
  155. }));