@loaders.gl/graphs 5.0.0-alpha.9 → 5.0.0-beta.1
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/LICENSE +24 -0
- package/README.md +121 -4
- package/THIRD-PARTY-NOTICES.md +17 -0
- package/dist/dot-loader-types.d.ts +25 -8
- package/dist/dot-loader-types.d.ts.map +1 -1
- package/dist/dot-loader-types.js +2 -2
- package/dist/dot-loader-types.js.map +1 -1
- package/dist/dot-loader.cjs +638 -47
- package/dist/dot-loader.cjs.map +3 -3
- package/dist/dot-loader.d.ts +10 -6
- package/dist/dot-loader.d.ts.map +1 -1
- package/dist/dot-loader.js +3 -3
- package/dist/dot-loader.js.map +1 -1
- package/dist/dot-types.d.ts +6 -1
- package/dist/dot-types.d.ts.map +1 -1
- package/dist/gexf-loader-types.d.ts +53 -0
- package/dist/gexf-loader-types.d.ts.map +1 -0
- package/dist/gexf-loader-types.js +26 -0
- package/dist/gexf-loader-types.js.map +1 -0
- package/dist/gexf-loader.cjs +1304 -0
- package/dist/gexf-loader.cjs.map +6 -0
- package/dist/gexf-loader.d.ts +24 -0
- package/dist/gexf-loader.d.ts.map +1 -0
- package/dist/gexf-loader.js +13 -0
- package/dist/gexf-loader.js.map +1 -0
- package/dist/gexf-types.d.ts +16 -0
- package/dist/gexf-types.d.ts.map +1 -0
- package/dist/gexf-types.js +5 -0
- package/dist/gexf-types.js.map +1 -0
- package/dist/graph-types.d.ts +28 -0
- package/dist/graph-types.d.ts.map +1 -1
- package/dist/graphml-loader-types.d.ts +24 -8
- package/dist/graphml-loader-types.d.ts.map +1 -1
- package/dist/graphml-loader-types.js +2 -2
- package/dist/graphml-loader-types.js.map +1 -1
- package/dist/graphml-loader.cjs +670 -60
- package/dist/graphml-loader.cjs.map +3 -3
- package/dist/graphml-loader.d.ts +10 -6
- package/dist/graphml-loader.d.ts.map +1 -1
- package/dist/graphml-loader.js +3 -3
- package/dist/graphml-loader.js.map +1 -1
- package/dist/index.cjs +927 -59
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/build-graph-tables.d.ts +4 -0
- package/dist/lib/build-graph-tables.d.ts.map +1 -0
- package/dist/lib/build-graph-tables.js +143 -0
- package/dist/lib/build-graph-tables.js.map +1 -0
- package/dist/lib/parse-dot.d.ts +3 -1
- package/dist/lib/parse-dot.d.ts.map +1 -1
- package/dist/lib/parse-dot.js +38 -21
- package/dist/lib/parse-dot.js.map +1 -1
- package/dist/lib/parse-gexf.d.ts +5 -0
- package/dist/lib/parse-gexf.d.ts.map +1 -0
- package/dist/lib/parse-gexf.js +380 -0
- package/dist/lib/parse-gexf.js.map +1 -0
- package/dist/lib/parse-graphml.d.ts +2 -1
- package/dist/lib/parse-graphml.d.ts.map +1 -1
- package/dist/lib/parse-graphml.js +50 -27
- package/dist/lib/parse-graphml.js.map +1 -1
- package/package.json +13 -5
- package/src/dot-loader-types.ts +15 -5
- package/src/dot-loader.ts +7 -4
- package/src/dot-types.ts +7 -1
- package/src/gexf-loader-types.ts +41 -0
- package/src/gexf-loader.ts +17 -0
- package/src/gexf-types.ts +21 -0
- package/src/graph-types.ts +35 -0
- package/src/graphml-loader-types.ts +14 -5
- package/src/graphml-loader.ts +7 -4
- package/src/index.ts +9 -0
- package/src/lib/build-graph-tables.ts +208 -0
- package/src/lib/parse-dot.ts +44 -21
- package/src/lib/parse-gexf.ts +388 -0
- package/src/lib/parse-graphml.ts +86 -34
package/dist/graphml-loader.cjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
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;
|
|
6
8
|
var __esm = (fn, res, err) => function __init() {
|
|
7
9
|
if (err) throw err[0];
|
|
@@ -23,6 +25,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
23
25
|
}
|
|
24
26
|
return to;
|
|
25
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
|
+
));
|
|
26
36
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
37
|
|
|
28
38
|
// src/graphml-loader-types.ts
|
|
@@ -46,14 +56,427 @@ var init_graphml_loader_types = __esm({
|
|
|
46
56
|
worker: false,
|
|
47
57
|
dataType: null,
|
|
48
58
|
batchType: null,
|
|
49
|
-
options: {},
|
|
59
|
+
options: { graphml: { shape: "arrow-table" } },
|
|
50
60
|
preload
|
|
51
61
|
};
|
|
52
62
|
}
|
|
53
63
|
});
|
|
54
64
|
|
|
65
|
+
// ../core/src/schema-utils/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
|
+
"../core/src/schema-utils/lib/schema/convert-arrow-schema.ts"() {
|
|
320
|
+
"use strict";
|
|
321
|
+
arrow = __toESM(require("apache-arrow"), 1);
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
// ../core/src/schema-utils/index.ts
|
|
326
|
+
var init_schema_utils = __esm({
|
|
327
|
+
"../core/src/schema-utils/index.ts"() {
|
|
328
|
+
"use strict";
|
|
329
|
+
init_convert_arrow_schema();
|
|
330
|
+
}
|
|
331
|
+
});
|
|
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_schema_utils();
|
|
475
|
+
}
|
|
476
|
+
});
|
|
477
|
+
|
|
55
478
|
// src/lib/parse-graphml.ts
|
|
56
|
-
function parseGraphML(graphml) {
|
|
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,19 +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(
|
|
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
|
|
70
|
-
const
|
|
71
|
-
(
|
|
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
|
|
72
508
|
);
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
+
}
|
|
80
522
|
}
|
|
81
523
|
function decodeGraphML(graphml) {
|
|
82
524
|
if (typeof graphml === "string") {
|
|
@@ -99,16 +541,21 @@ function getGraphMLRoot(document) {
|
|
|
99
541
|
}
|
|
100
542
|
function getGraphElement(graphmlRoot) {
|
|
101
543
|
const graph = graphmlRoot.graph;
|
|
544
|
+
if (typeof graph === "string" && !graph.trim()) {
|
|
545
|
+
return {};
|
|
546
|
+
}
|
|
102
547
|
if (!graph) {
|
|
103
548
|
return null;
|
|
104
549
|
}
|
|
105
550
|
if (Array.isArray(graph)) {
|
|
106
|
-
const firstGraph = graph.find(
|
|
107
|
-
|
|
551
|
+
const firstGraph = graph.find(
|
|
552
|
+
(entry) => isObject(entry) || typeof entry === "string" && !entry.trim()
|
|
553
|
+
);
|
|
554
|
+
return isObject(firstGraph) ? firstGraph : typeof firstGraph === "string" ? {} : null;
|
|
108
555
|
}
|
|
109
556
|
return isObject(graph) ? graph : null;
|
|
110
557
|
}
|
|
111
|
-
function collectKeyDefinitions(graphmlRoot, graphElement) {
|
|
558
|
+
function collectKeyDefinitions(graphmlRoot, graphElement, lossless) {
|
|
112
559
|
const keys = /* @__PURE__ */ new Map();
|
|
113
560
|
for (const candidate of [
|
|
114
561
|
...normalizeArray(graphmlRoot.key),
|
|
@@ -121,8 +568,8 @@ function collectKeyDefinitions(graphmlRoot, graphElement) {
|
|
|
121
568
|
const name = String(candidate[`${XML_ATTRIBUTE_PREFIX}attr.name`] ?? id).trim();
|
|
122
569
|
const type = normalizeType(candidate[`${XML_ATTRIBUTE_PREFIX}attr.type`]);
|
|
123
570
|
const defaultNode = candidate.default ?? null;
|
|
124
|
-
const defaultValue = defaultNode !== null ? castDataValue(defaultNode, type) : void 0;
|
|
125
|
-
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 });
|
|
126
573
|
}
|
|
127
574
|
}
|
|
128
575
|
}
|
|
@@ -200,12 +647,12 @@ function assignAttributeFromDataEntry(entry, keyDefinitions, attributes) {
|
|
|
200
647
|
}
|
|
201
648
|
const definition = keyDefinitions.get(keyId);
|
|
202
649
|
const attributeName = (definition == null ? void 0 : definition.name) ?? keyId;
|
|
203
|
-
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);
|
|
204
651
|
if (value !== void 0) {
|
|
205
652
|
attributes[attributeName] = value;
|
|
206
653
|
}
|
|
207
654
|
}
|
|
208
|
-
function castDataValue(value, type) {
|
|
655
|
+
function castDataValue(value, type, lossless = false) {
|
|
209
656
|
if (value === null || typeof value === "undefined") {
|
|
210
657
|
return void 0;
|
|
211
658
|
}
|
|
@@ -216,6 +663,9 @@ function castDataValue(value, type) {
|
|
|
216
663
|
if (type === "boolean") {
|
|
217
664
|
return parseBoolean(text);
|
|
218
665
|
}
|
|
666
|
+
if (type === "long" && lossless) {
|
|
667
|
+
return /^[+-]?\d+$/.test(text.trim()) ? BigInt(text.trim()) : text;
|
|
668
|
+
}
|
|
219
669
|
if (type === "int" || type === "long") {
|
|
220
670
|
const parsed = Number.parseInt(text, 10);
|
|
221
671
|
return Number.isNaN(parsed) ? text : parsed;
|
|
@@ -238,14 +688,14 @@ function extractTextContent(value) {
|
|
|
238
688
|
}
|
|
239
689
|
if (isObject(value)) {
|
|
240
690
|
const text = value[XML_TEXT_KEY];
|
|
241
|
-
if (typeof text === "string") {
|
|
242
|
-
return text;
|
|
243
|
-
}
|
|
244
691
|
const nonAttributeEntries = Object.entries(value).filter(
|
|
245
|
-
([key]) => !key.startsWith(XML_ATTRIBUTE_PREFIX)
|
|
692
|
+
([key]) => !key.startsWith(XML_ATTRIBUTE_PREFIX) && key !== XML_TEXT_KEY
|
|
246
693
|
);
|
|
247
694
|
if (nonAttributeEntries.length === 0) {
|
|
248
|
-
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]);
|
|
249
699
|
}
|
|
250
700
|
return JSON.stringify(Object.fromEntries(nonAttributeEntries));
|
|
251
701
|
}
|
|
@@ -306,20 +756,21 @@ function parseBoolean(value) {
|
|
|
306
756
|
if (normalized === "false" || normalized === "0" || normalized === "no" || normalized === "n") {
|
|
307
757
|
return false;
|
|
308
758
|
}
|
|
309
|
-
return Boolean(
|
|
759
|
+
return Boolean(normalized);
|
|
310
760
|
}
|
|
311
761
|
var import_fast_xml_parser, XML_ATTRIBUTE_PREFIX, XML_TEXT_KEY, graphmlParser;
|
|
312
762
|
var init_parse_graphml = __esm({
|
|
313
763
|
"src/lib/parse-graphml.ts"() {
|
|
314
764
|
"use strict";
|
|
315
765
|
import_fast_xml_parser = require("fast-xml-parser");
|
|
766
|
+
init_build_graph_tables();
|
|
316
767
|
XML_ATTRIBUTE_PREFIX = "@_";
|
|
317
768
|
XML_TEXT_KEY = "#text";
|
|
318
769
|
graphmlParser = new import_fast_xml_parser.XMLParser({
|
|
319
770
|
ignoreAttributes: false,
|
|
320
771
|
attributeNamePrefix: XML_ATTRIBUTE_PREFIX,
|
|
321
772
|
textNodeName: XML_TEXT_KEY,
|
|
322
|
-
trimValues:
|
|
773
|
+
trimValues: false,
|
|
323
774
|
parseAttributeValue: false,
|
|
324
775
|
parseTagValue: false,
|
|
325
776
|
removeNSPrefix: true
|
|
@@ -340,9 +791,18 @@ var init_graphml_loader = __esm({
|
|
|
340
791
|
init_parse_graphml();
|
|
341
792
|
GraphMLLoaderWithParser = {
|
|
342
793
|
...GraphMLLoader,
|
|
343
|
-
parse: async (data) =>
|
|
344
|
-
|
|
345
|
-
|
|
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
|
+
}
|
|
346
806
|
};
|
|
347
807
|
}
|
|
348
808
|
});
|
|
@@ -355,7 +815,7 @@ __export(graphml_loader_exports2, {
|
|
|
355
815
|
module.exports = __toCommonJS(graphml_loader_exports2);
|
|
356
816
|
|
|
357
817
|
// dist/graphml-loader-types.js
|
|
358
|
-
var VERSION2 = true ? "5.0.0-
|
|
818
|
+
var VERSION2 = true ? "5.0.0-beta.1" : "latest";
|
|
359
819
|
async function preload2() {
|
|
360
820
|
const { GraphMLLoaderWithParser: GraphMLLoaderWithParser3 } = await Promise.resolve().then(() => (init_graphml_loader(), graphml_loader_exports));
|
|
361
821
|
return GraphMLLoaderWithParser3;
|
|
@@ -371,24 +831,145 @@ var GraphMLLoader2 = {
|
|
|
371
831
|
worker: false,
|
|
372
832
|
dataType: null,
|
|
373
833
|
batchType: null,
|
|
374
|
-
options: {},
|
|
834
|
+
options: { graphml: { shape: "arrow-table" } },
|
|
375
835
|
preload: preload2
|
|
376
836
|
};
|
|
377
837
|
|
|
378
838
|
// dist/lib/parse-graphml.js
|
|
379
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_schema_utils();
|
|
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
|
|
380
961
|
var XML_ATTRIBUTE_PREFIX2 = "@_";
|
|
381
962
|
var XML_TEXT_KEY2 = "#text";
|
|
382
963
|
var graphmlParser2 = new import_fast_xml_parser2.XMLParser({
|
|
383
964
|
ignoreAttributes: false,
|
|
384
965
|
attributeNamePrefix: XML_ATTRIBUTE_PREFIX2,
|
|
385
966
|
textNodeName: XML_TEXT_KEY2,
|
|
386
|
-
trimValues:
|
|
967
|
+
trimValues: false,
|
|
387
968
|
parseAttributeValue: false,
|
|
388
969
|
parseTagValue: false,
|
|
389
970
|
removeNSPrefix: true
|
|
390
971
|
});
|
|
391
|
-
function parseGraphML2(graphml) {
|
|
972
|
+
function parseGraphML2(graphml, shape = "plain-graph-data") {
|
|
392
973
|
const xmlText = decodeGraphML2(graphml);
|
|
393
974
|
const document = graphmlParser2.parse(xmlText);
|
|
394
975
|
const graphmlRoot = getGraphMLRoot2(document);
|
|
@@ -399,17 +980,31 @@ function parseGraphML2(graphml) {
|
|
|
399
980
|
if (!graphElement) {
|
|
400
981
|
throw new Error("GraphML document does not contain a <graph> element.");
|
|
401
982
|
}
|
|
402
|
-
const keyDefinitions = collectKeyDefinitions2(graphmlRoot, graphElement);
|
|
983
|
+
const keyDefinitions = collectKeyDefinitions2(graphmlRoot, graphElement, shape !== "plain-graph-data");
|
|
403
984
|
const defaultDirected = parseEdgeDefault2(graphElement[`${XML_ATTRIBUTE_PREFIX2}edgedefault`]);
|
|
404
|
-
const
|
|
405
|
-
const
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
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
|
+
}
|
|
413
1008
|
}
|
|
414
1009
|
function decodeGraphML2(graphml) {
|
|
415
1010
|
if (typeof graphml === "string") {
|
|
@@ -432,16 +1027,19 @@ function getGraphMLRoot2(document) {
|
|
|
432
1027
|
}
|
|
433
1028
|
function getGraphElement2(graphmlRoot) {
|
|
434
1029
|
const graph = graphmlRoot.graph;
|
|
1030
|
+
if (typeof graph === "string" && !graph.trim()) {
|
|
1031
|
+
return {};
|
|
1032
|
+
}
|
|
435
1033
|
if (!graph) {
|
|
436
1034
|
return null;
|
|
437
1035
|
}
|
|
438
1036
|
if (Array.isArray(graph)) {
|
|
439
|
-
const firstGraph = graph.find((entry) => isObject2(entry));
|
|
440
|
-
return isObject2(firstGraph) ? firstGraph : null;
|
|
1037
|
+
const firstGraph = graph.find((entry) => isObject2(entry) || typeof entry === "string" && !entry.trim());
|
|
1038
|
+
return isObject2(firstGraph) ? firstGraph : typeof firstGraph === "string" ? {} : null;
|
|
441
1039
|
}
|
|
442
1040
|
return isObject2(graph) ? graph : null;
|
|
443
1041
|
}
|
|
444
|
-
function collectKeyDefinitions2(graphmlRoot, graphElement) {
|
|
1042
|
+
function collectKeyDefinitions2(graphmlRoot, graphElement, lossless) {
|
|
445
1043
|
const keys = /* @__PURE__ */ new Map();
|
|
446
1044
|
for (const candidate of [
|
|
447
1045
|
...normalizeArray2(graphmlRoot.key),
|
|
@@ -454,8 +1052,8 @@ function collectKeyDefinitions2(graphmlRoot, graphElement) {
|
|
|
454
1052
|
const name = String(candidate[`${XML_ATTRIBUTE_PREFIX2}attr.name`] ?? id).trim();
|
|
455
1053
|
const type = normalizeType2(candidate[`${XML_ATTRIBUTE_PREFIX2}attr.type`]);
|
|
456
1054
|
const defaultNode = candidate.default ?? null;
|
|
457
|
-
const defaultValue = defaultNode !== null ? castDataValue2(defaultNode, type) : void 0;
|
|
458
|
-
keys.set(id, { id, domain, name, type, defaultValue });
|
|
1055
|
+
const defaultValue = defaultNode !== null ? castDataValue2(defaultNode, type, lossless) : void 0;
|
|
1056
|
+
keys.set(id, { id, domain, name, type, defaultValue, lossless });
|
|
459
1057
|
}
|
|
460
1058
|
}
|
|
461
1059
|
}
|
|
@@ -533,12 +1131,12 @@ function assignAttributeFromDataEntry2(entry, keyDefinitions, attributes) {
|
|
|
533
1131
|
}
|
|
534
1132
|
const definition = keyDefinitions.get(keyId);
|
|
535
1133
|
const attributeName = (definition == null ? void 0 : definition.name) ?? keyId;
|
|
536
|
-
const value = castDataValue2(entry, (definition == null ? void 0 : definition.type) ?? "string");
|
|
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);
|
|
537
1135
|
if (value !== void 0) {
|
|
538
1136
|
attributes[attributeName] = value;
|
|
539
1137
|
}
|
|
540
1138
|
}
|
|
541
|
-
function castDataValue2(value, type) {
|
|
1139
|
+
function castDataValue2(value, type, lossless = false) {
|
|
542
1140
|
if (value === null || typeof value === "undefined") {
|
|
543
1141
|
return void 0;
|
|
544
1142
|
}
|
|
@@ -549,6 +1147,9 @@ function castDataValue2(value, type) {
|
|
|
549
1147
|
if (type === "boolean") {
|
|
550
1148
|
return parseBoolean2(text);
|
|
551
1149
|
}
|
|
1150
|
+
if (type === "long" && lossless) {
|
|
1151
|
+
return /^[+-]?\d+$/.test(text.trim()) ? BigInt(text.trim()) : text;
|
|
1152
|
+
}
|
|
552
1153
|
if (type === "int" || type === "long") {
|
|
553
1154
|
const parsed = Number.parseInt(text, 10);
|
|
554
1155
|
return Number.isNaN(parsed) ? text : parsed;
|
|
@@ -571,12 +1172,12 @@ function extractTextContent2(value) {
|
|
|
571
1172
|
}
|
|
572
1173
|
if (isObject2(value)) {
|
|
573
1174
|
const text = value[XML_TEXT_KEY2];
|
|
574
|
-
|
|
575
|
-
return text;
|
|
576
|
-
}
|
|
577
|
-
const nonAttributeEntries = Object.entries(value).filter(([key]) => !key.startsWith(XML_ATTRIBUTE_PREFIX2));
|
|
1175
|
+
const nonAttributeEntries = Object.entries(value).filter(([key]) => !key.startsWith(XML_ATTRIBUTE_PREFIX2) && key !== XML_TEXT_KEY2);
|
|
578
1176
|
if (nonAttributeEntries.length === 0) {
|
|
579
|
-
return void 0;
|
|
1177
|
+
return typeof text === "string" ? text : void 0;
|
|
1178
|
+
}
|
|
1179
|
+
if (typeof text === "string" && text.trim()) {
|
|
1180
|
+
nonAttributeEntries.push([XML_TEXT_KEY2, text]);
|
|
580
1181
|
}
|
|
581
1182
|
return JSON.stringify(Object.fromEntries(nonAttributeEntries));
|
|
582
1183
|
}
|
|
@@ -637,14 +1238,23 @@ function parseBoolean2(value) {
|
|
|
637
1238
|
if (normalized === "false" || normalized === "0" || normalized === "no" || normalized === "n") {
|
|
638
1239
|
return false;
|
|
639
1240
|
}
|
|
640
|
-
return Boolean(
|
|
1241
|
+
return Boolean(normalized);
|
|
641
1242
|
}
|
|
642
1243
|
|
|
643
1244
|
// dist/graphml-loader.js
|
|
644
1245
|
var GraphMLLoaderWithParser2 = {
|
|
645
1246
|
...GraphMLLoader2,
|
|
646
|
-
parse: async (data) =>
|
|
647
|
-
|
|
648
|
-
|
|
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
|
+
}
|
|
649
1259
|
};
|
|
650
1260
|
//# sourceMappingURL=graphml-loader.cjs.map
|