@bian-womp/spark-graph 0.1.25 → 0.1.27

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,50 +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
- // Trig and other unary functions on A
1996
- { value: 16, label: "Sin" },
1997
- { value: 17, label: "Cos" },
1998
- { value: 18, label: "Tan" },
1999
- { value: 19, label: "ASin" },
2000
- { value: 20, label: "ACos" },
2001
- { value: 21, label: "ATan" },
2002
- { value: 22, label: "Sqrt" },
2003
- { value: 23, label: "Exp" },
2004
- { value: 24, label: "Log" },
2005
- ],
2020
+ options: Object.entries(BaseMathOperation).map(([label, value]) => ({
2021
+ value,
2022
+ label,
2023
+ })),
2006
2024
  });
2025
+ const BaseCompareOperation = {
2026
+ LessThan: 0,
2027
+ LessThanOrEqual: 1,
2028
+ GreaterThan: 2,
2029
+ GreaterThanOrEqual: 3,
2030
+ Equal: 4,
2031
+ NotEqual: 5,
2032
+ };
2007
2033
  // Enums: Compare Operation
2008
2034
  registry.registerEnum({
2009
2035
  id: "enum:base.compare.operation",
2010
- options: [
2011
- { value: 0, label: "LessThan" },
2012
- { value: 1, label: "LessThanOrEqual" },
2013
- { value: 2, label: "GreaterThan" },
2014
- { value: 3, label: "GreaterThanOrEqual" },
2015
- { value: 4, label: "Equal" },
2016
- { value: 5, label: "NotEqual" },
2017
- ],
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
+ })),
2018
2054
  });
2019
2055
  // Number
2020
2056
  registry.registerNode({
@@ -2139,53 +2175,49 @@ function setupBasicGraphRegistry() {
2139
2175
  // Registry-level defaults: Add by default, A=[1], B=[1]
2140
2176
  inputDefaults: { Operation: 0, A: [1], B: [1] },
2141
2177
  impl: (ins) => {
2142
- // Gracefully handle missing inputs by treating them as zeros
2143
- const a = ins.A === undefined ? [] : asArray(ins.A);
2144
- const b = ins.B === undefined ? [] : asArray(ins.B);
2178
+ const a = asArray(ins.A ?? []);
2179
+ const b = asArray(ins.B ?? []);
2145
2180
  const op = Number(ins.Operation ?? 0) | 0;
2146
- // Unary ops on A
2147
- const unary = {
2148
- 8: (x) => Math.round(x),
2149
- 9: (x) => Math.floor(x),
2150
- 10: (x) => Math.ceil(x),
2151
- 11: (x) => Math.abs(x),
2152
- 16: (x) => Math.sin(x),
2153
- 17: (x) => Math.cos(x),
2154
- 18: (x) => Math.tan(x),
2155
- 19: (x) => Math.asin(x),
2156
- 20: (x) => Math.acos(x),
2157
- 21: (x) => Math.atan(x),
2158
- 22: (x) => Math.sqrt(x),
2159
- 23: (x) => Math.exp(x),
2160
- 24: (x) => Math.log(x),
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),
2161
2195
  };
2162
- if (op in unary)
2163
- return { Result: a.map((x) => unary[op](Number(x))) };
2164
- // Aggregate ops on A -> single-element array
2165
- const aggregates = {
2166
- 12: (arr) => arr.reduce((s, x) => s + Number(x || 0), 0),
2167
- 13: (arr) => {
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) => {
2168
2201
  const sum = arr.reduce((s, x) => s + Number(x || 0), 0);
2169
2202
  return arr.length ? sum / arr.length : 0;
2170
2203
  },
2171
- 14: (arr) => (arr.length ? Math.min(...arr.map((x) => Number(x))) : 0),
2172
- 15: (arr) => (arr.length ? Math.max(...arr.map((x) => Number(x))) : 0),
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,
2173
2206
  };
2174
- if (op in aggregates)
2175
- return { Result: [aggregates[op](a)] };
2176
- // Binary ops A (broadcast) B
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];
2177
2220
  const len = Math.max(a.length, b.length);
2178
- const ops = [
2179
- (x, y) => x + y,
2180
- (x, y) => x - y,
2181
- (x, y) => x * y,
2182
- (x, y) => x / (y || 1),
2183
- (x, y) => Math.min(x, y),
2184
- (x, y) => Math.max(x, y),
2185
- (x, y) => (y ? x % y : 0),
2186
- (x, y) => Math.pow(x, y),
2187
- ];
2188
- const fn = ops[op] ?? ops[0];
2189
2221
  const out = new Array(len).fill(0).map((_, i) => {
2190
2222
  const ax = a.length === 1 && len > 1 ? a[0] : a[i] ?? 0;
2191
2223
  const bx = b.length === 1 && len > 1 ? b[0] : b[i] ?? 0;
@@ -2206,16 +2238,16 @@ function setupBasicGraphRegistry() {
2206
2238
  outputs: { Result: "base.bool[]" },
2207
2239
  impl: (ins) => {
2208
2240
  const [a, b] = broadcast(ins.A, ins.B);
2209
- const op = Number(ins.Operation ?? 4) | 0; // default Equal
2210
- const ops = [
2211
- (x, y) => x < y,
2212
- (x, y) => x <= y,
2213
- (x, y) => x > y,
2214
- (x, y) => x >= y,
2215
- (x, y) => x === y,
2216
- (x, y) => x !== y,
2217
- ];
2218
- 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];
2219
2251
  return { Result: a.map((x, i) => fn(Number(x), Number(b[i] ?? 0))) };
2220
2252
  },
2221
2253
  });
@@ -2223,19 +2255,29 @@ function setupBasicGraphRegistry() {
2223
2255
  registry.registerNode({
2224
2256
  id: "base.logic",
2225
2257
  categoryId: "compute",
2226
- inputs: { Operation: "base.string", A: "base.bool", B: "base.bool" },
2227
- 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 },
2228
2265
  impl: (ins) => {
2229
- const op = String(ins.Operation || "not").toLowerCase();
2230
- const a = !!ins.A;
2231
- const b = !!ins.B;
2232
- if (op === "not")
2233
- return { Result: !a };
2234
- if (op === "and")
2235
- return { Result: a && b };
2236
- if (op === "or")
2237
- return { Result: a || b };
2238
- 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
+ };
2239
2281
  },
2240
2282
  });
2241
2283
  // Strings
@@ -2494,7 +2536,7 @@ function setupBasicGraphRegistry() {
2494
2536
  inputs: {
2495
2537
  Object: "base.object",
2496
2538
  Pointers: "base.string[]",
2497
- NewValues: "base.string[]",
2539
+ NewValues: "base.object",
2498
2540
  },
2499
2541
  outputs: { Result: "base.object" },
2500
2542
  impl: (ins) => {