ziko 0.50.2 → 0.51.1
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 +1515 -1398
- package/dist/ziko.mjs +1515 -1398
- package/package.json +1 -1
- package/src/__helpers__/checkers/index.js +1 -0
- package/src/data/converter/csv.js +1 -1
- package/src/events/binders/coordinates-based-event.js +25 -0
- package/src/events/binders/index.js +11 -6
- package/src/events/customizers/normalise-coordinates.js +0 -0
- package/src/events/details-setter/index.js +3 -1
- package/src/events/details-setter/mouse.js +35 -0
- package/src/events/details-setter/pointer.js +33 -31
- package/src/events/details-setter/touch.js +37 -0
- package/src/math/complex/index.js +2 -3
- package/src/math/discret/Conversion/index.js +1 -1
- package/src/math/discret/Logic/index.js +1 -1
- package/src/math/functions/index.js +8 -9
- package/src/math/matrix/helpers/det.js +36 -0
- package/src/math/matrix/helpers/index.js +3 -0
- package/src/math/matrix/helpers/inverse.js +52 -0
- package/src/math/matrix/helpers/stack.js +24 -0
- package/src/math/matrix/index.js +1 -1
- package/src/math/matrix/matrix.js +601 -0
- package/src/math/random/index.js +88 -92
- package/src/math/signal/functions.js +23 -25
- package/src/math/utils/arithmetic.js +15 -17
- package/src/math/utils/mapfun.js +3 -7
- package/src/router/file-based-router/index.js +46 -0
- package/src/router/index.js +2 -0
- package/src/router/utils/dynamic-routes-parser.js +76 -0
- package/src/router/utils/get-root.js +16 -0
- package/src/router/utils/index.js +5 -0
- package/src/router/utils/normalize-path.js +17 -0
- package/src/router/utils/routes-grouper.js +22 -0
- package/src/router/utils/routes-matcher.js +60 -0
- package/types/index.d.ts +1 -0
- package/types/router/file-based-router/index.d.ts +20 -0
- package/types/router/index.d.ts +2 -0
- package/types/router/utils/dynamic-routes-parser.d.ts +14 -0
- package/types/router/utils/get-root.d.ts +11 -0
- package/types/router/utils/index.d.ts +5 -0
- package/types/router/utils/normalize-path.d.ts +15 -0
- package/types/router/utils/routes-grouper.d.ts +29 -0
- package/types/router/utils/routes-matcher.d.ts +1 -0
- package/src/math/matrix/Matrix.js +0 -675
package/src/math/random/index.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
|
|
2
|
-
import{Complex}from"../complex/index.js"
|
|
3
2
|
import { Utils } from "../utils/index.js";
|
|
4
|
-
import{Base}from"../discret/index.js"
|
|
5
|
-
import { matrix } from "../matrix/index.js";
|
|
3
|
+
import{ Base } from "../discret/index.js"
|
|
6
4
|
class Random {
|
|
7
5
|
static float(a = 1, b) {
|
|
8
6
|
return b ? Math.random() * (b - a) + a : a * Math.random();
|
|
@@ -45,10 +43,6 @@ class Random {
|
|
|
45
43
|
static shuffleArr(arr){
|
|
46
44
|
return arr.sort(()=>0.5-Math.random())
|
|
47
45
|
}
|
|
48
|
-
static shuffleMatrix(M){
|
|
49
|
-
const {rows,cols,arr}=M;
|
|
50
|
-
return matrix(rows,cols,arr.flat().sort(()=>0.5-Math.random()))
|
|
51
|
-
}
|
|
52
46
|
static floats(n, a, b) {
|
|
53
47
|
return new Array(n).fill(0).map(() => this.float(a, b));
|
|
54
48
|
}
|
|
@@ -83,91 +77,93 @@ class Random {
|
|
|
83
77
|
static colors(n) {
|
|
84
78
|
return new Array(n).fill(null).map(()=>this.color());
|
|
85
79
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
80
|
+
|
|
81
|
+
// Should be Moved to Matrix and Complex to avoid Circular dependencies
|
|
82
|
+
// static complex(a = [0,1], b = [0,1]) {
|
|
83
|
+
// return a instanceof Array?
|
|
84
|
+
// new Complex(
|
|
85
|
+
// this.float(a[0], a[1]),
|
|
86
|
+
// this.float(b[0], b[1])
|
|
87
|
+
// ):
|
|
88
|
+
// new Complex(
|
|
89
|
+
// ...this.floats(2,a,b)
|
|
90
|
+
// )
|
|
95
91
|
|
|
96
|
-
}
|
|
97
|
-
static complexInt(a = [0,1], b = [0,1]) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
static complexBin() {
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
static complexOct() {
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
static complexDec() {
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
static complexHex() {
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
static complexes(n, a = 0, b = 1) {
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
static complexesInt(n, a = 0, b = 1) {
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
static complexesBin(n) {
|
|
122
|
-
|
|
123
|
-
}
|
|
124
|
-
static complexesOct(n) {
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
static complexesDec(n) {
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
static complexesHex(n) {
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
static matrix(r,c,min,max){
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
static matrixInt(r,c,min,max){
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
static matrixBin(r,c){
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
static matrixOct(r,c){
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
static matrixDec(r,c){
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
static matrixHex(r,c){
|
|
149
|
-
|
|
150
|
-
}
|
|
151
|
-
static matrixColor(r,c){
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
static matrixComplex(r,c,a,b){
|
|
155
|
-
|
|
156
|
-
}
|
|
157
|
-
static matrixComplexInt(r,c,a,b){
|
|
158
|
-
|
|
159
|
-
}
|
|
160
|
-
static matrixComplexBin(r,c){
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
static matrixComplexOct(r,c){
|
|
164
|
-
|
|
165
|
-
}
|
|
166
|
-
static matrixComplexDec(r,c){
|
|
167
|
-
|
|
168
|
-
}
|
|
169
|
-
static matrixComplexHex(r,c){
|
|
170
|
-
|
|
171
|
-
}
|
|
92
|
+
// }
|
|
93
|
+
// static complexInt(a = [0,1], b = [0,1]) {
|
|
94
|
+
// return new Complex(
|
|
95
|
+
// this.int(a[0], a[1]),
|
|
96
|
+
// this.int(b[0], b[1])
|
|
97
|
+
// );
|
|
98
|
+
// }
|
|
99
|
+
// static complexBin() {
|
|
100
|
+
// return new Complex(...this.bins(2));
|
|
101
|
+
// }
|
|
102
|
+
// static complexOct() {
|
|
103
|
+
// return new Complex(...this.octs(2));
|
|
104
|
+
// }
|
|
105
|
+
// static complexDec() {
|
|
106
|
+
// return new Complex(...this.decs(10));
|
|
107
|
+
// }
|
|
108
|
+
// static complexHex() {
|
|
109
|
+
// return new Complex(...this.octs(2));
|
|
110
|
+
// }
|
|
111
|
+
// static complexes(n, a = 0, b = 1) {
|
|
112
|
+
// return new Array(n).fill(0).map(() => this.complex(a, b));
|
|
113
|
+
// }
|
|
114
|
+
// static complexesInt(n, a = 0, b = 1) {
|
|
115
|
+
// return new Array(n).fill(0).map(() => this.complexInt(a, b));
|
|
116
|
+
// }
|
|
117
|
+
// static complexesBin(n) {
|
|
118
|
+
// return new Array(n).fill(0).map(() => this.complexBin());
|
|
119
|
+
// }
|
|
120
|
+
// static complexesOct(n) {
|
|
121
|
+
// return new Array(n).fill(0).map(() => this.complexOct());
|
|
122
|
+
// }
|
|
123
|
+
// static complexesDec(n) {
|
|
124
|
+
// return new Array(n).fill(0).map(() => this.complexDec());
|
|
125
|
+
// }
|
|
126
|
+
// static complexesHex(n) {
|
|
127
|
+
// return new Array(n).fill(0).map(() => this.complexHex());
|
|
128
|
+
// }
|
|
129
|
+
// static matrix(r,c,min,max){
|
|
130
|
+
// return matrix(r,c,this.floats(r*c,min,max))
|
|
131
|
+
// }
|
|
132
|
+
// static matrixInt(r,c,min,max){
|
|
133
|
+
// return matrix(r,c,this.ints(r*c,min,max))
|
|
134
|
+
// }
|
|
135
|
+
// static matrixBin(r,c){
|
|
136
|
+
// return matrix(r,c,this.bins(r*c))
|
|
137
|
+
// }
|
|
138
|
+
// static matrixOct(r,c){
|
|
139
|
+
// return matrix(r,c,this.octs(r*c))
|
|
140
|
+
// }
|
|
141
|
+
// static matrixDec(r,c){
|
|
142
|
+
// return matrix(r,c,this.decs(r*c))
|
|
143
|
+
// }
|
|
144
|
+
// static matrixHex(r,c){
|
|
145
|
+
// return matrix(r,c,this.hex(r*c))
|
|
146
|
+
// }
|
|
147
|
+
// static matrixColor(r,c){
|
|
148
|
+
// return matrix(r,c,this.colors(r*c))
|
|
149
|
+
// }
|
|
150
|
+
// static matrixComplex(r,c,a,b){
|
|
151
|
+
// return matrix(r,c,this.complexes(r*c,a,b))
|
|
152
|
+
// }
|
|
153
|
+
// static matrixComplexInt(r,c,a,b){
|
|
154
|
+
// return matrix(r,c,this.complexesInt(r*c,a,b))
|
|
155
|
+
// }
|
|
156
|
+
// static matrixComplexBin(r,c){
|
|
157
|
+
// return matrix(r,c,this.complexesBin(r*c))
|
|
158
|
+
// }
|
|
159
|
+
// static matrixComplexOct(r,c){
|
|
160
|
+
// return matrix(r,c,this.complexesBin(r*c))
|
|
161
|
+
// }
|
|
162
|
+
// static matrixComplexDec(r,c){
|
|
163
|
+
// return matrix(r,c,this.complexesBin(r*c))
|
|
164
|
+
// }
|
|
165
|
+
// static matrixComplexHex(r,c){
|
|
166
|
+
// return matrix(r,c,this.complexesBin(r*c))
|
|
167
|
+
// }
|
|
172
168
|
}
|
|
173
169
|
export{Random}
|
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
import { Complex , complex } from "../complex/index.js";
|
|
2
1
|
import { abs , pow , sqrtn , max , min} from "../functions/index.js";
|
|
3
2
|
import { mul } from "../utils/index.js";
|
|
4
3
|
import { E } from "../const.js";
|
|
5
|
-
import { Matrix } from "../matrix/Matrix.js";
|
|
6
4
|
const zeros=(n)=>new Array(n).fill(0);
|
|
7
5
|
const ones=(n)=>new Array(n).fill(1);
|
|
8
6
|
const nums=(num,n)=>new Array(n).fill(num);
|
|
9
7
|
const norm=(value, min, max)=>{
|
|
10
8
|
if (typeof value === "number") return min !== max ? (value - min) / (max - min) : 0;
|
|
11
|
-
else if (value
|
|
12
|
-
else if (value
|
|
9
|
+
else if (value.isMatrix?.()) return new value.constructor(value.rows, value.cols, norm(value.arr.flat(1), min, max));
|
|
10
|
+
else if (value.isComplex?.()) return new value.constructor(norm(value.a, min, max), norm(value.b, min, max));
|
|
13
11
|
else if (value instanceof Array) {
|
|
14
12
|
if (value.every((n) => typeof (n === "number"))) {
|
|
15
13
|
return value.map((n) => norm(n, min, max));
|
|
@@ -23,8 +21,8 @@ const norm=(value, min, max)=>{
|
|
|
23
21
|
}
|
|
24
22
|
const lerp=(value, min, max)=>{
|
|
25
23
|
if (typeof value === "number") return (max - min) * value + min;
|
|
26
|
-
else if (value
|
|
27
|
-
else if (value
|
|
24
|
+
else if (value.isMatrix?.()) return new value.constructor(value.rows, value.cols, lerp(value.arr.flat(1), min, max));
|
|
25
|
+
else if (value.isComplex?.()) return new value.constructor(lerp(value.a, min, max), lerp(value.b, min, max));
|
|
28
26
|
else if (value instanceof Array) {
|
|
29
27
|
if (value.every((n) => typeof (n === "number"))) {
|
|
30
28
|
return value.map((n) => lerp(n, min, max));
|
|
@@ -36,17 +34,17 @@ const lerp=(value, min, max)=>{
|
|
|
36
34
|
}
|
|
37
35
|
}
|
|
38
36
|
}
|
|
39
|
-
const map=(
|
|
40
|
-
if (typeof
|
|
41
|
-
else if (
|
|
42
|
-
else if (
|
|
43
|
-
else if (
|
|
44
|
-
if (
|
|
45
|
-
return
|
|
37
|
+
const map=(x, a, b, c, d)=>{
|
|
38
|
+
if (typeof x === "number") return lerp(norm(x, a, b), c, d);
|
|
39
|
+
else if (x.isMatrix?.()) return new x.constructor(x.rows, x.cols, map(x.arr.flat(1), a, b, c, d));
|
|
40
|
+
else if (x.isComplex?.()) return new x.constructor(map(x.a, b, c, d), map(x.b, a, b, c, d));
|
|
41
|
+
else if (x instanceof Array) {
|
|
42
|
+
if (x.every((n) => typeof (n === "number"))) {
|
|
43
|
+
return x.map((n) => map(n, a, b, c, d));
|
|
46
44
|
} else {
|
|
47
|
-
let y = new Array(
|
|
48
|
-
for (let i = 0; i <
|
|
49
|
-
y[i] = map(
|
|
45
|
+
let y = new Array(x.length);
|
|
46
|
+
for (let i = 0; i < x.length; i++) {
|
|
47
|
+
y[i] = map(x[i], a, b, c, d);
|
|
50
48
|
}
|
|
51
49
|
}
|
|
52
50
|
}
|
|
@@ -54,8 +52,8 @@ const map=(value, a, b, c, d)=>{
|
|
|
54
52
|
const clamp=(x, a , b)=>{
|
|
55
53
|
const [min_value,max_value]=[min(a,b),max(a,b)]
|
|
56
54
|
if (typeof x === "number") return min(max(x, min_value), max_value);
|
|
57
|
-
else if (x
|
|
58
|
-
else if (x
|
|
55
|
+
else if (x.isMatrix?.()) return new x.constructor(x.rows, x.cols, clamp(x.arr.flat(1), min_value, max_value));
|
|
56
|
+
else if (x.isComplex?.()) return new x.constructor(clamp(x.a, min_value, max_value), clamp(x.b, min_value, max_value));
|
|
59
57
|
else if (x instanceof Array) {
|
|
60
58
|
if (x.every((n) => typeof (n === "number"))) {
|
|
61
59
|
return x.map((n) => clamp(n, min_value, max_value));
|
|
@@ -90,14 +88,14 @@ const linspace=(a,b,n=abs(b-a)+1,endpoint=true)=>{
|
|
|
90
88
|
return Y
|
|
91
89
|
}
|
|
92
90
|
|
|
93
|
-
if([a,b].some(n=>n
|
|
94
|
-
const z1=
|
|
95
|
-
const z2=
|
|
91
|
+
if([a,b].some(n=>n.isComplex?.())){
|
|
92
|
+
const z1 = new n.constructor(a)
|
|
93
|
+
const z2 = new n.constructor(b)
|
|
96
94
|
n=n||Math.abs(z1.a-z2.a)+1;
|
|
97
95
|
const X=linspace(z1.a,z2.a,n,endpoint);
|
|
98
96
|
const Y=linspace(z1.b,z2.b,n,endpoint);
|
|
99
97
|
let Z=new Array(n).fill(null);
|
|
100
|
-
Z=Z.map((n,i)=>
|
|
98
|
+
Z=Z.map((n,i)=> new n.constructor(X[i],Y[i]));
|
|
101
99
|
return Z;
|
|
102
100
|
}
|
|
103
101
|
}
|
|
@@ -117,9 +115,9 @@ const geomspace=(a,b,n=abs(b-a)+1,endpoint=true)=>{
|
|
|
117
115
|
return a<b?Y:Y.reverse()
|
|
118
116
|
}
|
|
119
117
|
|
|
120
|
-
if([a,b].some(n=>n
|
|
121
|
-
const z1=
|
|
122
|
-
const z2=
|
|
118
|
+
if([a,b].some(n=>n.isComplex?.())){
|
|
119
|
+
const z1 = new n.constructor(a)
|
|
120
|
+
const z2 = new n.constructor(b)
|
|
123
121
|
n=n||Math.abs(z1.a-z2.a)+1;
|
|
124
122
|
let base;
|
|
125
123
|
endpoint ? base = sqrtn(z2.div(z1),n-1) : base = sqrtn(z2.div(z1),n) ;
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import { Complex,complex} from "../complex/index.js";
|
|
2
|
-
import { Matrix } from "../matrix/index.js";
|
|
3
1
|
const _add=(a,b)=>{
|
|
4
2
|
if(typeof(a)==="number"){
|
|
5
3
|
if (typeof b == "number") return a + b;
|
|
6
|
-
else if (b
|
|
7
|
-
else if (b
|
|
4
|
+
else if (b.isComplex?.())return new b.constructor(a + b.a, b.b);
|
|
5
|
+
else if (b.isMatrix?.()) return b.constructor.nums(b.rows, b.cols, a).add(b);
|
|
8
6
|
else if (b instanceof Array)return b.map(n=>add(n,a));
|
|
9
7
|
}
|
|
10
|
-
else if(a
|
|
8
|
+
else if(a.isComplex?.()||a.isMatrix?.()){
|
|
11
9
|
if(b instanceof Array)return b.map(n=>a.clone.add(n));
|
|
12
10
|
return a.clone.add(b);
|
|
13
11
|
}
|
|
@@ -23,11 +21,11 @@ const _add=(a,b)=>{
|
|
|
23
21
|
const _sub=(a,b)=>{
|
|
24
22
|
if(typeof(a)==="number"){
|
|
25
23
|
if (typeof b == "number") return a - b;
|
|
26
|
-
else if (b
|
|
27
|
-
else if (b
|
|
24
|
+
else if (b.isComplex?.())return new b.constructor(a - b.a, -b.b);
|
|
25
|
+
else if (b.isMatrix?.()) return b.constructor.nums(b.rows, b.cols, a).sub(b);
|
|
28
26
|
else if (b instanceof Array)return b.map(n=>sub(n,a));
|
|
29
27
|
}
|
|
30
|
-
else if(a
|
|
28
|
+
else if(a.isComplex?.()||a.isMatrix?.()){
|
|
31
29
|
if(b instanceof Array)return b.map(n=>a.clone.sub(n));
|
|
32
30
|
return a.clone.sub(b);
|
|
33
31
|
}
|
|
@@ -45,11 +43,11 @@ const _sub=(a,b)=>{
|
|
|
45
43
|
const _mul=(a,b)=>{
|
|
46
44
|
if(typeof(a)==="number"){
|
|
47
45
|
if (typeof b == "number") return a * b;
|
|
48
|
-
else if (b
|
|
49
|
-
else if (b
|
|
46
|
+
else if (b.isComplex?.())return new b.constructor(a * b.a,a * b.b);
|
|
47
|
+
else if (b.isMatrix?.()) return b.constructor.nums(b.rows, b.cols, a).mul(b);
|
|
50
48
|
else if (b instanceof Array)return b.map(n=>mul(a,n));
|
|
51
49
|
}
|
|
52
|
-
else if(a
|
|
50
|
+
else if(a.isComplex?.()||a.isMatrix?.()){
|
|
53
51
|
if(b instanceof Array)return b.map(n=>a.clone.mul(n));
|
|
54
52
|
return a.clone.mul(b);
|
|
55
53
|
}
|
|
@@ -67,11 +65,11 @@ const _mul=(a,b)=>{
|
|
|
67
65
|
const _div=(a,b)=>{
|
|
68
66
|
if(typeof(a)==="number"){
|
|
69
67
|
if (typeof b == "number") return a / b;
|
|
70
|
-
else if (b
|
|
71
|
-
else if (b
|
|
68
|
+
else if (b.isComplex?.())return new b.constructor(a / b.a,a / b.b);
|
|
69
|
+
else if (b.isMatrix?.()) return b.constructor.nums(b.rows, b.cols, a).div(b);
|
|
72
70
|
else if (b instanceof Array)return b.map(n=>div(a,n));
|
|
73
71
|
}
|
|
74
|
-
else if(a
|
|
72
|
+
else if(a.isComplex?.()||a.isMatrix?.()){
|
|
75
73
|
if(b instanceof Array)return b.map(n=>a.clone.div(n));
|
|
76
74
|
return a.clone.div(b);
|
|
77
75
|
}
|
|
@@ -89,11 +87,11 @@ const _div=(a,b)=>{
|
|
|
89
87
|
const _modulo=(a,b)=>{
|
|
90
88
|
if(typeof(a)==="number"){
|
|
91
89
|
if (typeof b == "number") return a % b;
|
|
92
|
-
else if (b
|
|
93
|
-
else if (b
|
|
90
|
+
else if (b.isComplex?.())return new b.constructor(a % b.a,a % b.b);
|
|
91
|
+
else if (b.isMatrix?.()) return b.constructor.nums(b.rows, b.cols, a).modulo(b);
|
|
94
92
|
else if (b instanceof Array)return b.map(n=>div(a,n));
|
|
95
93
|
}
|
|
96
|
-
else if(a
|
|
94
|
+
else if(a.isComplex?.()||a.isMatrix?.()){
|
|
97
95
|
if(b instanceof Array)return b.map(n=>a.clone.div(n));
|
|
98
96
|
return a.clone.div(b);
|
|
99
97
|
}
|
package/src/math/utils/mapfun.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import { Matrix } from "../matrix/index.js";
|
|
2
|
-
import { Complex , complex } from "../complex/index.js";
|
|
3
1
|
import {ln,e,cos,sin,sqrt,cosh,sinh} from "../functions/index.js";
|
|
4
2
|
import { Fixed } from "../functions/helper.js";
|
|
5
3
|
// To generalise
|
|
6
|
-
|
|
7
4
|
const mapfun=(fun,...X)=>{
|
|
8
5
|
const Y=X.map(x=>{
|
|
9
6
|
if(x===null) return fun(null);
|
|
@@ -12,10 +9,9 @@ const mapfun=(fun,...X)=>{
|
|
|
12
9
|
if(ArrayBuffer.isView(x)) return x.map(n=>fun(n));
|
|
13
10
|
if(x instanceof Set) return new Set(mapfun(fun,...[...x]));
|
|
14
11
|
if(x instanceof Map) return new Map([...x].map(n=>[n[0],mapfun(fun,n[1])]));
|
|
15
|
-
if(x
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if(x instanceof Complex){
|
|
12
|
+
if(x.isMatrix?.()) return new x.constructor(x.rows, x.cols, mapfun(x.arr.flat(1)))
|
|
13
|
+
if(x.isComplex?.()){
|
|
14
|
+
const complex = (...args) => new x.constructor(...args)
|
|
19
15
|
const [a,b,z,phi]=[x.a,x.b,x.z,x.phi];
|
|
20
16
|
switch(fun){
|
|
21
17
|
case Math.log: return complex(ln(z),phi); // Done
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {
|
|
2
|
+
get_root,
|
|
3
|
+
normalize_path,
|
|
4
|
+
routes_matcher,
|
|
5
|
+
is_dynamic,
|
|
6
|
+
dynamic_routes_parser
|
|
7
|
+
} from "../utils/index.js"
|
|
8
|
+
export async function createSPAFileBasedRouter(
|
|
9
|
+
pages,
|
|
10
|
+
target = globalThis?.document?.body
|
|
11
|
+
) {
|
|
12
|
+
if(!(target instanceof HTMLElement) && target?.element instanceof HTMLElement) target = target?.element;
|
|
13
|
+
if (!(target instanceof HTMLElement)) {
|
|
14
|
+
throw new Error("Invalid mount target: must be HTMLElement or UIElement");
|
|
15
|
+
}
|
|
16
|
+
let path = decodeURIComponent(globalThis.location.pathname.replace(/\/$/, ''));
|
|
17
|
+
const routes = Object.keys(pages);
|
|
18
|
+
const root = get_root(routes);
|
|
19
|
+
|
|
20
|
+
const pairs = {};
|
|
21
|
+
for (const route of routes) {
|
|
22
|
+
const module = await pages[route]();
|
|
23
|
+
const modComponent = await module.default;
|
|
24
|
+
pairs[normalize_path(route, root)] = modComponent;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let mask = null;
|
|
28
|
+
let component = null;
|
|
29
|
+
|
|
30
|
+
for (const [routePath, comp] of Object.entries(pairs)) {
|
|
31
|
+
if (routes_matcher(routePath, `/${path}`)) {
|
|
32
|
+
mask = routePath;
|
|
33
|
+
component = comp;
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (!mask) return; // no route matched
|
|
39
|
+
|
|
40
|
+
const params = is_dynamic(mask) ? dynamic_routes_parser(mask, path) : undefined;
|
|
41
|
+
const mounted = params ? await component(params) : await component();
|
|
42
|
+
|
|
43
|
+
if(mounted instanceof HTMLElement) target.append(mounted);
|
|
44
|
+
else mounted.mount(target);
|
|
45
|
+
|
|
46
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
export function dynamic_routes_parser(mask, route) {
|
|
2
|
+
const maskSegments = mask.split("/").filter(Boolean);
|
|
3
|
+
const routeSegments = route.split("/").filter(Boolean);
|
|
4
|
+
const params = {};
|
|
5
|
+
let i = 0, j = 0;
|
|
6
|
+
while (i < maskSegments.length && j < routeSegments.length) {
|
|
7
|
+
const maskSegment = maskSegments[i];
|
|
8
|
+
const routeSegment = routeSegments[j];
|
|
9
|
+
if (maskSegment.startsWith("[...") && maskSegment.endsWith("]")) {
|
|
10
|
+
const paramName = maskSegment.slice(4, -1);
|
|
11
|
+
const remainingMaskSegments = maskSegments.length - i - 1;
|
|
12
|
+
if (remainingMaskSegments === 0) {
|
|
13
|
+
params[paramName] = routeSegments.slice(j).join("/");
|
|
14
|
+
break;
|
|
15
|
+
}
|
|
16
|
+
let requiredSegments = 0;
|
|
17
|
+
for (let k = i + 1; k < maskSegments.length; k++) {
|
|
18
|
+
if (!maskSegments[k].endsWith("]+")) requiredSegments++;
|
|
19
|
+
}
|
|
20
|
+
const remainingRouteSegments = routeSegments.length - j;
|
|
21
|
+
const segmentsToConsume = remainingRouteSegments - requiredSegments;
|
|
22
|
+
if (segmentsToConsume >= 1) {
|
|
23
|
+
params[paramName] = routeSegments
|
|
24
|
+
.slice(j, j + segmentsToConsume)
|
|
25
|
+
.join("/");
|
|
26
|
+
j += segmentsToConsume;
|
|
27
|
+
}
|
|
28
|
+
else return {};
|
|
29
|
+
i++;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (maskSegment.startsWith("[") && maskSegment.endsWith("]+")) {
|
|
33
|
+
const paramName = maskSegment.slice(1, -2);
|
|
34
|
+
if (routeSegment) {
|
|
35
|
+
params[paramName] = routeSegment;
|
|
36
|
+
j++;
|
|
37
|
+
}
|
|
38
|
+
i++;
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (maskSegment.startsWith("[") && maskSegment.endsWith("]")) {
|
|
42
|
+
const paramName = maskSegment.slice(1, -1);
|
|
43
|
+
params[paramName] = routeSegment;
|
|
44
|
+
}
|
|
45
|
+
else if (maskSegment !== routeSegment) return {};
|
|
46
|
+
i++;
|
|
47
|
+
j++;
|
|
48
|
+
}
|
|
49
|
+
return params;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
// console.log("\n=== PARSER TESTS ===");
|
|
54
|
+
// console.log(dynamic_routes_parser("/user/[id]+", "/user"));
|
|
55
|
+
// // 👉 {}
|
|
56
|
+
|
|
57
|
+
// console.log(dynamic_routes_parser("/user/[id]+", "/user/42"));
|
|
58
|
+
// // 👉 { id: "42" }
|
|
59
|
+
|
|
60
|
+
// console.log(dynamic_routes_parser("/blog/[...slug]", "/blog/2025/oct/post"));
|
|
61
|
+
// // 👉 { slug: "2025/oct/post" }
|
|
62
|
+
|
|
63
|
+
// console.log(
|
|
64
|
+
// dynamic_routes_parser("/product/[category]/[id]+", "/product/electronics"),
|
|
65
|
+
// );
|
|
66
|
+
// // 👉 { category: "electronics" }
|
|
67
|
+
|
|
68
|
+
// console.log("\n=== FIX TEST ===");
|
|
69
|
+
// console.log(dynamic_routes_parser("/[...slug]/[id]", "/sl1/sl2/9"));
|
|
70
|
+
// // 👉 { slug: "sl1/sl2", id: "9" }
|
|
71
|
+
|
|
72
|
+
// console.log(dynamic_routes_parser("/[slug]/[...id]", "/sl1/id1/id2"));
|
|
73
|
+
// // 👉 { slug: "sl1", id: "id1/id2" }
|
|
74
|
+
|
|
75
|
+
// console.log(dynamic_routes_parser("/blog/lang/[lang]/id/[id]", "/blog/lang/en/id/10"));
|
|
76
|
+
// // 👉 { lang: "en", id: "10" }
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export function get_root(paths) {
|
|
2
|
+
if (paths.length === 0) return '';
|
|
3
|
+
const splitPaths = paths.map(path => path.split('/'));
|
|
4
|
+
const minLength = Math.min(...splitPaths.map(parts => parts.length));
|
|
5
|
+
let commonParts = [];
|
|
6
|
+
for (let i = 0; i < minLength; i++) {
|
|
7
|
+
const part = splitPaths[0][i];
|
|
8
|
+
if (splitPaths.every(parts => parts[i] === part || parts[i].startsWith('['))) {
|
|
9
|
+
commonParts.push(part);
|
|
10
|
+
}
|
|
11
|
+
else break;
|
|
12
|
+
|
|
13
|
+
}
|
|
14
|
+
const root = commonParts.join('/') + (commonParts.length ? '/' : '');
|
|
15
|
+
return root;
|
|
16
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export function normalize_path(inputPath, root = './src/pages', extensions = ['js', 'ts']) {
|
|
2
|
+
if(root.at(-1)==="/") root = root.slice(0, -1)
|
|
3
|
+
const normalizedPath = inputPath.replace(/\\/g, '/')
|
|
4
|
+
// .replace(/\[(\w+)\]/g, '$1/:$1');
|
|
5
|
+
const parts = normalizedPath.split('/');
|
|
6
|
+
const rootParts = root.split('/');
|
|
7
|
+
const rootIndex = parts.indexOf(rootParts[rootParts.length - 1]);
|
|
8
|
+
if (rootIndex !== -1) {
|
|
9
|
+
const subsequentParts = parts.slice(rootIndex + 1);
|
|
10
|
+
const lastPart = subsequentParts[subsequentParts.length - 1];
|
|
11
|
+
const isIndexFile = lastPart === 'index.js' || lastPart === 'index.ts';
|
|
12
|
+
const hasValidExtension = extensions.some(ext => lastPart === `.${ext}` || lastPart.endsWith(`.${ext}`));
|
|
13
|
+
if (isIndexFile) return '/' + (subsequentParts.length > 1 ? subsequentParts.slice(0, -1).join('/') : '');
|
|
14
|
+
if (hasValidExtension) return '/' + subsequentParts.join('/').replace(/\.(js|ts)$/, '');
|
|
15
|
+
}
|
|
16
|
+
return '';
|
|
17
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function is_dynamic(path) {
|
|
2
|
+
const DynamicPattern = /(:\w+|\[\.\.\.\w+\]|\[\w+\]\+?)/;
|
|
3
|
+
return DynamicPattern.test(path);
|
|
4
|
+
}
|
|
5
|
+
export function routes_grouper(routeMap) {
|
|
6
|
+
const grouped = {
|
|
7
|
+
static: {},
|
|
8
|
+
dynamic: {},
|
|
9
|
+
};
|
|
10
|
+
for (const [path, value] of Object.entries(routeMap)) {
|
|
11
|
+
if (is_dynamic(path)) {
|
|
12
|
+
const segments = path.split("/").filter(Boolean);
|
|
13
|
+
const optionalIndex = segments.findIndex(seg => seg.endsWith("]+"));
|
|
14
|
+
const hasInvalidOptional =
|
|
15
|
+
optionalIndex !== -1 && optionalIndex !== segments.length - 1;
|
|
16
|
+
if (hasInvalidOptional) throw new Error(`Invalid optional param position in route: "${path}" — optional parameters can only appear at the end.`);
|
|
17
|
+
grouped.dynamic[path] = value;
|
|
18
|
+
}
|
|
19
|
+
else grouped.static[path] = value;
|
|
20
|
+
}
|
|
21
|
+
return grouped;
|
|
22
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export function routes_matcher(mask, route) {
|
|
2
|
+
const maskSegments = mask.split("/").filter(Boolean);
|
|
3
|
+
const routeSegments = route.split("/").filter(Boolean);
|
|
4
|
+
let i = 0, j = 0;
|
|
5
|
+
while (i < maskSegments.length && j < routeSegments.length) {
|
|
6
|
+
const maskSegment = maskSegments[i];
|
|
7
|
+
const routeSegment = routeSegments[j];
|
|
8
|
+
if (maskSegment.startsWith("[...") && maskSegment.endsWith("]")) {
|
|
9
|
+
const remainingMaskSegments = maskSegments.length - i - 1;
|
|
10
|
+
if (remainingMaskSegments === 0) return true;
|
|
11
|
+
// Calculate minimum required route segments for remaining mask
|
|
12
|
+
let requiredSegments = 0;
|
|
13
|
+
for (let k = i + 1; k < maskSegments.length; k++) {
|
|
14
|
+
if (!maskSegments[k].endsWith("]+")) {
|
|
15
|
+
requiredSegments++;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
const remainingRouteSegments = routeSegments.length - j;
|
|
19
|
+
if (remainingRouteSegments < requiredSegments) return false;
|
|
20
|
+
const segmentsToConsume = remainingRouteSegments - requiredSegments;
|
|
21
|
+
if (segmentsToConsume < 1) return false;
|
|
22
|
+
j += segmentsToConsume;
|
|
23
|
+
i++;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (maskSegment.startsWith("[") && maskSegment.endsWith("]+")) {
|
|
27
|
+
if (routeSegment) j++;
|
|
28
|
+
i++;
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
if (maskSegment.startsWith("[") && maskSegment.endsWith("]")) {
|
|
32
|
+
i++;
|
|
33
|
+
j++;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (maskSegment !== routeSegment) return false;
|
|
37
|
+
i++;
|
|
38
|
+
j++;
|
|
39
|
+
}
|
|
40
|
+
while (i < maskSegments.length) {
|
|
41
|
+
const seg = maskSegments[i];
|
|
42
|
+
if (seg.endsWith("]+")) {
|
|
43
|
+
i++;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
return i === maskSegments.length && j === routeSegments.length;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
// // DEMO
|
|
53
|
+
// console.log("=== EXISTING TESTS ===");
|
|
54
|
+
// console.log(routes_matcher("/user/[id]+", "/user")); // true
|
|
55
|
+
// console.log(routes_matcher("/user/[id]+", "/user/42")); // true
|
|
56
|
+
// console.log(routes_matcher("/blog/[...slug]", "/blog/a/b")); // true
|
|
57
|
+
// console.log(routes_matcher("/blog/[id]", "/blog")); // false
|
|
58
|
+
// console.log(routes_matcher("/product/:id", "/product/99")); // true
|
|
59
|
+
|
|
60
|
+
|