@loaders.gl/graphs 4.5.3 → 5.0.0-alpha.10

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.
Files changed (77) hide show
  1. package/LICENSE +14 -3
  2. package/README.md +122 -10
  3. package/dist/dot-loader-types.d.ts +40 -3
  4. package/dist/dot-loader-types.d.ts.map +1 -1
  5. package/dist/dot-loader-types.js +8 -2
  6. package/dist/dot-loader-types.js.map +1 -1
  7. package/dist/dot-loader.cjs +1449 -108
  8. package/dist/dot-loader.cjs.map +3 -4
  9. package/dist/dot-loader.d.ts +11 -6
  10. package/dist/dot-loader.d.ts.map +1 -1
  11. package/dist/dot-loader.js +3 -3
  12. package/dist/dot-loader.js.map +1 -1
  13. package/dist/dot-types.d.ts +6 -1
  14. package/dist/dot-types.d.ts.map +1 -1
  15. package/dist/gexf-loader-types.d.ts +53 -0
  16. package/dist/gexf-loader-types.d.ts.map +1 -0
  17. package/dist/gexf-loader-types.js +26 -0
  18. package/dist/gexf-loader-types.js.map +1 -0
  19. package/dist/gexf-loader.cjs +1304 -0
  20. package/dist/gexf-loader.cjs.map +6 -0
  21. package/dist/gexf-loader.d.ts +24 -0
  22. package/dist/gexf-loader.d.ts.map +1 -0
  23. package/dist/gexf-loader.js +13 -0
  24. package/dist/gexf-loader.js.map +1 -0
  25. package/dist/gexf-types.d.ts +16 -0
  26. package/dist/gexf-types.d.ts.map +1 -0
  27. package/dist/gexf-types.js +5 -0
  28. package/dist/gexf-types.js.map +1 -0
  29. package/dist/graph-types.d.ts +28 -0
  30. package/dist/graph-types.d.ts.map +1 -1
  31. package/dist/graphml-loader-types.d.ts +39 -3
  32. package/dist/graphml-loader-types.d.ts.map +1 -1
  33. package/dist/graphml-loader-types.js +8 -2
  34. package/dist/graphml-loader-types.js.map +1 -1
  35. package/dist/graphml-loader.cjs +1006 -61
  36. package/dist/graphml-loader.cjs.map +3 -4
  37. package/dist/graphml-loader.d.ts +11 -6
  38. package/dist/graphml-loader.d.ts.map +1 -1
  39. package/dist/graphml-loader.js +3 -3
  40. package/dist/graphml-loader.js.map +1 -1
  41. package/dist/index.cjs +1452 -470
  42. package/dist/index.cjs.map +3 -4
  43. package/dist/index.d.ts +9 -2
  44. package/dist/index.d.ts.map +1 -1
  45. package/dist/index.js +3 -2
  46. package/dist/index.js.map +1 -1
  47. package/dist/lib/build-graph-tables.d.ts +4 -0
  48. package/dist/lib/build-graph-tables.d.ts.map +1 -0
  49. package/dist/lib/build-graph-tables.js +143 -0
  50. package/dist/lib/build-graph-tables.js.map +1 -0
  51. package/dist/lib/parse-dot.d.ts +3 -1
  52. package/dist/lib/parse-dot.d.ts.map +1 -1
  53. package/dist/lib/parse-dot.js +41 -24
  54. package/dist/lib/parse-dot.js.map +1 -1
  55. package/dist/lib/parse-gexf.d.ts +5 -0
  56. package/dist/lib/parse-gexf.d.ts.map +1 -0
  57. package/dist/lib/parse-gexf.js +380 -0
  58. package/dist/lib/parse-gexf.js.map +1 -0
  59. package/dist/lib/parse-graphml.d.ts +2 -1
  60. package/dist/lib/parse-graphml.d.ts.map +1 -1
  61. package/dist/lib/parse-graphml.js +50 -27
  62. package/dist/lib/parse-graphml.js.map +1 -1
  63. package/package.json +15 -6
  64. package/src/dot-loader-types.ts +21 -4
  65. package/src/dot-loader.ts +7 -4
  66. package/src/dot-types.ts +7 -1
  67. package/src/gexf-loader-types.ts +41 -0
  68. package/src/gexf-loader.ts +17 -0
  69. package/src/gexf-types.ts +21 -0
  70. package/src/graph-types.ts +35 -0
  71. package/src/graphml-loader-types.ts +20 -4
  72. package/src/graphml-loader.ts +7 -4
  73. package/src/index.ts +11 -2
  74. package/src/lib/build-graph-tables.ts +208 -0
  75. package/src/lib/parse-dot.ts +47 -24
  76. package/src/lib/parse-gexf.ts +388 -0
  77. package/src/lib/parse-graphml.ts +86 -34
@@ -1,8 +1,18 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __esm = (fn, res, err) => function __init() {
9
+ if (err) throw err[0];
10
+ try {
11
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
12
+ } catch (e) {
13
+ throw err = [e], e;
14
+ }
15
+ };
6
16
  var __export = (target, all) => {
7
17
  for (var name in all)
8
18
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -15,45 +25,458 @@ var __copyProps = (to, from, except, desc) => {
15
25
  }
16
26
  return to;
17
27
  };
28
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
29
+ // If the importer is in node compatibility mode or this is not an ESM
30
+ // file that has been converted to a CommonJS file using a Babel-
31
+ // compatible transform (i.e. "__esModule" has not been set), then set
32
+ // "default" to the CommonJS "module.exports" for node compatibility.
33
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
34
+ mod
35
+ ));
18
36
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
37
 
