Skip to content

Commit a9a5894

Browse files
authored
fix: improve types by defining all methods and removing eval (#15)
1 parent 57b7516 commit a9a5894

File tree

2 files changed

+1290
-139
lines changed

2 files changed

+1290
-139
lines changed

scripts/makeMathMethods.mjs

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/* eslint-disable no-console */
2+
3+
const inplaceOperator = `
4+
/**
5+
* @param {number|SparseMatrix} value
6+
* @returns {this}
7+
*/
8+
%name%(value) {
9+
if (typeof value === 'number') return this.%name%S(value);
10+
return this.%name%M(value);
11+
}`;
12+
13+
const inplaceOperatorScalar = `
14+
/**
15+
* @param {number} value
16+
* @returns {this}
17+
*/
18+
%name%(value) {
19+
this.forEachNonZero((i, j, v) => v %op% value);
20+
return this;
21+
}`;
22+
23+
const inplaceOperatorMatrix = `
24+
/**
25+
* @param {SparseMatrix} matrix
26+
* @returns {this}
27+
*/
28+
%name%(matrix) {
29+
matrix.forEachNonZero((i, j, v) => {
30+
this.set(i, j, this.get(i, j) %op% v);
31+
return v;
32+
});
33+
return this;
34+
}`;
35+
36+
const staticOperator = `
37+
/**
38+
* @param {SparseMatrix} matrix
39+
* @param {number} value
40+
* @returns {SparseMatrix}
41+
*/
42+
static %name%(matrix, value) {
43+
return new SparseMatrix(matrix).%name%S(value);
44+
}`;
45+
46+
const inplaceMethod = `
47+
/**
48+
* @returns {this}
49+
*/
50+
%name%() {
51+
this.forEachNonZero((i, j, v) => %method%(v));
52+
return this;
53+
}`;
54+
55+
const staticMethod = `
56+
/**
57+
* @param {SparseMatrix} matrix
58+
* @returns {SparseMatrix}
59+
*/
60+
static %name%(matrix) {
61+
return new SparseMatrix(matrix).%name%();
62+
}`;
63+
64+
const operators = [
65+
// Arithmetic operators
66+
['+', 'add'],
67+
['-', 'sub', 'subtract'],
68+
['*', 'mul', 'multiply'],
69+
['/', 'div', 'divide'],
70+
['%', 'mod', 'modulus'],
71+
// Bitwise operators
72+
['&', 'and'],
73+
['|', 'or'],
74+
['^', 'xor'],
75+
['<<', 'leftShift'],
76+
['>>', 'signPropagatingRightShift'],
77+
['>>>', 'rightShift', 'zeroFillRightShift'],
78+
];
79+
80+
for (const operator of operators) {
81+
for (let i = 1; i < operator.length; i++) {
82+
console.log(
83+
fillTemplateFunction(inplaceOperator, {
84+
name: operator[i],
85+
op: operator[0],
86+
}),
87+
);
88+
console.log(
89+
fillTemplateFunction(inplaceOperatorScalar, {
90+
name: `${operator[i]}S`,
91+
op: operator[0],
92+
}),
93+
);
94+
console.log(
95+
fillTemplateFunction(inplaceOperatorMatrix, {
96+
name: `${operator[i]}M`,
97+
op: operator[0],
98+
}),
99+
);
100+
101+
console.log(fillTemplateFunction(staticOperator, { name: operator[i] }));
102+
}
103+
}
104+
105+
let methods = [['~', 'not']];
106+
107+
for (const mathMethod of [
108+
'abs',
109+
'acos',
110+
'acosh',
111+
'asin',
112+
'asinh',
113+
'atan',
114+
'atanh',
115+
'cbrt',
116+
'ceil',
117+
'clz32',
118+
'cos',
119+
'cosh',
120+
'exp',
121+
'expm1',
122+
'floor',
123+
'fround',
124+
'log',
125+
'log1p',
126+
'log10',
127+
'log2',
128+
'round',
129+
'sign',
130+
'sin',
131+
'sinh',
132+
'sqrt',
133+
'tan',
134+
'tanh',
135+
'trunc',
136+
]) {
137+
methods.push([`Math.${mathMethod}`, mathMethod]);
138+
}
139+
140+
for (const method of methods) {
141+
for (let i = 1; i < method.length; i++) {
142+
console.log(
143+
fillTemplateFunction(inplaceMethod, {
144+
name: method[i],
145+
method: method[0],
146+
}),
147+
);
148+
console.log(fillTemplateFunction(staticMethod, { name: method[i] }));
149+
}
150+
}
151+
152+
function fillTemplateFunction(template, values) {
153+
for (const i in values) {
154+
template = template.replaceAll(new RegExp(`%${i}%`, 'g'), values[i]);
155+
}
156+
return template;
157+
}

0 commit comments

Comments
 (0)