@graphty/graph-io 0.3.16 → 0.3.18
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 +1 -1
- package/README.md +29 -3
- package/dist/chunks/{escape-D1f9-cwf.js → escape-D-gZWO26.js} +3 -2
- package/dist/chunks/{escape-D1f9-cwf.js.map → escape-D-gZWO26.js.map} +1 -1
- package/dist/chunks/{importer-D7ZcGCeb.js → importer-Br_QeAeE.js} +4 -3
- package/dist/chunks/{importer-D7ZcGCeb.js.map → importer-Br_QeAeE.js.map} +1 -1
- package/dist/chunks/{importer-CXEiicAN.js → importer-DHagxvDD.js} +4 -3
- package/dist/chunks/{importer-CXEiicAN.js.map → importer-DHagxvDD.js.map} +1 -1
- package/dist/chunks/{importer-C7mnGdr_.js → importer-Du5crN9l.js} +510 -26
- package/dist/chunks/importer-Du5crN9l.js.map +1 -0
- package/dist/chunks/importer-aNJfe0qu.js +1614 -0
- package/dist/chunks/importer-aNJfe0qu.js.map +1 -0
- package/dist/chunks/{importer-B8lsjFWx.js → importer-d0uQxFp6.js} +4 -3
- package/dist/chunks/{importer-B8lsjFWx.js.map → importer-d0uQxFp6.js.map} +1 -1
- package/dist/chunks/ontology-BnrJ4I98.js +113 -0
- package/dist/chunks/ontology-BnrJ4I98.js.map +1 -0
- package/dist/chunks/{records-IHsCfv7s.js → records-Bk9jgodz.js} +2 -2
- package/dist/chunks/{records-IHsCfv7s.js.map → records-Bk9jgodz.js.map} +1 -1
- package/dist/chunks/{writer-BtWpUaiH.js → report-BOk0p5y8.js} +181 -912
- package/dist/chunks/report-BOk0p5y8.js.map +1 -0
- package/dist/chunks/writer-GAdltGmC.js +827 -0
- package/dist/chunks/writer-GAdltGmC.js.map +1 -0
- package/dist/csv.js +4 -3
- package/dist/csv.js.map +1 -1
- package/dist/dot.js +1 -1
- package/dist/gexf.js +3 -2
- package/dist/gexf.js.map +1 -1
- package/dist/gml.js +3 -2
- package/dist/gml.js.map +1 -1
- package/dist/graph-io.js +191 -134
- package/dist/graph-io.js.map +1 -1
- package/dist/graphml.js +1 -1
- package/dist/json.js +1 -1
- package/dist/neo4j.js +4 -3
- package/dist/neo4j.js.map +1 -1
- package/dist/obo.d.ts +1 -0
- package/dist/obo.js +6 -0
- package/dist/obo.js.map +1 -0
- package/dist/pajek.js +1 -1
- package/dist/src/common/codes.d.ts +43 -0
- package/dist/src/common/codes.d.ts.map +1 -1
- package/dist/src/common/codes.js +43 -0
- package/dist/src/common/codes.js.map +1 -1
- package/dist/src/common/input.d.ts +9 -0
- package/dist/src/common/input.d.ts.map +1 -1
- package/dist/src/common/input.js +16 -0
- package/dist/src/common/input.js.map +1 -1
- package/dist/src/common/ontology.d.ts +59 -0
- package/dist/src/common/ontology.d.ts.map +1 -0
- package/dist/src/common/ontology.js +147 -0
- package/dist/src/common/ontology.js.map +1 -0
- package/dist/src/common/options.d.ts +12 -1
- package/dist/src/common/options.d.ts.map +1 -1
- package/dist/src/common/options.js +51 -1
- package/dist/src/common/options.js.map +1 -1
- package/dist/src/formats/json/dialect.d.ts +8 -6
- package/dist/src/formats/json/dialect.d.ts.map +1 -1
- package/dist/src/formats/json/dialect.js +26 -3
- package/dist/src/formats/json/dialect.js.map +1 -1
- package/dist/src/formats/json/importer.d.ts +324 -7
- package/dist/src/formats/json/importer.d.ts.map +1 -1
- package/dist/src/formats/json/importer.js +154 -23
- package/dist/src/formats/json/importer.js.map +1 -1
- package/dist/src/formats/json/obographs.d.ts +21 -0
- package/dist/src/formats/json/obographs.d.ts.map +1 -0
- package/dist/src/formats/json/obographs.js +476 -0
- package/dist/src/formats/json/obographs.js.map +1 -0
- package/dist/src/formats/obo/importer.d.ts +90 -0
- package/dist/src/formats/obo/importer.d.ts.map +1 -0
- package/dist/src/formats/obo/importer.js +1248 -0
- package/dist/src/formats/obo/importer.js.map +1 -0
- package/dist/src/formats/obo/index.d.ts +7 -0
- package/dist/src/formats/obo/index.d.ts.map +1 -0
- package/dist/src/formats/obo/index.js +7 -0
- package/dist/src/formats/obo/index.js.map +1 -0
- package/dist/src/formats/obo/syntax.d.ts +121 -0
- package/dist/src/formats/obo/syntax.d.ts.map +1 -0
- package/dist/src/formats/obo/syntax.js +424 -0
- package/dist/src/formats/obo/syntax.js.map +1 -0
- package/dist/src/index.d.ts +5 -4
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +4 -3
- package/dist/src/index.js.map +1 -1
- package/dist/src/registry.d.ts +21 -3
- package/dist/src/registry.d.ts.map +1 -1
- package/dist/src/registry.js +32 -1
- package/dist/src/registry.js.map +1 -1
- package/dist/src/sniff.d.ts +1 -1
- package/dist/src/sniff.d.ts.map +1 -1
- package/dist/src/sniff.js +23 -3
- package/dist/src/sniff.js.map +1 -1
- package/dist/src/types.d.ts +31 -0
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/types.js.map +1 -1
- package/package.json +6 -1
- package/src/common/codes.ts +58 -0
- package/src/common/input.ts +16 -0
- package/src/common/ontology.ts +169 -0
- package/src/common/options.ts +78 -2
- package/src/formats/json/dialect.ts +37 -7
- package/src/formats/json/importer.ts +206 -28
- package/src/formats/json/obographs.ts +563 -0
- package/src/formats/obo/importer.ts +1695 -0
- package/src/formats/obo/index.ts +7 -0
- package/src/formats/obo/syntax.ts +466 -0
- package/src/index.ts +6 -0
- package/src/registry.ts +38 -3
- package/src/sniff.ts +35 -5
- package/src/types.ts +40 -1
- package/dist/chunks/importer-C7mnGdr_.js.map +0 -1
- package/dist/chunks/writer-BtWpUaiH.js.map +0 -1
|
@@ -0,0 +1,827 @@
|
|
|
1
|
+
import { GraphFormatError, INVALID_INDEX } from "@graphty/graph-format";
|
|
2
|
+
import { aO as parseDecimalText, U as OPTIONS_GAINED_CODE, al as XML_ILLEGAL_CHAR_CODE, ac as TEXT_INFERRED_CODE, y as INTEGRAL_F64_CODE, a6 as STORAGE_CLASS_CODE, E as EMPTY_COLUMN_DROPPED_CODE, f as COLUMN_RENAMED_LOSS_CODE, a0 as ROLE_ASSUMED_CODE, aj as WEIGHT_KEY_CLASH_CODE, w as ID_TEXT_COLLISION_CODE, x as ID_TEXT_TYPE_CODE, Q as MUTUAL_AS_UNDIRECTED_CODE, S as MUTUAL_EXPANDED_CODE, X as PARENTS_DROPPED_CODE, a1 as ROLE_DROPPED_CODE, ab as TEMPORAL_TEXT_DROPPED_CODE, aa as TEMPORAL_DROPPED_CODE, O as MIXED_DIRECTION_CODE } from "./report-BOk0p5y8.js";
|
|
3
|
+
function formatF32(value) {
|
|
4
|
+
if (!Number.isFinite(value)) {
|
|
5
|
+
return String(value);
|
|
6
|
+
}
|
|
7
|
+
if (value === 0) {
|
|
8
|
+
return Object.is(value, -0) ? "-0" : "0";
|
|
9
|
+
}
|
|
10
|
+
for (let digits = 1; digits <= 9; digits++) {
|
|
11
|
+
const text = value.toPrecision(digits);
|
|
12
|
+
if (Math.fround(Number(text)) === value) {
|
|
13
|
+
return String(Number(text));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return String(value);
|
|
17
|
+
}
|
|
18
|
+
function formatF64(value) {
|
|
19
|
+
return Object.is(value, -0) ? "-0" : String(value);
|
|
20
|
+
}
|
|
21
|
+
function formatDecimal(value, dtype = "f64") {
|
|
22
|
+
if (!Number.isFinite(value)) {
|
|
23
|
+
return String(value);
|
|
24
|
+
}
|
|
25
|
+
if (Object.is(value, -0)) {
|
|
26
|
+
return "-0.0";
|
|
27
|
+
}
|
|
28
|
+
const text = formatNumber(value, dtype);
|
|
29
|
+
return text.includes(".") || text.includes("e") ? text : `${text}.0`;
|
|
30
|
+
}
|
|
31
|
+
function formatGmlReal(value, dtype = "f64") {
|
|
32
|
+
if (Number.isNaN(value)) {
|
|
33
|
+
return "NAN";
|
|
34
|
+
}
|
|
35
|
+
if (!Number.isFinite(value)) {
|
|
36
|
+
return value > 0 ? "+INF" : "-INF";
|
|
37
|
+
}
|
|
38
|
+
const text = formatDecimal(value, dtype);
|
|
39
|
+
if (text.includes(".")) {
|
|
40
|
+
return text;
|
|
41
|
+
}
|
|
42
|
+
const e = text.indexOf("e");
|
|
43
|
+
return `${text.slice(0, e)}.0${text.slice(e)}`;
|
|
44
|
+
}
|
|
45
|
+
function formatInteger(value) {
|
|
46
|
+
if (Number.isSafeInteger(value) || !Number.isFinite(value)) {
|
|
47
|
+
return String(value);
|
|
48
|
+
}
|
|
49
|
+
return BigInt(value).toString();
|
|
50
|
+
}
|
|
51
|
+
function formatNumber(value, dtype) {
|
|
52
|
+
return dtype === "f32" ? formatF32(value) : formatF64(value);
|
|
53
|
+
}
|
|
54
|
+
const DEFAULT_WEIGHT = 1;
|
|
55
|
+
function isWeightField(name, weightFrom) {
|
|
56
|
+
return weightFrom !== null && name === weightFrom;
|
|
57
|
+
}
|
|
58
|
+
function parseWeightText(text) {
|
|
59
|
+
const trimmed = text.trim();
|
|
60
|
+
if (trimmed.length === 0) {
|
|
61
|
+
return void 0;
|
|
62
|
+
}
|
|
63
|
+
let value;
|
|
64
|
+
try {
|
|
65
|
+
value = parseDecimalText(trimmed);
|
|
66
|
+
} catch (err) {
|
|
67
|
+
throw invalidWeight(text, err);
|
|
68
|
+
}
|
|
69
|
+
if (Number.isNaN(value)) {
|
|
70
|
+
throw invalidWeight(text, null);
|
|
71
|
+
}
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
function weightFromValue(value) {
|
|
75
|
+
if (value === void 0 || value === null) {
|
|
76
|
+
return void 0;
|
|
77
|
+
}
|
|
78
|
+
if (typeof value === "number") {
|
|
79
|
+
if (Number.isNaN(value)) {
|
|
80
|
+
throw invalidWeight(value, null);
|
|
81
|
+
}
|
|
82
|
+
return value;
|
|
83
|
+
}
|
|
84
|
+
if (typeof value === "string") {
|
|
85
|
+
return parseWeightText(value);
|
|
86
|
+
}
|
|
87
|
+
throw invalidWeight(value, null);
|
|
88
|
+
}
|
|
89
|
+
function invalidWeight(value, cause) {
|
|
90
|
+
let shown;
|
|
91
|
+
switch (typeof value) {
|
|
92
|
+
case "string":
|
|
93
|
+
shown = JSON.stringify(value);
|
|
94
|
+
break;
|
|
95
|
+
case "number":
|
|
96
|
+
shown = String(value);
|
|
97
|
+
break;
|
|
98
|
+
default:
|
|
99
|
+
shown = typeof value;
|
|
100
|
+
}
|
|
101
|
+
return new GraphFormatError("E_INVALID_WEIGHT", `invalid edge weight ${shown}`, {
|
|
102
|
+
value: typeof value === "object" ? typeof value : value,
|
|
103
|
+
cause: cause instanceof Error ? cause.message : null
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
const NEGATIVE_ZERO_TEXT = "-0";
|
|
107
|
+
const NUMERIC_DTYPES = /* @__PURE__ */ new Set(["f32", "f64", "i32", "u32", "u8"]);
|
|
108
|
+
function weightFormatter(dtype) {
|
|
109
|
+
switch (dtype) {
|
|
110
|
+
case "f32":
|
|
111
|
+
return formatF32;
|
|
112
|
+
case "f64":
|
|
113
|
+
return formatF64;
|
|
114
|
+
default:
|
|
115
|
+
return formatInteger;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function explicitWeights(snapshot) {
|
|
119
|
+
const shadowColumn = snapshot.edges.byRole("weight");
|
|
120
|
+
const shadow = shadowColumn !== null && NUMERIC_DTYPES.has(shadowColumn.dtype) ? shadowColumn : null;
|
|
121
|
+
if (shadow !== null) {
|
|
122
|
+
const dtype = shadow.dtype === "f32" ? "f32" : "f64";
|
|
123
|
+
const format = weightFormatter(shadow.dtype);
|
|
124
|
+
return {
|
|
125
|
+
weighted: true,
|
|
126
|
+
dtype,
|
|
127
|
+
isExplicit: (e) => shadow.isSet(e),
|
|
128
|
+
value: (e) => shadow.value(e),
|
|
129
|
+
text: (e, integral = false) => {
|
|
130
|
+
if (!shadow.isSet(e)) {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
const value = shadow.value(e);
|
|
134
|
+
if (Object.is(value, -0)) {
|
|
135
|
+
return NEGATIVE_ZERO_TEXT;
|
|
136
|
+
}
|
|
137
|
+
return integral && Number.isInteger(value) ? formatInteger(value) : format(value);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
const { weights } = snapshot.edgeList();
|
|
142
|
+
if (weights === null || !snapshot.flags.weighted) {
|
|
143
|
+
return {
|
|
144
|
+
weighted: false,
|
|
145
|
+
dtype: "f32",
|
|
146
|
+
isExplicit: () => false,
|
|
147
|
+
value: () => DEFAULT_WEIGHT,
|
|
148
|
+
text: () => null
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
weighted: true,
|
|
153
|
+
dtype: "f32",
|
|
154
|
+
isExplicit: () => true,
|
|
155
|
+
value: (e) => weights[e],
|
|
156
|
+
text: (e, integral = false) => {
|
|
157
|
+
const value = weights[e];
|
|
158
|
+
if (Object.is(value, -0)) {
|
|
159
|
+
return NEGATIVE_ZERO_TEXT;
|
|
160
|
+
}
|
|
161
|
+
return integral && Number.isInteger(value) ? formatInteger(value) : formatF32(value);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
const LOSS = Object.freeze({
|
|
166
|
+
/** The format has no mixed direction; expanded pairs are written per onMixedDirection. */
|
|
167
|
+
MIXED_DIRECTION: "W_MIXED_DIRECTION",
|
|
168
|
+
/** The format has no mixed direction and onMixedDirection is "error": export() will throw E_DIRECTED. */
|
|
169
|
+
MIXED_DIRECTION_ERROR: MIXED_DIRECTION_CODE,
|
|
170
|
+
/** Parallel edges in a format without them. */
|
|
171
|
+
MULTI_EDGES: "W_MULTI_EDGES",
|
|
172
|
+
/** Self-loops in a format without them. */
|
|
173
|
+
SELF_LOOPS: "W_SELF_LOOPS",
|
|
174
|
+
/** The format requires edge ids and the snapshot has none; canonical e0..e{E-1} are generated. */
|
|
175
|
+
EDGE_IDS_GENERATED: "W_EDGE_IDS_GENERATED",
|
|
176
|
+
/** The snapshot has an edge id column and the format cannot write one. */
|
|
177
|
+
EDGE_IDS_DROPPED: "W_EDGE_IDS_DROPPED",
|
|
178
|
+
/** Ids outside the format's charset are rewritten (sanitizeIds "mangle"). */
|
|
179
|
+
ID_MANGLED: "W_ID_MANGLED",
|
|
180
|
+
/** Ids outside the format's charset and sanitizeIds "error": export() will throw E_INVALID_ID. */
|
|
181
|
+
ID_CHARSET: "E_ID_CHARSET",
|
|
182
|
+
/** The format numbers nodes 1..N; ids that are not their 1-based index are kept as labels only. */
|
|
183
|
+
ID_RENUMBERED: "W_ID_RENUMBERED",
|
|
184
|
+
/** A column dtype the format does not keep as declared. */
|
|
185
|
+
DTYPE: "W_DTYPE_UNSUPPORTED",
|
|
186
|
+
/** A multi-component column in a format without strides. */
|
|
187
|
+
COMPONENTS: "W_COMPONENTS_FLATTENED",
|
|
188
|
+
/** A list column in a format without lists. */
|
|
189
|
+
LIST: "W_LIST_UNSUPPORTED",
|
|
190
|
+
/** A json column in a format without nested values. */
|
|
191
|
+
JSON: "W_JSON_UNSUPPORTED",
|
|
192
|
+
/** A declared default in a format without defaults. */
|
|
193
|
+
DEFAULT: "W_DEFAULT_DROPPED",
|
|
194
|
+
/** Declared options in a format without enumerations. */
|
|
195
|
+
OPTIONS: "W_OPTIONS_DROPPED",
|
|
196
|
+
/** A parent / parents column in a format without containment. */
|
|
197
|
+
HIERARCHY: "W_HIERARCHY_DROPPED",
|
|
198
|
+
/** A start / end / timestamp column in a format without temporal support. */
|
|
199
|
+
TEMPORAL: TEMPORAL_DROPPED_CODE,
|
|
200
|
+
/** A `<column>.text` companion (design section 5.1) the format cannot carry. */
|
|
201
|
+
TEMPORAL_TEXT: TEMPORAL_TEXT_DROPPED_CODE,
|
|
202
|
+
/** A role column the format has no slot for; written as a plain attribute, the role lost. */
|
|
203
|
+
ROLE: ROLE_DROPPED_CODE,
|
|
204
|
+
/** A parents (multi-parent) column in a format with single containment only. */
|
|
205
|
+
PARENTS: PARENTS_DROPPED_CODE,
|
|
206
|
+
/** A mutual pair written as two directed edges without the mutual mark. */
|
|
207
|
+
MUTUAL_EXPANDED: MUTUAL_EXPANDED_CODE,
|
|
208
|
+
/** A mutual pair written as one undirected edge; the mark is lost and the pair reads back undirected. */
|
|
209
|
+
MUTUAL_AS_UNDIRECTED: MUTUAL_AS_UNDIRECTED_CODE,
|
|
210
|
+
/** Node ids whose written text reads back as the other type under the importer's id rule. */
|
|
211
|
+
ID_TEXT_TYPE: ID_TEXT_TYPE_CODE,
|
|
212
|
+
/** Two node ids share one written text; export() throws E_INVALID_ID. */
|
|
213
|
+
ID_TEXT_COLLISION: ID_TEXT_COLLISION_CODE,
|
|
214
|
+
/** A plain column named like the importer's weight key reads back as THE weight. */
|
|
215
|
+
WEIGHT_KEY_CLASH: WEIGHT_KEY_CLASH_CODE,
|
|
216
|
+
/** A role-less column whose written name the importer maps to a role. */
|
|
217
|
+
ROLE_ASSUMED: ROLE_ASSUMED_CODE,
|
|
218
|
+
/** A role column written into the format's slot reads back under the importer's fixed name. */
|
|
219
|
+
COLUMN_NAME_CHANGED: COLUMN_RENAMED_LOSS_CODE,
|
|
220
|
+
/** A declared column whose every row is unset vanishes through a format without declarations. */
|
|
221
|
+
EMPTY_COLUMN: EMPTY_COLUMN_DROPPED_CODE,
|
|
222
|
+
/** A string / dict column that reads back as the other storage class (the dictionary heuristic). */
|
|
223
|
+
STORAGE_CLASS: STORAGE_CLASS_CODE,
|
|
224
|
+
/** An f64 column whose set values are all integral reads back as i32 through an untyped format. */
|
|
225
|
+
INTEGRAL_F64: INTEGRAL_F64_CODE,
|
|
226
|
+
/** Text cells that read back as numbers or booleans under the 5.1 grammar. */
|
|
227
|
+
TEXT_INFERRED: TEXT_INFERRED_CODE,
|
|
228
|
+
/** A string cell holding a character XML 1.0 forbids; export() throws E_COLUMN_TYPE. */
|
|
229
|
+
XML_ILLEGAL_CHAR: XML_ILLEGAL_CHAR_CODE,
|
|
230
|
+
/** A dict column without declared options gains one on re-import. */
|
|
231
|
+
OPTIONS_GAINED: OPTIONS_GAINED_CODE,
|
|
232
|
+
/** A spells column in a format without spells. */
|
|
233
|
+
SPELLS: "W_SPELLS_DROPPED",
|
|
234
|
+
/** Dynamic attribute values (extension tables) in a format without them. */
|
|
235
|
+
DYNAMIC_VALUES: "W_DYNAMIC_VALUES_DROPPED",
|
|
236
|
+
/** An open-interval column in a format without open intervals (design section 5.1). */
|
|
237
|
+
OPEN_INTERVAL: "W_OPEN_INTERVAL",
|
|
238
|
+
/** Graph-level attributes in a format without them. */
|
|
239
|
+
GRAPH_ATTRIBUTES: "W_GRAPH_ATTRIBUTES_DROPPED",
|
|
240
|
+
/** A position column in a format without positions. */
|
|
241
|
+
POSITIONS: "W_POSITIONS_DROPPED",
|
|
242
|
+
/** Visual columns in a format without them. */
|
|
243
|
+
VIZ: "W_VIZ_DROPPED",
|
|
244
|
+
/** An extension table the format cannot carry. */
|
|
245
|
+
EXTENSION_TABLE: "W_EXTENSION_TABLE_DROPPED"
|
|
246
|
+
});
|
|
247
|
+
const NO_CAPABILITIES = Object.freeze({
|
|
248
|
+
mixedDirection: false,
|
|
249
|
+
multiEdges: false,
|
|
250
|
+
selfLoops: false,
|
|
251
|
+
edgeIds: "none",
|
|
252
|
+
idCharset: "any",
|
|
253
|
+
dtypes: Object.freeze([]),
|
|
254
|
+
components: false,
|
|
255
|
+
lists: false,
|
|
256
|
+
json: false,
|
|
257
|
+
defaults: false,
|
|
258
|
+
options: false,
|
|
259
|
+
hierarchy: false,
|
|
260
|
+
temporal: "none",
|
|
261
|
+
graphAttributes: false,
|
|
262
|
+
positions: false,
|
|
263
|
+
viz: false
|
|
264
|
+
});
|
|
265
|
+
function capabilities(supported) {
|
|
266
|
+
return Object.freeze({ ...NO_CAPABILITIES, ...supported, dtypes: Object.freeze([...supported.dtypes ?? []]) });
|
|
267
|
+
}
|
|
268
|
+
const STRUCTURAL_ROLES = /* @__PURE__ */ new Set(["directed", "pair", "mutual", "weight", "originalId"]);
|
|
269
|
+
const VIZ_ROLES = /* @__PURE__ */ new Set(["color", "size", "shape", "thickness"]);
|
|
270
|
+
const TEMPORAL_ROLES = /* @__PURE__ */ new Set(["start", "end", "timestamp", "timestamps"]);
|
|
271
|
+
const HIERARCHY_ROLES = /* @__PURE__ */ new Set(["parent", "parents"]);
|
|
272
|
+
function checkCapabilities(snapshot, caps, options, extras = {}) {
|
|
273
|
+
const notes = [];
|
|
274
|
+
const note = (code, message, column = null, count = null) => {
|
|
275
|
+
notes.push(Object.freeze({ code, message, column, count }));
|
|
276
|
+
};
|
|
277
|
+
const mixed = countMixedEdges(snapshot);
|
|
278
|
+
if (mixed > 0 && !caps.mixedDirection) {
|
|
279
|
+
if (options.onMixedDirection === "error") {
|
|
280
|
+
note(
|
|
281
|
+
LOSS.MIXED_DIRECTION_ERROR,
|
|
282
|
+
`${mixed} undirected edge(s) in a directed graph; the format has no mixed direction and onMixedDirection is "error"`,
|
|
283
|
+
null,
|
|
284
|
+
mixed
|
|
285
|
+
);
|
|
286
|
+
} else {
|
|
287
|
+
note(
|
|
288
|
+
LOSS.MIXED_DIRECTION,
|
|
289
|
+
`${mixed} undirected edge(s) in a directed graph written as ${options.onMixedDirection}`,
|
|
290
|
+
null,
|
|
291
|
+
mixed
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
if (snapshot.flags.multigraph && !caps.multiEdges) {
|
|
296
|
+
const parallel = countParallelArcs(snapshot);
|
|
297
|
+
note(LOSS.MULTI_EDGES, `${parallel} parallel edge(s); the format has no parallel edges`, null, parallel);
|
|
298
|
+
}
|
|
299
|
+
if (snapshot.selfLoopCount > 0 && !caps.selfLoops) {
|
|
300
|
+
note(
|
|
301
|
+
LOSS.SELF_LOOPS,
|
|
302
|
+
`${snapshot.selfLoopCount} self-loop(s); the format has no self-loops`,
|
|
303
|
+
null,
|
|
304
|
+
snapshot.selfLoopCount
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
const edgeIds = snapshot.edges.byRole("id");
|
|
308
|
+
if (caps.edgeIds === "required" && edgeIds === null && snapshot.edgeCount > 0) {
|
|
309
|
+
note(
|
|
310
|
+
LOSS.EDGE_IDS_GENERATED,
|
|
311
|
+
"the format requires edge ids; canonical e0..e{E-1} are generated and not written back",
|
|
312
|
+
null,
|
|
313
|
+
snapshot.edgeCount
|
|
314
|
+
);
|
|
315
|
+
} else if (caps.edgeIds === "none" && edgeIds !== null) {
|
|
316
|
+
note(
|
|
317
|
+
LOSS.EDGE_IDS_DROPPED,
|
|
318
|
+
`edge id column "${edgeIds.meta.name}" cannot be written`,
|
|
319
|
+
edgeIds.meta.name,
|
|
320
|
+
snapshot.edgeCount
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
const unrepresentable = countUnrepresentableIds(snapshot, caps.idCharset);
|
|
324
|
+
if (unrepresentable > 0) {
|
|
325
|
+
if (caps.idCharset === "dense-1-based") {
|
|
326
|
+
note(
|
|
327
|
+
LOSS.ID_RENUMBERED,
|
|
328
|
+
options.sanitizeIds === "mangle" ? `${unrepresentable} node id(s) are not their 1-based index; nodes are numbered 1..N, the originals kept in the exporter's originalId attribute (restored by restoreMangledIds) and as labels of the nodes without a label value` : `${unrepresentable} node id(s) are not their 1-based index; nodes are numbered 1..N and ids kept as labels of the nodes without a label value (a node with one loses its id)`,
|
|
329
|
+
null,
|
|
330
|
+
unrepresentable
|
|
331
|
+
);
|
|
332
|
+
} else if (options.sanitizeIds === "mangle") {
|
|
333
|
+
note(
|
|
334
|
+
LOSS.ID_MANGLED,
|
|
335
|
+
`${unrepresentable} node id(s) outside the ${caps.idCharset} charset are rewritten; originals kept in the originalId attribute`,
|
|
336
|
+
null,
|
|
337
|
+
unrepresentable
|
|
338
|
+
);
|
|
339
|
+
} else {
|
|
340
|
+
note(
|
|
341
|
+
LOSS.ID_CHARSET,
|
|
342
|
+
`${unrepresentable} node id(s) outside the ${caps.idCharset} charset; export() will throw unless sanitizeIds is "mangle"`,
|
|
343
|
+
null,
|
|
344
|
+
unrepresentable
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
checkColumns(snapshot.nodes, "node", caps, extras, note);
|
|
349
|
+
checkColumns(snapshot.edges, "edge", caps, extras, note);
|
|
350
|
+
const graphColumns = snapshot.graph.names();
|
|
351
|
+
if (graphColumns.length > 0) {
|
|
352
|
+
if (caps.graphAttributes) {
|
|
353
|
+
checkColumns(snapshot.graph, "graph", caps, extras, note);
|
|
354
|
+
} else {
|
|
355
|
+
note(
|
|
356
|
+
LOSS.GRAPH_ATTRIBUTES,
|
|
357
|
+
`${graphColumns.length} graph attribute(s) cannot be written`,
|
|
358
|
+
null,
|
|
359
|
+
graphColumns.length
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
for (const [name, table] of snapshot.extensions) {
|
|
364
|
+
if (name.startsWith("temporal:")) {
|
|
365
|
+
if (caps.temporal !== "dynamic-values") {
|
|
366
|
+
note(
|
|
367
|
+
LOSS.DYNAMIC_VALUES,
|
|
368
|
+
`dynamic values of "${name}" (${table.rowCount} row(s)) cannot be written`,
|
|
369
|
+
name,
|
|
370
|
+
table.rowCount
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
} else {
|
|
374
|
+
note(
|
|
375
|
+
LOSS.EXTENSION_TABLE,
|
|
376
|
+
`extension table "${name}" (${table.rowCount} row(s)) cannot be written`,
|
|
377
|
+
name,
|
|
378
|
+
table.rowCount
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return notes;
|
|
383
|
+
}
|
|
384
|
+
function checkColumns(table, domain, caps, extras, note) {
|
|
385
|
+
for (const column of table) {
|
|
386
|
+
const { meta } = column;
|
|
387
|
+
const { name, role } = meta;
|
|
388
|
+
if (role !== null && STRUCTURAL_ROLES.has(role)) {
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
const label = `${domain} column "${name}"`;
|
|
392
|
+
if (role !== null && extras.roleNames !== void 0) {
|
|
393
|
+
const fixed = extras.roleNames[role];
|
|
394
|
+
if (fixed !== void 0 && fixed !== name) {
|
|
395
|
+
note(
|
|
396
|
+
LOSS.COLUMN_NAME_CHANGED,
|
|
397
|
+
`${label} (${role}) is written into the format's ${role} slot and reads back as "${fixed}"`,
|
|
398
|
+
name,
|
|
399
|
+
column.length - column.nullCount
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
if (domain === "edge" && role === "id") {
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
if (role === "timeText") {
|
|
407
|
+
if (extras.temporalText !== true) {
|
|
408
|
+
note(
|
|
409
|
+
LOSS.TEMPORAL_TEXT,
|
|
410
|
+
`${label} (the lexical form of a temporal column, design section 5.1) cannot be written`,
|
|
411
|
+
name,
|
|
412
|
+
column.length - column.nullCount
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
if (role === "position") {
|
|
418
|
+
if (!caps.positions) {
|
|
419
|
+
note(LOSS.POSITIONS, `${label} (positions) cannot be written`, name, column.length - column.nullCount);
|
|
420
|
+
} else if (extras.positionDtype !== void 0 && meta.dtype !== extras.positionDtype) {
|
|
421
|
+
note(
|
|
422
|
+
LOSS.DTYPE,
|
|
423
|
+
`${label} (positions) is ${meta.dtype}; the format reads positions back as ${extras.positionDtype}`,
|
|
424
|
+
name,
|
|
425
|
+
column.length - column.nullCount
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
if (role !== null && VIZ_ROLES.has(role)) {
|
|
431
|
+
if (!caps.viz) {
|
|
432
|
+
note(LOSS.VIZ, `${label} (${role}) cannot be written`, name, column.length - column.nullCount);
|
|
433
|
+
}
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
436
|
+
if (role !== null && HIERARCHY_ROLES.has(role)) {
|
|
437
|
+
if (!caps.hierarchy) {
|
|
438
|
+
note(LOSS.HIERARCHY, `${label} (${role}) cannot be written`, name, column.length - column.nullCount);
|
|
439
|
+
}
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
if (role !== null && TEMPORAL_ROLES.has(role)) {
|
|
443
|
+
if (caps.temporal === "none") {
|
|
444
|
+
note(LOSS.TEMPORAL, `${label} (${role}) cannot be written`, name, column.length - column.nullCount);
|
|
445
|
+
}
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
if (role === "spells") {
|
|
449
|
+
if (caps.temporal !== "spells" && caps.temporal !== "dynamic-values") {
|
|
450
|
+
note(LOSS.SPELLS, `${label} (spells) cannot be written`, name, column.length - column.nullCount);
|
|
451
|
+
}
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
if (role === "open" || role === "spellsOpen") {
|
|
455
|
+
if (extras.openIntervals !== true) {
|
|
456
|
+
note(
|
|
457
|
+
LOSS.OPEN_INTERVAL,
|
|
458
|
+
`${label} (open intervals) cannot be written`,
|
|
459
|
+
name,
|
|
460
|
+
column.length - column.nullCount
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
if (role !== null && !(extras.roles?.has(role) ?? false)) {
|
|
466
|
+
note(
|
|
467
|
+
LOSS.ROLE,
|
|
468
|
+
`${label} (${role}) is written as a plain attribute; the format has no ${role} slot and the role is lost`,
|
|
469
|
+
name,
|
|
470
|
+
column.length - column.nullCount
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
if (meta.dynamic && caps.temporal !== "dynamic-values") {
|
|
474
|
+
note(LOSS.DYNAMIC_VALUES, `${label} is dynamic; only its static value can be written`, name, null);
|
|
475
|
+
}
|
|
476
|
+
checkDtype(column, label, caps, note);
|
|
477
|
+
if (meta.default !== void 0 && !caps.defaults) {
|
|
478
|
+
note(LOSS.DEFAULT, `${label} declares a default; the format has no defaults`, name, null);
|
|
479
|
+
}
|
|
480
|
+
if (meta.options !== null && !caps.options) {
|
|
481
|
+
note(LOSS.OPTIONS, `${label} declares options; the format has no enumerations`, name, null);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
function checkDtype(column, label, caps, note) {
|
|
486
|
+
const { meta } = column;
|
|
487
|
+
const { name } = meta;
|
|
488
|
+
const set = column.length - column.nullCount;
|
|
489
|
+
switch (meta.dtype) {
|
|
490
|
+
case "list":
|
|
491
|
+
if (!caps.lists) {
|
|
492
|
+
note(LOSS.LIST, `${label} is a list; the format has no lists`, name, set);
|
|
493
|
+
} else if (meta.itemDtype !== null && !caps.dtypes.includes(meta.itemDtype)) {
|
|
494
|
+
note(
|
|
495
|
+
LOSS.DTYPE,
|
|
496
|
+
`${label} holds ${meta.itemDtype} items; the format cannot keep that dtype`,
|
|
497
|
+
name,
|
|
498
|
+
set
|
|
499
|
+
);
|
|
500
|
+
} else if (meta.itemDtype === "dict") {
|
|
501
|
+
note(LOSS.DTYPE, `${label} holds dict items; list items read back as string`, name, set);
|
|
502
|
+
}
|
|
503
|
+
break;
|
|
504
|
+
case "json":
|
|
505
|
+
if (!caps.json) {
|
|
506
|
+
note(LOSS.JSON, `${label} holds nested values; the format has no nested values`, name, set);
|
|
507
|
+
}
|
|
508
|
+
break;
|
|
509
|
+
default:
|
|
510
|
+
if (!caps.dtypes.includes(meta.dtype)) {
|
|
511
|
+
note(LOSS.DTYPE, `${label} is ${meta.dtype}; the format cannot keep that dtype`, name, set);
|
|
512
|
+
}
|
|
513
|
+
if (meta.components > 1 && !caps.components) {
|
|
514
|
+
note(
|
|
515
|
+
LOSS.COMPONENTS,
|
|
516
|
+
`${label} has ${meta.components} components; the format has no strides`,
|
|
517
|
+
name,
|
|
518
|
+
set
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
break;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
function countMixedEdges(snapshot) {
|
|
525
|
+
if (!snapshot.directed) {
|
|
526
|
+
return 0;
|
|
527
|
+
}
|
|
528
|
+
const directed = snapshot.edges.byRole("directed");
|
|
529
|
+
if (directed === null || directed.dtype !== "bool") {
|
|
530
|
+
return 0;
|
|
531
|
+
}
|
|
532
|
+
const pair = snapshot.edges.byRole("pair");
|
|
533
|
+
let undirectedRows = 0;
|
|
534
|
+
let paired = 0;
|
|
535
|
+
for (let e = 0; e < snapshot.edgeCount; e++) {
|
|
536
|
+
if (directed.isSet(e) && directed.value(e) === false) {
|
|
537
|
+
undirectedRows++;
|
|
538
|
+
if (pair !== null && pair.dtype === "u32" && pair.isSet(e) && pair.data[e] !== INVALID_INDEX) {
|
|
539
|
+
paired++;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
return paired / 2 + (undirectedRows - paired);
|
|
544
|
+
}
|
|
545
|
+
function countParallelArcs(snapshot) {
|
|
546
|
+
const { rowPtr, colIdx, nodeCount } = snapshot;
|
|
547
|
+
let count = 0;
|
|
548
|
+
for (let u = 0; u < nodeCount; u++) {
|
|
549
|
+
for (let a = rowPtr[u] + 1; a < rowPtr[u + 1]; a++) {
|
|
550
|
+
if (colIdx[a] === colIdx[a - 1]) {
|
|
551
|
+
count++;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
return snapshot.directed ? count : Math.ceil(count / 2);
|
|
556
|
+
}
|
|
557
|
+
const NAME_CHAR_RANGES = [
|
|
558
|
+
[45, 46],
|
|
559
|
+
[48, 58],
|
|
560
|
+
[65, 90],
|
|
561
|
+
[95, 95],
|
|
562
|
+
[97, 122],
|
|
563
|
+
[183, 183],
|
|
564
|
+
[192, 214],
|
|
565
|
+
[216, 246],
|
|
566
|
+
[248, 893],
|
|
567
|
+
[895, 8191],
|
|
568
|
+
[8204, 8205],
|
|
569
|
+
[8255, 8256],
|
|
570
|
+
[8304, 8591],
|
|
571
|
+
[11264, 12271],
|
|
572
|
+
[12289, 55295],
|
|
573
|
+
[63744, 64975],
|
|
574
|
+
[65008, 65533],
|
|
575
|
+
[65536, 983039]
|
|
576
|
+
];
|
|
577
|
+
function isNameChar(cp) {
|
|
578
|
+
for (const [lo, hi] of NAME_CHAR_RANGES) {
|
|
579
|
+
if (cp < lo) {
|
|
580
|
+
return false;
|
|
581
|
+
}
|
|
582
|
+
if (cp <= hi) {
|
|
583
|
+
return true;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
return false;
|
|
587
|
+
}
|
|
588
|
+
function isNmtoken(text) {
|
|
589
|
+
if (text.length === 0) {
|
|
590
|
+
return false;
|
|
591
|
+
}
|
|
592
|
+
for (const ch of text) {
|
|
593
|
+
const cp = ch.codePointAt(0);
|
|
594
|
+
if (cp === void 0 || !isNameChar(cp)) {
|
|
595
|
+
return false;
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
return true;
|
|
599
|
+
}
|
|
600
|
+
function isRepresentableId(id, charset) {
|
|
601
|
+
switch (charset) {
|
|
602
|
+
case "any":
|
|
603
|
+
return true;
|
|
604
|
+
case "nmtoken":
|
|
605
|
+
return isNmtoken(String(id));
|
|
606
|
+
case "integer":
|
|
607
|
+
return typeof id === "number" && Number.isSafeInteger(id);
|
|
608
|
+
case "dense-1-based":
|
|
609
|
+
return false;
|
|
610
|
+
default: {
|
|
611
|
+
const name = charset;
|
|
612
|
+
throw new GraphFormatError("E_UNSUPPORTED", `unknown id charset ${name}`, { charset: name });
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
function countUnrepresentableIds(snapshot, charset) {
|
|
617
|
+
const { ids } = snapshot;
|
|
618
|
+
if (charset === "any") {
|
|
619
|
+
return 0;
|
|
620
|
+
}
|
|
621
|
+
if (charset === "dense-1-based") {
|
|
622
|
+
if (ids.kind === "identity" && ids.offset === 1) {
|
|
623
|
+
return 0;
|
|
624
|
+
}
|
|
625
|
+
let changed = 0;
|
|
626
|
+
for (let i = 0; i < ids.size; i++) {
|
|
627
|
+
if (ids.idOf(i) !== i + 1) {
|
|
628
|
+
changed++;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
return changed;
|
|
632
|
+
}
|
|
633
|
+
let count = 0;
|
|
634
|
+
for (let i = 0; i < ids.size; i++) {
|
|
635
|
+
if (!isRepresentableId(ids.idOf(i), charset)) {
|
|
636
|
+
count++;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
return count;
|
|
640
|
+
}
|
|
641
|
+
function sanitizeIds(snapshot, charset, mode) {
|
|
642
|
+
const { ids } = snapshot;
|
|
643
|
+
const n = ids.size;
|
|
644
|
+
if (charset === "dense-1-based") {
|
|
645
|
+
const changed = countUnrepresentableIds(snapshot, charset);
|
|
646
|
+
return {
|
|
647
|
+
charset,
|
|
648
|
+
changed,
|
|
649
|
+
idAt: (i) => i + 1,
|
|
650
|
+
isChanged: (i) => ids.idOf(i) !== i + 1,
|
|
651
|
+
originalAt: (i) => ids.idOf(i)
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
const unchanged = {
|
|
655
|
+
charset,
|
|
656
|
+
changed: 0,
|
|
657
|
+
idAt: (i) => ids.idOf(i),
|
|
658
|
+
isChanged: () => false,
|
|
659
|
+
originalAt: (i) => ids.idOf(i)
|
|
660
|
+
};
|
|
661
|
+
if (charset === "any") {
|
|
662
|
+
return unchanged;
|
|
663
|
+
}
|
|
664
|
+
const bad = [];
|
|
665
|
+
for (let i = 0; i < n; i++) {
|
|
666
|
+
if (!isRepresentableId(ids.idOf(i), charset)) {
|
|
667
|
+
bad.push(i);
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
if (bad.length === 0) {
|
|
671
|
+
return unchanged;
|
|
672
|
+
}
|
|
673
|
+
if (mode === "error") {
|
|
674
|
+
const first = ids.idOf(bad[0]);
|
|
675
|
+
throw new GraphFormatError(
|
|
676
|
+
"E_INVALID_ID",
|
|
677
|
+
`${bad.length} node id(s) cannot be written as ${charset} (first: ${JSON.stringify(first)} at index ${bad[0]}); pass sanitizeIds: "mangle" to rewrite them`,
|
|
678
|
+
{ reason: "charset", charset, count: bad.length, id: first, index: bad[0] }
|
|
679
|
+
);
|
|
680
|
+
}
|
|
681
|
+
const written = /* @__PURE__ */ new Map();
|
|
682
|
+
if (charset === "nmtoken") {
|
|
683
|
+
const used = /* @__PURE__ */ new Set();
|
|
684
|
+
for (let i = 0; i < n; i++) {
|
|
685
|
+
const id = ids.idOf(i);
|
|
686
|
+
if (isRepresentableId(id, charset)) {
|
|
687
|
+
used.add(String(id));
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
for (const i of bad) {
|
|
691
|
+
const base = mangleNmtoken(String(ids.idOf(i)));
|
|
692
|
+
let candidate = base;
|
|
693
|
+
for (let k = 2; used.has(candidate); k++) {
|
|
694
|
+
candidate = `${base}_${k}`;
|
|
695
|
+
}
|
|
696
|
+
used.add(candidate);
|
|
697
|
+
written.set(i, candidate);
|
|
698
|
+
}
|
|
699
|
+
} else {
|
|
700
|
+
const used = /* @__PURE__ */ new Set();
|
|
701
|
+
for (let i = 0; i < n; i++) {
|
|
702
|
+
const id = ids.idOf(i);
|
|
703
|
+
if (typeof id === "number" && Number.isSafeInteger(id)) {
|
|
704
|
+
used.add(id);
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
let next = 0;
|
|
708
|
+
for (const i of bad) {
|
|
709
|
+
while (used.has(next)) {
|
|
710
|
+
next++;
|
|
711
|
+
}
|
|
712
|
+
used.add(next);
|
|
713
|
+
written.set(i, next);
|
|
714
|
+
next++;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
return {
|
|
718
|
+
charset,
|
|
719
|
+
changed: bad.length,
|
|
720
|
+
idAt: (i) => written.get(i) ?? ids.idOf(i),
|
|
721
|
+
isChanged: (i) => written.has(i),
|
|
722
|
+
originalAt: (i) => ids.idOf(i)
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
function mangleNmtoken(text) {
|
|
726
|
+
let out = "";
|
|
727
|
+
for (const ch of text) {
|
|
728
|
+
const cp = ch.codePointAt(0);
|
|
729
|
+
out += cp !== void 0 && isNameChar(cp) ? ch : "_";
|
|
730
|
+
}
|
|
731
|
+
return out.length === 0 ? "_" : out;
|
|
732
|
+
}
|
|
733
|
+
const DEFAULT_CHUNK_BYTES = 64 * 1024;
|
|
734
|
+
async function* encodeChunks(parts, chunkBytes = DEFAULT_CHUNK_BYTES) {
|
|
735
|
+
const encoder = new TextEncoder();
|
|
736
|
+
let pending = [];
|
|
737
|
+
let pendingUnits = 0;
|
|
738
|
+
for await (const part of parts) {
|
|
739
|
+
if (part.length === 0) {
|
|
740
|
+
continue;
|
|
741
|
+
}
|
|
742
|
+
pending.push(part);
|
|
743
|
+
pendingUnits += part.length;
|
|
744
|
+
if (pendingUnits >= chunkBytes) {
|
|
745
|
+
yield encoder.encode(pending.length === 1 ? pending[0] : pending.join(""));
|
|
746
|
+
pending = [];
|
|
747
|
+
pendingUnits = 0;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
if (pending.length > 0) {
|
|
751
|
+
yield encoder.encode(pending.length === 1 ? pending[0] : pending.join(""));
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
async function joinText(parts) {
|
|
755
|
+
const collected = [];
|
|
756
|
+
for await (const part of parts) {
|
|
757
|
+
collected.push(part);
|
|
758
|
+
}
|
|
759
|
+
return collected.join("");
|
|
760
|
+
}
|
|
761
|
+
async function decodeChunks(chunks) {
|
|
762
|
+
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
763
|
+
const parts = [];
|
|
764
|
+
for await (const chunk of chunks) {
|
|
765
|
+
parts.push(decoder.decode(chunk, { stream: true }));
|
|
766
|
+
}
|
|
767
|
+
parts.push(decoder.decode());
|
|
768
|
+
return parts.join("");
|
|
769
|
+
}
|
|
770
|
+
async function collectBytes(chunks) {
|
|
771
|
+
const parts = [];
|
|
772
|
+
let total = 0;
|
|
773
|
+
for await (const chunk of chunks) {
|
|
774
|
+
parts.push(chunk);
|
|
775
|
+
total += chunk.byteLength;
|
|
776
|
+
}
|
|
777
|
+
const out = new Uint8Array(total);
|
|
778
|
+
let offset = 0;
|
|
779
|
+
for (const part of parts) {
|
|
780
|
+
out.set(part, offset);
|
|
781
|
+
offset += part.byteLength;
|
|
782
|
+
}
|
|
783
|
+
return out;
|
|
784
|
+
}
|
|
785
|
+
function toReadableStream(chunks) {
|
|
786
|
+
const iterator = chunks[Symbol.asyncIterator]();
|
|
787
|
+
return new ReadableStream({
|
|
788
|
+
async pull(controller) {
|
|
789
|
+
const { done, value } = await iterator.next();
|
|
790
|
+
if (done) {
|
|
791
|
+
controller.close();
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
794
|
+
controller.enqueue(value);
|
|
795
|
+
},
|
|
796
|
+
async cancel() {
|
|
797
|
+
await iterator.return?.(void 0);
|
|
798
|
+
}
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
export {
|
|
802
|
+
DEFAULT_CHUNK_BYTES as D,
|
|
803
|
+
LOSS as L,
|
|
804
|
+
NO_CAPABILITIES as N,
|
|
805
|
+
checkCapabilities as a,
|
|
806
|
+
collectBytes as b,
|
|
807
|
+
capabilities as c,
|
|
808
|
+
decodeChunks as d,
|
|
809
|
+
encodeChunks as e,
|
|
810
|
+
explicitWeights as f,
|
|
811
|
+
formatDecimal as g,
|
|
812
|
+
formatF32 as h,
|
|
813
|
+
formatF64 as i,
|
|
814
|
+
formatGmlReal as j,
|
|
815
|
+
formatInteger as k,
|
|
816
|
+
isNmtoken as l,
|
|
817
|
+
isWeightField as m,
|
|
818
|
+
joinText as n,
|
|
819
|
+
mangleNmtoken as o,
|
|
820
|
+
parseWeightText as p,
|
|
821
|
+
isNameChar as q,
|
|
822
|
+
countMixedEdges as r,
|
|
823
|
+
sanitizeIds as s,
|
|
824
|
+
toReadableStream as t,
|
|
825
|
+
weightFromValue as w
|
|
826
|
+
};
|
|
827
|
+
//# sourceMappingURL=writer-GAdltGmC.js.map
|