ziko 1.1.0 → 1.2.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/ziko.js CHANGED
@@ -2,7 +2,7 @@
2
2
  /*
3
3
  Project: ziko.js
4
4
  Author: Zakaria Elalaoui
5
- Date : Thu Jun 04 2026 14:34:29 GMT+0100 (UTC+01:00)
5
+ Date : Thu Jul 30 2026 14:31:45 GMT+0100 (UTC+01:00)
6
6
  Git-Repo : https://github.com/zakarialaoui10/ziko.js
7
7
  Git-Wiki : https://github.com/zakarialaoui10/ziko.js/wiki
8
8
  Released under MIT License
@@ -19,18 +19,18 @@
19
19
 
20
20
  const is_primitive = value => typeof value !== 'object' && typeof value !== 'function' || value === null;
21
21
 
22
- const mapfun=(fun,...X)=>{
22
+ const mapfun$1=(fun,...X)=>{
23
23
  const Y=X.map(x=>{
24
24
  if(is_primitive(x) || x?.__mapfun__) return fun(x)
25
- if(x instanceof Array) return x.map(n=>mapfun(fun,n));
25
+ if(x instanceof Array) return x.map(n=>mapfun$1(fun,n));
26
26
  if(ArrayBuffer.isView(x)) return x.map(n=>fun(n));
27
- if(x instanceof Set) return new Set(mapfun(fun,...[...x]));
28
- if(x instanceof Map) return new Map([...x].map(n=>[n[0],mapfun(fun,n[1])]));
29
- if(x.isMatrix?.()) return new x.constructor(x.rows, x.cols, mapfun(x.arr.flat(1)))
27
+ if(x instanceof Set) return new Set(mapfun$1(fun,...[...x]));
28
+ if(x instanceof Map) return new Map([...x].map(n=>[n[0],mapfun$1(fun,n[1])]));
29
+ if(x.isMatrix?.()) return new x.constructor(x.rows, x.cols, mapfun$1(x.arr.flat(1)))
30
30
  else if(x instanceof Object){
31
31
  return Object.fromEntries(
32
32
  Object.entries(x).map(
33
- n=>n=[n[0],mapfun(fun,n[1])]
33
+ n=>n=[n[0],mapfun$1(fun,n[1])]
34
34
  )
35
35
  )
36
36
  }
@@ -48,7 +48,7 @@
48
48
  x.cols,
49
49
  x.arr.flat(1).map(fn)
50
50
  )
51
- if (x instanceof Array) mapfun(fn, ...x);
51
+ if (x instanceof Array) mapfun$1(fn, ...x);
52
52
  return fn(x)
53
53
  };
54
54
 
@@ -531,7 +531,7 @@
531
531
 
532
532
  const PRECESION = 8;
533
533
 
534
- const abs = (...x) => mapfun(
534
+ const abs = (...x) => mapfun$1(
535
535
  x =>{
536
536
  if(x.isComplex?.()) return x.z;
537
537
  return Math.abs(x)
@@ -541,7 +541,7 @@
541
541
 
542
542
  const pow$1 = (...x) => {
543
543
  const n = x.pop();
544
- return mapfun(
544
+ return mapfun$1(
545
545
  x => {
546
546
  if(x.isComplex?.()) {
547
547
  if(n.isComplex?.()) return new x.constructor({
@@ -560,7 +560,7 @@
560
560
  )
561
561
  };
562
562
 
563
- const sqrt$2 = (...x) => mapfun(
563
+ const sqrt$2 = (...x) => mapfun$1(
564
564
  x=>{
565
565
  if(x.isComplex?.())
566
566
  return new x.constructor({z: x.z**(1/2), phi: x.phi/2});
@@ -570,7 +570,7 @@
570
570
  ...x
571
571
  );
572
572
 
573
- const cbrt = (...x) => mapfun(
573
+ const cbrt = (...x) => mapfun$1(
574
574
  x=>{
575
575
  if(x.isComplex?.())
576
576
  return new x.constructor({z: x.z**(1/3), phi: x.phi/3}).toFixed(PRECESION)
@@ -582,7 +582,7 @@
582
582
  const nthr = (...x) => {
583
583
  const n = x.pop();
584
584
  if(typeof n !== 'number') throw Error('nthr expects a real number n');
585
- return mapfun(
585
+ return mapfun$1(
586
586
  x => {
587
587
  if(x.isComplex?.()) return new x.constructor({z: x.z ** (1/n), phi: x.phi / n});
588
588
  if(x<0) return n %2 ===2
@@ -597,7 +597,7 @@
597
597
  const croot = (...x) =>{
598
598
  const c = x.pop();
599
599
  if(!c.isComplex?.()) throw Error('croot expect Complex number as root')
600
- return mapfun(
600
+ return mapfun$1(
601
601
  x => {
602
602
  if(typeof x === 'number') x = new c.constructor(x, 0);
603
603
  const {a : c_a, b : c_b} = c;
@@ -614,7 +614,7 @@
614
614
  )
615
615
  };
616
616
 
617
- const exp$1 = (...x) => mapfun(
617
+ const exp$1 = (...x) => mapfun$1(
618
618
  x => {
619
619
  if(x.isComplex?.()) return new x.constructor(
620
620
  Math.exp(x.a) * Math.cos(x.b),
@@ -625,7 +625,7 @@
625
625
  ,...x
626
626
  );
627
627
 
628
- const ln = (...x) => mapfun(
628
+ const ln = (...x) => mapfun$1(
629
629
  x => {
630
630
  if(x.isComplex?.()) return new x.constructor(
631
631
  Math.log(x.z),
@@ -636,7 +636,7 @@
636
636
  ,...x
637
637
  );
638
638
 
639
- const sign = (...x) => mapfun(
639
+ const sign = (...x) => mapfun$1(
640
640
  x => {
641
641
  if(x.isComplex?.()){
642
642
  const {z, phi} = x;
@@ -648,7 +648,7 @@
648
648
  ,...x
649
649
  );
650
650
 
651
- const floor = (...x) => mapfun(
651
+ const floor = (...x) => mapfun$1(
652
652
  x => {
653
653
  if(x.isComplex?.()) return new x.constructor(
654
654
  Math.floor(x.a),
@@ -658,7 +658,7 @@
658
658
  },
659
659
  ...x
660
660
  );
661
- const ceil = (...x) => mapfun(
661
+ const ceil = (...x) => mapfun$1(
662
662
  x => {
663
663
  if(x.isComplex?.()) return new x.constructor(
664
664
  Math.ceil(x.a),
@@ -668,7 +668,7 @@
668
668
  },
669
669
  ...x
670
670
  );
671
- const round = (...x) => mapfun(
671
+ const round = (...x) => mapfun$1(
672
672
  x => {
673
673
  if(x.isComplex?.()) return new x.constructor(
674
674
  Math.round(x.a),
@@ -679,7 +679,7 @@
679
679
  ...x
680
680
  );
681
681
 
682
- const trunc = (...x) => mapfun(
682
+ const trunc = (...x) => mapfun$1(
683
683
  x => {
684
684
  if(x.isComplex?.()) return new x.constructor(
685
685
  Math.trunc(x.a),
@@ -690,7 +690,7 @@
690
690
  ...x
691
691
  );
692
692
 
693
- const fract = (...x) => mapfun(
693
+ const fract = (...x) => mapfun$1(
694
694
  x => {
695
695
  if(x.isComplex?.()) return new x.constructor(
696
696
  x.a - Math.trunc(x.a),
@@ -701,7 +701,7 @@
701
701
  ...x
702
702
  );
703
703
 
704
- const cos$3 = (...x) => mapfun(
704
+ const cos$3 = (...x) => mapfun$1(
705
705
  x => {
706
706
  if(x.isComplex?.()) return new x.constructor(
707
707
  Math.cos(x.a) * Math.cosh(x.b),
@@ -712,7 +712,7 @@
712
712
  ,...x
713
713
  );
714
714
 
715
- const sin$3 = (...x) => mapfun(
715
+ const sin$3 = (...x) => mapfun$1(
716
716
  x =>{
717
717
  if(x?.isComplex) return new x.constructor(
718
718
  Math.sin(x.a) * Math.cosh(x.b),
@@ -723,7 +723,7 @@
723
723
  , ...x
724
724
  );
725
725
 
726
- const tan = (...x) => mapfun(
726
+ const tan = (...x) => mapfun$1(
727
727
  x =>{
728
728
  if(x?.isComplex){
729
729
  const D = Math.cos(2*x.a) + Math.cosh(2*x.b);
@@ -737,7 +737,7 @@
737
737
  ...x
738
738
  );
739
739
 
740
- const sec = (...x) => mapfun(
740
+ const sec = (...x) => mapfun$1(
741
741
  x => {
742
742
  if(x.isComplex?.()) ;
743
743
  return + (1 / Math.cos(x)).toFixed(PRECESION)
@@ -745,7 +745,7 @@
745
745
  ,...x
746
746
  );
747
747
 
748
- const acos$1 = (...x) => mapfun(
748
+ const acos$1 = (...x) => mapfun$1(
749
749
  x =>{
750
750
  if(x?.isComplex){
751
751
  const { a, b } = x;
@@ -763,7 +763,7 @@
763
763
  ...x
764
764
  );
765
765
 
766
- const asin = (...x) => mapfun(
766
+ const asin = (...x) => mapfun$1(
767
767
  x => {
768
768
  if(x?.isComplex){
769
769
  const { a, b } = x;
@@ -779,7 +779,7 @@
779
779
  ...x
780
780
  );
781
781
 
782
- const atan = (...x) => mapfun(
782
+ const atan = (...x) => mapfun$1(
783
783
  x => {
784
784
  if(x?.isComplex){
785
785
  const { a, b } = x;
@@ -793,7 +793,7 @@
793
793
  ...x
794
794
  );
795
795
 
796
- const acot = (...x) => mapfun(
796
+ const acot = (...x) => mapfun$1(
797
797
  x => {
798
798
  if(x?.isComplex){
799
799
  const { a, b } = x;
@@ -808,7 +808,7 @@
808
808
  );
809
809
 
810
810
 
811
- const cosh$2 = (...x) => mapfun(
811
+ const cosh$2 = (...x) => mapfun$1(
812
812
  x =>{
813
813
  if(x?.isComplex) return new x.constructor(
814
814
  Math.cosh(x.a) * Math.cos(x.b),
@@ -818,7 +818,7 @@
818
818
  },
819
819
  ...x
820
820
  );
821
- const sinh$1 = (...x) => mapfun(
821
+ const sinh$1 = (...x) => mapfun$1(
822
822
  x =>{
823
823
  if(x?.isComplex) return new x.constructor(
824
824
  Math.sinh(x.a) * Math.cos(x.b),
@@ -828,7 +828,7 @@
828
828
  },
829
829
  ...x
830
830
  );
831
- const tanh = (...x) => mapfun(
831
+ const tanh = (...x) => mapfun$1(
832
832
  x =>{
833
833
  if(x?.isComplex){
834
834
  const D = Math.cosh(2*a) + Math.cos(2*b);
@@ -842,7 +842,7 @@
842
842
  ...x
843
843
  );
844
844
 
845
- const coth = (...x) => mapfun(
845
+ const coth = (...x) => mapfun$1(
846
846
  x =>{
847
847
  if(x?.isComplex){
848
848
  const {a, b} = x;
@@ -857,7 +857,7 @@
857
857
  ...x
858
858
  );
859
859
 
860
- const acosh = (...x) => mapfun(
860
+ const acosh = (...x) => mapfun$1(
861
861
  x =>{
862
862
  if(x?.isComplex){
863
863
  return ln(x.clone().add(sqrt$2(x.clone().mul(x.clone()).sub(1))))
@@ -867,7 +867,7 @@
867
867
  ...x
868
868
  );
869
869
 
870
- const asinh = (...x) => mapfun(
870
+ const asinh = (...x) => mapfun$1(
871
871
  x =>{
872
872
  if(x?.isComplex){
873
873
  return ln(x.clone().add(sqrt$2(x.clone().mul(x.clone()).add(1))))
@@ -877,7 +877,7 @@
877
877
  ...x
878
878
  );
879
879
 
880
- const atanh = (...x) => mapfun(
880
+ const atanh = (...x) => mapfun$1(
881
881
  x =>{
882
882
  if(x?.isComplex);
883
883
  return + Math.atanh(x).toFixed(PRECESION)
@@ -885,7 +885,7 @@
885
885
  ...x
886
886
  );
887
887
 
888
- const sig = (...x) => mapfun(
888
+ const sig = (...x) => mapfun$1(
889
889
  x =>{
890
890
  if(x?.isComplex);
891
891
  return 1/(1 + Math.exp(-x)).toFixed(PRECESION)
@@ -893,8 +893,8 @@
893
893
  ...x
894
894
  );
895
895
 
896
- const deg2rad = (...deg) => mapfun(x => x * Math.PI / 180, ...deg);
897
- const rad2deg = (...rad) => mapfun(x => x / Math.PI * 180, ...rad);
896
+ const deg2rad = (...deg) => mapfun$1(x => x * Math.PI / 180, ...deg);
897
+ const rad2deg = (...rad) => mapfun$1(x => x / Math.PI * 180, ...rad);
898
898
 
899
899
  const norm = (x, min, max) => apply_fun(
900
900
  x,
@@ -925,10 +925,10 @@
925
925
 
926
926
  const atan2 = (y, x, rad = true) => {
927
927
  if (y instanceof Array && !(x instanceof Array))
928
- return mapfun(n => atan2(n, x, rad), ...y);
928
+ return mapfun$1(n => atan2(n, x, rad), ...y);
929
929
 
930
930
  if (x instanceof Array && !(y instanceof Array))
931
- return mapfun(n => atan2(y, n, rad), ...x);
931
+ return mapfun$1(n => atan2(y, n, rad), ...x);
932
932
 
933
933
  if (y instanceof Array && x instanceof Array)
934
934
  return y.map((v, i) => atan2(v, x[i], rad));
@@ -1165,7 +1165,7 @@
1165
1165
  return new Matrix(this.rows, this.cols, this.arr.flat(1));
1166
1166
  }
1167
1167
  toComplex(){
1168
- this.arr = mapfun(
1168
+ this.arr = mapfun$1(
1169
1169
  x => x?.isComplex?.() ? x : new Complex(x, 0),
1170
1170
  ...this.arr
1171
1171
  );
@@ -1629,7 +1629,7 @@
1629
1629
  return new Matrix(v.length, 1, v);
1630
1630
  }
1631
1631
  serialize() {
1632
- const arr = mapfun(x => x.serialize?.() || x, ...this.arr);
1632
+ const arr = mapfun$1(x => x.serialize?.() || x, ...this.arr);
1633
1633
  return JSON.stringify({
1634
1634
  type : 'matrix',
1635
1635
  data : {
@@ -1644,7 +1644,7 @@
1644
1644
  const {type, data} = json;
1645
1645
  if(type !== 'matrix') return TypeError('Not a valid Matrix')
1646
1646
  let {arr} = data;
1647
- arr = mapfun(x => {
1647
+ arr = mapfun$1(x => {
1648
1648
  if(typeof x === 'string') {
1649
1649
  const x_obj = JSON.parse(x);
1650
1650
  const {type} = x_obj;
@@ -1675,186 +1675,6 @@
1675
1675
  const matrix3=(...element)=>new Matrix(3, 3, element);
1676
1676
  const matrix4=(...element)=>new Matrix(4, 4, element);
1677
1677
 
1678
- const zeros=(n)=>new Array(n).fill(0);
1679
- const ones=(n)=>new Array(n).fill(1);
1680
- const nums=(num,n)=>new Array(n).fill(num);
1681
-
1682
- const arange=(a, b, step , include = false)=>{
1683
- let tab = [];
1684
- if(a<b){
1685
- for (let i = a; include?i<=b:i<b; i += step) tab.push((i * 10) / 10);
1686
- }
1687
- else {
1688
- for(let i = a; include?i>=b:i>b; i -= step) tab.push((i * 10) / 10);
1689
- }
1690
- return tab;
1691
- };
1692
- const linspace=(a,b,n=abs(b-a)+1,endpoint=true)=>{
1693
- if(Math.floor(n)!==n)return;
1694
- if([a,b].every(n=>typeof n==="number")){
1695
- const [max,min]=[a,b].sort((a,b)=>b-a);
1696
- var Y = [];
1697
- let step ;
1698
- endpoint ? step = (max - min) / (n - 1) : step = (max - min) / n;
1699
- for (var i = 0; i < n; i++) {
1700
- a<b?Y.push(min+step*i):Y.push(max-step*i);
1701
- }
1702
- return Y
1703
- }
1704
-
1705
- if([a,b].some(n=>n.isComplex?.())){
1706
- const z1 = new n.constructor(a);
1707
- const z2 = new n.constructor(b);
1708
- n=n||Math.abs(z1.a-z2.a)+1;
1709
- const X=linspace(z1.a,z2.a,n,endpoint);
1710
- const Y=linspace(z1.b,z2.b,n,endpoint);
1711
- let Z=new Array(n).fill(null);
1712
- Z=Z.map((n,i)=> new n.constructor(X[i],Y[i]));
1713
- return Z;
1714
- }
1715
- };
1716
- const logspace=(a,b,n=b-a+1,base=E,endpoint=true)=>{
1717
- return linspace(a,b,n,endpoint).map(n=>pow$1(base,n))
1718
- };
1719
- const geomspace=(a,b,n=abs(b-a)+1,endpoint=true)=>{
1720
- if(Math.floor(n)!==n)return;
1721
- if([a,b].every(n=>typeof n==="number")){
1722
- const [max,min]=[a,b].sort((a,b)=>b-a);
1723
- let base;
1724
- endpoint ? base = nthr(max/min,n-1) : base = nthr(max/min,n) ;
1725
- const Y = [min];
1726
- for (let i = 1; i < n; i++) {
1727
- Y.push(Y[i-1]*base);
1728
- }
1729
- return a<b?Y:Y.reverse()
1730
- }
1731
-
1732
- if([a,b].some(n=>n.isComplex?.())){
1733
- const z1 = new n.constructor(a);
1734
- const z2 = new n.constructor(b);
1735
- n=n||Math.abs(z1.a-z2.a)+1;
1736
- let base;
1737
- endpoint ? base = nthr(z2.div(z1),n-1) : base = nthr(z2.div(z1),n) ;
1738
- const Y = [z1];
1739
- for (let i = 1; i < n; i++) {
1740
- Y.push(mul(Y[i-1],base));
1741
- }
1742
- return Y;
1743
- }
1744
- };
1745
-
1746
- /** @module Math */
1747
- /**
1748
- * Checks if a value is within the specified range.
1749
- * @function
1750
- * @param {number} x - The value to check.
1751
- * @param {number} a - The start of the range.
1752
- * @param {number} b - The end of the range.
1753
- * @returns {boolean} Returns true if the value is within the range [a, b], otherwise false.
1754
- */
1755
- const inRange = (x, a, b) => {
1756
- const [min, max] = [Math.min(a, b), Math.max(a, b)];
1757
- return x >= min && x <= max;
1758
- };
1759
-
1760
- /**
1761
- * Checks if two numbers are approximately equal within a given error margin.
1762
- * @param {number} a - The first number.
1763
- * @param {number} b - The second number.
1764
- * @param {number} [Err=0.0001] - The maximum acceptable difference between the two numbers.
1765
- * @returns {boolean} Returns true if the two numbers are approximately equal within the specified error margin, otherwise false.
1766
- */
1767
- const isApproximatlyEqual = (a, b, Err = 0.0001) => {
1768
- return Math.abs(a - b) <= Err;
1769
- };
1770
-
1771
- /** @module Math */
1772
-
1773
- /**
1774
- * Computes the cartesian product of two arrays.
1775
- * @param {Array} a - The first array.
1776
- * @param {Array} b - The second array.
1777
- * @returns {Array} Returns an array representing the cartesian product of the input arrays.
1778
- */
1779
- const cartesianProduct = (a, b) => a.reduce((p, x) => [...p, ...b.map((y) => [x, y])], []);
1780
-
1781
- /**
1782
- * Computes the greatest common divisor (GCD) of two numbers.
1783
- * @param {number} n1 - The first number.
1784
- * @param {number} n2 - The second number.
1785
- * @returns {number} Returns the greatest common divisor of the two input numbers.
1786
- */
1787
- const pgcd = (n1, n2) => {
1788
- let i,
1789
- pgcd = 1;
1790
- if (n1 == floor(n1) && n2 == floor(n2)) {
1791
- for (i = 2; i <= n1 && i <= n2; ++i) {
1792
- if (n1 % i == 0 && n2 % i == 0) pgcd = i;
1793
- }
1794
- return pgcd;
1795
- } else console.log("error");
1796
- };
1797
-
1798
- /**
1799
- * Computes the least common multiple (LCM) of two numbers.
1800
- * @param {number} n1 - The first number.
1801
- * @param {number} n2 - The second number.
1802
- * @returns {number} Returns the least common multiple of the two input numbers.
1803
- */
1804
- const ppcm = (n1, n2) => {
1805
- let ppcm;
1806
- if (n1 == floor(n1) && n2 == floor(n2)) {
1807
- ppcm = n1 > n2 ? n1 : n2;
1808
- while (true) {
1809
- if (ppcm % n1 == 0 && ppcm % n2 == 0) break;
1810
- ++ppcm;
1811
- }
1812
- return ppcm;
1813
- } else console.log("error");
1814
- };
1815
-
1816
- // import { mapfun } from "../mapfun/index.js";
1817
- // import {
1818
- // add,
1819
- // sub,
1820
- // mul,
1821
- // div,
1822
- // modulo
1823
- // } from "./arithmetic.js";
1824
- const Utils={
1825
- // add,
1826
- // sub,
1827
- // mul,
1828
- // div,
1829
- // modulo,
1830
-
1831
- zeros,
1832
- ones,
1833
- nums,
1834
- // norm,
1835
- // lerp,
1836
- // map,
1837
- // clamp,
1838
- arange,
1839
- linspace,
1840
- logspace,
1841
- geomspace,
1842
-
1843
- // sum,
1844
- // prod,
1845
- // accum,
1846
-
1847
- cartesianProduct,
1848
- ppcm,
1849
- pgcd,
1850
-
1851
- // deg2rad,
1852
- // rad2deg,
1853
-
1854
- inRange,
1855
- isApproximatlyEqual
1856
- };
1857
-
1858
1678
  class UINode {
1859
1679
  constructor(node){
1860
1680
  this.cache = {
@@ -2060,115 +1880,134 @@
2060
1880
  const useIPC = (name) => new UseIPC(name);
2061
1881
 
2062
1882
  class UseStorage {
2063
- constructor(storage, globalKey, initialValue, use_channel = true) {
2064
- this.cache = {
2065
- storage,
2066
- globalKey,
2067
- channel: use_channel ? useIPC(`Ziko:useStorage-${globalKey}`) : null,
2068
- oldItemKeys: new Set()
2069
- };
1883
+ static RESERVED_KEYS = new Set([
1884
+ "cache", "items", "set", "add", "remove", "get", "clear", "onStorageUpdated"
1885
+ ]);
2070
1886
 
2071
- this.#init(initialValue, use_channel);
2072
- }
1887
+ constructor(storage, globalKey, initialValue, use_channel = true) {
1888
+ this.cache = {
1889
+ storage,
1890
+ globalKey,
1891
+ channel: use_channel ? useIPC(`Ziko:useStorage-${globalKey}`) : null,
1892
+ oldItemKeys: new Set()
1893
+ };
2073
1894
 
2074
- get items() {
2075
- const raw = this.cache.storage.getItem(this.cache.globalKey);
2076
- if (!raw) return {};
2077
- try {
2078
- return JSON.parse(raw);
2079
- } catch {
2080
- return {};
2081
- }
2082
- }
1895
+ this.#init(initialValue, use_channel);
1896
+ }
2083
1897
 
2084
- #maintain() {
2085
- const items = this.items;
2086
- this.cache.oldItemKeys.forEach(k => delete this[k]);
2087
- for (const key in items) {
2088
- this[key] = items[key];
2089
- this.cache.oldItemKeys.add(key);
2090
- }
2091
- }
2092
- #init(initialValue, use_channel) {
2093
- if (use_channel && this.cache.channel) this.cache.channel.on("Ziko-Storage-Updated", () => this.#maintain());
2094
- if (!initialValue) {
2095
- this.#maintain();
2096
- return;
2097
- }
2098
- if (this.cache.storage.getItem(this.cache.globalKey)) {
2099
- const existing = this.items;
2100
- Object.keys(existing).forEach(k => this.cache.oldItemKeys.add(k));
2101
- this.#maintain();
2102
- }
2103
- else this.set(initialValue);
1898
+ get items() {
1899
+ const raw = this.cache.storage.getItem(this.cache.globalKey);
1900
+ if (!raw) return {};
1901
+ try {
1902
+ return JSON.parse(raw);
1903
+ } catch {
1904
+ return {};
2104
1905
  }
1906
+ }
2105
1907
 
2106
- set(data) {
2107
- this.cache.storage.setItem(this.cache.globalKey, JSON.stringify(data));
2108
- if (this.cache.channel) this.cache.channel.emit("Ziko-Storage-Updated", data);
2109
- this.#maintain();
2110
- return this;
2111
- }
1908
+ #maintain() {
1909
+ const currentItems = this.items;
1910
+ const currentKeys = new Set(Object.keys(currentItems));
2112
1911
 
2113
- add(data) {
2114
- this.set({
2115
- ...this.items,
2116
- ...data
2117
- });
2118
- return this;
1912
+ // Cleanup keys that were removed
1913
+ this.cache.oldItemKeys.forEach(key => {
1914
+ if (!currentKeys.has(key)) {
1915
+ delete this[key];
1916
+ this.cache.oldItemKeys.delete(key);
1917
+ }
1918
+ });
1919
+
1920
+ // Populate keys from storage onto `this`
1921
+ for (const key in currentItems) {
1922
+ if (!UseStorage.RESERVED_KEYS.has(key)) {
1923
+ this[key] = currentItems[key];
1924
+ this.cache.oldItemKeys.add(key);
1925
+ }
2119
1926
  }
2120
- remove(...keys) {
2121
- const items = { ...this.items };
2122
- keys.forEach(key => {
2123
- delete items[key];
2124
- delete this[key];
2125
- this.cache.oldItemKeys.delete(key);
2126
- });
2127
- this.set(items);
2128
- return this;
1927
+ }
1928
+
1929
+ #init(initialValue, use_channel) {
1930
+ if (use_channel && this.cache.channel) {
1931
+ this.cache.channel.on("Ziko-Storage-Updated", () => this.#maintain());
2129
1932
  }
2130
- get(key) {
2131
- return this.items[key];
1933
+
1934
+ const hasStoredData = this.cache.storage.getItem(this.cache.globalKey) !== null;
1935
+
1936
+ if (hasStoredData) {
1937
+ // Key already exists in storage -> Restore data to instance
1938
+ this.#maintain();
1939
+ } else if (initialValue !== undefined) {
1940
+ // Key doesn't exist -> Seed with initialValue
1941
+ this.set(initialValue);
1942
+ } else {
1943
+ // Key doesn't exist and no initialValue provided -> Default to empty
1944
+ this.#maintain();
2132
1945
  }
2133
- clear() {
2134
- this.cache.storage.removeItem(this.cache.globalKey);
2135
- this.cache.oldItemKeys.forEach(k => delete this[k]);
2136
- this.cache.oldItemKeys.clear();
2137
- this.#maintain();
2138
- return this;
1946
+ }
1947
+
1948
+ set(data) {
1949
+ this.cache.storage.setItem(this.cache.globalKey, JSON.stringify(data));
1950
+ if (this.cache.channel) {
1951
+ this.cache.channel.emit("Ziko-Storage-Updated", data);
2139
1952
  }
2140
- onStorageUpdated(callback) {
2141
- if (this.cache.channel) {
2142
- this.cache.channel.on("Ziko-Storage-Updated", callback);
2143
- }
2144
- return this;
1953
+ this.#maintain();
1954
+ return this;
1955
+ }
1956
+
1957
+ add(data) {
1958
+ return this.set({
1959
+ ...this.items,
1960
+ ...data
1961
+ });
1962
+ }
1963
+
1964
+ remove(...keys) {
1965
+ const items = { ...this.items };
1966
+ keys.forEach(key => delete items[key]);
1967
+ return this.set(items);
1968
+ }
1969
+
1970
+ get(key) {
1971
+ return this.items[key];
1972
+ }
1973
+
1974
+ clear() {
1975
+ this.cache.storage.removeItem(this.cache.globalKey);
1976
+ this.#maintain();
1977
+ return this;
1978
+ }
1979
+
1980
+ onStorageUpdated(callback) {
1981
+ if (this.cache.channel) {
1982
+ this.cache.channel.on("Ziko-Storage-Updated", callback);
2145
1983
  }
1984
+ return this;
1985
+ }
2146
1986
  }
2147
1987
 
2148
- // factory functions
2149
- const useLocaleStorage = (key, initialValue, use_channel = true) =>
2150
- new UseStorage(localStorage, key, initialValue, use_channel);
1988
+ const useLocalStorage = (key, initialValue, use_channel = true) =>
1989
+ new UseStorage(localStorage, key, initialValue, use_channel);
2151
1990
 
2152
1991
  const useSessionStorage = (key, initialValue, use_channel = true) =>
2153
- new UseStorage(sessionStorage, key, initialValue, use_channel);
1992
+ new UseStorage(sessionStorage, key, initialValue, use_channel);
2154
1993
 
2155
1994
  var __State__ = {
2156
1995
  store : new Map(),
2157
1996
  index : 0,
2158
1997
  session_storage : null,
2159
1998
  register: function(state){
2160
- if(!undefined.SSR && undefined.DEV){
2161
- if(!this.session) this.session_storage = useSessionStorage('ziko-state', {});
2162
- const savedValue = this.session_storage.get(this.index);
2163
- if(!savedValue) this.session_storage.add({[this.index] : state.value});
2164
- else state.value = savedValue;
2165
- }
2166
- this.store.set(this.index++, state);
1999
+ // if(!import.meta?.env?.SSR && import.meta?.env?.DEV){
2000
+ // if(!this.session) this.session_storage = useSessionStorage('ziko-state', {})
2001
+ // const savedValue = this.session_storage.get(this.index)
2002
+ // if(!savedValue) this.session_storage.add({[this.index] : state.value});
2003
+ // else state.value = savedValue
2004
+ // }
2005
+ // this.store.set(this.index++, state)
2167
2006
  },
2168
2007
  update: function(index, value){
2169
- if(!undefined.SSR && undefined.DEV){
2170
- this.session_storage.add({[index] : value});
2171
- }
2008
+ // if(!import.meta?.env?.SSR && import.meta?.env?.DEV){
2009
+ // this.session_storage.add({[index] : value})
2010
+ // }
2172
2011
  },
2173
2012
 
2174
2013
  };
@@ -2345,57 +2184,61 @@
2345
2184
  unmount: unmount
2346
2185
  });
2347
2186
 
2348
- if(!globalThis.__Ziko__) __init__global__();
2187
+ // import { __init__global__ } from "../__ziko__/index.js";
2188
+
2189
+ // if (!globalThis.__Ziko__) __init__global__();
2349
2190
 
2350
2191
  function useState(initialValue) {
2351
2192
 
2352
- const {store, index} = __Ziko__.__State__;
2353
- __Ziko__.__State__.register({
2354
- value : initialValue,
2355
- subscribers : new Set(),
2356
- paused : false
2357
- });
2358
-
2359
- let current = store.get(index);
2193
+ const state = {
2194
+ value: initialValue,
2195
+ subscribers: new Set(),
2196
+ paused: false,
2197
+ };
2360
2198
 
2361
2199
  function getValue() {
2362
2200
  return {
2363
- value: current.value,
2201
+ value: state.value,
2364
2202
  isStateGetter: () => true,
2365
- _subscribe: (fn) => current.subscribers.add(fn),
2203
+ _subscribe: (fn) => {
2204
+ state.subscribers.add(fn);
2205
+ return () => state.subscribers.delete(fn);
2206
+ },
2366
2207
  };
2367
2208
  }
2368
2209
 
2369
2210
  function setValue(newValue) {
2370
- if (current.paused) return;
2211
+ if (state.paused) return;
2212
+
2371
2213
  if (typeof newValue === "function") {
2372
- newValue = newValue(current.value);
2214
+ newValue = newValue(state.value);
2373
2215
  }
2374
- if (newValue !== current.value) {
2375
- current.value = newValue;
2376
- current.subscribers.forEach(fn => fn(current.value));
2377
- __Ziko__.__State__.update(index, newValue);
2216
+
2217
+ if (!Object.is(newValue, state.value)) {
2218
+ state.value = newValue;
2219
+ state.subscribers.forEach((fn) => fn(state.value));
2378
2220
  }
2379
2221
  }
2380
2222
 
2381
2223
  const controller = {
2382
- pause: () => { current.paused = true; },
2383
- resume: () => { current.paused = false; },
2384
- clear: () => { current.subscribers.clear(); },
2224
+ pause: () => { state.paused = true; },
2225
+ resume: () => { state.paused = false; },
2226
+ clear: () => { state.subscribers.clear(); },
2385
2227
  force: (newValue) => {
2386
- if (typeof newValue === "function") newValue = newValue(current.value);
2387
- current.value = newValue;
2388
- current.subscribers.forEach(fn => fn(current.value));
2228
+ if (typeof newValue === "function") {
2229
+ newValue = newValue(state.value);
2230
+ }
2231
+ state.value = newValue;
2232
+ state.subscribers.forEach((fn) => fn(state.value));
2389
2233
  },
2390
- getSubscribers: () => new Set(current.subscribers),
2234
+ getSubscribers: () => new Set(state.subscribers),
2391
2235
  };
2392
2236
 
2393
2237
  return [getValue, setValue, controller];
2394
2238
  }
2395
2239
 
2396
-
2397
2240
  const isStateGetter = (arg) => {
2398
- return typeof arg === 'function' && arg?.()?.isStateGetter?.();
2241
+ return typeof arg === "function" && arg?.()?.isStateGetter?.() === true;
2399
2242
  };
2400
2243
 
2401
2244
  const camel2hyphencase = (text = '') => text.replace(/[A-Z]/g, match => '-' + match.toLowerCase());
@@ -4322,110 +4165,6 @@
4322
4165
  const svg2imgUrl=svg=>'data:image/svg+xml;base64,'+svg2ascii(svg);
4323
4166
  const svg2img=(svg,render=true)=>tags.img(svg2imgUrl(svg)).mount(render);
4324
4167
 
4325
- // const obj2str=(object)=>{
4326
- // const recursiveToString = (obj) => {
4327
- // if (Array.isArray(obj)) return arr2str(obj);
4328
- // if (typeof obj === 'object' && obj !== null) {
4329
- // return `{ ${Object.entries(obj)
4330
- // .map(([key, value]) => `${key}:${recursiveToString(value)}`)
4331
- // .join(" , ")} }`;
4332
- // }
4333
- // return String(obj);
4334
- // };
4335
- // return recursiveToString(object);
4336
- // };
4337
- // const obj2str = (object) => {
4338
- // const recursiveToString = (obj, indentLevel = 0) => {
4339
- // const indent = ' '.repeat(indentLevel);
4340
- // const nextIndent = ' '.repeat(indentLevel + 1);
4341
- // if(Array.isArray(obj)) return arr2str(obj, indentLevel);
4342
- // if(obj instanceof Complex || obj instanceof Matrix) return obj.toString();
4343
- // if (typeof obj === 'object' && obj !== null) {
4344
- // const entries = Object.entries(obj)
4345
- // .map(([key, value]) => `${nextIndent}${key}: ${recursiveToString(value, indentLevel + 1)}`)
4346
- // .join(",\n");
4347
-
4348
- // return `{\n${entries}\n${indent}}`;
4349
- // }
4350
-
4351
- // return String(obj);
4352
- // };
4353
-
4354
- // return recursiveToString(object);
4355
- // };
4356
- // const obj2str = (object, useIndentation = true, indentLevel = 0) => {
4357
- // const recursiveToString = (obj, level = 0) => {
4358
- // const indent = useIndentation ? ' '.repeat(level) : '';
4359
- // const nextIndent = useIndentation ? ' '.repeat(level + 1) : '';
4360
- // if (Array.isArray(obj)) return arr2str(obj, false, level);
4361
- // if(obj instanceof Complex || obj instanceof Matrix) return obj.toString();
4362
- // if (typeof obj === 'object' && obj !== null) {
4363
- // const entries = Object.entries(obj)
4364
- // .map(([key, value]) => useIndentation
4365
- // ? `${nextIndent}${key}: ${recursiveToString(value, level + 1)}`
4366
- // : `${key}: ${recursiveToString(value, level + 1)}`
4367
- // ).join(useIndentation ? ",\n" : ", ");
4368
-
4369
- // return useIndentation
4370
- // ? `{\n${entries}\n${indent}}`
4371
- // : `{${entries}}`;
4372
- // }
4373
-
4374
- // return String(obj);
4375
- // };
4376
-
4377
- // return recursiveToString(object, indentLevel);
4378
- // };
4379
-
4380
- const obj2str=(obj)=>JSON.stringify(
4381
- mapfun(n=>{
4382
- if(["number","string","boolean","bigint"].includes(typeof n)) return String(n);
4383
- if(n instanceof Complex || n instanceof Matrix) return n.toString();
4384
- if(n instanceof Array) return arr2str(n)
4385
- },
4386
- obj), null, " ")
4387
- .replace(/"([^"]+)":/g, '$1:') // Remove Quotes from Keys
4388
- .replace(/: "([^"]+)"/g, ': $1'); // Remove Quotes from str values
4389
-
4390
- const getMaxDepth = arr=> {
4391
- if (!Array.isArray(arr)) return 0;
4392
- let maxDepth = 1;
4393
- for (const element of arr) {
4394
- if (Array.isArray(element)) {
4395
- const depth = getMaxDepth(element);
4396
- if (depth + 1 > maxDepth) {
4397
- maxDepth = depth + 1;
4398
- }
4399
- }
4400
- }
4401
- return maxDepth;
4402
- };
4403
- const arr2str = (arr) => {
4404
- let level = 0;
4405
- function arrStringify(arr) {
4406
- let max = getMaxDepth(arr);
4407
- let useIdentation = 0;
4408
- if (arr.some((n) => Array.isArray(n))) {
4409
- level++;
4410
- useIdentation = 1;
4411
- }
4412
- return (
4413
- "[" +
4414
- arr.map((n, i) => {
4415
- if (["number", "string", "boolean", "bigint"].includes(typeof n))
4416
- return String(n);
4417
- if (n instanceof Complex) return n.toString();
4418
- if (n instanceof Array) {
4419
- return `\n${" ".repeat(level)}${arrStringify(n)}${i === arr.length - 1 ? "\n" : ""}`;
4420
- }
4421
- if( n instanceof Object) return obj2str(n);
4422
- })
4423
- + `${" ".repeat((max+level+1) * useIdentation)}]`
4424
- );
4425
- }
4426
- return arrStringify(arr);
4427
- };
4428
-
4429
4168
  const json2css=(json, indentLevel = 0)=>{
4430
4169
  json = trimKeys(json);
4431
4170
  let cssText = '';
@@ -4698,6 +4437,49 @@
4698
4437
  })
4699
4438
  }
4700
4439
 
4440
+ const mapfun=(fun,...X)=>{
4441
+ const Y=X.map(x=>{
4442
+ if(
4443
+ x===null||
4444
+ ["number","string","boolean","bigint","undefined"].includes(typeof x)||
4445
+ x?.__mapfun__
4446
+ ) return fun(x)
4447
+ if(x instanceof Array) return x.map(n=>mapfun(fun,n));
4448
+ if(ArrayBuffer.isView(x)) return x.map(n=>fun(n));
4449
+ if(x instanceof Set) return new Set(mapfun(fun,...[...x]));
4450
+ if(x instanceof Map) return new Map([...x].map(n=>[n[0],mapfun(fun,n[1])]));
4451
+ if(x.isMatrix?.()) return new x.constructor(x.rows, x.cols, mapfun(x.arr.flat(1)))
4452
+ // if(x.isComplex?.()){
4453
+ // const [a,b,z,phi]=[x.a,x.b,x.z,x.phi];
4454
+ // switch(fun){
4455
+ // // Moved to Fixed to avoid Circular Dep
4456
+ // // case Math.log: return new x.constructor(ln(z),phi); // Done
4457
+ // // case Math.exp: return new x.constructor(e(a)*cos(b),e(a)*sin(b)); // Done
4458
+ // // case Math.abs: return z; // Done
4459
+ // // case Math.sqrt: return new x.constructor(sqrt(z)*cos(phi/2),sqrt(z)*sin(phi/2)); // Done
4460
+ // // case Fixed.cos: return new x.constructor(cos(a)*cosh(b),-(sin(a)*sinh(b)));
4461
+ // // case Fixed.sin: return new x.constructor(sin(a)*cosh(b),cos(a)*sinh(b));
4462
+ // // case Fixed.tan:{
4463
+ // // const DEN = cos(2*a)+cosh(2*b);
4464
+ // // return new x.constructor(sin(2*a)/DEN,sinh(2*b)/DEN);
4465
+ // // }
4466
+ // // case Fixed.cosh:return new x.constructor(cosh(a)*cos(b),sinh(a)*sin(b));
4467
+ // // case Fixed.sinh:return new x.constructor(sinh(a)*cos(b),cosh(a)*sin(b));
4468
+ // // case Fixed.tanh:{
4469
+ // // const DEN=cosh(2*a)+cos(2*b);
4470
+ // // return new x.constructor(sinh(2*a)/DEN,sin(2*b)/DEN)
4471
+ // // }
4472
+ // default : return fun(x)
4473
+ // }
4474
+ // }
4475
+ else if(x instanceof Object){
4476
+ return Object.fromEntries(Object.entries(x).map(n=>n=[n[0],mapfun(fun,n[1])]))
4477
+ // return fun(Object) || Object.fromEntries(Object.entries(x).map(n=>n=[n[0],mapfun(fun,n[1])]))
4478
+ }
4479
+ });
4480
+ return Y.length==1? Y[0]: Y;
4481
+ };
4482
+
4701
4483
  const useReactive = (nested_value) => mapfun(
4702
4484
  n => {
4703
4485
  const state = useState(n);
@@ -5167,7 +4949,6 @@
5167
4949
  exports.UIView = UIView;
5168
4950
  exports.UseRoot = UseRoot;
5169
4951
  exports.UseThread = UseThread;
5170
- exports.Utils = Utils;
5171
4952
  exports.View = View;
5172
4953
  exports.ViewEvent = ViewEvent;
5173
4954
  exports.ViewListeners = ViewListeners;
@@ -5187,9 +4968,7 @@
5187
4968
  exports.and = and;
5188
4969
  exports.animation = animation;
5189
4970
  exports.apply_fun = apply_fun;
5190
- exports.arange = arange;
5191
4971
  exports.arc = arc;
5192
- exports.arr2str = arr2str;
5193
4972
  exports.asin = asin;
5194
4973
  exports.asinh = asinh;
5195
4974
  exports.atan = atan;
@@ -5198,7 +4977,6 @@
5198
4977
  exports.back = back;
5199
4978
  exports.binomial = binomial;
5200
4979
  exports.call_with_optional_props = call_with_optional_props;
5201
- exports.cartesianProduct = cartesianProduct;
5202
4980
  exports.cbrt = cbrt;
5203
4981
  exports.ceil = ceil;
5204
4982
  exports.clamp = clamp;
@@ -5224,9 +5002,7 @@
5224
5002
  exports.exp = exp$1;
5225
5003
  exports.floor = floor;
5226
5004
  exports.fract = fract;
5227
- exports.geomspace = geomspace;
5228
5005
  exports.hypot = hypot;
5229
- exports.inRange = inRange;
5230
5006
  exports.in_back = in_back;
5231
5007
  exports.in_bounce = in_bounce;
5232
5008
  exports.in_circ = in_circ;
@@ -5247,7 +5023,6 @@
5247
5023
  exports.in_quart = in_quart;
5248
5024
  exports.in_quint = in_quint;
5249
5025
  exports.in_sin = in_sin;
5250
- exports.isApproximatlyEqual = isApproximatlyEqual;
5251
5026
  exports.isPrimitive = isPrimitive;
5252
5027
  exports.isStateGetter = isStateGetter;
5253
5028
  exports.json2arr = json2arr;
@@ -5261,12 +5036,10 @@
5261
5036
  exports.lerp = lerp;
5262
5037
  exports.linear = linear;
5263
5038
  exports.linkStyle = linkStyle;
5264
- exports.linspace = linspace;
5265
5039
  exports.ln = ln;
5266
- exports.logspace = logspace;
5267
5040
  exports.loop = loop;
5268
5041
  exports.map = map$1;
5269
- exports.mapfun = mapfun;
5042
+ exports.mapfun = mapfun$1;
5270
5043
  exports.matrix = matrix;
5271
5044
  exports.matrix2 = matrix2;
5272
5045
  exports.matrix3 = matrix3;
@@ -5282,9 +5055,6 @@
5282
5055
  exports.norm = norm;
5283
5056
  exports.normalize_css_value = normalize_css_value;
5284
5057
  exports.nthr = nthr;
5285
- exports.nums = nums;
5286
- exports.obj2str = obj2str;
5287
- exports.ones = ones;
5288
5058
  exports.or = or;
5289
5059
  exports.out_back = out_back;
5290
5060
  exports.out_bounce = out_bounce;
@@ -5297,9 +5067,7 @@
5297
5067
  exports.out_quint = out_quint;
5298
5068
  exports.out_sin = out_sin;
5299
5069
  exports.percentile = percentile;
5300
- exports.pgcd = pgcd;
5301
5070
  exports.pow = pow$1;
5302
- exports.ppcm = ppcm;
5303
5071
  exports.rad2deg = rad2deg;
5304
5072
  exports.register_click_away_event = register_click_away_event;
5305
5073
  exports.register_swipe_event = register_swipe_event;
@@ -5336,7 +5104,7 @@
5336
5104
  exports.useDerived = useDerived;
5337
5105
  exports.useEventEmitter = useEventEmitter;
5338
5106
  exports.useIPC = useIPC;
5339
- exports.useLocaleStorage = useLocaleStorage;
5107
+ exports.useLocalStorage = useLocalStorage;
5340
5108
  exports.useMediaQuery = useMediaQuery;
5341
5109
  exports.useQueryParams = useQueryParams;
5342
5110
  exports.useReactive = useReactive;
@@ -5353,6 +5121,5 @@
5353
5121
  exports.watchQueryParams = watchQueryParams;
5354
5122
  exports.xnor = xnor;
5355
5123
  exports.xor = xor;
5356
- exports.zeros = zeros;
5357
5124
 
5358
5125
  }));