-
Notifications
You must be signed in to change notification settings - Fork 1
/
browsedb.min.js
7 lines (7 loc) · 7.14 KB
/
browsedb.min.js
1
2
3
4
5
6
7
function ownKeys(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);t&&(o=o.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),r.push.apply(r,o)}return r}function _objectSpread(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(Object(r),!0).forEach(function(t){_defineProperty(e,t,r[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):ownKeys(Object(r)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))})}return e}function _defineProperty(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function _toConsumableArray(e){return _arrayWithoutHoles(e)||_iterableToArray(e)||_unsupportedIterableToArray(e)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(e,t){if(e){if("string"==typeof e)return _arrayLikeToArray(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(e,t):void 0}}function _iterableToArray(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function _arrayWithoutHoles(e){if(Array.isArray(e))return _arrayLikeToArray(e)}function _arrayLikeToArray(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,o=new Array(t);r<t;r++)o[r]=e[r];return o}function _typeof(e){"@babel/helpers - typeof";return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var r=0;r<t.length;r++){var o=t[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}function _createClass(e,t,r){return t&&_defineProperties(e.prototype,t),r&&_defineProperties(e,r),e}
/**
* @author Spiff Jekey-Green <spiffjekeygreen@gmail.com>
* @see https://browsedb.github.io
* @version 1.0.0
* @license GPL-3.0-or-later
*/var BrowseDB=function(){"use strict";function e(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;_classCallCheck(this,e),null===localStorage.getItem(t)&&localStorage.setItem(t,"[]"),this.collectionName=t,this.schema=r,this.create=this.create.bind(this),this.find=this.find.bind(this),this.findById=this.findById.bind(this),this.update=this.update.bind(this),this.updateById=this.updateById.bind(this),this.updateAll=this.updateAll.bind(this),this.delete=this.delete.bind(this),this.deleteById=this.deleteById.bind(this),this.deleteAll=this.deleteAll.bind(this),this.remove=this.remove.bind(this),this.idGenerator=this.idGenerator.bind(this),this.objLength=this.objLength.bind(this),this.checkSchema=this.checkSchema.bind(this)}return _createClass(e,[{key:"idGenerator",value:function(){for(var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:10,t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",r="",o=0;o<e;o++)r+=t.charAt(Math.floor(Math.random()*t.length));return r}},{key:"objLength",value:function(e){if("object"!==_typeof(e))throw new TypeError("Parameter '1' of create should be an object");var t=0;for(var r in e)++t;return t}},{key:"checkSchema",value:function(e,t){var r=e;for(var o in t)if("object"!==_typeof(t[o])){if("function"!=typeof t[o])throw new TypeError("Expected a function");if(_typeof(e[o])!==_typeof(t[o]()))throw new TypeError("Expected a ".concat(_typeof(t[o]())," instead got a ").concat(_typeof(e[o])))}else{if(void 0!==t[o].default&&null==e[o]&&(r[o]="function"==typeof t[o].default?t[o].default():t[o].default),!0===t[o].required&&null==e[o])throw new EvalError("'".concat(o,"' is required"));if("function"==typeof t[o].type&&_typeof(t[o].type())!==_typeof(r[o]))throw new TypeError("Expected a ".concat(_typeof(t[o]())," instead got a ").concat(_typeof(e[o])))}return r}},{key:"create",value:function(e){if("object"!==_typeof(e))throw new TypeError("Parameter '1' of create should be an object");var t=this.idGenerator(10);null!==this.schema&&(e=this.checkSchema(e,this.schema));var r=[_objectSpread(_objectSpread({},e),{},{id:t})].concat(_toConsumableArray(JSON.parse(localStorage.getItem(this.collectionName))));return localStorage.setItem(this.collectionName,JSON.stringify(r)),_objectSpread(_objectSpread({},e),{},{id:t})}},{key:"find",value:function(e,t){if(void 0===e||this.objLength(e)<1){var r=JSON.parse(localStorage.getItem(this.collectionName));return void 0===t?r:r.map(function(e){for(var r in t)0===t[r]&&delete e[r];return e})}if("object"!==_typeof(e))throw new TypeError("Parameter '1' of function should be object");var o=JSON.parse(localStorage.getItem(this.collectionName)),n=[];return o.forEach(function(r){for(var o in e)if(r[o]===e[o]){if(void 0!==t)for(var i in t)if(0===t[i])delete r[i];else for(var a in r)a!==i&&delete r[a];n.push(r)}}),n}},{key:"findById",value:function(e){if("string"!=typeof e)throw TypeError("Parameter '1' of function should be string");return JSON.parse(localStorage.getItem(this.collectionName)).filter(function(t){return t.id===e})[0]}},{key:"update",value:function(e,t){if("object"!==_typeof(e))throw new TypeError("Parameter '1' of function should be object");var r=JSON.parse(localStorage.getItem(this.collectionName)),o=[],n={};return r.forEach(function(r){var i=!1;for(var a in e)i=r[a]===e[a];if(i){for(var l in t)r[l]&&(r[l]=t[l]);n=r}o.push(r)}),localStorage.setItem(this.collectionName,JSON.stringify(o)),n}},{key:"updateById",value:function(e,t){var r=JSON.parse(localStorage.getItem(this.collectionName)),o={},n=[];return r.forEach(function(r){if(r.id===e){for(var i in t)r[i]&&(r[i]=t[i]);o=r}n.push(r)}),localStorage.setItem(this.collectionName,JSON.stringify(n)),o}},{key:"updateAll",value:function(e){var t=JSON.parse(localStorage.getItem(this.collectionName)),r=[];return t.forEach(function(t){for(var o in e)t[o]&&(t[o]=e[o]);r.push(t)}),localStorage.setItem(this.collectionName,JSON.stringify(r)),r}},{key:"delete",value:function(e){var t=JSON.parse(localStorage.getItem(this.collectionName)),r={},o=t.filter(function(t){var o=!1;for(var n in e)o=t[n]===e[n];if(!o)return t;r=t});return localStorage.setItem(this.collectionName,JSON.stringify(o)),r}},{key:"deleteById",value:function(e){var t=JSON.parse(localStorage.getItem(this.collectionName)),r={},o=t.filter(function(t){return t.id===e&&(r=t),t.id!==e});return localStorage.setItem(this.collectionName,JSON.stringify(o)),r}},{key:"deleteAll",value:function(){return localStorage.setItem(this.collectionName,"[]"),[]}},{key:"remove",value:function(e){if("object"!==_typeof(e)||void 0!==e)throw new TypeError("Parameter '1' should be type object");return(this.objLength(e)<1||void 0===e)&&localStorage.setItem(this.collectionName,"[]"),this.delete(e)}}]),e}();