20
- // dist/graphml-loader.js
21
- var graphml_loader_exports = {};
22
- __export(graphml_loader_exports, {
23
- GraphMLLoaderWithParser: () => GraphMLLoaderWithParser
38
+ // src/graphml-loader-types.ts
39
+ async function preload() {
40
+ const { GraphMLLoaderWithParser: GraphMLLoaderWithParser3 } = await Promise.resolve().then(() => (init_graphml_loader(), graphml_loader_exports));
41
+ return GraphMLLoaderWithParser3;
42
+ }
43
+ var VERSION, GraphMLLoader;
44
+ var init_graphml_loader_types = __esm({
45
+ "src/graphml-loader-types.ts"() {
46
+ "use strict";
47
+ VERSION = typeof __VERSION__ !== "undefined" ? __VERSION__ : "latest";
48
+ GraphMLLoader = {
49
+ name: "GraphML",
50
+ id: "graphml",
51
+ module: "graphs",
52
+ version: VERSION,
53
+ extensions: ["graphml"],
54
+ mimeTypes: ["application/graphml+xml"],
55
+ text: true,
56
+ worker: false,
57
+ dataType: null,
58
+ batchType: null,
59
+ options: { graphml: { shape: "arrow-table" } },
60
+ preload
61
+ };
62
+ }
24
63
  });
25
- module.exports = __toCommonJS(graphml_loader_exports);
26
64
 
27
- // dist/graphml-loader-types.js
28
- var VERSION = true ? "4.5.3" : "latest";
29
- var GraphMLLoader = {
30
- name: "GraphML",
31
- id: "graphml",
32
- module: "graphs",
33
- version: VERSION,
34
- extensions: ["graphml"],
35
- mimeTypes: ["application/graphml+xml"],
36
- text: true,
37
- worker: false,
38
- dataType: null,
39
- batchType: null,
40
- options: {}
41
- };
65
+ // ../schema-utils/src/lib/schema/convert-arrow-schema.ts
66
+ function convertArrowToSchema(arrowSchema) {
67
+ return serializeArrowSchema(arrowSchema);
68
+ }
69
+ function serializeArrowSchema(arrowSchema) {
70
+ return {
71
+ fields: arrowSchema.fields.map((arrowField) => serializeArrowField(arrowField)),
72
+ metadata: serializeArrowMetadata(arrowSchema.metadata)
73
+ };
74
+ }
75
+ function serializeArrowMetadata(arrowMetadata) {
76
+ return Object.fromEntries(arrowMetadata);
77
+ }
78
+ function serializeArrowField(field) {
79
+ return {
80
+ name: field.name,
81
+ type: serializeArrowType(field.type),
82
+ nullable: field.nullable,
83
+ metadata: serializeArrowMetadata(field.metadata)
84
+ };
85
+ }
86
+ function serializeArrowType(arrowType) {
87
+ if (isArrowViewType(arrowType, "BinaryView")) {
88
+ return "binary-view";
89
+ }
90
+ if (isArrowViewType(arrowType, "Utf8View")) {
91
+ return "utf8-view";
92
+ }
93
+ if (isOptionalArrowType(arrowType, "LargeList")) {
94
+ const largeListType = arrowType;
95
+ return {
96
+ type: "large-list",
97
+ children: [serializeArrowField(largeListType.valueField)]
98
+ };
99
+ }
100
+ switch (arrowType.constructor) {
101
+ case arrow.Null:
102
+ return "null";
103
+ case arrow.Binary:
104
+ return "binary";
105
+ case arrow.FixedSizeBinary:
106
+ return {
107
+ type: "fixed-size-binary",
108
+ byteWidth: arrowType.byteWidth
109
+ };
110
+ case arrow.Bool:
111
+ return "bool";
112
+ case arrow.Int:
113
+ const intType = arrowType;
114
+ return `${intType.isSigned ? "" : "u"}int${intType.bitWidth}`;
115
+ case arrow.Int8:
116
+ return "int8";
117
+ case arrow.Int16:
118
+ return "int16";
119
+ case arrow.Int32:
120
+ return "int32";
121
+ case arrow.Int64:
122
+ return "int64";
123
+ case arrow.Uint8:
124
+ return "uint8";
125
+ case arrow.Uint16:
126
+ return "uint16";
127
+ case arrow.Uint32:
128
+ return "uint32";
129
+ case arrow.Uint64:
130
+ return "uint64";
131
+ case arrow.Float:
132
+ const precision = arrowType.precision;
133
+ switch (precision) {
134
+ case arrow.Precision.HALF:
135
+ return "float16";
136
+ case arrow.Precision.SINGLE:
137
+ return "float32";
138
+ case arrow.Precision.DOUBLE:
139
+ return "float64";
140
+ default:
141
+ return "float16";
142
+ }
143
+ case arrow.Float16:
144
+ return "float16";
145
+ case arrow.Float32:
146
+ return "float32";
147
+ case arrow.Float64:
148
+ return "float64";
149
+ case arrow.Utf8:
150
+ return "utf8";
151
+ case arrow.Decimal:
152
+ const decimal = arrowType;
153
+ return {
154
+ type: "decimal",
155
+ bitWidth: decimal.bitWidth,
156
+ precision: decimal.precision,
157
+ scale: decimal.scale
158
+ };
159
+ case arrow.Date_:
160
+ const dateUnit = arrowType.unit;
161
+ return dateUnit === arrow.DateUnit.DAY ? "date-day" : "date-millisecond";
162
+ case arrow.DateDay:
163
+ return "date-day";
164
+ case arrow.DateMillisecond:
165
+ return "date-millisecond";
166
+ case arrow.Time:
167
+ const timeUnit = arrowType.unit;
168
+ switch (timeUnit) {
169
+ case arrow.TimeUnit.SECOND:
170
+ return "time-second";
171
+ case arrow.TimeUnit.MILLISECOND:
172
+ return "time-millisecond";
173
+ case arrow.TimeUnit.MICROSECOND:
174
+ return "time-microsecond";
175
+ case arrow.TimeUnit.NANOSECOND:
176
+ return "time-nanosecond";
177
+ default:
178
+ return "time-second";
179
+ }
180
+ case arrow.TimeMillisecond:
181
+ return "time-millisecond";
182
+ case arrow.TimeSecond:
183
+ return "time-second";
184
+ case arrow.TimeMicrosecond:
185
+ return "time-microsecond";
186
+ case arrow.TimeNanosecond:
187
+ return "time-nanosecond";
188
+ case arrow.Timestamp:
189
+ const timeStampUnit = arrowType.unit;
190
+ switch (timeStampUnit) {
191
+ case arrow.TimeUnit.SECOND:
192
+ return "timestamp-second";
193
+ case arrow.TimeUnit.MILLISECOND:
194
+ return "timestamp-millisecond";
195
+ case arrow.TimeUnit.MICROSECOND:
196
+ return "timestamp-microsecond";
197
+ case arrow.TimeUnit.NANOSECOND:
198
+ return "timestamp-nanosecond";
199
+ default:
200
+ return "timestamp-second";
201
+ }
202
+ case arrow.TimestampSecond:
203
+ return "timestamp-second";
204
+ case arrow.TimestampMillisecond:
205
+ return "timestamp-millisecond";
206
+ case arrow.TimestampMicrosecond:
207
+ return "timestamp-microsecond";
208
+ case arrow.TimestampNanosecond:
209
+ return "timestamp-nanosecond";
210
+ case arrow.Interval:
211
+ const intervalUnit = arrowType.unit;
212
+ switch (intervalUnit) {
213
+ case arrow.IntervalUnit.DAY_TIME:
214
+ return "interval-daytime";
215
+ case arrow.IntervalUnit.YEAR_MONTH:
216
+ return "interval-yearmonth";
217
+ default:
218
+ return "interval-daytime";
219
+ }
220
+ case arrow.IntervalDayTime:
221
+ return "interval-daytime";
222
+ case arrow.IntervalYearMonth:
223
+ return "interval-yearmonth";
224
+ case arrow.Map_:
225
+ const mapType = arrowType;
226
+ return {
227
+ type: "map",
228
+ keysSorted: mapType.keysSorted,
229
+ children: mapType.children.map((arrowField) => serializeArrowField(arrowField))
230
+ };
231
+ case arrow.List:
232
+ const listType = arrowType;
233
+ const listField = listType.valueField;
234
+ return {
235
+ type: "list",
236
+ children: [serializeArrowField(listField)]
237
+ };
238
+ case arrow.FixedSizeList:
239
+ const fixedSizeList = arrowType;
240
+ return {
241
+ type: "fixed-size-list",
242
+ listSize: fixedSizeList.listSize,
243
+ children: [serializeArrowField(fixedSizeList.children[0])]
244
+ };
245
+ case arrow.Struct:
246
+ const structType = arrowType;
247
+ return {
248
+ type: "struct",
249
+ children: structType.children.map((arrowField) => serializeArrowField(arrowField))
250
+ };
251
+ case arrow.SparseUnion:
252
+ case arrow.DenseUnion:
253
+ const unionType = arrowType;
254
+ return {
255
+ type: unionType instanceof arrow.DenseUnion ? "dense-union" : "sparse-union",
256
+ typeIds: unionType.typeIds,
257
+ children: unionType.children.map((arrowField) => serializeArrowField(arrowField)),
258
+ typeIdToChildIndex: { ...unionType.typeIdToChildIndex }
259
+ };
260
+ case arrow.Dictionary:
261
+ const dictionaryType = arrowType;
262
+ return {
263
+ type: "dictionary",
264
+ id: dictionaryType.id,
265
+ indices: serializeArrowDictionaryKeyType(dictionaryType.indices),
266
+ dictionary: serializeArrowType(dictionaryType.dictionary),
267
+ isOrdered: dictionaryType.isOrdered
268
+ };
269
+ default:
270
+ throw new Error(`arrow type not supported: ${arrowType.constructor.name}`);
271
+ }
272
+ }
273
+ function getOptionalArrowConstructor(typeName) {
274
+ return arrow[typeName];
275
+ }
276
+ function isArrowViewType(arrowType, viewTypeName) {
277
+ const constructor = getOptionalArrowConstructor(viewTypeName);
278
+ if (constructor && arrowType instanceof constructor) {
279
+ return true;
280
+ }
281
+ const guards = arrow.DataType;
282
+ const guard = viewTypeName === "BinaryView" ? guards.isBinaryView : guards.isUtf8View;
283
+ return Boolean(guard == null ? void 0 : guard(arrowType));
284
+ }
285
+ function isOptionalArrowType(arrowType, typeName) {
286
+ const constructor = getOptionalArrowConstructor(typeName);
287
+ return Boolean(constructor && arrowType instanceof constructor);
288
+ }
289
+ function serializeArrowDictionaryKeyType(arrowType) {
290
+ if (arrowType instanceof arrow.Int) {
291
+ const prefix = arrowType.isSigned ? "int" : "uint";
292
+ const keyType = `${prefix}${arrowType.bitWidth}`;
293
+ if (isSchemaDictionaryKeyType(keyType)) {
294
+ return keyType;
295
+ }
296
+ }
297
+ switch (arrowType.constructor) {
298
+ case arrow.Int8:
299
+ return "int8";
300
+ case arrow.Int16:
301
+ return "int16";
302
+ case arrow.Int32:
303
+ return "int32";
304
+ case arrow.Uint8:
305
+ return "uint8";
306
+ case arrow.Uint16:
307
+ return "uint16";
308
+ case arrow.Uint32:
309
+ return "uint32";
310
+ default:
311
+ throw new Error(`arrow dictionary index type not supported: ${arrowType.constructor.name}`);
312
+ }
313
+ }
314
+ function isSchemaDictionaryKeyType(keyType) {
315
+ return ["int8", "int16", "int32", "uint8", "uint16", "uint32"].includes(keyType);
316
+ }
317
+ var arrow;
318
+ var init_convert_arrow_schema = __esm({
319
+ "../schema-utils/src/lib/schema/convert-arrow-schema.ts"() {
320
+ "use strict";
321
+ arrow = __toESM(require("apache-arrow"), 1);
322
+ }
323
+ });
42
324
 
43
- // dist/lib/parse-graphml.js
44
- var import_fast_xml_parser = require("fast-xml-parser");
45
- var XML_ATTRIBUTE_PREFIX = "@_";
46
- var XML_TEXT_KEY = "#text";
47
- var graphmlParser = new import_fast_xml_parser.XMLParser({
48
- ignoreAttributes: false,
49
- attributeNamePrefix: XML_ATTRIBUTE_PREFIX,
50
- textNodeName: XML_TEXT_KEY,
51
- trimValues: true,
52
- parseAttributeValue: false,
53
- parseTagValue: false,
54
- removeNSPrefix: true
325
+ // ../schema-utils/src/index.ts
326
+ var init_src = __esm({
327
+ "../schema-utils/src/index.ts"() {
328
+ "use strict";
329
+ init_convert_arrow_schema();
330
+ }
55
331
  });
56
- function parseGraphML(graphml) {
332
+
333
+ // src/lib/build-graph-tables.ts
334
+ function buildGraphTables(nodes, edges, shape, declarations) {
335
+ if (shape === "plain-graph-data") {
336
+ return { shape, nodes: Array.from(nodes), edges: Array.from(edges) };
337
+ }
338
+ if (shape !== "arrow-table" && shape !== "object-row-table") {
339
+ throw new Error(`Unsupported graph shape: ${shape}`);
340
+ }
341
+ const nodeTable = buildGraphTable(nodes, false, shape, declarations == null ? void 0 : declarations.nodes);
342
+ const edgeTable = buildGraphTable(edges, true, shape, declarations == null ? void 0 : declarations.edges);
343
+ return {
344
+ shape: "tables",
345
+ tables: [
346
+ { name: "nodes", table: nodeTable },
347
+ { name: "edges", table: edgeTable }
348
+ ]
349
+ };
350
+ }
351
+ function buildGraphTable(records, isEdge, shape, declarations) {
352
+ const columnNames = isEdge ? ["id", "sourceId", "targetId", "directed", "label", "attributes"] : ["id", "label", "attributes"];
353
+ const columns = new Map(columnNames.map((name) => [name, []]));
354
+ for (const record of records) {
355
+ for (const name of columnNames) {
356
+ const value = record[name];
357
+ columns.get(name).push(
358
+ name === "id" || name === "sourceId" || name === "targetId" ? String(value) : value ?? null
359
+ );
360
+ }
361
+ }
362
+ if (shape === "object-row-table") {
363
+ return {
364
+ shape,
365
+ data: columns.get("id").map(
366
+ (_, index) => Object.fromEntries(columnNames.map((name) => [name, columns.get(name)[index]]))
367
+ )
368
+ };
369
+ }
370
+ const vectors = /* @__PURE__ */ Object.create(null);
371
+ for (const [name, values] of columns) {
372
+ const type = name === "attributes" ? inferAttributeType(values, declarations) : name === "directed" ? new import_apache_arrow.Bool() : new import_apache_arrow.Utf8();
373
+ vectors[name] = (0, import_apache_arrow.vectorFromArray)(
374
+ values.map((value) => normalizeValue(value, type)),
375
+ type
376
+ );
377
+ }
378
+ const data = new import_apache_arrow.Table(vectors);
379
+ return { shape, schema: convertArrowToSchema(data.schema), data };
380
+ }
381
+ function inferAttributeType(values, declarations) {
382
+ const names = new Set(declarations == null ? void 0 : declarations.keys());
383
+ for (const value of values) {
384
+ if (value && typeof value === "object") {
385
+ for (const name of Object.keys(value)) {
386
+ names.add(name);
387
+ }
388
+ }
389
+ }
390
+ return new import_apache_arrow.Struct(
391
+ Array.from(names, (name) => {
392
+ const fieldValues = values.map(
393
+ (value) => (value == null ? void 0 : value[name]) ?? null
394
+ );
395
+ return new import_apache_arrow.Field(name, inferValueType(fieldValues, declarations == null ? void 0 : declarations.get(name)), true);
396
+ })
397
+ );
398
+ }
399
+ function inferValueType(values, declaration) {
400
+ const present = values.filter((value) => value !== null && value !== void 0);
401
+ if (declaration && typeof declaration === "object") {
402
+ if (!present.every((value) => Array.isArray(value))) return new import_apache_arrow.Utf8();
403
+ return new import_apache_arrow.List(
404
+ new import_apache_arrow.Field(
405
+ "item",
406
+ inferValueType(
407
+ present.flatMap((value) => value),
408
+ declaration.list
409
+ ),
410
+ true
411
+ )
412
+ );
413
+ }
414
+ if (declaration) {
415
+ const declaredTypes = {
416
+ boolean: new import_apache_arrow.Bool(),
417
+ int: new import_apache_arrow.Int32(),
418
+ long: new import_apache_arrow.Int64(),
419
+ float: new import_apache_arrow.Float32(),
420
+ double: new import_apache_arrow.Float64(),
421
+ string: new import_apache_arrow.Utf8()
422
+ };
423
+ const valid = present.every((value) => {
424
+ switch (declaration) {
425
+ case "boolean":
426
+ return typeof value === "boolean";
427
+ case "int":
428
+ return typeof value === "number" && Number.isInteger(value) && value >= -2147483648 && value <= 2147483647;
429
+ case "long":
430
+ return typeof value === "bigint" && value >= -(2n ** 63n) && value < 2n ** 63n;
431
+ case "float":
432
+ case "double":
433
+ return typeof value === "number";
434
+ case "string":
435
+ return typeof value === "string";
436
+ }
437
+ });
438
+ return valid ? declaredTypes[declaration] : new import_apache_arrow.Utf8();
439
+ }
440
+ if (present.length && present.every((value) => typeof value === "boolean")) return new import_apache_arrow.Bool();
441
+ if (present.length && present.every((value) => typeof value === "number")) return new import_apache_arrow.Float64();
442
+ if (present.length && present.every((value) => Array.isArray(value))) {
443
+ return new import_apache_arrow.List(
444
+ new import_apache_arrow.Field("item", inferValueType(present.flatMap((value) => value)), true)
445
+ );
446
+ }
447
+ if (present.length && present.every((value) => typeof value === "object" && !Array.isArray(value))) {
448
+ return inferAttributeType(present);
449
+ }
450
+ return new import_apache_arrow.Utf8();
451
+ }
452
+ function normalizeValue(value, type) {
453
+ if (value === null || value === void 0) return null;
454
+ if (type instanceof import_apache_arrow.Struct) {
455
+ return Object.fromEntries(
456
+ type.children.map((field) => [
457
+ field.name,
458
+ normalizeValue(value[field.name], field.type)
459
+ ])
460
+ );
461
+ }
462
+ if (type instanceof import_apache_arrow.List)
463
+ return value.map((item) => normalizeValue(item, type.valueType));
464
+ if (type instanceof import_apache_arrow.Utf8 && typeof value !== "string") {
465
+ return typeof value === "object" ? JSON.stringify(value) : String(value);
466
+ }
467
+ return value;
468
+ }
469
+ var import_apache_arrow;
470
+ var init_build_graph_tables = __esm({
471
+ "src/lib/build-graph-tables.ts"() {
472
+ "use strict";
473
+ import_apache_arrow = require("apache-arrow");
474
+ init_src();
475
+ }
476
+ });
477
+
478
+ // src/lib/parse-graphml.ts
479
+ function parseGraphML(graphml, shape = "plain-graph-data") {
57
480
  const xmlText = decodeGraphML(graphml);
58
481
  const document = graphmlParser.parse(xmlText);
59
482
  const graphmlRoot = getGraphMLRoot(document);
@@ -64,17 +487,38 @@ function parseGraphML(graphml) {
64
487
  if (!graphElement) {
65
488
  throw new Error("GraphML document does not contain a <graph> element.");
66
489
  }
67
- const keyDefinitions = collectKeyDefinitions(graphmlRoot, graphElement);
490
+ const keyDefinitions = collectKeyDefinitions(
491
+ graphmlRoot,
492
+ graphElement,
493
+ shape !== "plain-graph-data"
494
+ );
68
495
  const defaultDirected = parseEdgeDefault(graphElement[`${XML_ATTRIBUTE_PREFIX}edgedefault`]);
69
- const nodes = normalizeArray(graphElement.node).map((node) => parseNode(node, keyDefinitions));
70
- const edges = normalizeArray(graphElement.edge).map((edge, index) => parseEdge(edge, index, keyDefinitions, defaultDirected));
71
- const filteredNodes = nodes.filter((node) => Boolean(node));
72
- const filteredEdges = edges.filter((edge) => Boolean(edge));
73
- return {
74
- shape: "plain-graph-data",
75
- nodes: filteredNodes,
76
- edges: filteredEdges
77
- };
496
+ const declarations = { nodes: /* @__PURE__ */ new Map(), edges: /* @__PURE__ */ new Map() };
497
+ for (const definition of keyDefinitions.values()) {
498
+ if (definition.domain === "all" || definition.domain === "node")
499
+ declarations.nodes.set(definition.name, definition.type);
500
+ if (definition.domain === "all" || definition.domain === "edge")
501
+ declarations.edges.set(definition.name, definition.type);
502
+ }
503
+ return buildGraphTables(
504
+ iterateNodes(graphElement.node, keyDefinitions),
505
+ iterateEdges(graphElement.edge, keyDefinitions, defaultDirected),
506
+ shape,
507
+ declarations
508
+ );
509
+ }
510
+ function* iterateNodes(nodes, definitions) {
511
+ for (const node of normalizeArray(nodes)) {
512
+ const record = parseNode(node, definitions);
513
+ if (record) yield record;
514
+ }
515
+ }
516
+ function* iterateEdges(edges, definitions, directed) {
517
+ let index = 0;
518
+ for (const edge of normalizeArray(edges)) {
519
+ const record = parseEdge(edge, index++, definitions, directed);
520
+ if (record) yield record;
521
+ }
78
522
  }
79
523
  function decodeGraphML(graphml) {
80
524
  if (typeof graphml === "string") {
@@ -97,16 +541,21 @@ function getGraphMLRoot(document) {
97
541
  }
98
542
  function getGraphElement(graphmlRoot) {
99
543
  const graph = graphmlRoot.graph;
544
+ if (typeof graph === "string" && !graph.trim()) {
545
+ return {};
546
+ }
100
547
  if (!graph) {
101
548
  return null;
102
549
  }
103
550
  if (Array.isArray(graph)) {
104
- const firstGraph = graph.find((entry) => isObject(entry));
105
- return isObject(firstGraph) ? firstGraph : null;
551
+ const firstGraph = graph.find(
552
+ (entry) => isObject(entry) || typeof entry === "string" && !entry.trim()
553
+ );
554
+ return isObject(firstGraph) ? firstGraph : typeof firstGraph === "string" ? {} : null;
106
555
  }
107
556
  return isObject(graph) ? graph : null;
108
557
  }
109
- function collectKeyDefinitions(graphmlRoot, graphElement) {
558
+ function collectKeyDefinitions(graphmlRoot, graphElement, lossless) {
110
559
  const keys = /* @__PURE__ */ new Map();
111
560
  for (const candidate of [
112
561
  ...normalizeArray(graphmlRoot.key),
@@ -119,8 +568,8 @@ function collectKeyDefinitions(graphmlRoot, graphElement) {
119
568
  const name = String(candidate[`${XML_ATTRIBUTE_PREFIX}attr.name`] ?? id).trim();
120
569
  const type = normalizeType(candidate[`${XML_ATTRIBUTE_PREFIX}attr.type`]);
121
570
  const defaultNode = candidate.default ?? null;
122
- const defaultValue = defaultNode !== null ? castDataValue(defaultNode, type) : void 0;
123
- keys.set(id, { id, domain, name, type, defaultValue });
571
+ const defaultValue = defaultNode !== null ? castDataValue(defaultNode, type, lossless) : void 0;
572
+ keys.set(id, { id, domain, name, type, defaultValue, lossless });
124
573
  }
125
574
  }
126
575
  }
@@ -198,12 +647,12 @@ function assignAttributeFromDataEntry(entry, keyDefinitions, attributes) {
198
647
  }
199
648
  const definition = keyDefinitions.get(keyId);
200
649
  const attributeName = (definition == null ? void 0 : definition.name) ?? keyId;
201
- const value = castDataValue(entry, (definition == null ? void 0 : definition.type) ?? "string");
650
+ const value = (definition == null ? void 0 : definition.type) === "boolean" && extractTextContent(entry) === void 0 ? false : castDataValue(entry, (definition == null ? void 0 : definition.type) ?? "string", definition == null ? void 0 : definition.lossless);
202
651
  if (value !== void 0) {
203
652
  attributes[attributeName] = value;
204
653
  }
205
654
  }
206
- function castDataValue(value, type) {
655
+ function castDataValue(value, type, lossless = false) {
207
656
  if (value === null || typeof value === "undefined") {
208
657
  return void 0;
209
658
  }
@@ -214,6 +663,9 @@ function castDataValue(value, type) {
214
663
  if (type === "boolean") {
215
664
  return parseBoolean(text);
216
665
  }
666
+ if (type === "long" && lossless) {
667
+ return /^[+-]?\d+$/.test(text.trim()) ? BigInt(text.trim()) : text;
668
+ }
217
669
  if (type === "int" || type === "long") {
218
670
  const parsed = Number.parseInt(text, 10);
219
671
  return Number.isNaN(parsed) ? text : parsed;
@@ -236,12 +688,14 @@ function extractTextContent(value) {
236
688
  }
237
689
  if (isObject(value)) {
238
690
  const text = value[XML_TEXT_KEY];
239
- if (typeof text === "string") {
240
- return text;
241
- }
242
- const nonAttributeEntries = Object.entries(value).filter(([key]) => !key.startsWith(XML_ATTRIBUTE_PREFIX));
691
+ const nonAttributeEntries = Object.entries(value).filter(
692
+ ([key]) => !key.startsWith(XML_ATTRIBUTE_PREFIX) && key !== XML_TEXT_KEY
693
+ );
243
694
  if (nonAttributeEntries.length === 0) {
244
- return void 0;
695
+ return typeof text === "string" ? text : void 0;
696
+ }
697
+ if (typeof text === "string" && text.trim()) {
698
+ nonAttributeEntries.push([XML_TEXT_KEY, text]);
245
699
  }
246
700
  return JSON.stringify(Object.fromEntries(nonAttributeEntries));
247
701
  }
@@ -302,14 +756,505 @@ function parseBoolean(value) {
302
756
  if (normalized === "false" || normalized === "0" || normalized === "no" || normalized === "n") {
303
757
  return false;
304
758
  }
305
- return Boolean(value);
759
+ return Boolean(normalized);
306
760
  }
761
+ var import_fast_xml_parser, XML_ATTRIBUTE_PREFIX, XML_TEXT_KEY, graphmlParser;
762
+ var init_parse_graphml = __esm({
763
+ "src/lib/parse-graphml.ts"() {
764
+ "use strict";
765
+ import_fast_xml_parser = require("fast-xml-parser");
766
+ init_build_graph_tables();
767
+ XML_ATTRIBUTE_PREFIX = "@_";
768
+ XML_TEXT_KEY = "#text";
769
+ graphmlParser = new import_fast_xml_parser.XMLParser({
770
+ ignoreAttributes: false,
771
+ attributeNamePrefix: XML_ATTRIBUTE_PREFIX,
772
+ textNodeName: XML_TEXT_KEY,
773
+ trimValues: false,
774
+ parseAttributeValue: false,
775
+ parseTagValue: false,
776
+ removeNSPrefix: true
777
+ });
778
+ }
779
+ });
780
+
781
+ // src/graphml-loader.ts
782
+ var graphml_loader_exports = {};
783
+ __export(graphml_loader_exports, {
784
+ GraphMLLoaderWithParser: () => GraphMLLoaderWithParser
785
+ });
786
+ var GraphMLLoaderWithParser;
787
+ var init_graphml_loader = __esm({
788
+ "src/graphml-loader.ts"() {
789
+ "use strict";
790
+ init_graphml_loader_types();
791
+ init_parse_graphml();
792
+ GraphMLLoaderWithParser = {
793
+ ...GraphMLLoader,
794
+ parse: async (data, options) => {
795
+ var _a;
796
+ return parseGraphML(data, ((_a = options == null ? void 0 : options.graphml) == null ? void 0 : _a.shape) ?? "arrow-table");
797
+ },
798
+ parseSync: (data, options) => {
799
+ var _a;
800
+ return parseGraphML(data, ((_a = options == null ? void 0 : options.graphml) == null ? void 0 : _a.shape) ?? "arrow-table");
801
+ },
802
+ parseTextSync: (data, options) => {
803
+ var _a;
804
+ return parseGraphML(data, ((_a = options == null ? void 0 : options.graphml) == null ? void 0 : _a.shape) ?? "arrow-table");
805
+ }
806
+ };
807
+ }
808
+ });
307
809
 
308
810
  // dist/graphml-loader.js
309
- var GraphMLLoaderWithParser = {
310
- ...GraphMLLoader,
311
- parse: async (data) => parseGraphML(data),
312
- parseSync: parseGraphML,
313
- parseTextSync: parseGraphML
811
+ var graphml_loader_exports2 = {};
812
+ __export(graphml_loader_exports2, {
813
+ GraphMLLoaderWithParser: () => GraphMLLoaderWithParser2
814
+ });
815
+ module.exports = __toCommonJS(graphml_loader_exports2);
816
+
817
+ // dist/graphml-loader-types.js
818
+ var VERSION2 = true ? "5.0.0-alpha.10" : "latest";
819
+ async function preload2() {
820
+ const { GraphMLLoaderWithParser: GraphMLLoaderWithParser3 } = await Promise.resolve().then(() => (init_graphml_loader(), graphml_loader_exports));
821
+ return GraphMLLoaderWithParser3;
822
+ }
823
+ var GraphMLLoader2 = {
824
+ name: "GraphML",
825
+ id: "graphml",
826
+ module: "graphs",
827
+ version: VERSION2,
828
+ extensions: ["graphml"],
829
+ mimeTypes: ["application/graphml+xml"],
830
+ text: true,
831
+ worker: false,
832
+ dataType: null,
833
+ batchType: null,
834
+ options: { graphml: { shape: "arrow-table" } },
835
+ preload: preload2
836
+ };
837
+
838
+ // dist/lib/parse-graphml.js
839
+ var import_fast_xml_parser2 = require("fast-xml-parser");
840
+
841
+ // dist/lib/build-graph-tables.js
842
+ var import_apache_arrow2 = require("apache-arrow");
843
+ init_src();
844
+ function buildGraphTables2(nodes, edges, shape, declarations) {
845
+ if (shape === "plain-graph-data") {
846
+ return { shape, nodes: Array.from(nodes), edges: Array.from(edges) };
847
+ }
848
+ if (shape !== "arrow-table" && shape !== "object-row-table") {
849
+ throw new Error(`Unsupported graph shape: ${shape}`);
850
+ }
851
+ const nodeTable = buildGraphTable2(nodes, false, shape, declarations == null ? void 0 : declarations.nodes);
852
+ const edgeTable = buildGraphTable2(edges, true, shape, declarations == null ? void 0 : declarations.edges);
853
+ return {
854
+ shape: "tables",
855
+ tables: [
856
+ { name: "nodes", table: nodeTable },
857
+ { name: "edges", table: edgeTable }
858
+ ]
859
+ };
860
+ }
861
+ function buildGraphTable2(records, isEdge, shape, declarations) {
862
+ const columnNames = isEdge ? ["id", "sourceId", "targetId", "directed", "label", "attributes"] : ["id", "label", "attributes"];
863
+ const columns = new Map(columnNames.map((name) => [name, []]));
864
+ for (const record of records) {
865
+ for (const name of columnNames) {
866
+ const value = record[name];
867
+ columns.get(name).push(name === "id" || name === "sourceId" || name === "targetId" ? String(value) : value ?? null);
868
+ }
869
+ }
870
+ if (shape === "object-row-table") {
871
+ return {
872
+ shape,
873
+ data: columns.get("id").map((_, index) => Object.fromEntries(columnNames.map((name) => [name, columns.get(name)[index]])))
874
+ };
875
+ }
876
+ const vectors = /* @__PURE__ */ Object.create(null);
877
+ for (const [name, values] of columns) {
878
+ const type = name === "attributes" ? inferAttributeType2(values, declarations) : name === "directed" ? new import_apache_arrow2.Bool() : new import_apache_arrow2.Utf8();
879
+ vectors[name] = (0, import_apache_arrow2.vectorFromArray)(values.map((value) => normalizeValue2(value, type)), type);
880
+ }
881
+ const data = new import_apache_arrow2.Table(vectors);
882
+ return { shape, schema: convertArrowToSchema(data.schema), data };
883
+ }
884
+ function inferAttributeType2(values, declarations) {
885
+ const names = new Set(declarations == null ? void 0 : declarations.keys());
886
+ for (const value of values) {
887
+ if (value && typeof value === "object") {
888
+ for (const name of Object.keys(value)) {
889
+ names.add(name);
890
+ }
891
+ }
892
+ }
893
+ return new import_apache_arrow2.Struct(Array.from(names, (name) => {
894
+ const fieldValues = values.map((value) => (value == null ? void 0 : value[name]) ?? null);
895
+ return new import_apache_arrow2.Field(name, inferValueType2(fieldValues, declarations == null ? void 0 : declarations.get(name)), true);
896
+ }));
897
+ }
898
+ function inferValueType2(values, declaration) {
899
+ const present = values.filter((value) => value !== null && value !== void 0);
900
+ if (declaration && typeof declaration === "object") {
901
+ if (!present.every((value) => Array.isArray(value)))
902
+ return new import_apache_arrow2.Utf8();
903
+ return new import_apache_arrow2.List(new import_apache_arrow2.Field("item", inferValueType2(present.flatMap((value) => value), declaration.list), true));
904
+ }
905
+ if (declaration) {
906
+ const declaredTypes = {
907
+ boolean: new import_apache_arrow2.Bool(),
908
+ int: new import_apache_arrow2.Int32(),
909
+ long: new import_apache_arrow2.Int64(),
910
+ float: new import_apache_arrow2.Float32(),
911
+ double: new import_apache_arrow2.Float64(),
912
+ string: new import_apache_arrow2.Utf8()
913
+ };
914
+ const valid = present.every((value) => {
915
+ switch (declaration) {
916
+ case "boolean":
917
+ return typeof value === "boolean";
918
+ case "int":
919
+ return typeof value === "number" && Number.isInteger(value) && value >= -2147483648 && value <= 2147483647;
920
+ case "long":
921
+ return typeof value === "bigint" && value >= -(2n ** 63n) && value < 2n ** 63n;
922
+ case "float":
923
+ case "double":
924
+ return typeof value === "number";
925
+ case "string":
926
+ return typeof value === "string";
927
+ }
928
+ });
929
+ return valid ? declaredTypes[declaration] : new import_apache_arrow2.Utf8();
930
+ }
931
+ if (present.length && present.every((value) => typeof value === "boolean"))
932
+ return new import_apache_arrow2.Bool();
933
+ if (present.length && present.every((value) => typeof value === "number"))
934
+ return new import_apache_arrow2.Float64();
935
+ if (present.length && present.every((value) => Array.isArray(value))) {
936
+ return new import_apache_arrow2.List(new import_apache_arrow2.Field("item", inferValueType2(present.flatMap((value) => value)), true));
937
+ }
938
+ if (present.length && present.every((value) => typeof value === "object" && !Array.isArray(value))) {
939
+ return inferAttributeType2(present);
940
+ }
941
+ return new import_apache_arrow2.Utf8();
942
+ }
943
+ function normalizeValue2(value, type) {
944
+ if (value === null || value === void 0)
945
+ return null;
946
+ if (type instanceof import_apache_arrow2.Struct) {
947
+ return Object.fromEntries(type.children.map((field) => [
948
+ field.name,
949
+ normalizeValue2(value[field.name], field.type)
950
+ ]));
951
+ }
952
+ if (type instanceof import_apache_arrow2.List)
953
+ return value.map((item) => normalizeValue2(item, type.valueType));
954
+ if (type instanceof import_apache_arrow2.Utf8 && typeof value !== "string") {
955
+ return typeof value === "object" ? JSON.stringify(value) : String(value);
956
+ }
957
+ return value;
958
+ }
959
+
960
+ // dist/lib/parse-graphml.js
961
+ var XML_ATTRIBUTE_PREFIX2 = "@_";
962
+ var XML_TEXT_KEY2 = "#text";
963
+ var graphmlParser2 = new import_fast_xml_parser2.XMLParser({
964
+ ignoreAttributes: false,
965
+ attributeNamePrefix: XML_ATTRIBUTE_PREFIX2,
966
+ textNodeName: XML_TEXT_KEY2,
967
+ trimValues: false,
968
+ parseAttributeValue: false,
969
+ parseTagValue: false,
970
+ removeNSPrefix: true
971
+ });
972
+ function parseGraphML2(graphml, shape = "plain-graph-data") {
973
+ const xmlText = decodeGraphML2(graphml);
974
+ const document = graphmlParser2.parse(xmlText);
975
+ const graphmlRoot = getGraphMLRoot2(document);
976
+ if (!graphmlRoot) {
977
+ throw new Error("GraphML document does not contain a <graphml> element.");
978
+ }
979
+ const graphElement = getGraphElement2(graphmlRoot);
980
+ if (!graphElement) {
981
+ throw new Error("GraphML document does not contain a <graph> element.");
982
+ }
983
+ const keyDefinitions = collectKeyDefinitions2(graphmlRoot, graphElement, shape !== "plain-graph-data");
984
+ const defaultDirected = parseEdgeDefault2(graphElement[`${XML_ATTRIBUTE_PREFIX2}edgedefault`]);
985
+ const declarations = { nodes: /* @__PURE__ */ new Map(), edges: /* @__PURE__ */ new Map() };
986
+ for (const definition of keyDefinitions.values()) {
987
+ if (definition.domain === "all" || definition.domain === "node")
988
+ declarations.nodes.set(definition.name, definition.type);
989
+ if (definition.domain === "all" || definition.domain === "edge")
990
+ declarations.edges.set(definition.name, definition.type);
991
+ }
992
+ return buildGraphTables2(iterateNodes2(graphElement.node, keyDefinitions), iterateEdges2(graphElement.edge, keyDefinitions, defaultDirected), shape, declarations);
993
+ }
994
+ function* iterateNodes2(nodes, definitions) {
995
+ for (const node of normalizeArray2(nodes)) {
996
+ const record = parseNode2(node, definitions);
997
+ if (record)
998
+ yield record;
999
+ }
1000
+ }
1001
+ function* iterateEdges2(edges, definitions, directed) {
1002
+ let index = 0;
1003
+ for (const edge of normalizeArray2(edges)) {
1004
+ const record = parseEdge2(edge, index++, definitions, directed);
1005
+ if (record)
1006
+ yield record;
1007
+ }
1008
+ }
1009
+ function decodeGraphML2(graphml) {
1010
+ if (typeof graphml === "string") {
1011
+ return graphml;
1012
+ }
1013
+ if (graphml instanceof Uint8Array) {
1014
+ return new TextDecoder().decode(graphml);
1015
+ }
1016
+ if (graphml instanceof ArrayBuffer) {
1017
+ return new TextDecoder().decode(new Uint8Array(graphml));
1018
+ }
1019
+ throw new Error("Unsupported GraphML input. Expected a string, ArrayBuffer, or Uint8Array.");
1020
+ }
1021
+ function getGraphMLRoot2(document) {
1022
+ const root = document.graphml;
1023
+ if (isObject2(root)) {
1024
+ return root;
1025
+ }
1026
+ return null;
1027
+ }
1028
+ function getGraphElement2(graphmlRoot) {
1029
+ const graph = graphmlRoot.graph;
1030
+ if (typeof graph === "string" && !graph.trim()) {
1031
+ return {};
1032
+ }
1033
+ if (!graph) {
1034
+ return null;
1035
+ }
1036
+ if (Array.isArray(graph)) {
1037
+ const firstGraph = graph.find((entry) => isObject2(entry) || typeof entry === "string" && !entry.trim());
1038
+ return isObject2(firstGraph) ? firstGraph : typeof firstGraph === "string" ? {} : null;
1039
+ }
1040
+ return isObject2(graph) ? graph : null;
1041
+ }
1042
+ function collectKeyDefinitions2(graphmlRoot, graphElement, lossless) {
1043
+ const keys = /* @__PURE__ */ new Map();
1044
+ for (const candidate of [
1045
+ ...normalizeArray2(graphmlRoot.key),
1046
+ ...normalizeArray2(graphElement.key)
1047
+ ]) {
1048
+ if (isObject2(candidate)) {
1049
+ const id = String(candidate[`${XML_ATTRIBUTE_PREFIX2}id`] ?? "").trim();
1050
+ if (id) {
1051
+ const domain = normalizeDomain2(candidate[`${XML_ATTRIBUTE_PREFIX2}for`]);
1052
+ const name = String(candidate[`${XML_ATTRIBUTE_PREFIX2}attr.name`] ?? id).trim();
1053
+ const type = normalizeType2(candidate[`${XML_ATTRIBUTE_PREFIX2}attr.type`]);
1054
+ const defaultNode = candidate.default ?? null;
1055
+ const defaultValue = defaultNode !== null ? castDataValue2(defaultNode, type, lossless) : void 0;
1056
+ keys.set(id, { id, domain, name, type, defaultValue, lossless });
1057
+ }
1058
+ }
1059
+ }
1060
+ return keys;
1061
+ }
1062
+ function parseNode2(node, keyDefinitions) {
1063
+ if (!isObject2(node)) {
1064
+ return null;
1065
+ }
1066
+ const id = node[`${XML_ATTRIBUTE_PREFIX2}id`];
1067
+ if (typeof id !== "string") {
1068
+ return null;
1069
+ }
1070
+ const attributes = buildAttributeBag2("node", node.data, keyDefinitions);
1071
+ const graphNode = {
1072
+ id,
1073
+ attributes: Object.keys(attributes).length > 0 ? attributes : void 0
1074
+ };
1075
+ const label = attributes.label;
1076
+ if (typeof label === "string") {
1077
+ graphNode.label = label;
1078
+ }
1079
+ return graphNode;
1080
+ }
1081
+ function parseEdge2(edge, index, keyDefinitions, defaultDirected) {
1082
+ if (!isObject2(edge)) {
1083
+ return null;
1084
+ }
1085
+ const sourceId = edge[`${XML_ATTRIBUTE_PREFIX2}source`];
1086
+ const targetId = edge[`${XML_ATTRIBUTE_PREFIX2}target`];
1087
+ if (typeof sourceId !== "string") {
1088
+ return null;
1089
+ }
1090
+ if (typeof targetId !== "string") {
1091
+ return null;
1092
+ }
1093
+ const rawId = edge[`${XML_ATTRIBUTE_PREFIX2}id`];
1094
+ const id = typeof rawId === "string" ? rawId : `edge-${index}`;
1095
+ const directed = parseDirected2(edge[`${XML_ATTRIBUTE_PREFIX2}directed`], defaultDirected);
1096
+ const attributes = buildAttributeBag2("edge", edge.data, keyDefinitions);
1097
+ const graphEdge = {
1098
+ id,
1099
+ sourceId,
1100
+ targetId,
1101
+ directed,
1102
+ attributes: Object.keys(attributes).length > 0 ? attributes : void 0
1103
+ };
1104
+ const label = attributes.label;
1105
+ if (typeof label === "string") {
1106
+ graphEdge.label = label;
1107
+ }
1108
+ return graphEdge;
1109
+ }
1110
+ function buildAttributeBag2(domain, data, keyDefinitions) {
1111
+ const attributes = /* @__PURE__ */ Object.create(null);
1112
+ for (const key of keyDefinitions.values()) {
1113
+ if (key.domain === "all" || key.domain === domain) {
1114
+ if (key.defaultValue !== void 0) {
1115
+ attributes[key.name] = key.defaultValue;
1116
+ }
1117
+ }
1118
+ }
1119
+ for (const entry of normalizeArray2(data)) {
1120
+ assignAttributeFromDataEntry2(entry, keyDefinitions, attributes);
1121
+ }
1122
+ return attributes;
1123
+ }
1124
+ function assignAttributeFromDataEntry2(entry, keyDefinitions, attributes) {
1125
+ if (!isObject2(entry)) {
1126
+ return;
1127
+ }
1128
+ const keyId = entry[`${XML_ATTRIBUTE_PREFIX2}key`];
1129
+ if (typeof keyId !== "string") {
1130
+ return;
1131
+ }
1132
+ const definition = keyDefinitions.get(keyId);
1133
+ const attributeName = (definition == null ? void 0 : definition.name) ?? keyId;
1134
+ const value = (definition == null ? void 0 : definition.type) === "boolean" && extractTextContent2(entry) === void 0 ? false : castDataValue2(entry, (definition == null ? void 0 : definition.type) ?? "string", definition == null ? void 0 : definition.lossless);
1135
+ if (value !== void 0) {
1136
+ attributes[attributeName] = value;
1137
+ }
1138
+ }
1139
+ function castDataValue2(value, type, lossless = false) {
1140
+ if (value === null || typeof value === "undefined") {
1141
+ return void 0;
1142
+ }
1143
+ const text = extractTextContent2(value);
1144
+ if (text === void 0) {
1145
+ return void 0;
1146
+ }
1147
+ if (type === "boolean") {
1148
+ return parseBoolean2(text);
1149
+ }
1150
+ if (type === "long" && lossless) {
1151
+ return /^[+-]?\d+$/.test(text.trim()) ? BigInt(text.trim()) : text;
1152
+ }
1153
+ if (type === "int" || type === "long") {
1154
+ const parsed = Number.parseInt(text, 10);
1155
+ return Number.isNaN(parsed) ? text : parsed;
1156
+ }
1157
+ if (type === "float" || type === "double") {
1158
+ const parsed = Number.parseFloat(text);
1159
+ return Number.isNaN(parsed) ? text : parsed;
1160
+ }
1161
+ return text;
1162
+ }
1163
+ function extractTextContent2(value) {
1164
+ if (typeof value === "string") {
1165
+ return value;
1166
+ }
1167
+ if (typeof value === "number" || typeof value === "boolean") {
1168
+ return String(value);
1169
+ }
1170
+ if (Array.isArray(value)) {
1171
+ return extractTextContent2(value[0]);
1172
+ }
1173
+ if (isObject2(value)) {
1174
+ const text = value[XML_TEXT_KEY2];
1175
+ const nonAttributeEntries = Object.entries(value).filter(([key]) => !key.startsWith(XML_ATTRIBUTE_PREFIX2) && key !== XML_TEXT_KEY2);
1176
+ if (nonAttributeEntries.length === 0) {
1177
+ return typeof text === "string" ? text : void 0;
1178
+ }
1179
+ if (typeof text === "string" && text.trim()) {
1180
+ nonAttributeEntries.push([XML_TEXT_KEY2, text]);
1181
+ }
1182
+ return JSON.stringify(Object.fromEntries(nonAttributeEntries));
1183
+ }
1184
+ return void 0;
1185
+ }
1186
+ function normalizeArray2(value) {
1187
+ if (!value) {
1188
+ return [];
1189
+ }
1190
+ return Array.isArray(value) ? value : [value];
1191
+ }
1192
+ function isObject2(value) {
1193
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
1194
+ }
1195
+ function normalizeDomain2(value) {
1196
+ if (typeof value !== "string") {
1197
+ return "all";
1198
+ }
1199
+ const lower = value.toLowerCase();
1200
+ if (lower === "node" || lower === "edge" || lower === "graph" || lower === "all") {
1201
+ return lower;
1202
+ }
1203
+ return "all";
1204
+ }
1205
+ function normalizeType2(value) {
1206
+ if (typeof value !== "string") {
1207
+ return "string";
1208
+ }
1209
+ const lower = value.toLowerCase();
1210
+ if (lower === "boolean" || lower === "int" || lower === "long" || lower === "float" || lower === "double") {
1211
+ return lower;
1212
+ }
1213
+ return "string";
1214
+ }
1215
+ function parseEdgeDefault2(value) {
1216
+ if (typeof value !== "string") {
1217
+ return false;
1218
+ }
1219
+ return value.toLowerCase() !== "undirected";
1220
+ }
1221
+ function parseDirected2(value, defaultDirected) {
1222
+ if (typeof value === "string") {
1223
+ const normalized = value.trim().toLowerCase();
1224
+ if (normalized === "true" || normalized === "1" || normalized === "yes" || normalized === "directed") {
1225
+ return true;
1226
+ }
1227
+ if (normalized === "false" || normalized === "0" || normalized === "no" || normalized === "undirected") {
1228
+ return false;
1229
+ }
1230
+ }
1231
+ return defaultDirected;
1232
+ }
1233
+ function parseBoolean2(value) {
1234
+ const normalized = value.trim().toLowerCase();
1235
+ if (normalized === "true" || normalized === "1" || normalized === "yes" || normalized === "y") {
1236
+ return true;
1237
+ }
1238
+ if (normalized === "false" || normalized === "0" || normalized === "no" || normalized === "n") {
1239
+ return false;
1240
+ }
1241
+ return Boolean(normalized);
1242
+ }
1243
+
1244
+ // dist/graphml-loader.js
1245
+ var GraphMLLoaderWithParser2 = {
1246
+ ...GraphMLLoader2,
1247
+ parse: async (data, options) => {
1248
+ var _a;
1249
+ return parseGraphML2(data, ((_a = options == null ? void 0 : options.graphml) == null ? void 0 : _a.shape) ?? "arrow-table");
1250
+ },
1251
+ parseSync: (data, options) => {
1252
+ var _a;
1253
+ return parseGraphML2(data, ((_a = options == null ? void 0 : options.graphml) == null ? void 0 : _a.shape) ?? "arrow-table");
1254
+ },
1255
+ parseTextSync: (data, options) => {
1256
+ var _a;
1257
+ return parseGraphML2(data, ((_a = options == null ? void 0 : options.graphml) == null ? void 0 : _a.shape) ?? "arrow-table");
1258
+ }
314
1259
  };
315
1260
  //# sourceMappingURL=graphml-loader.cjs.map