@bian-womp/spark-graph 0.1.24 → 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 CHANGED
@@ -1780,6 +1780,22 @@ const broadcast = (a, b) => {
1780
1780
  const len = Math.max(aa.length, bb.length);
1781
1781
  return [new Array(len).fill(aa[0] ?? 0), new Array(len).fill(bb[0] ?? 0)];
1782
1782
  };
1783
+ const asBoolArray = (v) => Array.isArray(v) ? v.map((x) => Boolean(x)) : [Boolean(v)];
1784
+ const broadcastBool = (a, b) => {
1785
+ const aa = asBoolArray(a);
1786
+ const bb = asBoolArray(b);
1787
+ if (aa.length === bb.length)
1788
+ return [aa, bb];
1789
+ if (aa.length === 1)
1790
+ return [new Array(bb.length).fill(aa[0]), bb];
1791
+ if (bb.length === 1)
1792
+ return [aa, new Array(aa.length).fill(bb[0])];
1793
+ const len = Math.max(aa.length, bb.length);
1794
+ return [
1795
+ new Array(len).fill(aa[0] ?? false),
1796
+ new Array(len).fill(bb[0] ?? false),
1797
+ ];
1798
+ };
1783
1799
  const clamp = (x, min, max) => Math.min(max, Math.max(min, x));
1784
1800
  const lerp = (a, b, t) => a + (b - a) * t;
1785
1801
  const lcg = (seed) => {
@@ -1971,40 +1987,70 @@ function setupBasicGraphRegistry() {
1971
1987
  return undefined;
1972
1988
  }
1973
1989
  });
1990
+ const BaseMathOperation = {
1991
+ Add: 0,
1992
+ Subtract: 1,
1993
+ Multiply: 2,
1994
+ Divide: 3,
1995
+ Min: 4,
1996
+ Max: 5,
1997
+ Modulo: 6,
1998
+ Power: 7,
1999
+ Round: 8,
2000
+ Floor: 9,
2001
+ Ceil: 10,
2002
+ Abs: 11,
2003
+ Sum: 12,
2004
+ Avg: 13,
2005
+ MinAll: 14,
2006
+ MaxAll: 15,
2007
+ Sin: 16,
2008
+ Cos: 17,
2009
+ Tan: 18,
2010
+ Asin: 19,
2011
+ Acos: 20,
2012
+ Atan: 21,
2013
+ Sqrt: 22,
2014
+ Exp: 23,
2015
+ Log: 24,
2016
+ };
1974
2017
  // Enums: Math Operation
1975
2018
  registry.registerEnum({
1976
2019
  id: "enum:base.math.operation",
1977
- options: [
1978
- { value: 0, label: "Add" },
1979
- { value: 1, label: "Subtract" },
1980
- { value: 2, label: "Multiply" },
1981
- { value: 3, label: "Divide" },
1982
- { value: 4, label: "Min" },
1983
- { value: 5, label: "Max" },
1984
- { value: 6, label: "Modulo" },
1985
- { value: 7, label: "Power" },
1986
- // Unary / aggregate operations on A only
1987
- { value: 8, label: "Round" },
1988
- { value: 9, label: "Floor" },
1989
- { value: 10, label: "Ceil" },
1990
- { value: 11, label: "Abs" },
1991
- { value: 12, label: "Sum" },
1992
- { value: 13, label: "Avg" },
1993
- { value: 14, label: "MinAll" },
1994
- { value: 15, label: "MaxAll" },
1995
- ],
2020
+ options: Object.entries(BaseMathOperation).map(([label, value]) => ({
2021
+ value,
2022
+ label,
2023
+ })),
1996
2024
  });
2025
+ const BaseCompareOperation = {
2026
+ LessThan: 0,
2027
+ LessThanOrEqual: 1,
2028
+ GreaterThan: 2,
2029
+ GreaterThanOrEqual: 3,
2030
+ Equal: 4,
2031
+ NotEqual: 5,
2032
+ };
1997
2033
  // Enums: Compare Operation
1998
2034
  registry.registerEnum({
1999
2035
  id: "enum:base.compare.operation",
2000
- options: [
2001
- { value: 0, label: "LessThan" },
2002
- { value: 1, label: "LessThanOrEqual" },
2003
- { value: 2, label: "GreaterThan" },
2004
- { value: 3, label: "GreaterThanOrEqual" },
2005
- { value: 4, label: "Equal" },
2006
- { value: 5, label: "NotEqual" },
2007
- ],
2036
+ options: Object.entries(BaseCompareOperation).map(([label, value]) => ({
2037
+ value,
2038
+ label,
2039
+ })),
2040
+ });
2041
+ const BaseLogicOperation = {
2042
+ Not: 0,
2043
+ And: 1,
2044
+ Or: 2,
2045
+ Xor: 3,
2046
+ };
2047
+ // Enums: Logic Operation
2048
+ registry.registerEnum({
2049
+ id: "enum:base.logic.operation",
2050
+ options: Object.entries(BaseLogicOperation).map(([label, value]) => ({
2051
+ value,
2052
+ label,
2053
+ })),
2008
2054
  });
