@bian-womp/spark-graph 0.1.22 → 0.1.24

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
@@ -121,8 +121,8 @@ class Registry {
121
121
  nonTransitive: !!opts?.nonTransitive,
122
122
  });
123
123
  // If both source and target have array variants, add derived array->array coercion
124
- const fromArr = `${fromTypeId}[]`;
125
- const toArr = `${toTypeId}[]`;
124
+ const fromArr = fromTypeId === "base.object" ? fromTypeId : `${fromTypeId}[]`;
125
+ const toArr = toTypeId === "base.object" ? toTypeId : `${toTypeId}[]`;
126
126
  const arrKey = `${fromArr}->${toArr}`;
127
127
  if (this.types.has(fromArr) && this.types.has(toArr)) {
128
128
  if (!this.coercions.has(arrKey) && !this.asyncCoercions.has(arrKey)) {
@@ -147,8 +147,8 @@ class Registry {
147
147
  nonTransitive: !!opts?.nonTransitive,
148
148
  });
149
149
  // If both source and target have array variants, add derived array->array async coercion
150
- const fromArr = `${fromTypeId}[]`;
151
- const toArr = `${toTypeId}[]`;
150
+ const fromArr = fromTypeId === "base.object" ? fromTypeId : `${fromTypeId}[]`;
151
+ const toArr = toTypeId === "base.object" ? toTypeId : `${toTypeId}[]`;
152
152
  const arrKey = `${fromArr}->${toArr}`;
153
153
  if (this.types.has(fromArr) && this.types.has(toArr)) {
154
154
  if (!this.coercions.has(arrKey) && !this.asyncCoercions.has(arrKey)) {
@@ -1950,7 +1950,7 @@ function setupBasicGraphRegistry() {
1950
1950
  return String(v);
1951
1951
  }
1952
1952
  });
1953
- registry.registerCoercion("base.vec3", "base.json", (v) => {
1953
+ registry.registerCoercion("base.vec3", "base.object", (v) => {
1954
1954
  try {
1955
1955
  return v ? JSON.stringify(v) : undefined;
1956
1956
  }
@@ -1958,7 +1958,7 @@ function setupBasicGraphRegistry() {
1958
1958
  return undefined;
1959
1959
  }
1960
1960
  });
1961
- registry.registerCoercion("base.json", "base.vec3", (v) => {
1961
+ registry.registerCoercion("base.object", "base.vec3", (v) => {
1962
1962
  try {
1963
1963
  const result = JSON.parse(v);
1964
1964
  if (result.length === 3 &&
@@ -1973,7 +1973,7 @@ function setupBasicGraphRegistry() {
1973
1973
  });
1974
1974
  // Enums: Math Operation
1975
1975
  registry.registerEnum({
1976
- id: "base.enum:math.operation",
1976
+ id: "enum:base.math.operation",
1977
1977
  options: [
1978
1978
  { value: 0, label: "Add" },
1979
1979
  { value: 1, label: "Subtract" },
@@ -1983,11 +1983,20 @@ function setupBasicGraphRegistry() {
1983
1983
  { value: 5, label: "Max" },
1984
1984
  { value: 6, label: "Modulo" },
1985
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" },
1986
1995
  ],
1987
1996
  });
1988
1997
  // Enums: Compare Operation
1989
1998
  registry.registerEnum({
1990
- id: "base.enum:compare.operation",
1999
+ id: "enum:base.compare.operation",
1991
2000
  options: [
1992
2001
  { value: 0, label: "LessThan" },
1993
2002
  { value: 1, label: "LessThanOrEqual" },
@@ -2112,7 +2121,7 @@ function setupBasicGraphRegistry() {
2112
2121
  id: "base.math",
2113
2122
  categoryId: "compute",
2114
2123
  inputs: {
2115
- Operation: "base.enum:math.operation",
2124
+ Operation: "enum:base.math.operation",
2116
2125
  A: "base.float[]",
2117
2126
  B: "base.float[]",
2118
2127
  },
@@ -2123,8 +2132,34 @@ function setupBasicGraphRegistry() {
2123
2132
  // Gracefully handle missing inputs by treating them as zeros
2124
2133
  const a = ins.A === undefined ? [] : asArray(ins.A);
2125
2134
  const b = ins.B === undefined ? [] : asArray(ins.B);
2126
- const len = Math.max(a.length, b.length);
2127
2135
  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
2162
+ const len = Math.max(a.length, b.length);
2128
2163
  const ops = [
2129
2164
  (x, y) => x + y,
2130
2165
  (x, y) => x - y,
@@ -2149,7 +2184,7 @@ function setupBasicGraphRegistry() {
2149
2184
  id: "base.compare",
2150
2185
  categoryId: "compute",
2151
2186
  inputs: {
2152
- Operation: "base.enum:compare.operation",
2187
+ Operation: "enum:base.compare.operation",
2153
2188
  A: "base.float[]",
2154
2189
  B: "base.float[]",
2155
2190
  },
@@ -2169,6 +2204,122 @@ function setupBasicGraphRegistry() {
2169
2204
  return { Result: a.map((x, i) => fn(Number(x), Number(b[i] ?? 0))) };
2170
2205
  },
2171
2206
  });
2207
+ // Logic
2208
+ registry.registerNode({
2209
+ id: "base.logic",
2210
+ categoryId: "compute",
2211
+ inputs: { Operation: "base.string", A: "base.bool", B: "base.bool" },
2212
+ outputs: { Result: "base.bool" },
2213
+ 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 };
2224
+ },
2225
+ });
2226
+ // Strings
2227
+ registry.registerNode({
2228
+ id: "base.string.length",
2229
+ categoryId: "compute",
2230
+ inputs: { Text: "base.string" },
2231
+ outputs: { Length: "base.float" },
2232
+ impl: (ins) => ({
2233
+ Length: String(ins.Text || "").length,
2234
+ }),
2235
+ });
2236
+ registry.registerNode({
2237
+ id: "base.string.op",
2238
+ categoryId: "compute",
2239
+ inputs: { Op: "base.string", Text: "base.string" },
2240
+ outputs: { Text: "base.string" },
2241
+ impl: (ins) => {
2242
+ const op = String(ins.Op || "trim").toLowerCase();
2243
+ const t = String(ins.Text || "");
2244
+ if (op === "lower")
2245
+ return { Text: t.toLowerCase() };
2246
+ if (op === "upper")
2247
+ return { Text: t.toUpperCase() };
2248
+ return { Text: t.trim() };
2249
+ },
2250
+ });
2251
+ registry.registerNode({
2252
+ id: "base.string.split",
2253
+ categoryId: "compute",
2254
+ inputs: { Text: "base.string", Sep: "base.string" },
2255
+ outputs: { Parts: "base.string[]" },
2256
+ impl: (ins) => {
2257
+ const t = String(ins.Text || "");
2258
+ const sep = String(ins.Sep || ",");
2259
+ return { Parts: t.split(sep) };
2260
+ },
2261
+ });
2262
+ registry.registerNode({
2263
+ id: "base.string.join",
2264
+ categoryId: "compute",
2265
+ inputs: { Parts: "base.string[]", Sep: "base.string" },
2266
+ outputs: { Text: "base.string" },
2267
+ impl: (ins) => {
2268
+ const parts = Array.isArray(ins.Parts) ? ins.Parts.map(String) : [];
2269
+ const sep = String(ins.Sep || "");
2270
+ return { Text: parts.join(sep) };
2271
+ },
2272
+ });
2273
+ registry.registerNode({
2274
+ id: "base.string.replace",
2275
+ categoryId: "compute",
2276
+ inputs: {
2277
+ Text: "base.string",
2278
+ Search: "base.string",
2279
+ Replace: "base.string",
2280
+ },
2281
+ outputs: { Text: "base.string" },
2282
+ impl: (ins) => {
2283
+ const t = String(ins.Text || "");
2284
+ const s = String(ins.Search || "");
2285
+ const r = String(ins.Replace || "");
2286
+ return { Text: t.split(s).join(r) };
2287
+ },
2288
+ });
2289
+ // Arrays (carried as base.object)
2290
+ registry.registerNode({
2291
+ id: "base.array.length",
2292
+ categoryId: "compute",
2293
+ inputs: { Items: "base.object" },
2294
+ outputs: { Length: "base.float" },
2295
+ impl: (ins) => ({
2296
+ Length: Array.isArray(ins.Items) ? ins.Items.length : 0,
2297
+ }),
2298
+ });
2299
+ registry.registerNode({
2300
+ id: "base.array.slice",
2301
+ categoryId: "compute",
2302
+ inputs: { Items: "base.object", Start: "base.float", End: "base.float" },
2303
+ outputs: { Result: "base.object" },
2304
+ impl: (ins) => {
2305
+ const arr = Array.isArray(ins.Items) ? ins.Items : [];
2306
+ const s = Math.trunc(Number(ins.Start ?? 0));
2307
+ const e = Number.isFinite(Number(ins.End))
2308
+ ? Math.trunc(Number(ins.End))
2309
+ : undefined;
2310
+ return { Result: arr.slice(s, e) };
2311
+ },
2312
+ });
2313
+ // Select
2314
+ registry.registerNode({
2315
+ id: "base.select",
2316
+ categoryId: "compute",
2317
+ inputs: { Cond: "base.bool", Then: "base.object", Else: "base.object" },
2318
+ outputs: { Result: "base.object" },
2319
+ impl: (ins) => ({
2320
+ Result: ins.Cond ? ins.Then : ins.Else,
2321
+ }),
2322
+ });
2172
2323
  // Combine XYZ
2173
2324
  registry.registerNode({
2174
2325
  id: "base.compareXYZ",
@@ -2309,7 +2460,7 @@ function setupBasicGraphRegistry() {
2309
2460
  });
2310
2461
  // ------------------------- JSON/object utilities -------------------------
2311
2462
  registry.registerNode({
2312
- id: "base.objectGet",
2463
+ id: "base.object.get",
2313
2464
  categoryId: "compute",
2314
2465
  inputs: { Object: "base.object", Pointers: "base.string[]" },
2315
2466
  outputs: { Values: "base.object" },
@@ -2323,7 +2474,7 @@ function setupBasicGraphRegistry() {
2323
2474
  },
2324
2475
  });
2325
2476
  registry.registerNode({
2326
- id: "base.objectSet",
2477
+ id: "base.object.set",
2327
2478
  categoryId: "compute",
2328
2479
  inputs: {
2329
2480
  Object: "base.object",
@@ -2353,7 +2504,7 @@ function setupBasicGraphRegistry() {
2353
2504
  },
2354
2505
  });
2355
2506
  registry.registerNode({
2356
- id: "base.objectRemove",
2507
+ id: "base.object.remove",
2357
2508
  categoryId: "compute",
2358
2509
  inputs: { Object: "base.object", Pointers: "base.string[]" },
2359
2510
  outputs: { Result: "base.object" },
@@ -2366,7 +2517,7 @@ function setupBasicGraphRegistry() {
2366
2517
  },
2367
2518
  });
2368
2519
  registry.registerNode({
2369
- id: "base.objectMerge",
2520
+ id: "base.object.merge",
2370
2521
  categoryId: "compute",
2371
2522
  inputs: { A: "base.object", B: "base.object" },
2372
2523
  outputs: { Result: "base.object" },
@@ -2375,7 +2526,7 @@ function setupBasicGraphRegistry() {
2375
2526
  }),
2376
2527
  });
2377
2528
  registry.registerNode({
2378
- id: "base.objectKeys",
2529
+ id: "base.object.keys",
2379
2530
  categoryId: "compute",
2380
2531
  inputs: { Object: "base.object" },
2381
2532
  outputs: { Keys: "base.string[]" },
@@ -2390,7 +2541,7 @@ function setupBasicGraphRegistry() {
2390
2541
  },
2391
2542
  });
2392
2543
  registry.registerNode({
2393
- id: "base.objectValues",
2544
+ id: "base.object.values",
2394
2545
  categoryId: "compute",
2395
2546
  inputs: { Object: "base.object" },
2396
2547
  outputs: { Values: "base.object" },
@@ -2405,7 +2556,7 @@ function setupBasicGraphRegistry() {
2405
2556
  },
2406
2557
  });
2407
2558
  registry.registerNode({
2408
- id: "base.objectPatch",
2559
+ id: "base.object.patch",
2409
2560
  categoryId: "compute",
2410
2561
  inputs: { Object: "base.object", Ops: "base.object" },
2411
2562
  outputs: { Result: "base.object" },
@@ -2441,7 +2592,7 @@ function setupBasicGraphRegistry() {
2441
2592
  },
2442
2593
  });
2443
2594
  registry.registerNode({
2444
- id: "base.arrayConcat",
2595
+ id: "base.array.concat",
2445
2596
  categoryId: "compute",
2446
2597
  inputs: { A: "base.object", B: "base.object" },
2447
2598
  outputs: { Result: "base.object" },
@@ -2452,7 +2603,7 @@ function setupBasicGraphRegistry() {
2452
2603
  },
2453
2604
  });
2454
2605
  registry.registerNode({
2455
- id: "base.arrayFlatten",
2606
+ id: "base.array.flatten",
2456
2607
  categoryId: "compute",
2457
2608
  inputs: { Objects: "base.object" },
2458
2609
  outputs: { Result: "base.object" },
@@ -2469,7 +2620,7 @@ function setupBasicGraphRegistry() {
2469
2620
  },
2470
2621
  });
2471
2622
  registry.registerNode({
2472
- id: "base.arraySortBy",
2623
+ id: "base.array.sortBy",
2473
2624
  categoryId: "compute",
2474
2625
  inputs: { Objects: "base.object", Pointers: "base.string[]" },
2475
2626
  outputs: { Result: "base.object" },