trusted
Version:
⚖️ Trustworthy localStorage. Validate against a schema, set default values.
3 lines (2 loc) • 3.07 kB
JavaScript
;function e(){return(e=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}Object.defineProperty(exports,"__esModule",{value:!0}),exports.Trusted=function(){function t(e){void 0===e&&(e={}),this.namespace=e.namespace,this.registeredKeys=new Set,this.accessor.bind(this)}var r=t.prototype;return r.registerKey=function(e){if(this.registeredKeys.has(e))throw new Error("Key has already been registered: "+e);this.registeredKeys.add(e)},r.unregisterKey=function(e){this.registeredKeys.delete(e)},r.accessor=function(e){var t=this,r=""+(this.namespace||"")+e.key,n=e.yupSchema,a=e.skipRegistration,o=e.validate,s=void 0===o?n?function(e){return n.isValidSync(e)}:void 0:o,i=e.unmarshal,u=e.marshal,c=e.defaultValue;if(null!=c&&"function"==typeof s&&!s(c))throw new Error("Invalid default value provided at key: "+r+". Please check your yupSchema.");if(null!=c&&"string"!=typeof c&&"function"!=typeof u)throw new Error("Invalid configuration provided at key: "+r+". Non-string default values must be accompanied by a marshal function.");return a||this.registerKey(r),{get:function(){var e;try{if(null==(e=localStorage.getItem(r)))throw new Error("Item not found at key: "+r+".");if(e="function"==typeof i?i(e):e,"function"==typeof s&&!s(e))throw new Error("Item is invalid at key: "+r+".");return e}catch(e){return null!=c&&"function"==typeof u?localStorage.setItem(r,u(c)):"string"==typeof c?localStorage.setItem(r,c):void 0===c&&localStorage.removeItem(r),c}},set:function(e){!s||null==e||s(e)?null!=e&&"function"==typeof u?localStorage.setItem(r,u(e)):"string"==typeof e?localStorage.setItem(r,e):console.warn("You have attempted to set a non-string value in local storage without marshaling. This operation has been prevented.\nkey: "+r+"\nvalue: "+e):console.error("Invalid value provided at key: "+r+".",e)},remove:function(){localStorage.removeItem(r)},unregister:function(){t.unregisterKey(r)},getDefaultValue:function(){return c},getKey:function(){return r}}},r.string=function(e){return this.accessor(e)},r.boolean=function(t){return this.accessor(e({},t,{marshal:JSON.stringify,unmarshal:JSON.parse}))},r.object=function(t){return this.accessor(e({},t,{marshal:JSON.stringify,unmarshal:JSON.parse}))},r.number=function(t){return this.accessor(e({},t,{marshal:JSON.stringify,unmarshal:JSON.parse}))},r.array=function(t){return this.accessor(e({},t,{marshal:JSON.stringify,unmarshal:JSON.parse}))},r.map=function(t){return this.accessor(e({},t,{marshal:function(e){return JSON.stringify(Array.from(e))},unmarshal:function(e){return new Map(JSON.parse(e))}}))},r.set=function(t){return this.accessor(e({},t,{marshal:function(e){return JSON.stringify(Array.from(e))},unmarshal:function(e){return new Set(JSON.parse(e))}}))},r.date=function(t){return this.accessor(e({},t,{marshal:function(e){return e.toISOString()},unmarshal:function(e){return new Date(e)}}))},t}();
//# sourceMappingURL=trusted.cjs.production.min.js.map