2009
2055
  // Number
2010
2056
  registry.registerNode({
@@ -2129,48 +2175,49 @@ function setupBasicGraphRegistry() {
2129
2175
  // Registry-level defaults: Add by default, A=[1], B=[1]
2130
2176
  inputDefaults: { Operation: 0, A: [1], B: [1] },
2131
2177
  impl: (ins) => {
2132
- // Gracefully handle missing inputs by treating them as zeros
2133
- const a = ins.A === undefined ? [] : asArray(ins.A);
2134
- const b = ins.B === undefined ? [] : asArray(ins.B);
2178
+ const a = asArray(ins.A ?? []);
2179
+ const b = asArray(ins.B ?? []);
2135
2180
  const op = Number(ins.Operation ?? 0) | 0;
2136
- // Unary ops on A
2137
- if (op === 8)
2138
- return { Result: a.map((x) => Math.round(Number(x))) };
2139
- if (op === 9)
2140
- return { Result: a.map((x) => Math.floor(Number(x))) };
2141
- if (op === 10)
2142
- return { Result: a.map((x) => Math.ceil(Number(x))) };
2143
- if (op === 11)
2144
- return { Result: a.map((x) => Math.abs(Number(x))) };
2145
- // Aggregate ops on A -> single-element array
2146
- if (op === 12)
2147
- return { Result: [a.reduce((s, x) => s + Number(x || 0), 0)] };
2148
- if (op === 13) {
2149
- const sum = a.reduce((s, x) => s + Number(x || 0), 0);
2150
- const avg = a.length ? sum / a.length : 0;
2151
- return { Result: [avg] };
2152
- }
2153
- if (op === 14)
2154
- return {
2155
- Result: [a.length ? Math.min(...a.map((x) => Number(x))) : 0],
2156
- };
2157
- if (op === 15)
2158
- return {
2159
- Result: [a.length ? Math.max(...a.map((x) => Number(x))) : 0],
2160
- };
2161
- // Binary ops A (broadcast) B
2181
+ const unaryByOp = {
2182
+ [BaseMathOperation.Round]: (x) => Math.round(x),
2183
+ [BaseMathOperation.Floor]: (x) => Math.floor(x),
2184
+ [BaseMathOperation.Ceil]: (x) => Math.ceil(x),
2185
+ [BaseMathOperation.Abs]: (x) => Math.abs(x),
2186
+ [BaseMathOperation.Sin]: (x) => Math.sin(x),
2187
+ [BaseMathOperation.Cos]: (x) => Math.cos(x),
2188
+ [BaseMathOperation.Tan]: (x) => Math.tan(x),
2189
+ [BaseMathOperation.Asin]: (x) => Math.asin(x),
2190
+ [BaseMathOperation.Acos]: (x) => Math.acos(x),
2191
+ [BaseMathOperation.Atan]: (x) => Math.atan(x),
2192
+ [BaseMathOperation.Sqrt]: (x) => Math.sqrt(x),
2193
+ [BaseMathOperation.Exp]: (x) => Math.exp(x),
2194
+ [BaseMathOperation.Log]: (x) => Math.log(x),
2195
+ };
2196
+ if (unaryByOp[op])
2197
+ return { Result: a.map((x) => unaryByOp[op](Number(x))) };
2198
+ const aggregateByOp = {
2199
+ [BaseMathOperation.Sum]: (arr) => arr.reduce((s, x) => s + Number(x || 0), 0),
2200
+ [BaseMathOperation.Avg]: (arr) => {
2201
+ const sum = arr.reduce((s, x) => s + Number(x || 0), 0);
2202
+ return arr.length ? sum / arr.length : 0;
2203
+ },
2204
+ [BaseMathOperation.MinAll]: (arr) => arr.length ? Math.min(...arr.map((x) => Number(x))) : 0,
2205
+ [BaseMathOperation.MaxAll]: (arr) => arr.length ? Math.max(...arr.map((x) => Number(x))) : 0,
2206
+ };
2207
+ if (aggregateByOp[op] !== undefined)
2208
+ return { Result: [aggregateByOp[op](a)] };
2209
+ const binaryByOp = {
2210
+ [BaseMathOperation.Add]: (x, y) => x + y,
2211
+ [BaseMathOperation.Subtract]: (x, y) => x - y,
2212
+ [BaseMathOperation.Multiply]: (x, y) => x * y,
2213
+ [BaseMathOperation.Divide]: (x, y) => x / (y || 1),
2214
+ [BaseMathOperation.Min]: (x, y) => Math.min(x, y),
2215
+ [BaseMathOperation.Max]: (x, y) => Math.max(x, y),
2216
+ [BaseMathOperation.Modulo]: (x, y) => (y ? x % y : 0),
2217
+ [BaseMathOperation.Power]: (x, y) => Math.pow(x, y),
2218
+ };
2219
+ const fn = binaryByOp[op] || binaryByOp[BaseMathOperation.Add];
2162
2220
  const len = Math.max(a.length, b.length);
2163
- const ops = [
2164
- (x, y) => x + y,
2165
- (x, y) => x - y,
2166
- (x, y) => x * y,
2167
- (x, y) => x / (y || 1),
2168
- (x, y) => Math.min(x, y),
2169
- (x, y) => Math.max(x, y),
2170
- (x, y) => (y ? x % y : 0),
2171
- (x, y) => Math.pow(x, y),
2172
- ];
2173
- const fn = ops[op] ?? ops[0];
2174
2221
  const out = new Array(len).fill(0).map((_, i) => {
2175
2222
  const ax = a.length === 1 && len > 1 ? a[0] : a[i] ?? 0;
2176
2223
  const bx = b.length === 1 && len > 1 ? b[0] : b[i] ?? 0;
@@ -2191,16 +2238,16 @@ function setupBasicGraphRegistry() {
2191
2238
  outputs: { Result: "base.bool[]" },
2192
2239
  impl: (ins) => {
2193
2240
  const [a, b] = broadcast(ins.A, ins.B);
2194
- const op = Number(ins.Operation ?? 4) | 0; // default Equal
2195
- const ops = [
2196
- (x, y) => x < y,
2197
- (x, y) => x <= y,
2198
- (x, y) => x > y,
2199
- (x, y) => x >= y,
2200
- (x, y) => x === y,
2201
- (x, y) => x !== y,
2202
- ];
2203
- const fn = ops[op] ?? ops[4];
2241
+ const op = Number(ins.Operation ?? BaseCompareOperation.Equal) | 0;
2242
+ const compareByOp = {
2243
+ [BaseCompareOperation.LessThan]: (x, y) => x < y,
2244
+ [BaseCompareOperation.LessThanOrEqual]: (x, y) => x <= y,
2245
+ [BaseCompareOperation.GreaterThan]: (x, y) => x > y,
2246
+ [BaseCompareOperation.GreaterThanOrEqual]: (x, y) => x >= y,
2247
+ [BaseCompareOperation.Equal]: (x, y) => x === y,
2248
+ [BaseCompareOperation.NotEqual]: (x, y) => x !== y,
2249
+ };
2250
+ const fn = compareByOp[op] || compareByOp[BaseCompareOperation.Equal];
2204
2251
  return { Result: a.map((x, i) => fn(Number(x), Number(b[i] ?? 0))) };
2205
2252
  },
2206
2253
  });
@@ -2208,19 +2255,29 @@ function setupBasicGraphRegistry() {
2208
2255
  registry.registerNode({
2209
2256
  id: "base.logic",
2210
2257
  categoryId: "compute",
2211
- inputs: { Operation: "base.string", A: "base.bool", B: "base.bool" },
2212
- outputs: { Result: "base.bool" },
2258
+ inputs: {
2259
+ Operation: "enum:base.logic.operation",
2260
+ A: "base.bool[]",
2261
+ B: "base.bool[]",
2262
+ },
2263
+ outputs: { Result: "base.bool[]" },
2264
+ inputDefaults: { Operation: BaseLogicOperation.Not },
2213
2265
  impl: (ins) => {
2214
- const op = String(ins.Operation || "not").toLowerCase();
2215
- const a = !!ins.A;
2216
- const b = !!ins.B;
2217
- if (op === "not")
2218
- return { Result: !a };
2219
- if (op === "and")
2220
- return { Result: a && b };
2221
- if (op === "or")
2222
- return { Result: a || b };
2223
- return { Result: false };
2266
+ const op = Number(ins.Operation ?? BaseLogicOperation.Not) | 0;
2267
+ if (op === BaseLogicOperation.Not) {
2268
+ const a = asBoolArray(ins.A ?? []);
2269
+ return { Result: a.map((x) => !x) };
2270
+ }
2271
+ const [a, b] = broadcastBool(ins.A ?? [], ins.B ?? []);
2272
+ const logicByOp = {
2273
+ [BaseLogicOperation.And]: (x, y) => x && y,
2274
+ [BaseLogicOperation.Or]: (x, y) => x || y,
2275
+ [BaseLogicOperation.Xor]: (x, y) => Boolean(x ? !y : y),
2276
+ };
2277
+ const fn = logicByOp[op] || logicByOp[BaseLogicOperation.And];
2278
+ return {
2279
+ Result: a.map((x, i) => fn(Boolean(x), Boolean(b[i] ?? false))),
2280
+ };
2224
2281
  },
2225
2282
  });
2226
2283
  // Strings
@@ -2479,7 +2536,7 @@ function setupBasicGraphRegistry() {
2479
2536
  inputs: {
2480
2537
  Object: "base.object",
2481
2538
  Pointers: "base.string[]",
2482
- NewValues: "base.string[]",
2539
+ NewValues: "base.object",
2483
2540
  },
2484
2541
  outputs: { Result: "base.object" },
2485
2542
  impl: (ins) => {