@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.
@@ -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;AA2HvB,wBAAgB,uBAAuB,IAAI,QAAQ,CAg1BlD;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"}
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
- { value: 0, label: "Add" },
1977
- { value: 1, label: "Subtract" },
1978
- { value: 2, label: "Multiply" },
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
- { value: 0, label: "LessThan" },
2010
- { value: 1, label: "LessThanOrEqual" },
2011
- { value: 2, label: "GreaterThan" },
2012
- { value: 3, label: "GreaterThanOrEqual" },
2013
- { value: 4, label: "Equal" },
2014
- { value: 5, label: "NotEqual" },
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
- // Gracefully handle missing inputs by treating them as zeros
2141
- const a = ins.A === undefined ? [] : asArray(ins.A);
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
- // Unary ops on A
2145
- const unary = {
2146
- 8: (x) => Math.round(x),
2147
- 9: (x) => Math.floor(x),
2148
- 10: (x) => Math.ceil(x),
2149
- 11: (x) => Math.abs(x),
2150
- 16: (x) => Math.sin(x),
2151
- 17: (x) => Math.cos(x),
2152
- 18: (x) => Math.tan(x),
2153
- 19: (x) => Math.asin(x),
2154
- 20: (x) => Math.acos(x),
2155
- 21: (x) => Math.atan(x),
2156
- 22: (x) => Math.sqrt(x),
2157
- 23: (x) => Math.exp(x),
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 in unary)
2161
- return { Result: a.map((x) => unary[op](Number(x))) };
2162
- // Aggregate ops on A -> single-element array
2163
- const aggregates = {
2164
- 12: (arr) => arr.reduce((s, x) => s + Number(x || 0), 0),
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
- 14: (arr) => (arr.length ? Math.min(...arr.map((x) => Number(x))) : 0),
2170
- 15: (arr) => (arr.length ? Math.max(...arr.map((x) => Number(x))) : 0),
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 in aggregates)
2173
- return { Result: [aggregates[op](a)] };
2174
- // Binary ops A (broadcast) B
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 ?? 4) | 0; // default Equal
2208
- const ops = [
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 = ops[op] ?? ops[4];
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: { Operation: "base.string", A: "base.bool", B: "base.bool" },
2225
- outputs: { Result: "base.bool" },
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 = String(ins.Operation || "not").toLowerCase();
2228
- const a = !!ins.A;
2229
- const b = !!ins.B;
2230
- if (op === "not")
2231
- return { Result: !a };
2232
- if (op === "and")
2233
- return { Result: a && b };
2234
- if (op === "or")
2235
- return { Result: a || b };
2236
- return { Result: false };
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.string[]",
2537
+ NewValues: "base.object",
2496
2538
  },
2497
2539
  outputs: { Result: "base.object" },
2498
2540
  impl: (ins) => {