@bian-womp/spark-graph 0.1.25 → 0.1.26
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/lib/cjs/index.cjs +142 -100
- package/lib/cjs/index.cjs.map +1 -1
- package/lib/cjs/src/misc/base.d.ts.map +1 -1
- package/lib/esm/index.js +142 -100
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/src/misc/base.d.ts.map +1 -1
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../src/misc/base.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,KAAK,EAEV,eAAe,EAEhB,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../src/misc/base.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,KAAK,EAEV,eAAe,EAEhB,MAAM,eAAe,CAAC;AA+IvB,wBAAgB,uBAAuB,IAAI,QAAQ,CAq3BlD;AAED,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,QAAQ,QA+BnD;AAqBD,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,QAAQ,QA6BvD;AAED,wBAAgB,aAAa,CAAC,QAAQ,EAAE,QAAQ,EAAE,GAAG,EAAE,eAAe,6BAUrE"}
|
package/lib/esm/index.js
CHANGED
|
@@ -1778,6 +1778,22 @@ const broadcast = (a, b) => {
|
|
|
1778
1778
|
const len = Math.max(aa.length, bb.length);
|
|
1779
1779
|
return [new Array(len).fill(aa[0] ?? 0), new Array(len).fill(bb[0] ?? 0)];
|
|
1780
1780
|
};
|
|
1781
|
+
const asBoolArray = (v) => Array.isArray(v) ? v.map((x) => Boolean(x)) : [Boolean(v)];
|
|
1782
|
+
const broadcastBool = (a, b) => {
|
|
1783
|
+
const aa = asBoolArray(a);
|
|
1784
|
+
const bb = asBoolArray(b);
|
|
1785
|
+
if (aa.length === bb.length)
|
|
1786
|
+
return [aa, bb];
|
|
1787
|
+
if (aa.length === 1)
|
|
1788
|
+
return [new Array(bb.length).fill(aa[0]), bb];
|
|
1789
|
+
if (bb.length === 1)
|
|
1790
|
+
return [aa, new Array(aa.length).fill(bb[0])];
|
|
1791
|
+
const len = Math.max(aa.length, bb.length);
|
|
1792
|
+
return [
|
|
1793
|
+
new Array(len).fill(aa[0] ?? false),
|
|
1794
|
+
new Array(len).fill(bb[0] ?? false),
|
|
1795
|
+
];
|
|
1796
|
+
};
|
|
1781
1797
|
const clamp = (x, min, max) => Math.min(max, Math.max(min, x));
|
|
1782
1798
|
const lerp = (a, b, t) => a + (b - a) * t;
|
|
1783
1799
|
const lcg = (seed) => {
|
|
@@ -1969,50 +1985,70 @@ function setupBasicGraphRegistry() {
|
|
|
1969
1985
|
return undefined;
|
|
1970
1986
|
}
|
|
1971
1987
|
});
|
|
1988
|
+
const BaseMathOperation = {
|
|
1989
|
+
Add: 0,
|
|
1990
|
+
Subtract: 1,
|
|
1991
|
+
Multiply: 2,
|
|
1992
|
+
Divide: 3,
|
|
1993
|
+
Min: 4,
|
|
1994
|
+
Max: 5,
|
|
1995
|
+
Modulo: 6,
|
|
1996
|
+
Power: 7,
|
|
1997
|
+
Round: 8,
|
|
1998
|
+
Floor: 9,
|
|
1999
|
+
Ceil: 10,
|
|
2000
|
+
Abs: 11,
|
|
2001
|
+
Sum: 12,
|
|
2002
|
+
Avg: 13,
|
|
2003
|
+
MinAll: 14,
|
|
2004
|
+
MaxAll: 15,
|
|
2005
|
+
Sin: 16,
|
|
2006
|
+
Cos: 17,
|
|
2007
|
+
Tan: 18,
|
|
2008
|
+
Asin: 19,
|
|
2009
|
+
Acos: 20,
|
|
2010
|
+
Atan: 21,
|
|
2011
|
+
Sqrt: 22,
|
|
2012
|
+
Exp: 23,
|
|
2013
|
+
Log: 24,
|
|
2014
|
+
};
|
|
1972
2015
|
// Enums: Math Operation
|
|
1973
2016
|
registry.registerEnum({
|
|
1974
2017
|
id: "enum:base.math.operation",
|
|
1975
|
-
options: [
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
{ value: 3, label: "Divide" },
|
|
1980
|
-
{ value: 4, label: "Min" },
|
|
1981
|
-
{ value: 5, label: "Max" },
|
|
1982
|
-
{ value: 6, label: "Modulo" },
|
|
1983
|
-
{ value: 7, label: "Power" },
|
|
1984
|
-
// Unary / aggregate operations on A only
|
|
1985
|
-
{ value: 8, label: "Round" },
|
|
1986
|
-
{ value: 9, label: "Floor" },
|
|
1987
|
-
{ value: 10, label: "Ceil" },
|
|
1988
|
-
{ value: 11, label: "Abs" },
|
|
1989
|
-
{ value: 12, label: "Sum" },
|
|
1990
|
-
{ value: 13, label: "Avg" },
|
|
1991
|
-
{ value: 14, label: "MinAll" },
|
|
1992
|
-
{ value: 15, label: "MaxAll" },
|
|
1993
|
-
// Trig and other unary functions on A
|
|
1994
|
-
{ value: 16, label: "Sin" },
|
|
1995
|
-
{ value: 17, label: "Cos" },
|
|
1996
|
-
{ value: 18, label: "Tan" },
|
|
1997
|
-
{ value: 19, label: "ASin" },
|
|
1998
|
-
{ value: 20, label: "ACos" },
|
|
1999
|
-
{ value: 21, label: "ATan" },
|
|
2000
|
-
{ value: 22, label: "Sqrt" },
|
|
2001
|
-
{ value: 23, label: "Exp" },
|
|
2002
|
-
{ value: 24, label: "Log" },
|
|
2003
|
-
],
|
|
2018
|
+
options: Object.entries(BaseMathOperation).map(([label, value]) => ({
|
|
2019
|
+
value,
|
|
2020
|
+
label,
|
|
2021
|
+
})),
|
|
2004
2022
|
});
|
|
2023
|
+
const BaseCompareOperation = {
|
|
2024
|
+
LessThan: 0,
|
|
2025
|
+
LessThanOrEqual: 1,
|
|
2026
|
+
GreaterThan: 2,
|
|
2027
|
+
GreaterThanOrEqual: 3,
|
|
2028
|
+
Equal: 4,
|
|
2029
|
+
NotEqual: 5,
|
|
2030
|
+
};
|
|
2005
2031
|
// Enums: Compare Operation
|
|
2006
2032
|
registry.registerEnum({
|
|
2007
2033
|
id: "enum:base.compare.operation",
|
|
2008
|
-
options: [
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2034
|
+
options: Object.entries(BaseCompareOperation).map(([label, value]) => ({
|
|
2035
|
+
value,
|
|
2036
|
+
label,
|
|
2037
|
+
})),
|
|
2038
|
+
});
|
|
2039
|
+
const BaseLogicOperation = {
|
|
2040
|
+
Not: 0,
|
|
2041
|
+
And: 1,
|
|
2042
|
+
Or: 2,
|
|
2043
|
+
Xor: 3,
|
|
2044
|
+
};
|
|
2045
|
+
// Enums: Logic Operation
|
|
2046
|
+
registry.registerEnum({
|
|
2047
|
+
id: "enum:base.logic.operation",
|
|
2048
|
+
options: Object.entries(BaseLogicOperation).map(([label, value]) => ({
|
|
2049
|
+
value,
|
|
2050
|
+
label,
|
|
2051
|
+
})),
|
|
2016
2052
|
});
|
|
2017
2053
|
// Number
|
|
2018
2054
|
registry.registerNode({
|
|
@@ -2137,53 +2173,49 @@ function setupBasicGraphRegistry() {
|
|
|
2137
2173
|
// Registry-level defaults: Add by default, A=[1], B=[1]
|
|
2138
2174
|
inputDefaults: { Operation: 0, A: [1], B: [1] },
|
|
2139
2175
|
impl: (ins) => {
|
|
2140
|
-
|
|
2141
|
-
const
|
|
2142
|
-
const b = ins.B === undefined ? [] : asArray(ins.B);
|
|
2176
|
+
const a = asArray(ins.A ?? []);
|
|
2177
|
+
const b = asArray(ins.B ?? []);
|
|
2143
2178
|
const op = Number(ins.Operation ?? 0) | 0;
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
24: (x) => Math.log(x),
|
|
2179
|
+
const unaryByOp = {
|
|
2180
|
+
[BaseMathOperation.Round]: (x) => Math.round(x),
|
|
2181
|
+
[BaseMathOperation.Floor]: (x) => Math.floor(x),
|
|
2182
|
+
[BaseMathOperation.Ceil]: (x) => Math.ceil(x),
|
|
2183
|
+
[BaseMathOperation.Abs]: (x) => Math.abs(x),
|
|
2184
|
+
[BaseMathOperation.Sin]: (x) => Math.sin(x),
|
|
2185
|
+
[BaseMathOperation.Cos]: (x) => Math.cos(x),
|
|
2186
|
+
[BaseMathOperation.Tan]: (x) => Math.tan(x),
|
|
2187
|
+
[BaseMathOperation.Asin]: (x) => Math.asin(x),
|
|
2188
|
+
[BaseMathOperation.Acos]: (x) => Math.acos(x),
|
|
2189
|
+
[BaseMathOperation.Atan]: (x) => Math.atan(x),
|
|
2190
|
+
[BaseMathOperation.Sqrt]: (x) => Math.sqrt(x),
|
|
2191
|
+
[BaseMathOperation.Exp]: (x) => Math.exp(x),
|
|
2192
|
+
[BaseMathOperation.Log]: (x) => Math.log(x),
|
|
2159
2193
|
};
|
|
2160
|
-
if (op
|
|
2161
|
-
return { Result: a.map((x) =>
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
13: (arr) => {
|
|
2194
|
+
if (unaryByOp[op])
|
|
2195
|
+
return { Result: a.map((x) => unaryByOp[op](Number(x))) };
|
|
2196
|
+
const aggregateByOp = {
|
|
2197
|
+
[BaseMathOperation.Sum]: (arr) => arr.reduce((s, x) => s + Number(x || 0), 0),
|
|
2198
|
+
[BaseMathOperation.Avg]: (arr) => {
|
|
2166
2199
|
const sum = arr.reduce((s, x) => s + Number(x || 0), 0);
|
|
2167
2200
|
return arr.length ? sum / arr.length : 0;
|
|
2168
2201
|
},
|
|
2169
|
-
|
|
2170
|
-
|
|
2202
|
+
[BaseMathOperation.MinAll]: (arr) => arr.length ? Math.min(...arr.map((x) => Number(x))) : 0,
|
|
2203
|
+
[BaseMathOperation.MaxAll]: (arr) => arr.length ? Math.max(...arr.map((x) => Number(x))) : 0,
|
|
2171
2204
|
};
|
|
2172
|
-
if (op
|
|
2173
|
-
return { Result: [
|
|
2174
|
-
|
|
2205
|
+
if (aggregateByOp[op] !== undefined)
|
|
2206
|
+
return { Result: [aggregateByOp[op](a)] };
|
|
2207
|
+
const binaryByOp = {
|
|
2208
|
+
[BaseMathOperation.Add]: (x, y) => x + y,
|
|
2209
|
+
[BaseMathOperation.Subtract]: (x, y) => x - y,
|
|
2210
|
+
[BaseMathOperation.Multiply]: (x, y) => x * y,
|
|
2211
|
+
[BaseMathOperation.Divide]: (x, y) => x / (y || 1),
|
|
2212
|
+
[BaseMathOperation.Min]: (x, y) => Math.min(x, y),
|
|
2213
|
+
[BaseMathOperation.Max]: (x, y) => Math.max(x, y),
|
|
2214
|
+
[BaseMathOperation.Modulo]: (x, y) => (y ? x % y : 0),
|
|
2215
|
+
[BaseMathOperation.Power]: (x, y) => Math.pow(x, y),
|
|
2216
|
+
};
|
|
2217
|
+
const fn = binaryByOp[op] || binaryByOp[BaseMathOperation.Add];
|
|
2175
2218
|
const len = Math.max(a.length, b.length);
|
|
2176
|
-
const ops = [
|
|
2177
|
-
(x, y) => x + y,
|
|
2178
|
-
(x, y) => x - y,
|
|
2179
|
-
(x, y) => x * y,
|
|
2180
|
-
(x, y) => x / (y || 1),
|
|
2181
|
-
(x, y) => Math.min(x, y),
|
|
2182
|
-
(x, y) => Math.max(x, y),
|
|
2183
|
-
(x, y) => (y ? x % y : 0),
|
|
2184
|
-
(x, y) => Math.pow(x, y),
|
|
2185
|
-
];
|
|
2186
|
-
const fn = ops[op] ?? ops[0];
|
|
2187
2219
|
const out = new Array(len).fill(0).map((_, i) => {
|
|
2188
2220
|
const ax = a.length === 1 && len > 1 ? a[0] : a[i] ?? 0;
|
|
2189
2221
|
const bx = b.length === 1 && len > 1 ? b[0] : b[i] ?? 0;
|
|
@@ -2204,16 +2236,16 @@ function setupBasicGraphRegistry() {
|
|
|
2204
2236
|
outputs: { Result: "base.bool[]" },
|
|
2205
2237
|
impl: (ins) => {
|
|
2206
2238
|
const [a, b] = broadcast(ins.A, ins.B);
|
|
2207
|
-
const op = Number(ins.Operation ??
|
|
2208
|
-
const
|
|
2209
|
-
(x, y) => x < y,
|
|
2210
|
-
(x, y) => x <= y,
|
|
2211
|
-
(x, y) => x > y,
|
|
2212
|
-
(x, y) => x >= y,
|
|
2213
|
-
(x, y) => x === y,
|
|
2214
|
-
(x, y) => x !== y,
|
|
2215
|
-
|
|
2216
|
-
const fn =
|
|
2239
|
+
const op = Number(ins.Operation ?? BaseCompareOperation.Equal) | 0;
|
|
2240
|
+
const compareByOp = {
|
|
2241
|
+
[BaseCompareOperation.LessThan]: (x, y) => x < y,
|
|
2242
|
+
[BaseCompareOperation.LessThanOrEqual]: (x, y) => x <= y,
|
|
2243
|
+
[BaseCompareOperation.GreaterThan]: (x, y) => x > y,
|
|
2244
|
+
[BaseCompareOperation.GreaterThanOrEqual]: (x, y) => x >= y,
|
|
2245
|
+
[BaseCompareOperation.Equal]: (x, y) => x === y,
|
|
2246
|
+
[BaseCompareOperation.NotEqual]: (x, y) => x !== y,
|
|
2247
|
+
};
|
|
2248
|
+
const fn = compareByOp[op] || compareByOp[BaseCompareOperation.Equal];
|
|
2217
2249
|
return { Result: a.map((x, i) => fn(Number(x), Number(b[i] ?? 0))) };
|
|
2218
2250
|
},
|
|
2219
2251
|
});
|
|
@@ -2221,19 +2253,29 @@ function setupBasicGraphRegistry() {
|
|
|
2221
2253
|
registry.registerNode({
|
|
2222
2254
|
id: "base.logic",
|
|
2223
2255
|
categoryId: "compute",
|
|
2224
|
-
inputs: {
|
|
2225
|
-
|
|
2256
|
+
inputs: {
|
|
2257
|
+
Operation: "enum:base.logic.operation",
|
|
2258
|
+
A: "base.bool[]",
|
|
2259
|
+
B: "base.bool[]",
|
|
2260
|
+
},
|
|
2261
|
+
outputs: { Result: "base.bool[]" },
|
|
2262
|
+
inputDefaults: { Operation: BaseLogicOperation.Not },
|
|
2226
2263
|
impl: (ins) => {
|
|
2227
|
-
const op =
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2264
|
+
const op = Number(ins.Operation ?? BaseLogicOperation.Not) | 0;
|
|
2265
|
+
if (op === BaseLogicOperation.Not) {
|
|
2266
|
+
const a = asBoolArray(ins.A ?? []);
|
|
2267
|
+
return { Result: a.map((x) => !x) };
|
|
2268
|
+
}
|
|
2269
|
+
const [a, b] = broadcastBool(ins.A ?? [], ins.B ?? []);
|
|
2270
|
+
const logicByOp = {
|
|
2271
|
+
[BaseLogicOperation.And]: (x, y) => x && y,
|
|
2272
|
+
[BaseLogicOperation.Or]: (x, y) => x || y,
|
|
2273
|
+
[BaseLogicOperation.Xor]: (x, y) => Boolean(x ? !y : y),
|
|
2274
|
+
};
|
|
2275
|
+
const fn = logicByOp[op] || logicByOp[BaseLogicOperation.And];
|
|
2276
|
+
return {
|
|
2277
|
+
Result: a.map((x, i) => fn(Boolean(x), Boolean(b[i] ?? false))),
|
|
2278
|
+
};
|
|
2237
2279
|
},
|
|
2238
2280
|
});
|
|
2239
2281
|
// Strings
|
|
@@ -2492,7 +2534,7 @@ function setupBasicGraphRegistry() {
|
|
|
2492
2534
|
inputs: {
|
|
2493
2535
|
Object: "base.object",
|
|
2494
2536
|
Pointers: "base.string[]",
|
|
2495
|
-
NewValues: "base.
|
|
2537
|
+
NewValues: "base.object",
|
|
2496
2538
|
},
|
|
2497
2539
|
outputs: { Result: "base.object" },
|
|
2498
2540
|
impl: (ins) => {
|