@graphty/graph-io 0.0.0 → 0.2.0

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 (339) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +250 -28
  3. package/dist/chunks/children-CL3Cy0ez.js +238 -0
  4. package/dist/chunks/children-CL3Cy0ez.js.map +1 -0
  5. package/dist/chunks/escape-DyI8JofU.js +938 -0
  6. package/dist/chunks/escape-DyI8JofU.js.map +1 -0
  7. package/dist/chunks/importer-CQnJuWJw.js +2987 -0
  8. package/dist/chunks/importer-CQnJuWJw.js.map +1 -0
  9. package/dist/chunks/importer-CpCpfbxr.js +2015 -0
  10. package/dist/chunks/importer-CpCpfbxr.js.map +1 -0
  11. package/dist/chunks/importer-DbnGYr3_.js +2342 -0
  12. package/dist/chunks/importer-DbnGYr3_.js.map +1 -0
  13. package/dist/chunks/importer-GozH8DkN.js +3050 -0
  14. package/dist/chunks/importer-GozH8DkN.js.map +1 -0
  15. package/dist/chunks/records-CGpxszm1.js +605 -0
  16. package/dist/chunks/records-CGpxszm1.js.map +1 -0
  17. package/dist/chunks/text-CajMdVFy.js +189 -0
  18. package/dist/chunks/text-CajMdVFy.js.map +1 -0
  19. package/dist/chunks/writer-DxSKC7TL.js +2842 -0
  20. package/dist/chunks/writer-DxSKC7TL.js.map +1 -0
  21. package/dist/csv.d.ts +1 -0
  22. package/dist/csv.js +1702 -0
  23. package/dist/csv.js.map +1 -0
  24. package/dist/dot.d.ts +1 -0
  25. package/dist/dot.js +8 -0
  26. package/dist/dot.js.map +1 -0
  27. package/dist/gexf.d.ts +1 -0
  28. package/dist/gexf.js +3466 -0
  29. package/dist/gexf.js.map +1 -0
  30. package/dist/gml.d.ts +1 -0
  31. package/dist/gml.js +2647 -0
  32. package/dist/gml.js.map +1 -0
  33. package/dist/graph-io.d.ts +1 -0
  34. package/dist/graph-io.js +790 -0
  35. package/dist/graph-io.js.map +1 -0
  36. package/dist/graphml.d.ts +1 -0
  37. package/dist/graphml.js +8 -0
  38. package/dist/graphml.js.map +1 -0
  39. package/dist/json.d.ts +1 -0
  40. package/dist/json.js +11 -0
  41. package/dist/json.js.map +1 -0
  42. package/dist/neo4j.d.ts +1 -0
  43. package/dist/neo4j.js +2046 -0
  44. package/dist/neo4j.js.map +1 -0
  45. package/dist/pajek.d.ts +1 -0
  46. package/dist/pajek.js +8 -0
  47. package/dist/pajek.js.map +1 -0
  48. package/dist/src/children.d.ts +134 -0
  49. package/dist/src/children.d.ts.map +1 -0
  50. package/dist/src/children.js +274 -0
  51. package/dist/src/children.js.map +1 -0
  52. package/dist/src/common/attributes.d.ts +229 -0
  53. package/dist/src/common/attributes.d.ts.map +1 -0
  54. package/dist/src/common/attributes.js +368 -0
  55. package/dist/src/common/attributes.js.map +1 -0
  56. package/dist/src/common/codes.d.ts +105 -0
  57. package/dist/src/common/codes.d.ts.map +1 -0
  58. package/dist/src/common/codes.js +107 -0
  59. package/dist/src/common/codes.js.map +1 -0
  60. package/dist/src/common/declared-types.d.ts +84 -0
  61. package/dist/src/common/declared-types.d.ts.map +1 -0
  62. package/dist/src/common/declared-types.js +326 -0
  63. package/dist/src/common/declared-types.js.map +1 -0
  64. package/dist/src/common/direction.d.ts +206 -0
  65. package/dist/src/common/direction.d.ts.map +1 -0
  66. package/dist/src/common/direction.js +370 -0
  67. package/dist/src/common/direction.js.map +1 -0
  68. package/dist/src/common/escape.d.ts +92 -0
  69. package/dist/src/common/escape.d.ts.map +1 -0
  70. package/dist/src/common/escape.js +212 -0
  71. package/dist/src/common/escape.js.map +1 -0
  72. package/dist/src/common/export.d.ts +249 -0
  73. package/dist/src/common/export.d.ts.map +1 -0
  74. package/dist/src/common/export.js +594 -0
  75. package/dist/src/common/export.js.map +1 -0
  76. package/dist/src/common/format.d.ts +59 -0
  77. package/dist/src/common/format.d.ts.map +1 -0
  78. package/dist/src/common/format.js +106 -0
  79. package/dist/src/common/format.js.map +1 -0
  80. package/dist/src/common/ids.d.ts +83 -0
  81. package/dist/src/common/ids.d.ts.map +1 -0
  82. package/dist/src/common/ids.js +158 -0
  83. package/dist/src/common/ids.js.map +1 -0
  84. package/dist/src/common/input.d.ts +100 -0
  85. package/dist/src/common/input.d.ts.map +1 -0
  86. package/dist/src/common/input.js +335 -0
  87. package/dist/src/common/input.js.map +1 -0
  88. package/dist/src/common/lists.d.ts +34 -0
  89. package/dist/src/common/lists.d.ts.map +1 -0
  90. package/dist/src/common/lists.js +185 -0
  91. package/dist/src/common/lists.js.map +1 -0
  92. package/dist/src/common/options.d.ts +108 -0
  93. package/dist/src/common/options.d.ts.map +1 -0
  94. package/dist/src/common/options.js +265 -0
  95. package/dist/src/common/options.js.map +1 -0
  96. package/dist/src/common/report.d.ts +187 -0
  97. package/dist/src/common/report.d.ts.map +1 -0
  98. package/dist/src/common/report.js +274 -0
  99. package/dist/src/common/report.js.map +1 -0
  100. package/dist/src/common/temporal.d.ts +71 -0
  101. package/dist/src/common/temporal.d.ts.map +1 -0
  102. package/dist/src/common/temporal.js +266 -0
  103. package/dist/src/common/temporal.js.map +1 -0
  104. package/dist/src/common/text.d.ts +104 -0
  105. package/dist/src/common/text.d.ts.map +1 -0
  106. package/dist/src/common/text.js +255 -0
  107. package/dist/src/common/text.js.map +1 -0
  108. package/dist/src/common/weights.d.ts +77 -0
  109. package/dist/src/common/weights.d.ts.map +1 -0
  110. package/dist/src/common/weights.js +156 -0
  111. package/dist/src/common/weights.js.map +1 -0
  112. package/dist/src/common/writer.d.ts +51 -0
  113. package/dist/src/common/writer.d.ts.map +1 -0
  114. package/dist/src/common/writer.js +108 -0
  115. package/dist/src/common/writer.js.map +1 -0
  116. package/dist/src/common/xml.d.ts +245 -0
  117. package/dist/src/common/xml.d.ts.map +1 -0
  118. package/dist/src/common/xml.js +942 -0
  119. package/dist/src/common/xml.js.map +1 -0
  120. package/dist/src/formats/csv/exporter.d.ts +70 -0
  121. package/dist/src/formats/csv/exporter.d.ts.map +1 -0
  122. package/dist/src/formats/csv/exporter.js +682 -0
  123. package/dist/src/formats/csv/exporter.js.map +1 -0
  124. package/dist/src/formats/csv/header.d.ts +66 -0
  125. package/dist/src/formats/csv/header.d.ts.map +1 -0
  126. package/dist/src/formats/csv/header.js +152 -0
  127. package/dist/src/formats/csv/header.js.map +1 -0
  128. package/dist/src/formats/csv/importer.d.ts +82 -0
  129. package/dist/src/formats/csv/importer.d.ts.map +1 -0
  130. package/dist/src/formats/csv/importer.js +849 -0
  131. package/dist/src/formats/csv/importer.js.map +1 -0
  132. package/dist/src/formats/csv/index.d.ts +60 -0
  133. package/dist/src/formats/csv/index.d.ts.map +1 -0
  134. package/dist/src/formats/csv/index.js +63 -0
  135. package/dist/src/formats/csv/index.js.map +1 -0
  136. package/dist/src/formats/csv/records.d.ts +188 -0
  137. package/dist/src/formats/csv/records.d.ts.map +1 -0
  138. package/dist/src/formats/csv/records.js +702 -0
  139. package/dist/src/formats/csv/records.js.map +1 -0
  140. package/dist/src/formats/csv/values.d.ts +105 -0
  141. package/dist/src/formats/csv/values.d.ts.map +1 -0
  142. package/dist/src/formats/csv/values.js +192 -0
  143. package/dist/src/formats/csv/values.js.map +1 -0
  144. package/dist/src/formats/dot/exporter.d.ts +52 -0
  145. package/dist/src/formats/dot/exporter.d.ts.map +1 -0
  146. package/dist/src/formats/dot/exporter.js +836 -0
  147. package/dist/src/formats/dot/exporter.js.map +1 -0
  148. package/dist/src/formats/dot/importer.d.ts +102 -0
  149. package/dist/src/formats/dot/importer.d.ts.map +1 -0
  150. package/dist/src/formats/dot/importer.js +1291 -0
  151. package/dist/src/formats/dot/importer.js.map +1 -0
  152. package/dist/src/formats/dot/index.d.ts +7 -0
  153. package/dist/src/formats/dot/index.d.ts.map +1 -0
  154. package/dist/src/formats/dot/index.js +7 -0
  155. package/dist/src/formats/dot/index.js.map +1 -0
  156. package/dist/src/formats/dot/names.d.ts +29 -0
  157. package/dist/src/formats/dot/names.d.ts.map +1 -0
  158. package/dist/src/formats/dot/names.js +28 -0
  159. package/dist/src/formats/dot/names.js.map +1 -0
  160. package/dist/src/formats/dot/tokenizer.d.ts +114 -0
  161. package/dist/src/formats/dot/tokenizer.d.ts.map +1 -0
  162. package/dist/src/formats/dot/tokenizer.js +341 -0
  163. package/dist/src/formats/dot/tokenizer.js.map +1 -0
  164. package/dist/src/formats/gexf/exporter.d.ts +56 -0
  165. package/dist/src/formats/gexf/exporter.d.ts.map +1 -0
  166. package/dist/src/formats/gexf/exporter.js +1395 -0
  167. package/dist/src/formats/gexf/exporter.js.map +1 -0
  168. package/dist/src/formats/gexf/importer.d.ts +73 -0
  169. package/dist/src/formats/gexf/importer.d.ts.map +1 -0
  170. package/dist/src/formats/gexf/importer.js +1880 -0
  171. package/dist/src/formats/gexf/importer.js.map +1 -0
  172. package/dist/src/formats/gexf/index.d.ts +96 -0
  173. package/dist/src/formats/gexf/index.d.ts.map +1 -0
  174. package/dist/src/formats/gexf/index.js +97 -0
  175. package/dist/src/formats/gexf/index.js.map +1 -0
  176. package/dist/src/formats/gexf/schema.d.ts +135 -0
  177. package/dist/src/formats/gexf/schema.d.ts.map +1 -0
  178. package/dist/src/formats/gexf/schema.js +323 -0
  179. package/dist/src/formats/gexf/schema.js.map +1 -0
  180. package/dist/src/formats/gml/exporter.d.ts +69 -0
  181. package/dist/src/formats/gml/exporter.d.ts.map +1 -0
  182. package/dist/src/formats/gml/exporter.js +1093 -0
  183. package/dist/src/formats/gml/exporter.js.map +1 -0
  184. package/dist/src/formats/gml/importer.d.ts +66 -0
  185. package/dist/src/formats/gml/importer.d.ts.map +1 -0
  186. package/dist/src/formats/gml/importer.js +1331 -0
  187. package/dist/src/formats/gml/importer.js.map +1 -0
  188. package/dist/src/formats/gml/index.d.ts +85 -0
  189. package/dist/src/formats/gml/index.d.ts.map +1 -0
  190. package/dist/src/formats/gml/index.js +88 -0
  191. package/dist/src/formats/gml/index.js.map +1 -0
  192. package/dist/src/formats/gml/syntax.d.ts +186 -0
  193. package/dist/src/formats/gml/syntax.d.ts.map +1 -0
  194. package/dist/src/formats/gml/syntax.js +467 -0
  195. package/dist/src/formats/gml/syntax.js.map +1 -0
  196. package/dist/src/formats/graphml/constants.d.ts +169 -0
  197. package/dist/src/formats/graphml/constants.d.ts.map +1 -0
  198. package/dist/src/formats/graphml/constants.js +165 -0
  199. package/dist/src/formats/graphml/constants.js.map +1 -0
  200. package/dist/src/formats/graphml/exporter.d.ts +34 -0
  201. package/dist/src/formats/graphml/exporter.d.ts.map +1 -0
  202. package/dist/src/formats/graphml/exporter.js +1176 -0
  203. package/dist/src/formats/graphml/exporter.js.map +1 -0
  204. package/dist/src/formats/graphml/importer.d.ts +31 -0
  205. package/dist/src/formats/graphml/importer.d.ts.map +1 -0
  206. package/dist/src/formats/graphml/importer.js +1607 -0
  207. package/dist/src/formats/graphml/importer.js.map +1 -0
  208. package/dist/src/formats/graphml/index.d.ts +8 -0
  209. package/dist/src/formats/graphml/index.d.ts.map +1 -0
  210. package/dist/src/formats/graphml/index.js +8 -0
  211. package/dist/src/formats/graphml/index.js.map +1 -0
  212. package/dist/src/formats/graphml/tree.d.ts +72 -0
  213. package/dist/src/formats/graphml/tree.d.ts.map +1 -0
  214. package/dist/src/formats/graphml/tree.js +290 -0
  215. package/dist/src/formats/graphml/tree.js.map +1 -0
  216. package/dist/src/formats/json/dialect.d.ts +125 -0
  217. package/dist/src/formats/json/dialect.d.ts.map +1 -0
  218. package/dist/src/formats/json/dialect.js +262 -0
  219. package/dist/src/formats/json/dialect.js.map +1 -0
  220. package/dist/src/formats/json/exporter.d.ts +89 -0
  221. package/dist/src/formats/json/exporter.d.ts.map +1 -0
  222. package/dist/src/formats/json/exporter.js +1358 -0
  223. package/dist/src/formats/json/exporter.js.map +1 -0
  224. package/dist/src/formats/json/importer.d.ts +108 -0
  225. package/dist/src/formats/json/importer.d.ts.map +1 -0
  226. package/dist/src/formats/json/importer.js +1838 -0
  227. package/dist/src/formats/json/importer.js.map +1 -0
  228. package/dist/src/formats/json/index.d.ts +8 -0
  229. package/dist/src/formats/json/index.d.ts.map +1 -0
  230. package/dist/src/formats/json/index.js +8 -0
  231. package/dist/src/formats/json/index.js.map +1 -0
  232. package/dist/src/formats/neo4j/exporter.d.ts +68 -0
  233. package/dist/src/formats/neo4j/exporter.d.ts.map +1 -0
  234. package/dist/src/formats/neo4j/exporter.js +1055 -0
  235. package/dist/src/formats/neo4j/exporter.js.map +1 -0
  236. package/dist/src/formats/neo4j/header.d.ts +52 -0
  237. package/dist/src/formats/neo4j/header.d.ts.map +1 -0
  238. package/dist/src/formats/neo4j/header.js +131 -0
  239. package/dist/src/formats/neo4j/header.js.map +1 -0
  240. package/dist/src/formats/neo4j/importer.d.ts +73 -0
  241. package/dist/src/formats/neo4j/importer.d.ts.map +1 -0
  242. package/dist/src/formats/neo4j/importer.js +932 -0
  243. package/dist/src/formats/neo4j/importer.js.map +1 -0
  244. package/dist/src/formats/neo4j/index.d.ts +79 -0
  245. package/dist/src/formats/neo4j/index.d.ts.map +1 -0
  246. package/dist/src/formats/neo4j/index.js +83 -0
  247. package/dist/src/formats/neo4j/index.js.map +1 -0
  248. package/dist/src/formats/pajek/exporter.d.ts +58 -0
  249. package/dist/src/formats/pajek/exporter.d.ts.map +1 -0
  250. package/dist/src/formats/pajek/exporter.js +825 -0
  251. package/dist/src/formats/pajek/exporter.js.map +1 -0
  252. package/dist/src/formats/pajek/importer.d.ts +88 -0
  253. package/dist/src/formats/pajek/importer.d.ts.map +1 -0
  254. package/dist/src/formats/pajek/importer.js +1047 -0
  255. package/dist/src/formats/pajek/importer.js.map +1 -0
  256. package/dist/src/formats/pajek/index.d.ts +7 -0
  257. package/dist/src/formats/pajek/index.d.ts.map +1 -0
  258. package/dist/src/formats/pajek/index.js +7 -0
  259. package/dist/src/formats/pajek/index.js.map +1 -0
  260. package/dist/src/formats/pajek/syntax.d.ts +112 -0
  261. package/dist/src/formats/pajek/syntax.d.ts.map +1 -0
  262. package/dist/src/formats/pajek/syntax.js +269 -0
  263. package/dist/src/formats/pajek/syntax.js.map +1 -0
  264. package/dist/src/index.d.ts +35 -0
  265. package/dist/src/index.d.ts.map +1 -0
  266. package/dist/src/index.js +39 -0
  267. package/dist/src/index.js.map +1 -0
  268. package/dist/src/registry.d.ts +207 -0
  269. package/dist/src/registry.d.ts.map +1 -0
  270. package/dist/src/registry.js +481 -0
  271. package/dist/src/registry.js.map +1 -0
  272. package/dist/src/sniff.d.ts +104 -0
  273. package/dist/src/sniff.d.ts.map +1 -0
  274. package/dist/src/sniff.js +357 -0
  275. package/dist/src/sniff.js.map +1 -0
  276. package/dist/src/types.d.ts +238 -0
  277. package/dist/src/types.d.ts.map +1 -0
  278. package/dist/src/types.js +29 -0
  279. package/dist/src/types.js.map +1 -0
  280. package/dist/tsconfig.build.tsbuildinfo +1 -0
  281. package/package.json +122 -7
  282. package/src/children.ts +335 -0
  283. package/src/common/attributes.ts +520 -0
  284. package/src/common/codes.ts +153 -0
  285. package/src/common/declared-types.ts +374 -0
  286. package/src/common/direction.ts +518 -0
  287. package/src/common/escape.ts +231 -0
  288. package/src/common/export.ts +817 -0
  289. package/src/common/format.ts +111 -0
  290. package/src/common/ids.ts +176 -0
  291. package/src/common/input.ts +378 -0
  292. package/src/common/lists.ts +196 -0
  293. package/src/common/options.ts +377 -0
  294. package/src/common/report.ts +352 -0
  295. package/src/common/temporal.ts +302 -0
  296. package/src/common/text.ts +294 -0
  297. package/src/common/weights.ts +202 -0
  298. package/src/common/writer.ts +123 -0
  299. package/src/common/xml.ts +1053 -0
  300. package/src/formats/csv/exporter.ts +894 -0
  301. package/src/formats/csv/header.ts +172 -0
  302. package/src/formats/csv/importer.ts +1104 -0
  303. package/src/formats/csv/index.ts +88 -0
  304. package/src/formats/csv/records.ts +813 -0
  305. package/src/formats/csv/values.ts +224 -0
  306. package/src/formats/dot/exporter.ts +1014 -0
  307. package/src/formats/dot/importer.ts +1549 -0
  308. package/src/formats/dot/index.ts +7 -0
  309. package/src/formats/dot/names.ts +40 -0
  310. package/src/formats/dot/tokenizer.ts +384 -0
  311. package/src/formats/gexf/exporter.ts +1696 -0
  312. package/src/formats/gexf/importer.ts +2333 -0
  313. package/src/formats/gexf/index.ts +142 -0
  314. package/src/formats/gexf/schema.ts +361 -0
  315. package/src/formats/gml/exporter.ts +1404 -0
  316. package/src/formats/gml/importer.ts +1591 -0
  317. package/src/formats/gml/index.ts +128 -0
  318. package/src/formats/gml/syntax.ts +545 -0
  319. package/src/formats/graphml/constants.ts +225 -0
  320. package/src/formats/graphml/exporter.ts +1458 -0
  321. package/src/formats/graphml/importer.ts +2027 -0
  322. package/src/formats/graphml/index.ts +8 -0
  323. package/src/formats/graphml/tree.ts +318 -0
  324. package/src/formats/json/dialect.ts +317 -0
  325. package/src/formats/json/exporter.ts +1616 -0
  326. package/src/formats/json/importer.ts +2271 -0
  327. package/src/formats/json/index.ts +8 -0
  328. package/src/formats/neo4j/exporter.ts +1287 -0
  329. package/src/formats/neo4j/header.ts +156 -0
  330. package/src/formats/neo4j/importer.ts +1220 -0
  331. package/src/formats/neo4j/index.ts +116 -0
  332. package/src/formats/pajek/exporter.ts +1000 -0
  333. package/src/formats/pajek/importer.ts +1311 -0
  334. package/src/formats/pajek/index.ts +7 -0
  335. package/src/formats/pajek/syntax.ts +307 -0
  336. package/src/index.ts +244 -0
  337. package/src/registry.ts +617 -0
  338. package/src/sniff.ts +397 -0
  339. package/src/types.ts +262 -0
