memorio 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +10 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1 +1,10 @@
|
|
|
1
|
-
"use strict";(()=>{Object.defineProperty(window,"memorio",{value:{},writable:!1,configurable:!1});Object.defineProperty(memorio,"setProps",{value:(e,t,
|
|
1
|
+
"use strict";(()=>{Object.defineProperty(window,"memorio",{value:{},writable:!1,configurable:!1});Object.defineProperty(memorio,"setProps",{value:(e,t,r)=>{Object.defineProperty(e,t.name,r||{writable:!1,configurable:!1,enumerable:!1}),r?.lock&&Object.freeze(e[t.name])},writable:!1,configurable:!1,enumerable:!1});Object.defineProperty(memorio,"setDescription",{value:(e,t)=>{Object.defineProperties(memorio,{[e.name]:{value:t,writable:!1,configurable:!1,enumerable:!1}}),Object.keys(t).forEach(function(r){Object.defineProperties(memorio[e.name],{[r]:{writable:!1,configurable:!1,enumerable:!1}})}),memorio.setProps(memorio,e,{writable:!1,configurable:!1,enumerable:!1})}});var m={name:"dispatch",active:!0,subCommand:[{name:"set",version:"0.0.1",example:"memorio.dispatch.set([name, value])",author:"Dario Passariello",creationDate:"20231231",lastMod:"20240612",type:"function",active:!0,description:"Dispatches a custom event with the specified name and value.",subCommand:[]},{name:"listen",version:"0.0.1",example:"memorio.dispatch.listen([eventNames, callback=null], flag = true)",author:"Dario Passariello",creationDate:"20231231",lastMod:"20240612",type:"function",active:!0,description:"Listens for the specified event names and executes the callback when the event is triggered.",subCommand:[]},{name:"remove",version:"0.0.1",example:"memorio.dispatch.remove([eventNames, callback=null], flag = true)",author:"Dario Passariello",creationDate:"20231231",lastMod:"20240612",type:"function",active:!0,description:"Removes the event listener for the specified event names.",subCommand:[]}]},f={set:(e,t={})=>{dispatchEvent(new CustomEvent(String(e),t))},listen:(e,t=null,r=!1)=>{observer.list?.[e]?.length>0&&observer.remove(e);let a=o=>t?setTimeout(()=>t(o),0):null;globalThis.events[e]=a},remove:e=>{globalThis.events.name=""}};memorio.setDescription(m,f);var b={name:"obj",active:!0,subCommand:[{name:"toArray",version:"0.0.1",example:"memorio.obj.toArray([obj])",description:"Convert an object to an array.",author:"Dario Passariello",creationDate:"20210101",lastMod:"20210101",type:"function",active:!0,subCommand:[]},{name:"replaceNullObjects",version:"0.0.1",example:"memorio.obj.replaceNullObjects([obj])",description:"Replace null values in an object with empty objects.",author:"Dario Passariello",creationDate:"20210101",lastMod:"20210101",type:"function",active:!0,subCommand:[]},{name:"serialize",version:"0.0.1",example:"memorio.obj.serialize([obj])",description:"Serialize an object to a JSON string.",author:"Dario Passariello",creationDate:"20210101",lastMod:"20210101",type:"function",active:!0,subCommand:[]},{name:"deSerialize",version:"0.0.1",example:"memorio.obj.deSerialize([obj])",description:"Deserialize a JSON string to an object.",author:"Dario Passariello",creationDate:"20210101",lastMod:"20210101",type:"function",active:!0,subCommand:[]},{name:"find",version:"0.0.1",example:"memorio.obj.find([array, key, value])",description:"Find an object in an array by key and value.",author:"Dario Passariello",creationDate:"20210101",lastMod:"20210101",type:"function",active:!0,subCommand:[]},{name:"toXML",version:"0.0.1",example:"memorio.obj.toXML([obj])",description:"Convert an object to an XML string.",author:"Dario Passariello",creationDate:"20240315",lastMod:"20240315",type:"function",active:!0,subCommand:[]},{name:"instance",version:"0.0.1",example:"memorio.obj.instance([obj])",description:"Create an instance of an object.",author:"Dario Passariello",creationDate:"20240924",lastMod:"20240924",type:"function",active:!0,subCommand:[]},{name:"updateByKey",version:"0.0.1",example:"memorio.obj.updateByKey([obj, key, newValue])",description:"Update an object's property by key.",author:"Dario Passariello",creationDate:"20240929",lastMod:"20240929",type:"function",active:!0,subCommand:[]},{name:"findindex",version:"0.0.1",example:"memorio.obj.findindex([obj, key])",description:"Find the index of an object in an array by key.",author:"Dario Passariello",creationDate:"20240929",lastMod:"20240929",type:"function",active:!0,subCommand:[]},{name:"parse",version:"0.0.1",example:"memorio.obj.parse(val)",description:"Check if value is an object or another type. Return object after parse or a different type. Used instead of JSON.parse to avoid crash.",author:"Dario Passariello",creationDate:"20241027",lastMod:"20240927",type:"function",active:!0,subCommand:[]},{name:"diff",version:"0.0.1",example:"memorio.obj.diff([obj1, obj2])",description:"Show the differences between two objects.",author:"Dario Passariello",creationDate:"20250101",lastMod:"20250101",type:"function",active:!0,subCommand:[]},{name:"shallow",version:"0.0.1",example:"memorio.obj.shallow([object])",author:"Dario Passariello",creationDate:"20250101",lastMod:"20250101",type:"function",active:!0,description:`
|
|
2
|
+
Generate a shallow copy of an object
|
|
3
|
+
Example: memorio.obj.shallow([object])
|
|
4
|
+
`,subCommand:[]},{name:"deepCopy",version:"0.0.1",example:"memorio.obj.deepCopy([object])",author:"Dario Passariello",creationDate:"20250101",lastMod:"20250101",type:"function",active:!0,description:`
|
|
5
|
+
Generate a deepCopy of an object
|
|
6
|
+
Example: memorio.obj.deepCopy([object])
|
|
7
|
+
`,subCommand:[]}]},d={toArray:e=>Object.entries(e),replaceNullObjects:e=>{let t={...e};return Object.keys(e).forEach(r=>{e[r]===null&&(t[r]="")}),t},serialize:e=>{if(typeof e=="function")return e.toString();if(typeof e=="object"){let t={};for(let[r,a]of Object.entries(e))t[r]=memorio.obj.serialize(a);return t}return e},deSerialize:e=>{if(typeof e=="string"&&e.toLowerCase().startsWith("function("))return Function('"use strict"; return '+e)();if(typeof e=="object"){let t={};for(let[r,a]of Object.entries(e))t[r]=memorio.obj.deSerialize(a);return t}return e},sort:e=>Object.keys(e).sort().reduce((t,r)=>(t[r]=e[r],t),{}),toXML:e=>{let t=(r,a="")=>{let o="";for(let n in r)if(r.hasOwnProperty(n)){let i=r[n];typeof i=="object"&&i!==null?o+=`${a}<${n}>
|
|
8
|
+
${t(i,a+" ")}${a}</${n}>
|
|
9
|
+
`:o+=`${a}<${n}>${i}</${n}>
|
|
10
|
+
`}return o};return t(e)},find:(e,t,r)=>e.find(a=>a[t]===r),instance:e=>Object.create(e),updateByKey:(e,t,r)=>(e.hasOwnProperty(t)&&(e[t]=r),e),findindex:(e,t)=>e.findIndex(r=>r[t]!==void 0),parse:e=>{try{return JSON.parse(e)}catch{return e}},isObject:e=>typeof e=="object"&&e!==null,diff:(e,t)=>{let r={};for(let a in e)e.hasOwnProperty(a)&&e[a]!==t[a]&&(r[a]={obj1:e[a],obj2:t[a]});for(let a in t)t.hasOwnProperty(a)&&e[a]!==t[a]&&(r[a]={obj1:e[a],obj2:t[a]});return r},path:(e,t,r=".")=>t.concat(e).join(r),shallow:e=>Object.assign({},e),deepCopy:e=>structuredClone(e)};memorio.setDescription(b,d);Object.defineProperty(globalThis,"store",{value:new Proxy({},{}),enumerable:!1,configurable:!1});Object.defineProperties(store,{get:{value(e){if(e)try{let t=localStorage.getItem(e);return t&&JSON.parse(t)}catch(t){console.error(`Error parsing store item "${e}":`,t)}}},set:{value(e,t){if(e)try{t==null?localStorage.setItem(e,JSON.stringify(null)):typeof t=="object"||typeof t=="number"||typeof t=="boolean"||typeof t=="string"?localStorage.setItem(e,JSON.stringify(t)):typeof t=="function"&&console.error("It's not secure to store functions.")}catch(r){console.error(`Error setting store item "${e}":`,r)}}},remove:{value(e){if(e&&localStorage.getItem(e))return localStorage.removeItem(e),!0}},delete:{value(e){store.remove(e)}},removeAll:{value(){return localStorage.clear(),!0}},clearAll:{value(){return store.removeAll(),!0}},quota:{value(){"storage"in navigator&&"estimate"in navigator.storage&&navigator.storage.estimate().then(({usage:e,quota:t})=>{e&&t&&console.debug(`Using ${e/1024} out of ${t/1024} Mb.`)}).catch(e=>{console.error("Error estimating quota:",e)})}},size:{value(){let e=0;for(let t in localStorage)if(localStorage.hasOwnProperty(t)){let r=localStorage.getItem(t);r&&(e+=r.length)}return e}}});Object.freeze(store);var c=(e,t,r=[])=>{let a=o=>{let n=o.split("-");n.forEach((i,s)=>{let l=n.slice(0,s+1).join("-");memorio.dispatch.set(l,{detail:{name:l}})})};return new Proxy(e,{get(o,n){if(Object.isFrozen(o[n]))return o[n];try{let i=Reflect.get(o,n);return i&&typeof i=="object"&&["Array","Object"].includes(i.constructor.name)?c(i,t,r.concat(n)):i}catch(i){return console.error("Error: ",i),!1}},set(o,n,i){if(o[n]&&typeof o[n]=="object"&&Object.isFrozen(o[n])){console.error(`Error: state "${n}" is locked`);return}try{let s=memorio.obj.path(n,r);return t({action:"set",path:s,target:o,newValue:i,previousValue:Reflect.get(o,n)}),a("state-"+s.replaceAll(".","-")),Reflect.set(o,n,i),o[n]&&typeof o[n]=="object"&&Reflect.defineProperty(o[n],"lock",{value(){Object.defineProperty(o,n,{writable:!1,enumerable:!1}),Object.freeze(o[n])}}),!0}catch(s){return console.error("Error in set trap:",s),!1}},deleteProperty(o,n){try{let i=memorio.obj.path(n,r);return t({action:"delete",path:i,target:o}),Reflect.deleteProperty(o,n)}catch(i){return console.error("Error in deleteProperty trap:",i),!1}}})};globalThis?.state?globalThis.state=state:globalThis.state=c({},()=>{});var u=new WeakSet;u.add(state);setInterval(()=>{if(!u.has(state)){alert("memorio State is compromised, check if you override it and please reload the page");for(let e=1;e<99999;e++)clearInterval(e);stop()}},1e3);Object.defineProperty(globalThis,"state",{enumerable:!1,configurable:!1});Object.defineProperties(state,{list:{get(){let e=memorio.array.deepClone(state);console.info(e)}},remove:{value(e){e in state?(delete state[e],console.debug(`State '${e}' deleted`)):console.error(`'${e}' not exist`)},writable:!1,configurable:!1},removeAll:{value(){state.forEach(e=>{delete state[e[0]]})},writable:!1,configurable:!1}});globalThis.observer||(globalThis.observer=null);Object.defineProperty(globalThis,"observer",{enumerable:!1});observer=(e,t=null,r=!0)=>{if((o=>o.split(".")[0]!=="state"?(console.error(`Observer Error: You need to declare "state." or "store.". The "${o}" string is incorrect!`),!1):!0)(e)){if(!e&&!t){console.error("Observer Error: You need to setup observer correctly, Some parameters are missed!");return}if(!e&&t){console.error('Observer Error: You need to declare what state need to be monitored as string like "state.test".');return}if(e&&!t){memorio.dispatch.listen(String(e),{detail:{name:String(e)}}),console.debug("called: ",e);return}if(e&&t){if(typeof e!="string"||typeof t!="function"){console.error('Observer Error: name of state need to be a "string" like "state.test" and the callback need to be a "function"');return}let o=e.replaceAll(".","-");memorio.dispatch.listen(o,t,r);return}}};Object.defineProperties(observer,{list:{get:()=>globalThis.events},remove:{value:e=>{e&&(globalThis.events[e]="")}},removeAll:{get:()=>{Object.entries(observer.list).forEach(e=>{globalThis.events[e[0]]})}}});Object.freeze(observer);})();
|