@descope/sdk-helpers 0.4.1 → 0.5.0
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/dist/cjs/index.cjs.js +1 -1
- package/dist/cjs/index.cjs.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
package/dist/cjs/index.cjs.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var e=require("tslib");const t=(e,t)=>e.length===t.length&&e.every(((e,s)=>e===t[s]));var s,r,i,a;function l(e,t){const s=Object.getOwnPropertyNames(e),r=Object.getOwnPropertyNames(t);if(s.length!==r.length)return!1;for(let r=0;r<s.length;r+=1){const i=s[r],a=e[i],n=t[i];if(null===a||null===n){if(a!==n)return!1}else if("object"==typeof a&&"object"==typeof n){if(!l(a,n))return!1}else if(a!==n)return!1}return!0}s=new WeakMap,r=new WeakMap,i=new WeakMap,a=new WeakMap,exports.State=class{constructor(t={},{forceUpdate:n=!1}={}){s.set(this,void 0),r.set(this,{}),i.set(this,0),a.set(this,!0),this.update=t=>{const i="function"==typeof t?t(e.__classPrivateFieldGet(this,s,"f")):t,n=Object.assign(Object.assign({},e.__classPrivateFieldGet(this,s,"f")),i);if(e.__classPrivateFieldGet(this,a,"f")||!l(e.__classPrivateFieldGet(this,s,"f"),n)){const t=e.__classPrivateFieldGet(this,s,"f");e.__classPrivateFieldSet(this,s,n,"f"),Object.freeze(e.__classPrivateFieldGet(this,s,"f")),setTimeout((()=>{Object.values(e.__classPrivateFieldGet(this,r,"f")).forEach((e=>e(n,t,((e,t)=>s=>e[s]!==t[s])(n,t))))}),0)}},e.__classPrivateFieldSet(this,s,t,"f"),e.__classPrivateFieldSet(this,a,n,"f")}get current(){return Object.assign({},e.__classPrivateFieldGet(this,s,"f"))}subscribe(t){return e.__classPrivateFieldSet(this,i,e.__classPrivateFieldGet(this,i,"f")+1,"f"),e.__classPrivateFieldGet(this,r,"f")[e.__classPrivateFieldGet(this,i,"f")]=t,e.__classPrivateFieldGet(this,i,"f").toString()}unsubscribe(t){const s=!!e.__classPrivateFieldGet(this,r,"f")[t];return s&&delete e.__classPrivateFieldGet(this,r,"f")[t],s}unsubscribeAll(){return e.__classPrivateFieldSet(this,r,{},"f"),!0}},exports.compareArrays=t,exports.compose=function(...e){return t=>e.reduce(((e,t)=>t(e)),t)},exports.createSingletonMixin=e=>{const t=Symbol((e=>{const t=e.toString();let s=0;for(let e=0;e<t.length;e++)s=(s<<5)-s+t.charCodeAt(e),s&=s;return s.toString(16)})(e));return s=>{if(s[t])return s;const r=e(s);return r[t]=!0,r}},exports.createTemplate=e=>{const t=document.createElement("template");return t.innerHTML=e,t},exports.debounce=(e,t=500)=>{let s;return function(...r){clearTimeout(s),s=setTimeout((()=>e.apply(this,r)),t)}},exports.isObjEmpty=e=>0===Object.keys(e).length&&e.constructor===Object,exports.kebabCase=e=>e.replace(/([a-z])([A-Z])/g,"$1-$2").replace(/[\s_.]+/g,"-").toLowerCase(),exports.pathJoin=(...e)=>e.join("/").replace(/\/+/g,"/"),exports.pluralize=e=>(t,...s)=>t.reduce(((t,r,i)=>{var a;return`${t}${r}${(null===(a=null==s?void 0:s[i])||void 0===a?void 0:a[e>1?1:0])||""}`}),""),exports.withMemCache=e=>{let s,r;return(...i)=>(s&&t(s,i)||(s=i,r=e(...i)),r)};
|
|
1
|
+
"use strict";var e=require("tslib");const t=(e,t)=>e.length===t.length&&e.every(((e,s)=>e===t[s]));var s,r,i,a;function l(e,t){const s=Object.getOwnPropertyNames(e),r=Object.getOwnPropertyNames(t);if(s.length!==r.length)return!1;for(let r=0;r<s.length;r+=1){const i=s[r],a=e[i],n=t[i];if(null===a||null===n){if(a!==n)return!1}else if("object"==typeof a&&"object"==typeof n){if(!l(a,n))return!1}else if(a!==n)return!1}return!0}s=new WeakMap,r=new WeakMap,i=new WeakMap,a=new WeakMap,exports.State=class{constructor(t={},{forceUpdate:n=!1}={}){s.set(this,void 0),r.set(this,{}),i.set(this,0),a.set(this,!0),this.update=t=>{const i="function"==typeof t?t(e.__classPrivateFieldGet(this,s,"f")):t,n=Object.assign(Object.assign({},e.__classPrivateFieldGet(this,s,"f")),i);if(e.__classPrivateFieldGet(this,a,"f")||!l(e.__classPrivateFieldGet(this,s,"f"),n)){const t=e.__classPrivateFieldGet(this,s,"f");e.__classPrivateFieldSet(this,s,n,"f"),Object.freeze(e.__classPrivateFieldGet(this,s,"f")),setTimeout((()=>{Object.values(e.__classPrivateFieldGet(this,r,"f")).forEach((e=>e(n,t,((e,t)=>s=>e[s]!==t[s])(n,t))))}),0)}},e.__classPrivateFieldSet(this,s,t,"f"),e.__classPrivateFieldSet(this,a,n,"f")}get current(){return Object.assign({},e.__classPrivateFieldGet(this,s,"f"))}subscribe(t){return e.__classPrivateFieldSet(this,i,e.__classPrivateFieldGet(this,i,"f")+1,"f"),e.__classPrivateFieldGet(this,r,"f")[e.__classPrivateFieldGet(this,i,"f")]=t,e.__classPrivateFieldGet(this,i,"f").toString()}unsubscribe(t){const s=!!e.__classPrivateFieldGet(this,r,"f")[t];return s&&delete e.__classPrivateFieldGet(this,r,"f")[t],s}unsubscribeAll(){return e.__classPrivateFieldSet(this,r,{},"f"),!0}},exports.compareArrays=t,exports.compose=function(...e){return t=>e.reduce(((e,t)=>t(e)),t)},exports.createSingletonMixin=e=>{const t=Symbol((e=>{const t=e.toString();let s=0;for(let e=0;e<t.length;e++)s=(s<<5)-s+t.charCodeAt(e),s&=s;return s.toString(16)})(e));return s=>{if(s[t])return s;const r=e(s);return r[t]=!0,r}},exports.createTemplate=e=>{const t=document.createElement("template");return t.innerHTML=e,t},exports.debounce=(e,t=500)=>{let s;return function(...r){clearTimeout(s),s=setTimeout((()=>e.apply(this,r)),t)}},exports.isObjEmpty=e=>0===Object.keys(e).length&&e.constructor===Object,exports.isPlainObject=e=>null!==e&&"object"==typeof e&&Object.getPrototypeOf(e)===Object.prototype,exports.kebabCase=e=>e.replace(/([a-z])([A-Z])/g,"$1-$2").replace(/[\s_.]+/g,"-").toLowerCase(),exports.pathJoin=(...e)=>e.join("/").replace(/\/+/g,"/"),exports.pluralize=e=>(t,...s)=>t.reduce(((t,r,i)=>{var a;return`${t}${r}${(null===(a=null==s?void 0:s[i])||void 0===a?void 0:a[e>1?1:0])||""}`}),""),exports.withMemCache=e=>{let s,r;return(...i)=>(s&&t(s,i)||(s=i,r=e(...i)),r)};
|
|
2
2
|
//# sourceMappingURL=index.cjs.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":["../../src/dom.ts","../../src/generic.ts","../../src/state.ts","../../src/compose.ts","../../src/mixins.ts"],"sourcesContent":["export const createTemplate = (templateString: string) => {\n const template = document.createElement('template');\n template.innerHTML = templateString;\n\n return template;\n};\n","// preventing duplicate separators\nexport const pathJoin = (...paths: string[]) =>\n paths.join('/').replace(/\\/+/g, '/');\n\nexport const compareArrays = (array1: any[], array2: any[]) =>\n array1.length === array2.length &&\n array1.every((value: any, index: number) => value === array2[index]);\n\nexport const withMemCache = <I extends any[], O>(fn: (...args: I) => O) => {\n let prevArgs: any[];\n let cache: any;\n return (...args: I) => {\n if (prevArgs && compareArrays(prevArgs, args)) return cache as O;\n\n prevArgs = args;\n cache = fn(...args);\n\n return cache as O;\n };\n};\n\nexport const kebabCase = (str: string) =>\n str\n .replace(/([a-z])([A-Z])/g, '$1-$2')\n .replace(/[\\s_.]+/g, '-')\n .toLowerCase();\n\nexport const isObjEmpty = (obj: object) =>\n Object.keys(obj).length === 0 && obj.constructor === Object;\n\nexport const pluralize =\n (amount: number) =>\n (strings: TemplateStringsArray, ...expressions: (string | number)[][]) =>\n strings.reduce(\n (acc, str, idx) =>\n `${acc}${str}${expressions?.[idx]?.[amount > 1 ? 1 : 0] || ''}`,\n '',\n );\n\nexport const debounce = (fn: Function, ms = 500) => {\n let timeoutId: ReturnType<typeof setTimeout>;\n return function debounced(this: any, ...args: any[]) {\n clearTimeout(timeoutId);\n timeoutId = setTimeout(() => fn.apply(this, args), ms);\n };\n};\n","const createIsChanged =\n <T extends Record<string, any>>(state: T, prevState: T) =>\n (attrName: keyof T) =>\n state[attrName] !== prevState[attrName];\n\ntype StateObject = Record<string, any>;\n\ntype UpdateStateCb<T> = (state: T) => Partial<T>;\ntype Subscribers<T> = Record<string, SubscribeCb<T>>;\n\n// eslint-disable-next-line import/exports-last\nexport type SubscribeCb<T> = (\n state: T,\n prevState: T,\n isChanged: ReturnType<typeof createIsChanged>,\n) => void | Promise<void>;\n\nfunction compareObjects(\n objectA: Record<string, any>,\n objectB: Record<string, any>,\n) {\n const aProperties = Object.getOwnPropertyNames(objectA);\n const bProperties = Object.getOwnPropertyNames(objectB);\n\n if (aProperties.length !== bProperties.length) {\n return false;\n }\n\n for (let i = 0; i < aProperties.length; i += 1) {\n const propName = aProperties[i];\n\n const valA = objectA[propName];\n const valB = objectB[propName];\n if (valA === null || valB === null) {\n if (valA !== valB) {\n return false;\n }\n } else if (typeof valA === 'object' && typeof valB === 'object') {\n // compare nested objects\n if (!compareObjects(valA, valB)) {\n return false;\n }\n } else if (valA !== valB) {\n return false;\n }\n }\n\n return true;\n}\n\nexport type IsChanged<T> = Parameters<SubscribeCb<T>>[2];\n\nexport class State<T extends StateObject> {\n #state: T;\n\n #subscribers: Subscribers<T> = {};\n\n #token = 0;\n\n #forceUpdate = true;\n\n constructor(init: T = {} as T, { forceUpdate = false } = {}) {\n this.#state = init;\n this.#forceUpdate = forceUpdate;\n }\n\n get current() {\n return { ...this.#state };\n }\n\n update = (newState: Partial<T> | UpdateStateCb<T>) => {\n const internalNewState =\n typeof newState === 'function' ? newState(this.#state) : newState;\n\n const nextState = { ...this.#state, ...internalNewState };\n if (this.#forceUpdate || !compareObjects(this.#state, nextState)) {\n const prevState = this.#state;\n this.#state = nextState;\n Object.freeze(this.#state);\n\n setTimeout(() => {\n Object.values(this.#subscribers).forEach((cb) =>\n cb(nextState, prevState, createIsChanged(nextState, prevState)),\n );\n }, 0);\n }\n };\n\n subscribe(cb: SubscribeCb<T>) {\n this.#token += 1;\n this.#subscribers[this.#token] = cb;\n\n return this.#token.toString();\n }\n\n unsubscribe(token: string) {\n const isFound = !!this.#subscribers[token];\n\n if (isFound) {\n delete this.#subscribers[token];\n }\n\n return isFound;\n }\n\n unsubscribeAll() {\n this.#subscribers = {};\n\n return true;\n }\n}\n","type Fn = (arg: any) => any;\n\nexport function compose<Input, A1>(\n fn1: (input: Input) => A1,\n): (input: Input) => A1;\n\nexport function compose<Input, A1, A2>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n): (input: Input) => A2;\n\nexport function compose<Input, A1, A2, A3>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n): (input: Input) => A3;\n\nexport function compose<Input, A1, A2, A3, A4>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n): (input: Input) => A4;\n\nexport function compose<Input, A1, A2, A3, A4, A5>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n): (input: Input) => A5;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n): (input: Input) => A6;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n): (input: Input) => A7;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n): (input: Input) => A8;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8, A9>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n): (input: Input) => A9;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n): (input: Input) => A10;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n fn11: (input: A10) => A11,\n): (input: Input) => A11;\n\nexport function compose<\n Input,\n A1,\n A2,\n A3,\n A4,\n A5,\n A6,\n A7,\n A8,\n A9,\n A10,\n A11,\n A12,\n>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n fn11: (input: A10) => A11,\n fn12: (input: A11) => A12,\n): (input: Input) => A12;\n\nexport function compose<\n Input,\n A1,\n A2,\n A3,\n A4,\n A5,\n A6,\n A7,\n A8,\n A9,\n A10,\n A11,\n A12,\n A13,\n>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n fn11: (input: A10) => A11,\n fn12: (input: A11) => A12,\n fn13: (input: A12) => A13,\n): (input: Input) => A13;\n\nexport function compose<\n Input,\n A1,\n A2,\n A3,\n A4,\n A5,\n A6,\n A7,\n A8,\n A9,\n A10,\n A11,\n A12,\n A13,\n A14,\n>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n fn11: (input: A10) => A11,\n fn12: (input: A11) => A12,\n fn13: (input: A12) => A13,\n fn14: (input: A13) => A14,\n): (input: Input) => A14;\n\n/**\n * Currently there is no way to create a compose function in Typescript without using overloading\n * This function currently support up to 10 wrappers\n * If needed you can add more by duplicating the type and add more parameters\n */\n\nexport function compose(...args: Fn[]) {\n return (data: any) => args.reduce((acc, elem) => elem(acc), data) as any;\n}\n","const getFunctionHash = (fn: Function) => {\n const functionSource = fn.toString();\n\n let hash = 0;\n\n // eslint-disable-next-line no-plusplus\n for (let i = 0; i < functionSource.length; i++) {\n const char = functionSource.charCodeAt(i);\n // eslint-disable-next-line no-bitwise\n hash = (hash << 5) - hash + char;\n // eslint-disable-next-line no-bitwise\n hash &= hash; // Convert to 32-bit integer\n }\n\n return hash.toString(16);\n};\n\ntype Mixin = (superclass: CustomElementConstructor) => CustomElementConstructor;\n\n// because a single mixin can be a dependency for many other mixins, a mixin can be loaded multiple times\n// some mixins should not be loaded multiple times, wrapping a mixin with this fn ensures it will load only once\nexport const createSingletonMixin = <T extends Mixin>(mixin: T): T => {\n const mixinNameSym = Symbol(getFunctionHash(mixin));\n\n const singletonMixin = (superclass: CustomElementConstructor) => {\n if (superclass[mixinNameSym]) {\n return superclass;\n }\n\n const cls = mixin(superclass);\n cls[mixinNameSym] = true;\n\n return cls;\n };\n\n return singletonMixin as T;\n};\n"],"names":["compareArrays","array1","array2","length","every","value","index","compareObjects","objectA","objectB","aProperties","Object","getOwnPropertyNames","bProperties","i","propName","valA","valB","constructor","init","forceUpdate","_State_state","set","this","_State_subscribers","_State_token","_State_forceUpdate","update","newState","internalNewState","__classPrivateFieldGet","nextState","prevState","__classPrivateFieldSet","freeze","setTimeout","values","forEach","cb","state","attrName","createIsChanged","current","assign","subscribe","toString","unsubscribe","token","isFound","unsubscribeAll","args","data","reduce","acc","elem","mixin","mixinNameSym","Symbol","fn","functionSource","hash","charCodeAt","getFunctionHash","superclass","cls","templateString","template","document","createElement","innerHTML","ms","timeoutId","clearTimeout","apply","obj","keys","str","replace","toLowerCase","paths","join","amount","strings","expressions","idx","_a","prevArgs","cache"],"mappings":"oCAAa,MCIAA,EAAgB,CAACC,EAAeC,IAC3CD,EAAOE,SAAWD,EAAOC,QACzBF,EAAOG,OAAM,CAACC,EAAYC,IAAkBD,IAAUH,EAAOI,iBCW/D,SAASC,EACPC,EACAC,GAEA,MAAMC,EAAcC,OAAOC,oBAAoBJ,GACzCK,EAAcF,OAAOC,oBAAoBH,GAE/C,GAAIC,EAAYP,SAAWU,EAAYV,OACrC,OAAO,EAGT,IAAK,IAAIW,EAAI,EAAGA,EAAIJ,EAAYP,OAAQW,GAAK,EAAG,CAC9C,MAAMC,EAAWL,EAAYI,GAEvBE,EAAOR,EAAQO,GACfE,EAAOR,EAAQM,GACrB,GAAa,OAATC,GAA0B,OAATC,GACnB,GAAID,IAASC,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CAACC,EAAKuB,EAAKQ,KAAO,IAAAC,EAChB,MAAA,GAAGhC,IAAMuB,KAA2B,QAArBS,EAAAF,aAAW,EAAXA,EAAcC,UAAO,IAAAC,OAAA,EAAAA,EAAAJ,EAAS,EAAI,EAAI,KAAM,IAAI,GACjE,yBA5B2CvB,IAC/C,IAAI4B,EACAC,EACJ,MAAO,IAAIrC,KACLoC,GAAYtF,EAAcsF,EAAUpC,KAExCoC,EAAWpC,EACXqC,EAAQ7B,KAAMR,IAHwCqC,EAMvD"}
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../../src/dom.ts","../../src/generic.ts","../../src/state.ts","../../src/compose.ts","../../src/mixins.ts"],"sourcesContent":["export const createTemplate = (templateString: string) => {\n const template = document.createElement('template');\n template.innerHTML = templateString;\n\n return template;\n};\n","// preventing duplicate separators\nexport const pathJoin = (...paths: string[]) =>\n paths.join('/').replace(/\\/+/g, '/');\n\nexport const compareArrays = (array1: any[], array2: any[]) =>\n array1.length === array2.length &&\n array1.every((value: any, index: number) => value === array2[index]);\n\nexport const withMemCache = <I extends any[], O>(fn: (...args: I) => O) => {\n let prevArgs: any[];\n let cache: any;\n return (...args: I) => {\n if (prevArgs && compareArrays(prevArgs, args)) return cache as O;\n\n prevArgs = args;\n cache = fn(...args);\n\n return cache as O;\n };\n};\n\nexport const kebabCase = (str: string) =>\n str\n .replace(/([a-z])([A-Z])/g, '$1-$2')\n .replace(/[\\s_.]+/g, '-')\n .toLowerCase();\n\nexport const isObjEmpty = (obj: object) =>\n Object.keys(obj).length === 0 && obj.constructor === Object;\n\nexport const pluralize =\n (amount: number) =>\n (strings: TemplateStringsArray, ...expressions: (string | number)[][]) =>\n strings.reduce(\n (acc, str, idx) =>\n `${acc}${str}${expressions?.[idx]?.[amount > 1 ? 1 : 0] || ''}`,\n '',\n );\n\nexport const debounce = (fn: Function, ms = 500) => {\n let timeoutId: ReturnType<typeof setTimeout>;\n return function debounced(this: any, ...args: any[]) {\n clearTimeout(timeoutId);\n timeoutId = setTimeout(() => fn.apply(this, args), ms);\n };\n};\n\nexport const isPlainObject = (obj: unknown) =>\n obj !== null &&\n typeof obj === 'object' &&\n Object.getPrototypeOf(obj) === Object.prototype;\n","const createIsChanged =\n <T extends Record<string, any>>(state: T, prevState: T) =>\n (attrName: keyof T) =>\n state[attrName] !== prevState[attrName];\n\ntype StateObject = Record<string, any>;\n\ntype UpdateStateCb<T> = (state: T) => Partial<T>;\ntype Subscribers<T> = Record<string, SubscribeCb<T>>;\n\n// eslint-disable-next-line import/exports-last\nexport type SubscribeCb<T> = (\n state: T,\n prevState: T,\n isChanged: ReturnType<typeof createIsChanged>,\n) => void | Promise<void>;\n\nfunction compareObjects(\n objectA: Record<string, any>,\n objectB: Record<string, any>,\n) {\n const aProperties = Object.getOwnPropertyNames(objectA);\n const bProperties = Object.getOwnPropertyNames(objectB);\n\n if (aProperties.length !== bProperties.length) {\n return false;\n }\n\n for (let i = 0; i < aProperties.length; i += 1) {\n const propName = aProperties[i];\n\n const valA = objectA[propName];\n const valB = objectB[propName];\n if (valA === null || valB === null) {\n if (valA !== valB) {\n return false;\n }\n } else if (typeof valA === 'object' && typeof valB === 'object') {\n // compare nested objects\n if (!compareObjects(valA, valB)) {\n return false;\n }\n } else if (valA !== valB) {\n return false;\n }\n }\n\n return true;\n}\n\nexport type IsChanged<T> = Parameters<SubscribeCb<T>>[2];\n\nexport class State<T extends StateObject> {\n #state: T;\n\n #subscribers: Subscribers<T> = {};\n\n #token = 0;\n\n #forceUpdate = true;\n\n constructor(init: T = {} as T, { forceUpdate = false } = {}) {\n this.#state = init;\n this.#forceUpdate = forceUpdate;\n }\n\n get current() {\n return { ...this.#state };\n }\n\n update = (newState: Partial<T> | UpdateStateCb<T>) => {\n const internalNewState =\n typeof newState === 'function' ? newState(this.#state) : newState;\n\n const nextState = { ...this.#state, ...internalNewState };\n if (this.#forceUpdate || !compareObjects(this.#state, nextState)) {\n const prevState = this.#state;\n this.#state = nextState;\n Object.freeze(this.#state);\n\n setTimeout(() => {\n Object.values(this.#subscribers).forEach((cb) =>\n cb(nextState, prevState, createIsChanged(nextState, prevState)),\n );\n }, 0);\n }\n };\n\n subscribe(cb: SubscribeCb<T>) {\n this.#token += 1;\n this.#subscribers[this.#token] = cb;\n\n return this.#token.toString();\n }\n\n unsubscribe(token: string) {\n const isFound = !!this.#subscribers[token];\n\n if (isFound) {\n delete this.#subscribers[token];\n }\n\n return isFound;\n }\n\n unsubscribeAll() {\n this.#subscribers = {};\n\n return true;\n }\n}\n","type Fn = (arg: any) => any;\n\nexport function compose<Input, A1>(\n fn1: (input: Input) => A1,\n): (input: Input) => A1;\n\nexport function compose<Input, A1, A2>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n): (input: Input) => A2;\n\nexport function compose<Input, A1, A2, A3>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n): (input: Input) => A3;\n\nexport function compose<Input, A1, A2, A3, A4>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n): (input: Input) => A4;\n\nexport function compose<Input, A1, A2, A3, A4, A5>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n): (input: Input) => A5;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n): (input: Input) => A6;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n): (input: Input) => A7;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n): (input: Input) => A8;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8, A9>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n): (input: Input) => A9;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n): (input: Input) => A10;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n fn11: (input: A10) => A11,\n): (input: Input) => A11;\n\nexport function compose<\n Input,\n A1,\n A2,\n A3,\n A4,\n A5,\n A6,\n A7,\n A8,\n A9,\n A10,\n A11,\n A12,\n>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n fn11: (input: A10) => A11,\n fn12: (input: A11) => A12,\n): (input: Input) => A12;\n\nexport function compose<\n Input,\n A1,\n A2,\n A3,\n A4,\n A5,\n A6,\n A7,\n A8,\n A9,\n A10,\n A11,\n A12,\n A13,\n>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n fn11: (input: A10) => A11,\n fn12: (input: A11) => A12,\n fn13: (input: A12) => A13,\n): (input: Input) => A13;\n\nexport function compose<\n Input,\n A1,\n A2,\n A3,\n A4,\n A5,\n A6,\n A7,\n A8,\n A9,\n A10,\n A11,\n A12,\n A13,\n A14,\n>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n fn11: (input: A10) => A11,\n fn12: (input: A11) => A12,\n fn13: (input: A12) => A13,\n fn14: (input: A13) => A14,\n): (input: Input) => A14;\n\nexport function compose<\n Input,\n A1,\n A2,\n A3,\n A4,\n A5,\n A6,\n A7,\n A8,\n A9,\n A10,\n A11,\n A12,\n A13,\n A14,\n A15,\n>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n fn11: (input: A10) => A11,\n fn12: (input: A11) => A12,\n fn13: (input: A12) => A13,\n fn14: (input: A13) => A14,\n fn15: (input: A14) => A15,\n): (input: Input) => A15;\n\n/**\n * Currently there is no way to create a compose function in Typescript without using overloading\n * This function currently support up to 15 wrappers\n * If needed you can add more by duplicating the type and add more parameters\n */\n\nexport function compose(...args: Fn[]) {\n return (data: any) => args.reduce((acc, elem) => elem(acc), data) as any;\n}\n","const getFunctionHash = (fn: Function) => {\n const functionSource = fn.toString();\n\n let hash = 0;\n\n // eslint-disable-next-line no-plusplus\n for (let i = 0; i < functionSource.length; i++) {\n const char = functionSource.charCodeAt(i);\n // eslint-disable-next-line no-bitwise\n hash = (hash << 5) - hash + char;\n // eslint-disable-next-line no-bitwise\n hash &= hash; // Convert to 32-bit integer\n }\n\n return hash.toString(16);\n};\n\ntype Mixin = (superclass: CustomElementConstructor) => CustomElementConstructor;\n\n// because a single mixin can be a dependency for many other mixins, a mixin can be loaded multiple times\n// some mixins should not be loaded multiple times, wrapping a mixin with this fn ensures it will load only once\nexport const createSingletonMixin = <T extends Mixin>(mixin: T): T => {\n const mixinNameSym = Symbol(getFunctionHash(mixin));\n\n const singletonMixin = (superclass: CustomElementConstructor) => {\n if (superclass[mixinNameSym]) {\n return superclass;\n }\n\n const cls = mixin(superclass);\n cls[mixinNameSym] = true;\n\n return cls;\n };\n\n return singletonMixin as T;\n};\n"],"names":["compareArrays","array1","array2","length","every","value","index","compareObjects","objectA","objectB","aProperties","Object","getOwnPropertyNames","bProperties","i","propName","valA","valB","constructor","init","forceUpdate","_State_state","set","this","_State_subscribers","_State_token","_State_forceUpdate","update","newState","internalNewState","__classPrivateFieldGet","nextState","prevState","__classPrivateFieldSet","freeze","setTimeout","values","forEach","cb","state","attrName","createIsChanged","current","assign","subscribe","toString","unsubscribe","token","isFound","unsubscribeAll","args","data","reduce","acc","elem","mixin","mixinNameSym","Symbol","fn","functionSource","hash","charCodeAt","getFunctionHash","superclass","cls","templateString","template","document","createElement","innerHTML","ms","timeoutId","clearTimeout","apply","obj","keys","getPrototypeOf","prototype","str","replace","toLowerCase","paths","join","amount","str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C,IAC7B,MAAMC,EAAWC,SAASC,cAAc,YAGxC,OAFAF,EAASG,UAAYJ,EAEdC,CAAQ,mBCmCO,CAACR,EAAcY,EAAK,OAC1C,IAAIC,EACJ,OAAO,YAAiCrB,GACtCsB,aAAaD,GACbA,EAAYpC,YAAW,IAAMuB,EAAGe,MAAMlD,KAAM2B,IAAOoB,EACrD,CAAC,qBAjBwBI,GACG,IAA5B/D,OAAOgE,KAAKD,GAAKvE,QAAgBuE,EAAIxD,cAAgBP,6BAmBzB+D,GACpB,OAARA,GACe,iBAARA,GACP/D,OAAOiE,eAAeF,KAAS/D,OAAOkE,4BA7BdC,GACxBA,EACGC,QAAQ,kBAAmB,SAC3BA,QAAQ,WAAY,KACpBC,+BAxBmB,IAAIC,IAC1BA,EAAMC,KAAK,KAAKH,QAAQ,OAAQ,uBA6B/BI,GACD,CAACC,KAAkCC,IACjCD,EAAQhC,QACN,CAACC,EAAKyB,EAAKQ,KAAO,IAAAC,EAChB,MAAA,GAAGlC,IAAMyB,KAA2B,QAArBS,EAAAF,aAAW,EAAXA,EAAcC,UAAO,IAAAC,OAAA,EAAAA,EAAAJ,EAAS,EAAI,EAAI,KAAM,IAAI,GACjE,yBA5B2CzB,IAC/C,IAAI8B,EACAC,EACJ,MAAO,IAAIvC,KACLsC,GAAYxF,EAAcwF,EAAUtC,KAExCsC,EAAWtC,EACXuC,EAAQ/B,KAAMR,IAHwCuC,EAMvD"}
|
package/dist/index.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ declare function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11>(fn
|
|
|
12
12
|
declare function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12>(fn1: (input: Input) => A1, fn2: (input: A1) => A2, fn3: (input: A2) => A3, fn4: (input: A3) => A4, fn5: (input: A4) => A5, fn6: (input: A5) => A6, fn7: (input: A6) => A7, fn8: (input: A7) => A8, fn9: (input: A8) => A9, fn10: (input: A9) => A10, fn11: (input: A10) => A11, fn12: (input: A11) => A12): (input: Input) => A12;
|
|
13
13
|
declare function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13>(fn1: (input: Input) => A1, fn2: (input: A1) => A2, fn3: (input: A2) => A3, fn4: (input: A3) => A4, fn5: (input: A4) => A5, fn6: (input: A5) => A6, fn7: (input: A6) => A7, fn8: (input: A7) => A8, fn9: (input: A8) => A9, fn10: (input: A9) => A10, fn11: (input: A10) => A11, fn12: (input: A11) => A12, fn13: (input: A12) => A13): (input: Input) => A13;
|
|
14
14
|
declare function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14>(fn1: (input: Input) => A1, fn2: (input: A1) => A2, fn3: (input: A2) => A3, fn4: (input: A3) => A4, fn5: (input: A4) => A5, fn6: (input: A5) => A6, fn7: (input: A6) => A7, fn8: (input: A7) => A8, fn9: (input: A8) => A9, fn10: (input: A9) => A10, fn11: (input: A10) => A11, fn12: (input: A11) => A12, fn13: (input: A12) => A13, fn14: (input: A13) => A14): (input: Input) => A14;
|
|
15
|
+
declare function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15>(fn1: (input: Input) => A1, fn2: (input: A1) => A2, fn3: (input: A2) => A3, fn4: (input: A3) => A4, fn5: (input: A4) => A5, fn6: (input: A5) => A6, fn7: (input: A6) => A7, fn8: (input: A7) => A8, fn9: (input: A8) => A9, fn10: (input: A9) => A10, fn11: (input: A10) => A11, fn12: (input: A11) => A12, fn13: (input: A12) => A13, fn14: (input: A13) => A14, fn15: (input: A14) => A15): (input: Input) => A15;
|
|
15
16
|
|
|
16
17
|
declare const createTemplate: (templateString: string) => HTMLTemplateElement;
|
|
17
18
|
|
|
@@ -22,6 +23,7 @@ declare const kebabCase: (str: string) => string;
|
|
|
22
23
|
declare const isObjEmpty: (obj: object) => boolean;
|
|
23
24
|
declare const pluralize: (amount: number) => (strings: TemplateStringsArray, ...expressions: (string | number)[][]) => string;
|
|
24
25
|
declare const debounce: (fn: Function, ms?: number) => (this: any, ...args: any[]) => void;
|
|
26
|
+
declare const isPlainObject: (obj: unknown) => boolean;
|
|
25
27
|
|
|
26
28
|
type Mixin = (superclass: CustomElementConstructor) => CustomElementConstructor;
|
|
27
29
|
declare const createSingletonMixin: <T extends Mixin>(mixin: T) => T;
|
|
@@ -43,4 +45,4 @@ declare class State<T extends StateObject> {
|
|
|
43
45
|
unsubscribeAll(): boolean;
|
|
44
46
|
}
|
|
45
47
|
|
|
46
|
-
export { type IsChanged, State, type SubscribeCb, compareArrays, compose, createSingletonMixin, createTemplate, debounce, isObjEmpty, kebabCase, pathJoin, pluralize, withMemCache };
|
|
48
|
+
export { type IsChanged, State, type SubscribeCb, compareArrays, compose, createSingletonMixin, createTemplate, debounce, isObjEmpty, isPlainObject, kebabCase, pathJoin, pluralize, withMemCache };
|
package/dist/index.esm.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{__classPrivateFieldGet as t,__classPrivateFieldSet as e}from"tslib";function r(...t){return e=>t.reduce(((t,e)=>e(t)),e)}const n=t=>{const e=document.createElement("template");return e.innerHTML=t,e},s=(...t)=>t.join("/").replace(/\/+/g,"/"),i=(t,e)=>t.length===e.length&&t.every(((t,r)=>t===e[r])),o=t=>{let e,r;return(...n)=>(e&&i(e,n)||(e=n,r=t(...n)),r)},c=t=>t.replace(/([a-z])([A-Z])/g,"$1-$2").replace(/[\s_.]+/g,"-").toLowerCase(),u=t=>0===Object.keys(t).length&&t.constructor===Object,f=t=>(e,...r)=>e.reduce(((e,n,s)=>{var i;return`${e}${n}${(null===(i=null==r?void 0:r[s])||void 0===i?void 0:i[t>1?1:0])||""}`}),""),l=(t,e=500)=>{let r;return function(...n){clearTimeout(r),r=setTimeout((()=>t.apply(this,n)),e)}},h=t=>{const e=Symbol((t=>{const e=t.toString();let r=0;for(let t=0;t<e.length;t++)r=(r<<5)-r+e.charCodeAt(t),r&=r;return r.toString(16)})(t));return r=>{if(r[e])return r;const n=t(r);return n[e]=!0,n}};var
|
|
1
|
+
import{__classPrivateFieldGet as t,__classPrivateFieldSet as e}from"tslib";function r(...t){return e=>t.reduce(((t,e)=>e(t)),e)}const n=t=>{const e=document.createElement("template");return e.innerHTML=t,e},s=(...t)=>t.join("/").replace(/\/+/g,"/"),i=(t,e)=>t.length===e.length&&t.every(((t,r)=>t===e[r])),o=t=>{let e,r;return(...n)=>(e&&i(e,n)||(e=n,r=t(...n)),r)},c=t=>t.replace(/([a-z])([A-Z])/g,"$1-$2").replace(/[\s_.]+/g,"-").toLowerCase(),u=t=>0===Object.keys(t).length&&t.constructor===Object,f=t=>(e,...r)=>e.reduce(((e,n,s)=>{var i;return`${e}${n}${(null===(i=null==r?void 0:r[s])||void 0===i?void 0:i[t>1?1:0])||""}`}),""),l=(t,e=500)=>{let r;return function(...n){clearTimeout(r),r=setTimeout((()=>t.apply(this,n)),e)}},h=t=>null!==t&&"object"==typeof t&&Object.getPrototypeOf(t)===Object.prototype,a=t=>{const e=Symbol((t=>{const e=t.toString();let r=0;for(let t=0;t<e.length;t++)r=(r<<5)-r+e.charCodeAt(t),r&=r;return r.toString(16)})(t));return r=>{if(r[e])return r;const n=t(r);return n[e]=!0,n}};var p,b,g,j;function O(t,e){const r=Object.getOwnPropertyNames(t),n=Object.getOwnPropertyNames(e);if(r.length!==n.length)return!1;for(let n=0;n<r.length;n+=1){const s=r[n],i=t[s],o=e[s];if(null===i||null===o){if(i!==o)return!1}else if("object"==typeof i&&"object"==typeof o){if(!O(i,o))return!1}else if(i!==o)return!1}return!0}class y{constructor(r={},{forceUpdate:n=!1}={}){p.set(this,void 0),b.set(this,{}),g.set(this,0),j.set(this,!0),this.update=r=>{const n="function"==typeof r?r(t(this,p,"f")):r,s=Object.assign(Object.assign({},t(this,p,"f")),n);if(t(this,j,"f")||!O(t(this,p,"f"),s)){const r=t(this,p,"f");e(this,p,s,"f"),Object.freeze(t(this,p,"f")),setTimeout((()=>{Object.values(t(this,b,"f")).forEach((t=>t(s,r,((t,e)=>r=>t[r]!==e[r])(s,r))))}),0)}},e(this,p,r,"f"),e(this,j,n,"f")}get current(){return Object.assign({},t(this,p,"f"))}subscribe(r){return e(this,g,t(this,g,"f")+1,"f"),t(this,b,"f")[t(this,g,"f")]=r,t(this,g,"f").toString()}unsubscribe(e){const r=!!t(this,b,"f")[e];return r&&delete t(this,b,"f")[e],r}unsubscribeAll(){return e(this,b,{},"f"),!0}}p=new WeakMap,b=new WeakMap,g=new WeakMap,j=new WeakMap;export{y as State,i as compareArrays,r as compose,a as createSingletonMixin,n as createTemplate,l as debounce,u as isObjEmpty,h as isPlainObject,c as kebabCase,s as pathJoin,f as pluralize,o as withMemCache};
|
|
2
2
|
//# sourceMappingURL=index.esm.js.map
|
package/dist/index.esm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.esm.js","sources":["../src/compose.ts","../src/dom.ts","../src/generic.ts","../src/mixins.ts","../src/state.ts"],"sourcesContent":["type Fn = (arg: any) => any;\n\nexport function compose<Input, A1>(\n fn1: (input: Input) => A1,\n): (input: Input) => A1;\n\nexport function compose<Input, A1, A2>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n): (input: Input) => A2;\n\nexport function compose<Input, A1, A2, A3>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n): (input: Input) => A3;\n\nexport function compose<Input, A1, A2, A3, A4>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n): (input: Input) => A4;\n\nexport function compose<Input, A1, A2, A3, A4, A5>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n): (input: Input) => A5;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n): (input: Input) => A6;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n): (input: Input) => A7;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n): (input: Input) => A8;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8, A9>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n): (input: Input) => A9;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n): (input: Input) => A10;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n fn11: (input: A10) => A11,\n): (input: Input) => A11;\n\nexport function compose<\n Input,\n A1,\n A2,\n A3,\n A4,\n A5,\n A6,\n A7,\n A8,\n A9,\n A10,\n A11,\n A12,\n>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n fn11: (input: A10) => A11,\n fn12: (input: A11) => A12,\n): (input: Input) => A12;\n\nexport function compose<\n Input,\n A1,\n A2,\n A3,\n A4,\n A5,\n A6,\n A7,\n A8,\n A9,\n A10,\n A11,\n A12,\n A13,\n>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n fn11: (input: A10) => A11,\n fn12: (input: A11) => A12,\n fn13: (input: A12) => A13,\n): (input: Input) => A13;\n\nexport function compose<\n Input,\n A1,\n A2,\n A3,\n A4,\n A5,\n A6,\n A7,\n A8,\n A9,\n A10,\n A11,\n A12,\n A13,\n A14,\n>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n fn11: (input: A10) => A11,\n fn12: (input: A11) => A12,\n fn13: (input: A12) => A13,\n fn14: (input: A13) => A14,\n): (input: Input) => A14;\n\n/**\n * Currently there is no way to create a compose function in Typescript without using overloading\n * This function currently support up to 10 wrappers\n * If needed you can add more by duplicating the type and add more parameters\n */\n\nexport function compose(...args: Fn[]) {\n return (data: any) => args.reduce((acc, elem) => elem(acc), data) as any;\n}\n","export const createTemplate = (templateString: string) => {\n const template = document.createElement('template');\n template.innerHTML = templateString;\n\n return template;\n};\n","// preventing duplicate separators\nexport const pathJoin = (...paths: string[]) =>\n paths.join('/').replace(/\\/+/g, '/');\n\nexport const compareArrays = (array1: any[], array2: any[]) =>\n array1.length === array2.length &&\n array1.every((value: any, index: number) => value === array2[index]);\n\nexport const withMemCache = <I extends any[], O>(fn: (...args: I) => O) => {\n let prevArgs: any[];\n let cache: any;\n return (...args: I) => {\n if (prevArgs && compareArrays(prevArgs, args)) return cache as O;\n\n prevArgs = args;\n cache = fn(...args);\n\n return cache as O;\n };\n};\n\nexport const kebabCase = (str: string) =>\n str\n .replace(/([a-z])([A-Z])/g, '$1-$2')\n .replace(/[\\s_.]+/g, '-')\n .toLowerCase();\n\nexport const isObjEmpty = (obj: object) =>\n Object.keys(obj).length === 0 && obj.constructor === Object;\n\nexport const pluralize =\n (amount: number) =>\n (strings: TemplateStringsArray, ...expressions: (string | number)[][]) =>\n strings.reduce(\n (acc, str, idx) =>\n `${acc}${str}${expressions?.[idx]?.[amount > 1 ? 1 : 0] || ''}`,\n '',\n );\n\nexport const debounce = (fn: Function, ms = 500) => {\n let timeoutId: ReturnType<typeof setTimeout>;\n return function debounced(this: any, ...args: any[]) {\n clearTimeout(timeoutId);\n timeoutId = setTimeout(() => fn.apply(this, args), ms);\n };\n};\n","const getFunctionHash = (fn: Function) => {\n const functionSource = fn.toString();\n\n let hash = 0;\n\n // eslint-disable-next-line no-plusplus\n for (let i = 0; i < functionSource.length; i++) {\n const char = functionSource.charCodeAt(i);\n // eslint-disable-next-line no-bitwise\n hash = (hash << 5) - hash + char;\n // eslint-disable-next-line no-bitwise\n hash &= hash; // Convert to 32-bit integer\n }\n\n return hash.toString(16);\n};\n\ntype Mixin = (superclass: CustomElementConstructor) => CustomElementConstructor;\n\n// because a single mixin can be a dependency for many other mixins, a mixin can be loaded multiple times\n// some mixins should not be loaded multiple times, wrapping a mixin with this fn ensures it will load only once\nexport const createSingletonMixin = <T extends Mixin>(mixin: T): T => {\n const mixinNameSym = Symbol(getFunctionHash(mixin));\n\n const singletonMixin = (superclass: CustomElementConstructor) => {\n if (superclass[mixinNameSym]) {\n return superclass;\n }\n\n const cls = mixin(superclass);\n cls[mixinNameSym] = true;\n\n return cls;\n };\n\n return singletonMixin as T;\n};\n","const createIsChanged =\n <T extends Record<string, any>>(state: T, prevState: T) =>\n (attrName: keyof T) =>\n state[attrName] !== prevState[attrName];\n\ntype StateObject = Record<string, any>;\n\ntype UpdateStateCb<T> = (state: T) => Partial<T>;\ntype Subscribers<T> = Record<string, SubscribeCb<T>>;\n\n// eslint-disable-next-line import/exports-last\nexport type SubscribeCb<T> = (\n state: T,\n prevState: T,\n isChanged: ReturnType<typeof createIsChanged>,\n) => void | Promise<void>;\n\nfunction compareObjects(\n objectA: Record<string, any>,\n objectB: Record<string, any>,\n) {\n const aProperties = Object.getOwnPropertyNames(objectA);\n const bProperties = Object.getOwnPropertyNames(objectB);\n\n if (aProperties.length !== bProperties.length) {\n return false;\n }\n\n for (let i = 0; i < aProperties.length; i += 1) {\n const propName = aProperties[i];\n\n const valA = objectA[propName];\n const valB = objectB[propName];\n if (valA === null || valB === null) {\n if (valA !== valB) {\n return false;\n }\n } else if (typeof valA === 'object' && typeof valB === 'object') {\n // compare nested objects\n if (!compareObjects(valA, valB)) {\n return false;\n }\n } else if (valA !== valB) {\n return false;\n }\n }\n\n return true;\n}\n\nexport type IsChanged<T> = Parameters<SubscribeCb<T>>[2];\n\nexport class State<T extends StateObject> {\n #state: T;\n\n #subscribers: Subscribers<T> = {};\n\n #token = 0;\n\n #forceUpdate = true;\n\n constructor(init: T = {} as T, { forceUpdate = false } = {}) {\n this.#state = init;\n this.#forceUpdate = forceUpdate;\n }\n\n get current() {\n return { ...this.#state };\n }\n\n update = (newState: Partial<T> | UpdateStateCb<T>) => {\n const internalNewState =\n typeof newState === 'function' ? newState(this.#state) : newState;\n\n const nextState = { ...this.#state, ...internalNewState };\n if (this.#forceUpdate || !compareObjects(this.#state, nextState)) {\n const prevState = this.#state;\n this.#state = nextState;\n Object.freeze(this.#state);\n\n setTimeout(() => {\n Object.values(this.#subscribers).forEach((cb) =>\n cb(nextState, prevState, createIsChanged(nextState, prevState)),\n );\n }, 0);\n }\n };\n\n subscribe(cb: SubscribeCb<T>) {\n this.#token += 1;\n this.#subscribers[this.#token] = cb;\n\n return this.#token.toString();\n }\n\n unsubscribe(token: string) {\n const isFound = !!this.#subscribers[token];\n\n if (isFound) {\n delete this.#subscribers[token];\n }\n\n return isFound;\n }\n\n unsubscribeAll() {\n this.#subscribers = {};\n\n return true;\n }\n}\n"],"names":["compose","args","data","reduce","acc","elem","createTemplate","templateString","template","document","createElement","innerHTML","pathJoin","paths","join","replace","compareArrays","array1","array2","length","every","value","index","withMemCache","fn","prevArgs","cache","kebabCase","str","toLowerCase","isObjEmpty","obj","Object","keys","constructor","pluralize","amount","strings","expressions","idx","_a","debounce","ms","timeoutId","clearTimeout","setTimeout","apply","this","createSingletonMixin","mixin","mixinNameSym","Symbol","functionSource","toString","hash","i","charCodeAt","getFunctionHash","superclass","cls","compareObjects","objectA","objectB","aProperties","getOwnPropertyNames","bProperties","propName","valA","valB","State","init","forceUpdate","_State_state","set","_State_subscribers","_State_token","_State_forceUpdate","update","newState","internalNewState","__classPrivateFieldGet","nextState","prevState","__classPrivateFieldSet","freeze","values","forEach","cb","state","attrName","createIsChanged","current","assign","subscribe","unsubscribe","token","isFound","unsubscribeAll"],"mappings":"2EAwMgB,SAAAA,KAAWC,GACzB,OAAQC,GAAcD,EAAKE,QAAO,CAACC,EAAKC,IAASA,EAAKD,IAAMF,EAC9D,CC1Ma,MAAAI,EAAkBC,IAC7B,MAAMC,EAAWC,SAASC,cAAc,YAGxC,OAFAF,EAASG,UAAYJ,EAEdC,CAAQ,ECHJI,EAAW,IAAIC,IA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F,EAAI,KAClBa,EAAApC,KAAI6B,EAAgBL,EAAW,IAChC,CAED,WAAIoB,GACF,OAAY3D,OAAA4D,OAAA,GAAAZ,EAAAjC,KAAIyB,EAAA,KACjB,CAoBD,SAAAqB,CAAUN,GAIR,OAHAJ,EAAepC,KAAA4B,EAAAK,EAAAjC,KAAA4B,EAAA,KAAA,OACfK,EAAAjC,YAAkBiC,EAAAjC,KAAW4B,EAAA,MAAIY,EAE1BP,EAAAjC,KAAI4B,EAAA,KAAQtB,UACpB,CAED,WAAAyC,CAAYC,GACV,MAAMC,IAAYhB,EAAAjC,KAAiB2B,EAAA,KAACqB,GAMpC,OAJIC,UACKhB,EAAAjC,KAAI2B,EAAA,KAAcqB,GAGpBC,CACR,CAED,cAAAC,GAGE,OAFAd,EAAApC,KAAI2B,EAAgB,CAAE,EAAA,MAEf,CACR"}
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/compose.ts","../src/dom.ts","../src/generic.ts","../src/mixins.ts","../src/state.ts"],"sourcesContent":["type Fn = (arg: any) => any;\n\nexport function compose<Input, A1>(\n fn1: (input: Input) => A1,\n): (input: Input) => A1;\n\nexport function compose<Input, A1, A2>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n): (input: Input) => A2;\n\nexport function compose<Input, A1, A2, A3>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n): (input: Input) => A3;\n\nexport function compose<Input, A1, A2, A3, A4>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n): (input: Input) => A4;\n\nexport function compose<Input, A1, A2, A3, A4, A5>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n): (input: Input) => A5;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n): (input: Input) => A6;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n): (input: Input) => A7;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n): (input: Input) => A8;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8, A9>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n): (input: Input) => A9;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n): (input: Input) => A10;\n\nexport function compose<Input, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n fn11: (input: A10) => A11,\n): (input: Input) => A11;\n\nexport function compose<\n Input,\n A1,\n A2,\n A3,\n A4,\n A5,\n A6,\n A7,\n A8,\n A9,\n A10,\n A11,\n A12,\n>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n fn11: (input: A10) => A11,\n fn12: (input: A11) => A12,\n): (input: Input) => A12;\n\nexport function compose<\n Input,\n A1,\n A2,\n A3,\n A4,\n A5,\n A6,\n A7,\n A8,\n A9,\n A10,\n A11,\n A12,\n A13,\n>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n fn11: (input: A10) => A11,\n fn12: (input: A11) => A12,\n fn13: (input: A12) => A13,\n): (input: Input) => A13;\n\nexport function compose<\n Input,\n A1,\n A2,\n A3,\n A4,\n A5,\n A6,\n A7,\n A8,\n A9,\n A10,\n A11,\n A12,\n A13,\n A14,\n>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n fn11: (input: A10) => A11,\n fn12: (input: A11) => A12,\n fn13: (input: A12) => A13,\n fn14: (input: A13) => A14,\n): (input: Input) => A14;\n\nexport function compose<\n Input,\n A1,\n A2,\n A3,\n A4,\n A5,\n A6,\n A7,\n A8,\n A9,\n A10,\n A11,\n A12,\n A13,\n A14,\n A15,\n>(\n fn1: (input: Input) => A1,\n fn2: (input: A1) => A2,\n fn3: (input: A2) => A3,\n fn4: (input: A3) => A4,\n fn5: (input: A4) => A5,\n fn6: (input: A5) => A6,\n fn7: (input: A6) => A7,\n fn8: (input: A7) => A8,\n fn9: (input: A8) => A9,\n fn10: (input: A9) => A10,\n fn11: (input: A10) => A11,\n fn12: (input: A11) => A12,\n fn13: (input: A12) => A13,\n fn14: (input: A13) => A14,\n fn15: (input: A14) => A15,\n): (input: Input) => A15;\n\n/**\n * Currently there is no way to create a compose function in Typescript without using overloading\n * This function currently support up to 15 wrappers\n * If needed you can add more by duplicating the type and add more parameters\n */\n\nexport function compose(...args: Fn[]) {\n return (data: any) => args.reduce((acc, elem) => elem(acc), data) as any;\n}\n","export const createTemplate = (templateString: string) => {\n const template = document.createElement('template');\n template.innerHTML = templateString;\n\n return template;\n};\n","// preventing duplicate separators\nexport const pathJoin = (...paths: string[]) =>\n paths.join('/').replace(/\\/+/g, '/');\n\nexport const compareArrays = (array1: any[], array2: any[]) =>\n array1.length === array2.length &&\n array1.every((value: any, index: number) => value === array2[index]);\n\nexport const withMemCache = <I extends any[], O>(fn: (...args: I) => O) => {\n let prevArgs: any[];\n let cache: any;\n return (...args: I) => {\n if (prevArgs && compareArrays(prevArgs, args)) return cache as O;\n\n prevArgs = args;\n cache = fn(...args);\n\n return cache as O;\n };\n};\n\nexport const kebabCase = (str: string) =>\n str\n .replace(/([a-z])([A-Z])/g, '$1-$2')\n .replace(/[\\s_.]+/g, '-')\n .toLowerCase();\n\nexport const isObjEmpty = (obj: object) =>\n Object.keys(obj).length === 0 && obj.constructor === Object;\n\nexport const pluralize =\n (amount: number) =>\n (strings: TemplateStringsArray, ...expressions: (string | number)[][]) =>\n strings.reduce(\n (acc, str, idx) =>\n `${acc}${str}${expressions?.[idx]?.[amount > 1 ? 1 : 0] || ''}`,\n '',\n );\n\nexport const debounce = (fn: Function, ms = 500) => {\n let timeoutId: ReturnType<typeof setTimeout>;\n return function debounced(this: any, ...args: any[]) {\n clearTimeout(timeoutId);\n timeoutId = setTimeout(() => fn.apply(this, args), ms);\n };\n};\n\nexport const isPlainObject = (obj: unknown) =>\n obj !== null &&\n typeof obj === 'object' &&\n Object.getPrototypeOf(obj) === Object.prototype;\n","const getFunctionHash = (fn: Function) => {\n const functionSource = fn.toString();\n\n let hash = 0;\n\n // eslint-disable-next-line no-plusplus\n for (let i = 0; i < functionSource.length; i++) {\n const char = functionSource.charCodeAt(i);\n // eslint-disable-next-line no-bitwise\n hash = (hash << 5) - hash + char;\n // eslint-disable-next-line no-bitwise\n hash &= hash; // Convert to 32-bit integer\n }\n\n return hash.toString(16);\n};\n\ntype Mixin = (superclass: CustomElementConstructor) => CustomElementConstructor;\n\n// because a single mixin can be a dependency for many other mixins, a mixin can be loaded multiple times\n// some mixins should not be loaded multiple times, wrapping a mixin with this fn ensures it will load only once\nexport const createSingletonMixin = <T extends Mixin>(mixin: T): T => {\n const mixinNameSym = Symbol(getFunctionHash(mixin));\n\n const singletonMixin = (superclass: CustomElementConstructor) => {\n if (superclass[mixinNameSym]) {\n return superclass;\n }\n\n const cls = mixin(superclass);\n cls[mixinNameSym] = true;\n\n return cls;\n };\n\n return singletonMixin as T;\n};\n","const createIsChanged =\n <T extends Record<string, any>>(state: T, prevState: T) =>\n (attrName: keyof T) =>\n state[attrName] !== prevState[attrName];\n\ntype StateObject = Record<string, any>;\n\ntype UpdateStateCb<T> = (state: T) => Partial<T>;\ntype Subscribers<T> = Record<string, SubscribeCb<T>>;\n\n// eslint-disable-next-line import/exports-last\nexport type SubscribeCb<T> = (\n state: T,\n prevState: T,\n isChanged: ReturnType<typeof createIsChanged>,\n) => void | Promise<void>;\n\nfunction compareObjects(\n objectA: Record<string, any>,\n objectB: Record<string, any>,\n) {\n const aProperties = Object.getOwnPropertyNames(objectA);\n const bProperties = Object.getOwnPropertyNames(objectB);\n\n if (aProperties.length !== bProperties.length) {\n return false;\n }\n\n for (let i = 0; i < aProperties.length; i += 1) {\n const propName = aProperties[i];\n\n const valA = objectA[propName];\n const valB = objectB[propName];\n if (valA === null || valB === null) {\n if (valA !== valB) {\n return false;\n }\n } else if (typeof valA === 'object' && typeof valB === 'object') {\n // compare nested objects\n if (!compareObjects(valA, valB)) {\n return false;\n }\n } else if (valA !== valB) {\n return false;\n }\n }\n\n return true;\n}\n\nexport type IsChanged<T> = Parameters<SubscribeCb<T>>[2];\n\nexport class State<T extends StateObject> {\n #state: T;\n\n #subscribers: Subscribers<T> = {};\n\n #token = 0;\n\n #forceUpdate = true;\n\n constructor(init: T = {} as T, { forceUpdate = false } = {}) {\n this.#state = init;\n this.#forceUpdate = forceUpdate;\n }\n\n get current() {\n return { ...this.#state };\n }\n\n update = (newState: Partial<T> | UpdateStateCb<T>) => {\n const internalNewState =\n typeof newState === 'function' ? newState(this.#state) : newState;\n\n const nextState = { ...this.#state, ...internalNewState };\n if (this.#forceUpdate || !compareObjects(this.#state, nextState)) {\n const prevState = this.#state;\n this.#state = nextState;\n Object.freeze(this.#state);\n\n setTimeout(() => {\n Object.values(this.#subscribers).forEach((cb) =>\n cb(nextState, prevState, createIsChanged(nextState, prevState)),\n );\n }, 0);\n }\n };\n\n subscribe(cb: SubscribeCb<T>) {\n this.#token += 1;\n this.#subscribers[this.#token] = cb;\n\n return this.#token.toString();\n }\n\n unsubscribe(token: string) {\n const isFound = !!this.#subscribers[token];\n\n if (isFound) {\n delete this.#subscribers[token];\n }\n\n return isFound;\n }\n\n unsubscribeAll() {\n this.#subscribers = {};\n\n return true;\n }\n}\n"],"names":["compose","args","data","reduce","acc","elem","createTemplate","templateString","template","document","createElement","innerHTML","pathJoin","paths","join","replace","compareArrays","array1","array2","length","every","value","index","withMemCache","fn","prevArgs","cache","kebabCase","str","toLowerCase","isObjEmpty","obj","Object","keys","constructor","pluralize","amount","strings","expressions","idx","_a","debounce","ms","timeoutId","clearTimeout","setTimeout","apply","this","isPlainObject","getPrototypeOf","prototype","createSingletonMixin","mixin","mixinNameSym","Symbol","functionSource","toString","hash","i","charCodeAt","getFunctionHash","superclass","cls","compareObjects","objectA","objectB","aProperties","getOwnPropertyNames","bProperties","propName","valA","valB","State","init","forceUpdate","_State_state","set","_State_subscribers","_State_token","_State_forceUpdate","update","newState","internalNewState","__classPrivateFieldGet","nextState","prevState","__classPrivateFieldSet","freeze","values","forEach","cb","state","attrName","createIsChanged","current","assign","subscribe","unsubscribe","token","isFound","unsubscribeAll"],"mappings":"2EA2OgB,SAAAA,KAAWC,GACzB,OAAQC,GAAcD,EAAKE,QAAO,CAACC,EAAKC,IAASA,EAAKD,IAAMF,EAC9D,CC7Oa,MAAAI,EAAkBC,IAC7B,MAAMC,EAAWC,SAASC,cAAc,YAGxC,OAFAF,EAASG,UAAYJ,EAEdC,CAAQ,ECHJI,EAAW,IAAIC,IAC1BA,EAAMC,KAAK,KAAKC,QAAQ,OAAQ,KAErBC,EAAgB,CAACC,EAAeC,IAC3CD,EAAOE,SAAWD,EAAOC,QACzBF,EAAOG,OAAM,CAACC,EAAYC,IAAkBD,IAAUH,EAAOI,KAElDC,EAAoCC,IAC/C,IAAIC,EACAC,EACJ,MAAO,IAAIzB,KACLwB,GAAYT,EAAcS,EAAUxB,KAExCwB,EAAWxB,EACXyB,EAAQF,KAAMvB,IAHwCyB,EAMvD,EAGUC,EAAaC,GACxBA,EACGb,QAAQ,kBAAmB,SAC3BA,QAAQ,WAAY,KACpBc,cAEQC,EAAcC,GACG,IAA5BC,OAAOC,KAAKF,GAAKZ,QAAgBY,EAAIG,cAAgBF,OAE1CG,EACVC,GACD,CAACC,KAAkCC,IACjCD,EAAQlC,QACN,CAACC,EAAKwB,EAAKW,KAAO,IAAAC,EAChB,MAAA,GAAGpC,IAAMwB,KAA2B,QAArBY,EAAAF,aAAW,EAAXA,EAAcC,UAAO,IAAAC,OAAA,EAAAA,EAAAJ,EAAS,EAAI,EAAI,KAAM,IAAI,GACjE,IAGOK,EAAW,CAACjB,EAAckB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