@@ -0,0 +1,294 @@
1
+ /**
2
+ * The fixed lexical grammar of design section 5.1 for untyped text sources (CSV cells, GML
3
+ * values, DOT strings, Pajek tokens), reproduced from the core's reference implementation so the
4
+ * two agree (invariant I15). Text importers parse each cell with parseTextCell() and push the JS
5
+ * value; the sink's per-column inference then widens `bool -> i32 -> f64 -> string` and never per
6
+ * cell, so a column that saw `1` and then `"01"` becomes string for all rows.
7
+ *
8
+ * - `bool` is exactly `true` / `false` (case-sensitive);
9
+ * - `i32` is `/^-?(0|[1-9][0-9]*)$/` within `[-2^31, 2^31)`; `0` / `1` are i32, never bool;
10
+ * - `f64` is a decimal or exponent literal accepted by `Number()` that is not empty, not
11
+ * whitespace, not `Infinity` / `NaN`, not a hex / octal / binary form, has no leading zeros in
12
+ * its integer part, and whose value is finite (an integer literal outside i32 range is f64);
13
+ * - everything else is `string`.
14
+ */
15
+
16
+ import { type ColumnHandle, GraphFormatError, type GraphSink, INVALID_INDEX } from "@graphty/graph-format";
17
+
18
+ import { type ImportReportBuilder } from "./report.js";
19
+
20
+ const I32_TEXT = /^-?(0|[1-9][0-9]*)$/;
21
+ const F64_TEXT = /^[+-]?((0|[1-9][0-9]*)(\.[0-9]*)?|\.[0-9]+)([eE][+-]?[0-9]+)?$/;
22
+ const I32_MIN = -2147483648;
23
+ const I32_MAX = 2147483647;
24
+
25
+ /**
26
+ * The dtype a text cell parses as under the design section 5.1 grammar.
27
+ * Consumed by the per-format importers and exporters under src/formats.
28
+ * @public
29
+ */
30
+ export type TextDtype = "bool" | "i32" | "f64" | "string";
31
+
32
+ /**
33
+ * Classify one text cell by the fixed grammar.
34
+ * @param text - the cell text, exactly as read (no trimming)
35
+ * @returns bool, i32, f64 or string
36
+ */
37
+ export function inferTextDtype(text: string): TextDtype {
38
+ if (text === "true" || text === "false") {
39
+ return "bool";
40
+ }
41
+ if (I32_TEXT.test(text)) {
42
+ const n = Number(text);
43
+ if (n >= I32_MIN && n <= I32_MAX) {
44
+ return "i32";
45
+ }
46
+ return Number.isFinite(n) ? "f64" : "string";
47
+ }
48
+ if (F64_TEXT.test(text) && Number.isFinite(Number(text))) {
49
+ return "f64";
50
+ }
51
+ return "string";
52
+ }
53
+
54
+ /**
55
+ * Parse one text cell into the JS value its grammar class implies: a boolean for bool, a number for
56
+ * i32 / f64, the text itself for string. The sink infers the column dtype from the value.
57
+ * @param text - the cell text, exactly as read
58
+ * @returns the value
59
+ */
60
+ export function parseTextCell(text: string): boolean | number | string {
61
+ switch (inferTextDtype(text)) {
62
+ case "bool":
63
+ return text === "true";
64
+ case "i32":
65
+ case "f64":
66
+ return Number(text);
67
+ case "string":
68
+ return text;
69
+ default:
70
+ return text;
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Whether a text cell is a number under the f64 grammar (an i32 or f64 literal).
76
+ * @param text - the cell text
77
+ * @returns true when parseTextCell(text) returns a number
78
+ */
79
+ export function isNumericText(text: string): boolean {
80
+ const dtype = inferTextDtype(text);
81
+ return dtype === "i32" || dtype === "f64";
82
+ }
83
+
84
+ /**
85
+ * Whether a numeric cell text is the canonical spelling of its value (`String(value) === text`),
86
+ * so that the sink's own re-formatting of the value would reproduce it.
87
+ * @param text - the cell text
88
+ * @param value - the parsed value
89
+ * @returns true when the lexical form survives a widening to string
90
+ */
91
+ function isCanonicalNumberText(text: string, value: number): boolean {
92
+ return String(value) === text;
93
+ }
94
+
95
+ /**
96
+ * The widening rank of a text dtype in the design section 5.1 order.
97
+ * @param dtype - the text dtype
98
+ * @returns 0 for bool, 1 for i32, 2 for f64, 3 for string
99
+ */
100
+ function textDtypeRank(dtype: TextDtype): number {
101
+ switch (dtype) {
102
+ case "bool":
103
+ return 0;
104
+ case "i32":
105
+ return 1;
106
+ case "f64":
107
+ return 2;
108
+ case "string":
109
+ return 3;
110
+ default: {
111
+ const name: string = dtype;
112
+ throw new GraphFormatError("E_UNSUPPORTED", `unknown text dtype ${name}`, { dtype: name });
113
+ }
114
+ }
115
+ }
116
+
117
+ /**
118
+ * The inferred-column writer of the untyped text formats (CSV, DOT, Pajek; design section 5.1):
119
+ * parses every cell by the fixed grammar and pushes the scalar, and keeps the COLUMN's dtype the
120
+ * one the grammar implies rather than the one the values happen to imply:
121
+ *
122
+ * - a column whose cells are all `2.0`-style f64 text is widened to f64 through the sink's
123
+ * widening call even though every value is integral (the values alone would infer i32);
124
+ * - a numeric cell whose text is not the canonical spelling of its value (`1e5`, `-0`, `1.0`) is
125
+ * remembered, and when a later cell widens the column to string the original texts are written
126
+ * back, so the lexical form is never rewritten by the widening.
127
+ *
128
+ * A sink without the optional widening call keeps the value-inferred dtype and the writer records
129
+ * one W_WIDENING_UNSUPPORTED warning per column.
130
+ * Consumed by the per-format importers under src/formats.
131
+ * @public
132
+ */
133
+ export class TextCellWriter {
134
+ /** The column name in the sink. */
135
+ readonly name: string;
136
+
137
+ private readonly domain: "node" | "edge";
138
+
139
+ private readonly sink: GraphSink;
140
+
141
+ private readonly report: ImportReportBuilder;
142
+
143
+ private handle: ColumnHandle = INVALID_INDEX as ColumnHandle;
144
+
145
+ /** The widest text dtype seen (the column's dtype under the 5.1 grammar). */
146
+ private textDtype: TextDtype | null = null;
147
+
148
+ /** The widest dtype the pushed values imply (what the sink inferred on its own). */
149
+ private valueDtype: TextDtype | null = null;
150
+
151
+ private readonly keptRows: number[] = [];
152
+
153
+ private readonly keptTexts: string[] = [];
154
+
155
+ /**
156
+ * Create a writer; the column is declared by the sink's inference on the first write.
157
+ * @param name - the column name
158
+ * @param domain - node or edge
159
+ * @param sink - the sink
160
+ * @param report - the report the widening warning is recorded in
161
+ */
162
+ constructor(name: string, domain: "node" | "edge", sink: GraphSink, report: ImportReportBuilder) {
163
+ this.name = name;
164
+ this.domain = domain;
165
+ this.sink = sink;
166
+ this.report = report;
167
+ }
168
+
169
+ /**
170
+ * The column handle once the first cell was written.
171
+ * @returns the handle, or INVALID_INDEX before the first write
172
+ */
173
+ get column(): ColumnHandle {
174
+ return this.handle;
175
+ }
176
+
177
+ /**
178
+ * Write one cell text.
179
+ * @param row - the node or edge index
180
+ * @param text - the cell text, exactly as read
181
+ */
182
+ write(row: number, text: string): void {
183
+ const kind = inferTextDtype(text);
184
+ const value = parseTextCell(text);
185
+ const wasString = this.textDtype === "string";
186
+ const textDtype =
187
+ this.textDtype === null || textDtypeRank(kind) > textDtypeRank(this.textDtype) ? kind : this.textDtype;
188
+ // a string column keeps every cell's lexical form; a numeric text is never re-spelled. The
189
+ // sink may refuse the value (a declared column of another dtype): the state advances only
190
+ // once the cell is written, so a refused cell leaves the writer as it was.
191
+ this.set(row, textDtype === "string" ? text : value);
192
+ this.textDtype = textDtype;
193
+ const valueKind = kindOfValue(value);
194
+ if (this.valueDtype === null || textDtypeRank(valueKind) > textDtypeRank(this.valueDtype)) {
195
+ this.valueDtype = valueKind;
196
+ }
197
+ if (this.textDtype === "string") {
198
+ if (!wasString && this.keptRows.length > 0) {
199
+ // the sink just widened the column to string from the values; restore the texts
200
+ // whose lexical form the values did not carry
201
+ for (let i = 0; i < this.keptRows.length; i++) {
202
+ this.set(this.keptRows[i], this.keptTexts[i]);
203
+ }
204
+ this.keptRows.length = 0;
205
+ this.keptTexts.length = 0;
206
+ }
207
+ return;
208
+ }
209
+ if (typeof value === "number" && !isCanonicalNumberText(text, value)) {
210
+ this.keptRows.push(row);
211
+ this.keptTexts.push(text);
212
+ }
213
+ if (this.textDtype === "f64" && this.valueDtype !== "f64") {
214
+ this.widen("f64");
215
+ }
216
+ }
217
+
218
+ /**
219
+ * Write a value (the parsed scalar, or a text into a widened column).
220
+ * @param row - the row
221
+ * @param value - the value
222
+ */
223
+ private set(row: number, value: unknown): void {
224
+ if (this.handle === INVALID_INDEX) {
225
+ if (this.domain === "node") {
226
+ this.sink.setNodeValue(this.name, row, value);
227
+ this.handle = this.sink.nodeColumn(this.name);
228
+ } else {
229
+ this.sink.setEdgeValue(this.name, row, value);
230
+ this.handle = this.sink.edgeColumn(this.name);
231
+ }
232
+ return;
233
+ }
234
+ if (this.domain === "node") {
235
+ this.sink.setNodeValue(this.handle, row, value);
236
+ } else {
237
+ this.sink.setEdgeValue(this.handle, row, value);
238
+ }
239
+ }
240
+
241
+ /**
242
+ * Widen the column to the dtype the text grammar implies, through the sink's optional call.
243
+ * @param dtype - the dtype
244
+ */
245
+ private widen(dtype: "f64"): void {
246
+ const { sink } = this;
247
+ const supported =
248
+ this.domain === "node" ? sink.widenNodeColumn !== undefined : sink.widenEdgeColumn !== undefined;
249
+ if (!supported) {
250
+ this.report.warnOnce(
251
+ "coercion",
252
+ WIDENING_UNSUPPORTED_CODE,
253
+ `column "${this.name}" holds ${dtype} text but the sink cannot widen an inferred column; it keeps the value-inferred dtype`,
254
+ { element: this.name },
255
+ );
256
+ this.valueDtype = dtype;
257
+ return;
258
+ }
259
+ try {
260
+ if (this.domain === "node") {
261
+ sink.widenNodeColumn?.(this.handle, dtype);
262
+ } else {
263
+ sink.widenEdgeColumn?.(this.handle, dtype);
264
+ }
265
+ this.valueDtype = dtype;
266
+ } catch (err) {
267
+ if (!(err instanceof GraphFormatError) || err.code !== "E_COLUMN_TYPE") {
268
+ throw err;
269
+ }
270
+ // a caller's declared column of the name: its dtype stands
271
+ this.valueDtype = dtype;
272
+ }
273
+ }
274
+ }
275
+
276
+ /** Issue code: the sink has no widening call, so a text column keeps the dtype its values imply. */
277
+ export const WIDENING_UNSUPPORTED_CODE = "W_WIDENING_UNSUPPORTED";
278
+
279
+ /**
280
+ * The text dtype a parsed cell value implies on its own (what the sink's inference sees).
281
+ * @param value - the parsed value
282
+ * @returns the dtype
283
+ */
284
+ function kindOfValue(value: boolean | number | string): TextDtype {
285
+ if (typeof value === "boolean") {
286
+ return "bool";
287
+ }
288
+ if (typeof value === "string") {
289
+ return "string";
290
+ }
291
+ return Number.isInteger(value) && value >= -2147483648 && value <= 2147483647 && !Object.is(value, -0)
292
+ ? "i32"
293
+ : "f64";
294
+ }
@@ -0,0 +1,202 @@
1
+ /**
2
+ * Weight resolution shared by every importer (design sections 3.7 and 8.4): which source field is
3
+ * THE weight (`weightFrom`, per-format default, null = unweighted), how its text or JSON value
4
+ * becomes the number passed to `addEdge`, and the E_INVALID_WEIGHT boundary.
5
+ *
6
+ * An edge whose weight field is absent or blank is pushed WITHOUT a weight argument, never with an
7
+ * explicit 1: the builder's weightSet bitmap then records that the weight was defaulted, and the
8
+ * exporter re-emits no weight for it (decision D-WSET). NaN and non-numeric text are
9
+ * E_INVALID_WEIGHT before the sink is touched, so the importer records the issue and skips the edge
10
+ * with the sink unchanged; Infinity and negative values are legal weights (design section 3.7).
11
+ */
12
+
13
+ import { type Column, GraphFormatError, type GraphSnapshot } from "@graphty/graph-format";
14
+
15
+ import { parseDecimalText } from "./declared-types.js";
16
+ import { formatF32, formatF64, formatInteger } from "./format.js";
17
+
18
+ /** The weight of an edge added without one (design section 3.7). */
19
+ export const DEFAULT_WEIGHT = 1;
20
+
21
+ /**
22
+ * Whether a source field is the weight field under the resolved `weightFrom` option.
23
+ * @param name - the field name (attribute title, CSV header, JSON key)
24
+ * @param weightFrom - the resolved option; null means unweighted
25
+ * @returns true when the field is THE weight
26
+ */
27
+ export function isWeightField(name: string, weightFrom: string | null): boolean {
28
+ return weightFrom !== null && name === weightFrom;
29
+ }
30
+
31
+ /**
32
+ * The weight argument of addEdge from a text cell: undefined for a blank cell (the weight is
33
+ * omitted), the number otherwise.
34
+ * @param text - the cell text
35
+ * @returns the weight, or undefined when blank; E_INVALID_WEIGHT for NaN or non-numeric text
36
+ */
37
+ export function parseWeightText(text: string): number | undefined {
38
+ const trimmed = text.trim();
39
+ if (trimmed.length === 0) {
40
+ return undefined;
41
+ }
42
+ let value: number;
43
+ try {
44
+ value = parseDecimalText(trimmed);
45
+ } catch (err) {
46
+ throw invalidWeight(text, err);
47
+ }
48
+ if (Number.isNaN(value)) {
49
+ throw invalidWeight(text, null);
50
+ }
51
+ return value;
52
+ }
53
+
54
+ /**
55
+ * The weight argument of addEdge from a typed value (JSON, a record field): undefined for null /
56
+ * undefined, the number for a finite or infinite number, the parsed number for numeric text.
57
+ * @param value - the field value
58
+ * @returns the weight, or undefined when absent; E_INVALID_WEIGHT for NaN, a boolean, an object or non-numeric text
59
+ */
60
+ export function weightFromValue(value: unknown): number | undefined {
61
+ if (value === undefined || value === null) {
62
+ return undefined;
63
+ }
64
+ if (typeof value === "number") {
65
+ if (Number.isNaN(value)) {
66
+ throw invalidWeight(value, null);
67
+ }
68
+ return value;
69
+ }
70
+ if (typeof value === "string") {
71
+ return parseWeightText(value);
72
+ }
73
+ throw invalidWeight(value, null);
74
+ }
75
+
76
+ /**
77
+ * The E_INVALID_WEIGHT error of a rejected value.
78
+ * @param value - the rejected value
79
+ * @param cause - the parse error, or null
80
+ * @returns the error
81
+ */
82
+ function invalidWeight(value: unknown, cause: unknown): GraphFormatError {
83
+ let shown: string;
84
+ switch (typeof value) {
85
+ case "string":
86
+ shown = JSON.stringify(value);
87
+ break;
88
+ case "number":
89
+ shown = String(value);
90
+ break;
91
+ default:
92
+ shown = typeof value;
93
+ }
94
+ return new GraphFormatError("E_INVALID_WEIGHT", `invalid edge weight ${shown}`, {
95
+ value: typeof value === "object" ? typeof value : value,
96
+ cause: cause instanceof Error ? cause.message : null,
97
+ });
98
+ }
99
+
100
+ // ============================================================ the exporter side
101
+
102
+ /**
103
+ * The explicit weights of a snapshot for exporters (design sections 3.7 and 8.5): the weight
104
+ * role column when present (its validity says which edges had an explicit weight, its dtype how
105
+ * the value is written), else `edgeList().weights` as f32 for every edge of a weighted snapshot;
106
+ * nothing for an unweighted one. One implementation for every exporter.
107
+ * Consumed by the per-format exporters under src/formats.
108
+ * @public
109
+ */
110
+ export interface ExplicitWeights {
111
+ /** Whether any edge can have an explicit weight (the snapshot is weighted). */
112
+ readonly weighted: boolean;
113
+ /** The dtype the values are read from: the shadow column's, or f32 for the arc array. */
114
+ readonly dtype: "f32" | "f64";
115
+ /**
116
+ * Whether an edge's weight was explicit in the source.
117
+ * @param e - the logical edge index
118
+ * @returns true when the exporter must write it
119
+ */
120
+ isExplicit(e: number): boolean;
121
+ /**
122
+ * The weight value of an edge (meaningful when explicit).
123
+ * @param e - the logical edge index
124
+ * @returns the value
125
+ */
126
+ value(e: number): number;
127
+ /**
128
+ * The weight text of an edge: the shortest round-tripping decimal for its dtype, or the
129
+ * integer digits when `integral` is requested and the value is an integer; null when the
130
+ * weight was defaulted.
131
+ * @param e - the logical edge index
132
+ * @param integral - write an integral value without a decimal part (formats declaring an int weight)
133
+ * @returns the text, or null
134
+ */
135
+ text(e: number, integral?: boolean): string | null;
136
+ }
137
+
138
+ /** The dtypes a weight role column may have (an integer column declared by a caller included). */
139
+ const NUMERIC_DTYPES: ReadonlySet<string> = new Set(["f32", "f64", "i32", "u32", "u8"]);
140
+
141
+ /**
142
+ * The shortest round-tripping text of a weight by the shadow column's dtype.
143
+ * @param dtype - the shadow column's dtype
144
+ * @returns the formatter
145
+ */
146
+ function weightFormatter(dtype: string): (value: number) => string {
147
+ switch (dtype) {
148
+ case "f32":
149
+ return formatF32;
150
+ case "f64":
151
+ return formatF64;
152
+ default:
153
+ return formatInteger;
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Build the explicit-weight view of a snapshot.
159
+ * @param snapshot - the snapshot
160
+ * @returns the view
161
+ */
162
+ export function explicitWeights(snapshot: GraphSnapshot): ExplicitWeights {
163
+ const shadowColumn = snapshot.edges.byRole("weight");
164
+ const shadow: Column | null = shadowColumn !== null && NUMERIC_DTYPES.has(shadowColumn.dtype) ? shadowColumn : null;
165
+ if (shadow !== null) {
166
+ const dtype = shadow.dtype === "f32" ? "f32" : "f64";
167
+ const format = weightFormatter(shadow.dtype);
168
+ return {
169
+ weighted: true,
170
+ dtype,
171
+ isExplicit: (e: number): boolean => shadow.isSet(e),
172
+ value: (e: number): number => shadow.value(e) as number,
173
+ text: (e: number, integral = false): string | null => {
174
+ if (!shadow.isSet(e)) {
175
+ return null;
176
+ }
177
+ const value = shadow.value(e) as number;
178
+ return integral && Number.isInteger(value) ? formatInteger(value) : format(value);
179
+ },
180
+ };
181
+ }
182
+ const { weights } = snapshot.edgeList();
183
+ if (weights === null || !snapshot.flags.weighted) {
184
+ return {
185
+ weighted: false,
186
+ dtype: "f32",
187
+ isExplicit: (): boolean => false,
188
+ value: (): number => DEFAULT_WEIGHT,
189
+ text: (): string | null => null,
190
+ };
191
+ }
192
+ return {
193
+ weighted: true,
194
+ dtype: "f32",
195
+ isExplicit: (): boolean => true,
196
+ value: (e: number): number => weights[e],
197
+ text: (e: number, integral = false): string => {
198
+ const value = weights[e];
199
+ return integral && Number.isInteger(value) ? formatInteger(value) : formatF32(value);
200
+ },
201
+ };
202
+ }
@@ -0,0 +1,123 @@
1
+ /**
2
+ * The output side of the exporter contract (design sections 8.5 and 12.4): an exporter produces
3
+ * its document as a sequence of text parts (a sync or async generator of strings, one per line or
4
+ * element); these helpers turn that sequence into `export()`'s `AsyncIterable<Uint8Array>` of
5
+ * UTF-8 chunks of a bounded size, into `exportToString()`'s one string, or into a
6
+ * `ReadableStream<Uint8Array>` for a caller that wants a stream.
7
+ */
8
+
9
+ /**
10
+ * Text parts an exporter produces.
11
+ * Consumed by the per-format importers and exporters under src/formats.
12
+ * @public
13
+ */
14
+ export type TextParts = Iterable<string> | AsyncIterable<string>;
15
+
16
+ /** The target size of one encoded chunk; parts are coalesced up to it and never split. */
17
+ export const DEFAULT_CHUNK_BYTES = 64 * 1024;
18
+
19
+ /**
20
+ * Encode text parts as UTF-8 chunks of about `chunkBytes` each. Small parts are coalesced; a part
21
+ * larger than the chunk size is emitted whole. The last chunk may be short; nothing is emitted for
22
+ * an empty document.
23
+ * @param parts - the text parts
24
+ * @param chunkBytes - the target chunk size in bytes
25
+ * @yields UTF-8 chunks
26
+ * @returns nothing
27
+ */
28
+ export async function* encodeChunks(
29
+ parts: TextParts,
30
+ chunkBytes: number = DEFAULT_CHUNK_BYTES,
31
+ ): AsyncGenerator<Uint8Array, void, undefined> {
32
+ const encoder = new TextEncoder();
33
+ let pending: string[] = [];
34
+ let pendingUnits = 0;
35
+ for await (const part of parts) {
36
+ if (part.length === 0) {
37
+ continue;
38
+ }
39
+ pending.push(part);
40
+ pendingUnits += part.length;
41
+ // UTF-16 code units are a lower bound on the UTF-8 byte count; flush when the lower bound reaches the target
42
+ if (pendingUnits >= chunkBytes) {
43
+ yield encoder.encode(pending.length === 1 ? pending[0] : pending.join(""));
44
+ pending = [];
45
+ pendingUnits = 0;
46
+ }
47
+ }
48
+ if (pending.length > 0) {
49
+ yield encoder.encode(pending.length === 1 ? pending[0] : pending.join(""));
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Join text parts into one string.
55
+ * @param parts - the text parts
56
+ * @returns the whole document
57
+ */
58
+ export async function joinText(parts: TextParts): Promise<string> {
59
+ const collected: string[] = [];
60
+ for await (const part of parts) {
61
+ collected.push(part);
62
+ }
63
+ return collected.join("");
64
+ }
65
+
66
+ /**
67
+ * Decode UTF-8 chunks back into one string (for tests and for callers holding an export() result).
68
+ * @param chunks - the chunks
69
+ * @returns the decoded text
70
+ */
71
+ export async function decodeChunks(chunks: AsyncIterable<Uint8Array>): Promise<string> {
72
+ const decoder = new TextDecoder("utf-8", { fatal: true });
73
+ const parts: string[] = [];
74
+ for await (const chunk of chunks) {
75
+ parts.push(decoder.decode(chunk, { stream: true }));
76
+ }
77
+ parts.push(decoder.decode());
78
+ return parts.join("");
79
+ }
80
+
81
+ /**
82
+ * Collect UTF-8 chunks into one Uint8Array.
83
+ * @param chunks - the chunks
84
+ * @returns the concatenated bytes
85
+ */
86
+ export async function collectBytes(chunks: AsyncIterable<Uint8Array>): Promise<Uint8Array> {
87
+ const parts: Uint8Array[] = [];
88
+ let total = 0;
89
+ for await (const chunk of chunks) {
90
+ parts.push(chunk);
91
+ total += chunk.byteLength;
92
+ }
93
+ const out = new Uint8Array(total);
94
+ let offset = 0;
95
+ for (const part of parts) {
96
+ out.set(part, offset);
97
+ offset += part.byteLength;
98
+ }
99
+ return out;
100
+ }
101
+
102
+ /**
103
+ * Wrap an async iterable of chunks as a ReadableStream, pulling one chunk per read and cancelling
104
+ * the iterable when the stream is cancelled.
105
+ * @param chunks - the chunks
106
+ * @returns a byte stream
107
+ */
108
+ export function toReadableStream(chunks: AsyncIterable<Uint8Array>): ReadableStream<Uint8Array> {
109
+ const iterator = chunks[Symbol.asyncIterator]();
110
+ return new ReadableStream<Uint8Array>({
111
+ async pull(controller): Promise<void> {
112
+ const { done, value } = await iterator.next();
113
+ if (done) {
114
+ controller.close();
115
+ return;
116
+ }
117
+ controller.enqueue(value);
118
+ },
119
+ async cancel(): Promise<void> {
120
+ await iterator.return?.(undefined);
121
+ },
122
+ });
123
+ }