@accounter/opcn1214-generator 0.0.0-bootstrap.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 (477) hide show
  1. package/.bob/cjs/api/generate.d.ts +44 -0
  2. package/.bob/cjs/api/generate.js +115 -0
  3. package/.bob/cjs/api/parse.d.ts +28 -0
  4. package/.bob/cjs/api/parse.js +38 -0
  5. package/.bob/cjs/api/response.d.ts +24 -0
  6. package/.bob/cjs/api/response.js +75 -0
  7. package/.bob/cjs/api/validate.d.ts +16 -0
  8. package/.bob/cjs/api/validate.js +15 -0
  9. package/.bob/cjs/codec/boolean.d.ts +7 -0
  10. package/.bob/cjs/codec/boolean.js +49 -0
  11. package/.bob/cjs/codec/date.d.ts +6 -0
  12. package/.bob/cjs/codec/date.js +68 -0
  13. package/.bob/cjs/codec/enum.d.ts +4 -0
  14. package/.bob/cjs/codec/enum.js +26 -0
  15. package/.bob/cjs/codec/numeric.d.ts +56 -0
  16. package/.bob/cjs/codec/numeric.js +186 -0
  17. package/.bob/cjs/codec/padding.d.ts +10 -0
  18. package/.bob/cjs/codec/padding.js +26 -0
  19. package/.bob/cjs/codec/percent.d.ts +6 -0
  20. package/.bob/cjs/codec/percent.js +33 -0
  21. package/.bob/cjs/codec/phone.d.ts +4 -0
  22. package/.bob/cjs/codec/phone.js +48 -0
  23. package/.bob/cjs/codec/record.d.ts +18 -0
  24. package/.bob/cjs/codec/record.js +176 -0
  25. package/.bob/cjs/codec/registry.d.ts +8 -0
  26. package/.bob/cjs/codec/registry.js +27 -0
  27. package/.bob/cjs/codec/sections.d.ts +41 -0
  28. package/.bob/cjs/codec/sections.js +177 -0
  29. package/.bob/cjs/codec/text.d.ts +5 -0
  30. package/.bob/cjs/codec/text.js +29 -0
  31. package/.bob/cjs/codec/types.d.ts +36 -0
  32. package/.bob/cjs/codec/types.js +16 -0
  33. package/.bob/cjs/codec/windows1255.d.ts +9 -0
  34. package/.bob/cjs/codec/windows1255.js +38 -0
  35. package/.bob/cjs/errors.d.ts +32 -0
  36. package/.bob/cjs/errors.js +103 -0
  37. package/.bob/cjs/file/assemble.d.ts +17 -0
  38. package/.bob/cjs/file/assemble.js +28 -0
  39. package/.bob/cjs/file/control.d.ts +13 -0
  40. package/.bob/cjs/file/control.js +45 -0
  41. package/.bob/cjs/file/parse.d.ts +18 -0
  42. package/.bob/cjs/file/parse.js +478 -0
  43. package/.bob/cjs/file/split.d.ts +17 -0
  44. package/.bob/cjs/file/split.js +71 -0
  45. package/.bob/cjs/index.d.ts +16 -0
  46. package/.bob/cjs/index.js +28 -0
  47. package/.bob/cjs/layout/2024/layout.generated.d.ts +6919 -0
  48. package/.bob/cjs/layout/2024/layout.generated.js +7224 -0
  49. package/.bob/cjs/layout/2025/index.d.ts +6 -0
  50. package/.bob/cjs/layout/2025/index.js +10 -0
  51. package/.bob/cjs/layout/capital-gains.d.ts +9 -0
  52. package/.bob/cjs/layout/capital-gains.js +15 -0
  53. package/.bob/cjs/layout/countries.d.ts +5 -0
  54. package/.bob/cjs/layout/countries.js +10 -0
  55. package/.bob/cjs/layout/freeze.d.ts +5 -0
  56. package/.bob/cjs/layout/freeze.js +16 -0
  57. package/.bob/cjs/layout/registry.d.ts +10 -0
  58. package/.bob/cjs/layout/registry.js +32 -0
  59. package/.bob/cjs/layout/types.d.ts +82 -0
  60. package/.bob/cjs/layout/types.js +35 -0
  61. package/.bob/cjs/model/codes.generated.d.ts +48 -0
  62. package/.bob/cjs/model/codes.generated.js +628 -0
  63. package/.bob/cjs/model/index.d.ts +3 -0
  64. package/.bob/cjs/model/index.js +5 -0
  65. package/.bob/cjs/model/input.d.ts +543 -0
  66. package/.bob/cjs/model/input.js +24 -0
  67. package/.bob/cjs/model/normalized.d.ts +43 -0
  68. package/.bob/cjs/model/normalized.js +0 -0
  69. package/.bob/cjs/model/options.d.ts +14 -0
  70. package/.bob/cjs/model/options.js +0 -0
  71. package/.bob/cjs/model/schema.d.ts +594 -0
  72. package/.bob/cjs/model/schema.js +339 -0
  73. package/.bob/cjs/normalize/canonical.d.ts +13 -0
  74. package/.bob/cjs/normalize/canonical.js +288 -0
  75. package/.bob/cjs/normalize/capital-gains/assets.d.ts +37 -0
  76. package/.bob/cjs/normalize/capital-gains/assets.js +94 -0
  77. package/.bob/cjs/normalize/capital-gains/index.d.ts +61 -0
  78. package/.bob/cjs/normalize/capital-gains/index.js +149 -0
  79. package/.bob/cjs/normalize/capital-gains/no-withholding.d.ts +18 -0
  80. package/.bob/cjs/normalize/capital-gains/no-withholding.js +36 -0
  81. package/.bob/cjs/normalize/capital-gains/other.d.ts +54 -0
  82. package/.bob/cjs/normalize/capital-gains/other.js +122 -0
  83. package/.bob/cjs/normalize/capital-gains/section6.d.ts +7 -0
  84. package/.bob/cjs/normalize/capital-gains/section6.js +13 -0
  85. package/.bob/cjs/normalize/capital-gains/securities.d.ts +26 -0
  86. package/.bob/cjs/normalize/capital-gains/securities.js +58 -0
  87. package/.bob/cjs/normalize/denormalize.d.ts +42 -0
  88. package/.bob/cjs/normalize/denormalize.js +359 -0
  89. package/.bob/cjs/normalize/derive.d.ts +57 -0
  90. package/.bob/cjs/normalize/derive.js +111 -0
  91. package/.bob/cjs/normalize/enums.d.ts +58 -0
  92. package/.bob/cjs/normalize/enums.js +69 -0
  93. package/.bob/cjs/normalize/fields.d.ts +28 -0
  94. package/.bob/cjs/normalize/fields.js +86 -0
  95. package/.bob/cjs/normalize/foreign.d.ts +22 -0
  96. package/.bob/cjs/normalize/foreign.js +85 -0
  97. package/.bob/cjs/normalize/main.d.ts +24 -0
  98. package/.bob/cjs/normalize/main.js +208 -0
  99. package/.bob/cjs/normalize/paths.d.ts +74 -0
  100. package/.bob/cjs/normalize/paths.js +169 -0
  101. package/.bob/cjs/normalize/phone.d.ts +12 -0
  102. package/.bob/cjs/normalize/phone.js +35 -0
  103. package/.bob/cjs/normalize/rd-investments.d.ts +10 -0
  104. package/.bob/cjs/normalize/rd-investments.js +25 -0
  105. package/.bob/cjs/normalize/related-companies.d.ts +10 -0
  106. package/.bob/cjs/normalize/related-companies.js +25 -0
  107. package/.bob/cjs/normalize/report.d.ts +9 -0
  108. package/.bob/cjs/normalize/report.js +53 -0
  109. package/.bob/cjs/normalize/shareholders.d.ts +10 -0
  110. package/.bob/cjs/normalize/shareholders.js +30 -0
  111. package/.bob/cjs/normalize/text.d.ts +11 -0
  112. package/.bob/cjs/normalize/text.js +41 -0
  113. package/.bob/cjs/normalize/today.d.ts +4 -0
  114. package/.bob/cjs/normalize/today.js +14 -0
  115. package/.bob/cjs/pipeline.d.ts +13 -0
  116. package/.bob/cjs/pipeline.js +37 -0
  117. package/.bob/cjs/response/codes.d.ts +2 -0
  118. package/.bob/cjs/response/codes.js +31 -0
  119. package/.bob/cjs/validate/capital-gains-rules.d.ts +6 -0
  120. package/.bob/cjs/validate/capital-gains-rules.js +162 -0
  121. package/.bob/cjs/validate/check-digit.d.ts +10 -0
  122. package/.bob/cjs/validate/check-digit.js +26 -0
  123. package/.bob/cjs/validate/engine.d.ts +5 -0
  124. package/.bob/cjs/validate/engine.js +35 -0
  125. package/.bob/cjs/validate/field-rules.d.ts +4 -0
  126. package/.bob/cjs/validate/field-rules.js +331 -0
  127. package/.bob/cjs/validate/foreign-rules.d.ts +6 -0
  128. package/.bob/cjs/validate/foreign-rules.js +171 -0
  129. package/.bob/cjs/validate/issue.d.ts +51 -0
  130. package/.bob/cjs/validate/issue.js +78 -0
  131. package/.bob/cjs/validate/main-rules.d.ts +24 -0
  132. package/.bob/cjs/validate/main-rules.js +425 -0
  133. package/.bob/cjs/validate/parse-rules.d.ts +31 -0
  134. package/.bob/cjs/validate/parse-rules.js +114 -0
  135. package/.bob/cjs/validate/rules.d.ts +746 -0
  136. package/.bob/cjs/validate/rules.js +680 -0
  137. package/.bob/cjs/validate/shape.d.ts +19 -0
  138. package/.bob/cjs/validate/shape.js +231 -0
  139. package/.bob/cjs/validate/shareholder-rules.d.ts +6 -0
  140. package/.bob/cjs/validate/shareholder-rules.js +83 -0
  141. package/.bob/cjs/validate/structure-rules.d.ts +26 -0
  142. package/.bob/cjs/validate/structure-rules.js +130 -0
  143. package/.bob/esm/api/generate.d.ts +44 -0
  144. package/.bob/esm/api/generate.js +111 -0
  145. package/.bob/esm/api/parse.d.ts +28 -0
  146. package/.bob/esm/api/parse.js +34 -0
  147. package/.bob/esm/api/response.d.ts +24 -0
  148. package/.bob/esm/api/response.js +72 -0
  149. package/.bob/esm/api/validate.d.ts +16 -0
  150. package/.bob/esm/api/validate.js +11 -0
  151. package/.bob/esm/codec/boolean.d.ts +7 -0
  152. package/.bob/esm/codec/boolean.js +46 -0
  153. package/.bob/esm/codec/date.d.ts +6 -0
  154. package/.bob/esm/codec/date.js +64 -0
  155. package/.bob/esm/codec/enum.d.ts +4 -0
  156. package/.bob/esm/codec/enum.js +23 -0
  157. package/.bob/esm/codec/numeric.d.ts +56 -0
  158. package/.bob/esm/codec/numeric.js +175 -0
  159. package/.bob/esm/codec/padding.d.ts +10 -0
  160. package/.bob/esm/codec/padding.js +21 -0
  161. package/.bob/esm/codec/percent.d.ts +6 -0
  162. package/.bob/esm/codec/percent.js +30 -0
  163. package/.bob/esm/codec/phone.d.ts +4 -0
  164. package/.bob/esm/codec/phone.js +45 -0
  165. package/.bob/esm/codec/record.d.ts +18 -0
  166. package/.bob/esm/codec/record.js +172 -0
  167. package/.bob/esm/codec/registry.d.ts +8 -0
  168. package/.bob/esm/codec/registry.js +23 -0
  169. package/.bob/esm/codec/sections.d.ts +41 -0
  170. package/.bob/esm/codec/sections.js +170 -0
  171. package/.bob/esm/codec/text.d.ts +5 -0
  172. package/.bob/esm/codec/text.js +26 -0
  173. package/.bob/esm/codec/types.d.ts +36 -0
  174. package/.bob/esm/codec/types.js +12 -0
  175. package/.bob/esm/codec/windows1255.d.ts +9 -0
  176. package/.bob/esm/codec/windows1255.js +32 -0
  177. package/.bob/esm/errors.d.ts +32 -0
  178. package/.bob/esm/errors.js +94 -0
  179. package/.bob/esm/file/assemble.d.ts +17 -0
  180. package/.bob/esm/file/assemble.js +25 -0
  181. package/.bob/esm/file/control.d.ts +13 -0
  182. package/.bob/esm/file/control.js +41 -0
  183. package/.bob/esm/file/parse.d.ts +18 -0
  184. package/.bob/esm/file/parse.js +474 -0
  185. package/.bob/esm/file/split.d.ts +17 -0
  186. package/.bob/esm/file/split.js +68 -0
  187. package/.bob/esm/index.d.ts +16 -0
  188. package/.bob/esm/index.js +11 -0
  189. package/.bob/esm/layout/2024/layout.generated.d.ts +6919 -0
  190. package/.bob/esm/layout/2024/layout.generated.js +7221 -0
  191. package/.bob/esm/layout/2025/index.d.ts +6 -0
  192. package/.bob/esm/layout/2025/index.js +7 -0
  193. package/.bob/esm/layout/capital-gains.d.ts +9 -0
  194. package/.bob/esm/layout/capital-gains.js +11 -0
  195. package/.bob/esm/layout/countries.d.ts +5 -0
  196. package/.bob/esm/layout/countries.js +7 -0
  197. package/.bob/esm/layout/freeze.d.ts +5 -0
  198. package/.bob/esm/layout/freeze.js +13 -0
  199. package/.bob/esm/layout/registry.d.ts +10 -0
  200. package/.bob/esm/layout/registry.js +27 -0
  201. package/.bob/esm/layout/types.d.ts +82 -0
  202. package/.bob/esm/layout/types.js +32 -0
  203. package/.bob/esm/model/codes.generated.d.ts +48 -0
  204. package/.bob/esm/model/codes.generated.js +625 -0
  205. package/.bob/esm/model/index.d.ts +3 -0
  206. package/.bob/esm/model/index.js +2 -0
  207. package/.bob/esm/model/input.d.ts +543 -0
  208. package/.bob/esm/model/input.js +21 -0
  209. package/.bob/esm/model/normalized.d.ts +43 -0
  210. package/.bob/esm/model/normalized.js +0 -0
  211. package/.bob/esm/model/options.d.ts +14 -0
  212. package/.bob/esm/model/options.js +0 -0
  213. package/.bob/esm/model/schema.d.ts +594 -0
  214. package/.bob/esm/model/schema.js +335 -0
  215. package/.bob/esm/normalize/canonical.d.ts +13 -0
  216. package/.bob/esm/normalize/canonical.js +285 -0
  217. package/.bob/esm/normalize/capital-gains/assets.d.ts +37 -0
  218. package/.bob/esm/normalize/capital-gains/assets.js +87 -0
  219. package/.bob/esm/normalize/capital-gains/index.d.ts +61 -0
  220. package/.bob/esm/normalize/capital-gains/index.js +142 -0
  221. package/.bob/esm/normalize/capital-gains/no-withholding.d.ts +18 -0
  222. package/.bob/esm/normalize/capital-gains/no-withholding.js +31 -0
  223. package/.bob/esm/normalize/capital-gains/other.d.ts +54 -0
  224. package/.bob/esm/normalize/capital-gains/other.js +112 -0
  225. package/.bob/esm/normalize/capital-gains/section6.d.ts +7 -0
  226. package/.bob/esm/normalize/capital-gains/section6.js +9 -0
  227. package/.bob/esm/normalize/capital-gains/securities.d.ts +26 -0
  228. package/.bob/esm/normalize/capital-gains/securities.js +52 -0
  229. package/.bob/esm/normalize/denormalize.d.ts +42 -0
  230. package/.bob/esm/normalize/denormalize.js +354 -0
  231. package/.bob/esm/normalize/derive.d.ts +57 -0
  232. package/.bob/esm/normalize/derive.js +99 -0
  233. package/.bob/esm/normalize/enums.d.ts +58 -0
  234. package/.bob/esm/normalize/enums.js +64 -0
  235. package/.bob/esm/normalize/fields.d.ts +28 -0
  236. package/.bob/esm/normalize/fields.js +81 -0
  237. package/.bob/esm/normalize/foreign.d.ts +22 -0
  238. package/.bob/esm/normalize/foreign.js +81 -0
  239. package/.bob/esm/normalize/main.d.ts +24 -0
  240. package/.bob/esm/normalize/main.js +204 -0
  241. package/.bob/esm/normalize/paths.d.ts +74 -0
  242. package/.bob/esm/normalize/paths.js +161 -0
  243. package/.bob/esm/normalize/phone.d.ts +12 -0
  244. package/.bob/esm/normalize/phone.js +32 -0
  245. package/.bob/esm/normalize/rd-investments.d.ts +10 -0
  246. package/.bob/esm/normalize/rd-investments.js +22 -0
  247. package/.bob/esm/normalize/related-companies.d.ts +10 -0
  248. package/.bob/esm/normalize/related-companies.js +22 -0
  249. package/.bob/esm/normalize/report.d.ts +9 -0
  250. package/.bob/esm/normalize/report.js +50 -0
  251. package/.bob/esm/normalize/shareholders.d.ts +10 -0
  252. package/.bob/esm/normalize/shareholders.js +27 -0
  253. package/.bob/esm/normalize/text.d.ts +11 -0
  254. package/.bob/esm/normalize/text.js +37 -0
  255. package/.bob/esm/normalize/today.d.ts +4 -0
  256. package/.bob/esm/normalize/today.js +11 -0
  257. package/.bob/esm/pipeline.d.ts +13 -0
  258. package/.bob/esm/pipeline.js +34 -0
  259. package/.bob/esm/response/codes.d.ts +2 -0
  260. package/.bob/esm/response/codes.js +28 -0
  261. package/.bob/esm/validate/capital-gains-rules.d.ts +6 -0
  262. package/.bob/esm/validate/capital-gains-rules.js +159 -0
  263. package/.bob/esm/validate/check-digit.d.ts +10 -0
  264. package/.bob/esm/validate/check-digit.js +23 -0
  265. package/.bob/esm/validate/engine.d.ts +5 -0
  266. package/.bob/esm/validate/engine.js +32 -0
  267. package/.bob/esm/validate/field-rules.d.ts +4 -0
  268. package/.bob/esm/validate/field-rules.js +328 -0
  269. package/.bob/esm/validate/foreign-rules.d.ts +6 -0
  270. package/.bob/esm/validate/foreign-rules.js +168 -0
  271. package/.bob/esm/validate/issue.d.ts +51 -0
  272. package/.bob/esm/validate/issue.js +72 -0
  273. package/.bob/esm/validate/main-rules.d.ts +24 -0
  274. package/.bob/esm/validate/main-rules.js +420 -0
  275. package/.bob/esm/validate/parse-rules.d.ts +31 -0
  276. package/.bob/esm/validate/parse-rules.js +110 -0
  277. package/.bob/esm/validate/rules.d.ts +746 -0
  278. package/.bob/esm/validate/rules.js +676 -0
  279. package/.bob/esm/validate/shape.d.ts +19 -0
  280. package/.bob/esm/validate/shape.js +227 -0
  281. package/.bob/esm/validate/shareholder-rules.d.ts +6 -0
  282. package/.bob/esm/validate/shareholder-rules.js +80 -0
  283. package/.bob/esm/validate/structure-rules.d.ts +26 -0
  284. package/.bob/esm/validate/structure-rules.js +122 -0
  285. package/.bob/tsconfig.tsbuildinfo +1 -0
  286. package/.gitattributes +1 -0
  287. package/CLAUDE.md +39 -0
  288. package/README.md +337 -0
  289. package/dist/cjs/index.js +28 -0
  290. package/dist/esm/index.js +11 -0
  291. package/documentation/IMPLEMENTATION_PLAN.md +2258 -0
  292. package/documentation/SPEC.md +1803 -0
  293. package/documentation/layout/2024/codes.csv +261 -0
  294. package/documentation/layout/2024/countries.csv +256 -0
  295. package/documentation/layout/2024/fields.csv +271 -0
  296. package/documentation/layout/2024/records.csv +18 -0
  297. package/documentation/layout/README.md +87 -0
  298. package/documentation/specifications/1214-2024.pdf +0 -0
  299. package/documentation/tools/extract_layout.py +191 -0
  300. package/package.json +77 -0
  301. package/scripts/generate-layout.ts +23 -0
  302. package/scripts/layout/csv.ts +111 -0
  303. package/scripts/layout/derived-codes.ts +12 -0
  304. package/scripts/layout/render.ts +364 -0
  305. package/scripts/layout/sources.ts +51 -0
  306. package/scripts/layout/values.ts +42 -0
  307. package/scripts/render-rules-table.ts +66 -0
  308. package/src/api/generate.ts +169 -0
  309. package/src/api/parse.ts +58 -0
  310. package/src/api/response.ts +110 -0
  311. package/src/api/validate.ts +26 -0
  312. package/src/codec/boolean.ts +62 -0
  313. package/src/codec/date.ts +75 -0
  314. package/src/codec/enum.ts +28 -0
  315. package/src/codec/numeric.ts +208 -0
  316. package/src/codec/padding.ts +33 -0
  317. package/src/codec/percent.ts +35 -0
  318. package/src/codec/phone.ts +54 -0
  319. package/src/codec/record.ts +206 -0
  320. package/src/codec/registry.ts +27 -0
  321. package/src/codec/sections.ts +208 -0
  322. package/src/codec/text.ts +31 -0
  323. package/src/codec/types.ts +47 -0
  324. package/src/codec/windows1255.ts +38 -0
  325. package/src/errors.ts +114 -0
  326. package/src/file/assemble.ts +42 -0
  327. package/src/file/control.ts +59 -0
  328. package/src/file/parse.ts +620 -0
  329. package/src/file/split.ts +86 -0
  330. package/src/index.ts +70 -0
  331. package/src/layout/2024/layout.generated.ts +7238 -0
  332. package/src/layout/2025/index.ts +9 -0
  333. package/src/layout/capital-gains.ts +20 -0
  334. package/src/layout/countries.ts +7 -0
  335. package/src/layout/freeze.ts +13 -0
  336. package/src/layout/registry.ts +33 -0
  337. package/src/layout/types.ts +134 -0
  338. package/src/model/codes.generated.ts +1190 -0
  339. package/src/model/index.ts +4 -0
  340. package/src/model/input.ts +594 -0
  341. package/src/model/normalized.ts +38 -0
  342. package/src/model/options.ts +17 -0
  343. package/src/model/schema.ts +381 -0
  344. package/src/normalize/canonical.ts +374 -0
  345. package/src/normalize/capital-gains/assets.ts +112 -0
  346. package/src/normalize/capital-gains/index.ts +212 -0
  347. package/src/normalize/capital-gains/no-withholding.ts +46 -0
  348. package/src/normalize/capital-gains/other.ts +153 -0
  349. package/src/normalize/capital-gains/section6.ts +14 -0
  350. package/src/normalize/capital-gains/securities.ts +73 -0
  351. package/src/normalize/denormalize.ts +426 -0
  352. package/src/normalize/derive.ts +136 -0
  353. package/src/normalize/enums.ts +87 -0
  354. package/src/normalize/fields.ts +110 -0
  355. package/src/normalize/foreign.ts +114 -0
  356. package/src/normalize/main.ts +284 -0
  357. package/src/normalize/paths.ts +184 -0
  358. package/src/normalize/phone.ts +45 -0
  359. package/src/normalize/rd-investments.ts +38 -0
  360. package/src/normalize/related-companies.ts +37 -0
  361. package/src/normalize/report.ts +59 -0
  362. package/src/normalize/shareholders.ts +42 -0
  363. package/src/normalize/text.ts +40 -0
  364. package/src/normalize/today.ts +15 -0
  365. package/src/pipeline.ts +42 -0
  366. package/src/response/codes.ts +30 -0
  367. package/src/validate/capital-gains-rules.ts +199 -0
  368. package/src/validate/check-digit.ts +25 -0
  369. package/src/validate/engine.ts +45 -0
  370. package/src/validate/field-rules.ts +383 -0
  371. package/src/validate/foreign-rules.ts +274 -0
  372. package/src/validate/issue.ts +121 -0
  373. package/src/validate/main-rules.ts +678 -0
  374. package/src/validate/parse-rules.ts +169 -0
  375. package/src/validate/rules.ts +747 -0
  376. package/src/validate/shape.ts +278 -0
  377. package/src/validate/shareholder-rules.ts +92 -0
  378. package/src/validate/structure-rules.ts +136 -0
  379. package/tests/api/batch.test.ts +227 -0
  380. package/tests/api/exports.test.ts +355 -0
  381. package/tests/api/generate.test.ts +409 -0
  382. package/tests/api/parse.test.ts +789 -0
  383. package/tests/api/response.test.ts +152 -0
  384. package/tests/api/validate.test.ts +86 -0
  385. package/tests/codec/numeric.test.ts +325 -0
  386. package/tests/codec/record.test.ts +318 -0
  387. package/tests/codec/registry.test.ts +17 -0
  388. package/tests/codec/sections.test.ts +333 -0
  389. package/tests/codec/text.test.ts +454 -0
  390. package/tests/codec/windows1255.test.ts +46 -0
  391. package/tests/docs/readme.test.ts +69 -0
  392. package/tests/edge-cases.test.ts +359 -0
  393. package/tests/errors.test.ts +138 -0
  394. package/tests/file/assemble.test.ts +188 -0
  395. package/tests/file/split.test.ts +61 -0
  396. package/tests/fixtures/builders.ts +512 -0
  397. package/tests/fixtures/files.ts +78 -0
  398. package/tests/fixtures/golden/README.md +37 -0
  399. package/tests/fixtures/golden/VERIFICATION.md +72 -0
  400. package/tests/fixtures/golden/batch-50.input.ts +92 -0
  401. package/tests/fixtures/golden/batch-50.opcn +150 -0
  402. package/tests/fixtures/golden/capital-gains-max.input.ts +177 -0
  403. package/tests/fixtures/golden/capital-gains-max.opcn +17 -0
  404. package/tests/fixtures/golden/cg-27.input.ts +63 -0
  405. package/tests/fixtures/golden/cg-27.opcn +4 -0
  406. package/tests/fixtures/golden/cg-50.input.ts +59 -0
  407. package/tests/fixtures/golden/cg-50.opcn +4 -0
  408. package/tests/fixtures/golden/cg-51.input.ts +60 -0
  409. package/tests/fixtures/golden/cg-51.opcn +4 -0
  410. package/tests/fixtures/golden/cg-54.input.ts +53 -0
  411. package/tests/fixtures/golden/cg-54.opcn +4 -0
  412. package/tests/fixtures/golden/cg-60.input.ts +77 -0
  413. package/tests/fixtures/golden/cg-60.opcn +4 -0
  414. package/tests/fixtures/golden/cg-63.input.ts +51 -0
  415. package/tests/fixtures/golden/cg-63.opcn +4 -0
  416. package/tests/fixtures/golden/cg-64.input.ts +62 -0
  417. package/tests/fixtures/golden/cg-64.opcn +4 -0
  418. package/tests/fixtures/golden/cg-65.input.ts +79 -0
  419. package/tests/fixtures/golden/cg-65.opcn +4 -0
  420. package/tests/fixtures/golden/cg-66.input.ts +53 -0
  421. package/tests/fixtures/golden/cg-66.opcn +4 -0
  422. package/tests/fixtures/golden/cg-77.input.ts +109 -0
  423. package/tests/fixtures/golden/cg-77.opcn +4 -0
  424. package/tests/fixtures/golden/foreign.input.ts +202 -0
  425. package/tests/fixtures/golden/foreign.opcn +27 -0
  426. package/tests/fixtures/golden/hebrew-edge.input.ts +90 -0
  427. package/tests/fixtures/golden/hebrew-edge.opcn +8 -0
  428. package/tests/fixtures/golden/kibbutz.input.ts +31 -0
  429. package/tests/fixtures/golden/kibbutz.opcn +2 -0
  430. package/tests/fixtures/golden/main-full.input.ts +780 -0
  431. package/tests/fixtures/golden/main-full.opcn +101 -0
  432. package/tests/fixtures/golden/minimal.input.ts +37 -0
  433. package/tests/fixtures/golden/minimal.opcn +3 -0
  434. package/tests/fixtures/golden/related-and-rd.input.ts +59 -0
  435. package/tests/fixtures/golden/related-and-rd.opcn +11 -0
  436. package/tests/fixtures/random.ts +843 -0
  437. package/tests/golden/fixtures.test.ts +627 -0
  438. package/tests/golden/golden.test.ts +95 -0
  439. package/tests/helpers/coverage.ts +18 -0
  440. package/tests/helpers/sample-values.ts +114 -0
  441. package/tests/layout/codegen-sync.test.ts +19 -0
  442. package/tests/layout/integrity.test.ts +232 -0
  443. package/tests/layout/registry.test.ts +98 -0
  444. package/tests/model/code-unions.test.ts +91 -0
  445. package/tests/model/schema.test.ts +1031 -0
  446. package/tests/normalize/canonical.test.ts +728 -0
  447. package/tests/normalize/capital-gains.test.ts +1850 -0
  448. package/tests/normalize/coverage.test.ts +88 -0
  449. package/tests/normalize/denormalize.test.ts +318 -0
  450. package/tests/normalize/enums.test.ts +118 -0
  451. package/tests/normalize/foreign.test.ts +273 -0
  452. package/tests/normalize/main.test.ts +839 -0
  453. package/tests/normalize/paths.test.ts +257 -0
  454. package/tests/normalize/rd-investments.test.ts +99 -0
  455. package/tests/normalize/related-companies.test.ts +106 -0
  456. package/tests/normalize/shareholders.test.ts +117 -0
  457. package/tests/normalize/text.test.ts +86 -0
  458. package/tests/pipeline.test.ts +96 -0
  459. package/tests/roundtrip/determinism.test.ts +207 -0
  460. package/tests/roundtrip/property.test.ts +126 -0
  461. package/tests/roundtrip/roundtrip.test.ts +573 -0
  462. package/tests/rules/cases.ts +873 -0
  463. package/tests/rules/rules.test.ts +69 -0
  464. package/tests/rules/spec-sync.test.ts +80 -0
  465. package/tests/scripts/csv.test.ts +78 -0
  466. package/tests/scripts/values.test.ts +80 -0
  467. package/tests/validate/capital-gains-rules.test.ts +307 -0
  468. package/tests/validate/check-digit.test.ts +42 -0
  469. package/tests/validate/field-rules.test.ts +867 -0
  470. package/tests/validate/foreign-rules.test.ts +255 -0
  471. package/tests/validate/issue.test.ts +96 -0
  472. package/tests/validate/main-rules.test.ts +761 -0
  473. package/tests/validate/parse-rules.test.ts +293 -0
  474. package/tests/validate/shareholder-rules.test.ts +143 -0
  475. package/tsconfig.json +10 -0
  476. package/tsconfig.typecheck.json +9 -0
  477. package/vitest.config.ts +11 -0
@@ -0,0 +1,620 @@
1
+ /**
2
+ * Parser (SPEC §8.2 steps 2–7): the lines of a file to the internal NormalizedReport, with the same
3
+ * record codec as generation. Structural problems (encoding, line shape, order, control record,
4
+ * undecodable content) are collected for every line and thrown together as Opcn1214ParseError.
5
+ */
6
+ import { decodeRecord } from '../codec/record.js';
7
+ import { COUNTRY_ENTRY_LENGTH } from '../codec/sections.js';
8
+ import { CodecDecodeError, type CodecDecodeReason } from '../codec/types.js';
9
+ import { Opcn1214InternalError, Opcn1214ParseError, unsupportedTaxYearIssue } from '../errors.js';
10
+ import { isCapitalGainsRecordType } from '../layout/capital-gains.js';
11
+ import { getLayout, REPORTS_PER_FILE, supportedTaxYears } from '../layout/registry.js';
12
+ import type { FieldDef, RecordDef, RecordType, YearLayout } from '../layout/types.js';
13
+ import type { NormalizedRecord, NormalizedReport } from '../model/normalized.js';
14
+ import {
15
+ BREAKDOWN_CODE_KEY,
16
+ codeKey,
17
+ countryEntryKey,
18
+ incomeEntryKey,
19
+ inputIndexOf,
20
+ pathFor,
21
+ } from '../normalize/paths.js';
22
+ import { isRecordType, recordRank, type Issue } from '../validate/issue.js';
23
+ import {
24
+ derivedMismatches,
25
+ parseIssue,
26
+ type LocatedRecord,
27
+ type Location,
28
+ } from '../validate/parse-rules.js';
29
+ import type { RuleId } from '../validate/rules.js';
30
+ import { controlValues } from './control.js';
31
+ import type { FileLine } from './split.js';
32
+
33
+ /** 0-based [start, end) offsets of the line prefix (SPEC §3.2) and of the 1000 tax year. */
34
+ const FORM_TYPE = { start: 0, end: 4, value: '1214' } as const;
35
+ const RECORD_TYPE = { start: 4, end: 8 } as const;
36
+ const RESERVED = { start: 8, end: 136 } as const;
37
+ const TAX_YEAR = { start: 145, end: 149 } as const;
38
+ const YEAR = /^\d{4}$/;
39
+ const NOT_ZERO = /[^0]/;
40
+
41
+ /** 0-based offset of the first character of `text` (from `start`) that differs from `expected`. */
42
+ function firstDifference(text: string, start: number, expected: string): number {
43
+ for (let offset = 0; offset < expected.length; offset++) {
44
+ if (text[start + offset] !== expected[offset]) {
45
+ return start + offset;
46
+ }
47
+ }
48
+ return -1;
49
+ }
50
+
51
+ function recordTypeText(line: FileLine): string {
52
+ return line.text.slice(RECORD_TYPE.start, RECORD_TYPE.end);
53
+ }
54
+
55
+ /**
56
+ * The layout selected by the tax year of the first 1000 record (SPEC §8.2 step 2), or undefined
57
+ * (with an issue) when there is no 1000 record or its tax year is not a supported year. `lines`
58
+ * start at line `firstLine` of the file.
59
+ */
60
+ function selectLayout(
61
+ lines: readonly FileLine[],
62
+ firstLine: number,
63
+ issues: Issue[],
64
+ ): YearLayout | undefined {
65
+ const index = lines.findIndex(line => recordTypeText(line) === '1000');
66
+ const main = lines[index];
67
+ if (main === undefined || main.invalidAt !== undefined) {
68
+ return undefined;
69
+ }
70
+ const at: Location = {
71
+ line: index + firstLine,
72
+ column: TAX_YEAR.start + 1,
73
+ recordType: '1000',
74
+ recordIndex: 0,
75
+ };
76
+ const year = main.text.slice(TAX_YEAR.start, TAX_YEAR.end);
77
+ if (main.text.length < TAX_YEAR.end) {
78
+ issues.push(
79
+ parseIssue(
80
+ 'parse.lineLength',
81
+ `Record 1000 is too short (${main.text.length} characters) to hold the tax year.`,
82
+ { ...at, column: main.text.length + 1 },
83
+ ),
84
+ );
85
+ return undefined;
86
+ }
87
+ if (!YEAR.test(year)) {
88
+ issues.push(
89
+ parseIssue('parse.numeric', `Tax year "${year}" is not a number.`, at, {
90
+ path: 'taxYear',
91
+ value: year,
92
+ }),
93
+ );
94
+ return undefined;
95
+ }
96
+ const taxYear = Number(year);
97
+ if (!supportedTaxYears().includes(taxYear)) {
98
+ const issue = unsupportedTaxYearIssue(taxYear, supportedTaxYears());
99
+ issues.push({ ...issue, recordType: '1000', recordIndex: 0, line: at.line, column: at.column });
100
+ return undefined;
101
+ }
102
+ return getLayout(taxYear);
103
+ }
104
+
105
+ /**
106
+ * The line checks that do not decode fields (SPEC §8.2 step 2): form type, record type, the
107
+ * 128-zero prefix and, with a layout, the exact length. Returns the record type when it is known
108
+ * and whether the line can be decoded.
109
+ */
110
+ function checkLine(
111
+ line: FileLine,
112
+ lineNumber: number,
113
+ layout: YearLayout | undefined,
114
+ issues: Issue[],
115
+ ): { type?: RecordType; decodable: boolean } {
116
+ const typeText = recordTypeText(line);
117
+ const type = isRecordType(typeText) ? typeText : undefined;
118
+ if (line.invalidAt !== undefined) {
119
+ issues.push(
120
+ parseIssue(
121
+ 'parse.encoding',
122
+ 'Bytes that do not decode in the selected encoding.',
123
+ { line: lineNumber, column: line.invalidAt + 1 },
124
+ type ? { recordType: type } : {},
125
+ ),
126
+ );
127
+ return { type, decodable: false };
128
+ }
129
+ const { text } = line;
130
+ let decodable = layout !== undefined;
131
+ const at = (column: number): Location => ({
132
+ line: lineNumber,
133
+ column,
134
+ ...(type ? { recordType: type } : {}),
135
+ });
136
+ const formOffset = firstDifference(text, FORM_TYPE.start, FORM_TYPE.value);
137
+ if (formOffset !== -1) {
138
+ issues.push(
139
+ parseIssue(
140
+ 'parse.formType',
141
+ `Form type "${text.slice(FORM_TYPE.start, FORM_TYPE.end)}" is not ${FORM_TYPE.value}.`,
142
+ at(formOffset + 1),
143
+ ),
144
+ );
145
+ decodable = false;
146
+ }
147
+ if (!type) {
148
+ issues.push(
149
+ parseIssue(
150
+ 'parse.recordType',
151
+ `Unknown record type "${typeText}".`,
152
+ at(RECORD_TYPE.start + 1),
153
+ ),
154
+ );
155
+ return { decodable: false };
156
+ }
157
+ const reservedOffset = text.slice(RESERVED.start, RESERVED.end).search(NOT_ZERO);
158
+ if (reservedOffset !== -1) {
159
+ issues.push(
160
+ parseIssue(
161
+ 'parse.prefixFiller',
162
+ 'Positions 9–136 must all be 0.',
163
+ at(RESERVED.start + reservedOffset + 1),
164
+ ),
165
+ );
166
+ decodable = false;
167
+ }
168
+ const length = layout?.records[type].length;
169
+ if (length !== undefined && text.length !== length) {
170
+ issues.push(
171
+ parseIssue(
172
+ 'parse.lineLength',
173
+ `Record ${type} must have ${length} characters, not ${text.length}.`,
174
+ at(Math.min(text.length, length) + 1),
175
+ ),
176
+ );
177
+ decodable = false;
178
+ }
179
+ return { type, decodable };
180
+ }
181
+
182
+ /**
183
+ * Rule of a CodecDecodeError: by reason (a 'section' error by the section kind of the record of
184
+ * type `type`), and for 'format' by where in the line it was raised.
185
+ */
186
+ function decodeRule(type: RecordType, reason: CodecDecodeReason, offset: number): RuleId {
187
+ switch (reason) {
188
+ case 'numeric':
189
+ return 'parse.numeric';
190
+ case 'date':
191
+ return 'parse.date';
192
+ case 'section':
193
+ return type === '2200' ? 'parse.countrySection' : 'parse.codeSection';
194
+ case 'length':
195
+ return 'parse.lineLength';
196
+ case 'format':
197
+ if (offset < RECORD_TYPE.start) {
198
+ return 'parse.formType';
199
+ }
200
+ if (offset < RECORD_TYPE.end) {
201
+ return 'parse.recordType';
202
+ }
203
+ return offset < RESERVED.end ? 'parse.prefixFiller' : 'parse.fieldValue';
204
+ }
205
+ }
206
+
207
+ function fieldAt(def: RecordDef, offset: number): FieldDef | undefined {
208
+ return def.fields.find(
209
+ field => offset >= field.position - 1 && offset < field.position - 1 + field.length,
210
+ );
211
+ }
212
+
213
+ /** 0-based offsets of 2200@137, the 2000 code of a breakdown; its input key (SPEC §7.3). */
214
+ const BREAKDOWN_CODE = { start: 136, end: 139 } as const;
215
+ const CODE_DIGITS = /^\d{3}$/;
216
+
217
+ /**
218
+ * The pathFor `inputIndex` of the record of type `def.type` on `line`, before it is decoded:
219
+ * `inputIndex` (its index, or for a capital-gains record its index among all capital-gains
220
+ * records), or for a 2200 record the code at 2200@137 (undefined when that is not 3 digits).
221
+ */
222
+ function lineInputIndex(def: RecordDef, line: string, inputIndex: number): number | undefined {
223
+ if (def.type !== '2200') {
224
+ return inputIndex;
225
+ }
226
+ const code = line.slice(BREAKDOWN_CODE.start, BREAKDOWN_CODE.end);
227
+ return CODE_DIGITS.test(code) ? Number(code) : undefined;
228
+ }
229
+
230
+ /**
231
+ * The parse issue of undecodable content of a record (offset within the line);
232
+ * `recordInputIndex` is the `inputIndex` lineInputIndex takes.
233
+ */
234
+ function decodeIssue(
235
+ def: RecordDef,
236
+ line: string,
237
+ error: CodecDecodeError,
238
+ at: Location,
239
+ recordInputIndex: number,
240
+ ): Issue {
241
+ const field = fieldAt(def, error.offset);
242
+ const location = { ...at, column: error.offset + 1 };
243
+ const ruleId = decodeRule(def.type, error.reason, error.offset);
244
+ const inputIndex = lineInputIndex(def, line, recordInputIndex);
245
+ if (!field) {
246
+ return parseIssue(ruleId, error.message, location);
247
+ }
248
+ const start = field.position - 1;
249
+ return parseIssue(ruleId, error.message, location, {
250
+ path: inputIndex === undefined ? '' : pathFor(def.type, inputIndex, field.key),
251
+ field: { position: field.position, length: field.length, nameHe: field.nameHe },
252
+ value: line.slice(start, start + field.length),
253
+ });
254
+ }
255
+
256
+ /**
257
+ * `record` with the input path of every value, code and country entry (emission index = input
258
+ * index: among records of the type, or among all capital-gains records for a 30xx record, whose
259
+ * inputIndex the parser sets; a 2200 record is keyed by its code).
260
+ */
261
+ function withPaths(record: NormalizedRecord): NormalizedRecord {
262
+ const keys = [
263
+ ...Object.keys(record.values),
264
+ ...[...record.codes.keys()].map(codeKey),
265
+ ...record.countries.flatMap((_, index) => [countryEntryKey(index), incomeEntryKey(index)]),
266
+ ];
267
+ const paths: Record<string, string> = {};
268
+ for (const key of keys) {
269
+ paths[key] = pathFor(record.recordType, inputIndexOf(record), key);
270
+ }
271
+ return { ...record, paths };
272
+ }
273
+
274
+ /**
275
+ * The structural issues of a decoded 2200 `record` on line `lineNumber` (SPEC §3.6): more country
276
+ * entries than one record holds (parse.countrySection, at the first extra entry), and a code
277
+ * (2200@137) that is not a per-country code of record 2000, or that an earlier 2200 record of the
278
+ * report already has (parse.fieldValue). `seenCodes` holds the codes of the earlier records; the
279
+ * record's code is added to it.
280
+ */
281
+ function breakdownIssues(
282
+ layout: YearLayout,
283
+ record: NormalizedRecord,
284
+ lineNumber: number,
285
+ seenCodes: Set<string>,
286
+ ): Issue[] {
287
+ const def = layout.records['2200'];
288
+ const field = def.fields.find(candidate => candidate.key === BREAKDOWN_CODE_KEY);
289
+ if (!field || !def.section) {
290
+ throw new Opcn1214InternalError('Record 2200 has no code field or no section');
291
+ }
292
+ const at = { line: lineNumber, recordType: def.type, recordIndex: record.index };
293
+ const issues: Issue[] = [];
294
+ const max = layout.limits.countriesPerRecord;
295
+ if (record.countries.length > max) {
296
+ issues.push(
297
+ parseIssue(
298
+ 'parse.countrySection',
299
+ `Record 2200 has ${record.countries.length} country entries; at most ${max} are allowed.`,
300
+ { ...at, column: def.section.start + max * COUNTRY_ENTRY_LENGTH },
301
+ { value: record.countries.length },
302
+ ),
303
+ );
304
+ }
305
+ const code = String(inputIndexOf(record)).padStart(field.length, '0');
306
+ const isPerCountry = layout.records['2000'].codes.some(
307
+ def2000 => def2000.perCountryBreakdown && def2000.code === code,
308
+ );
309
+ let problem: string | undefined;
310
+ if (!isPerCountry) {
311
+ problem = `${code} is not a per-country breakdown code of record 2000.`;
312
+ } else if (seenCodes.has(code)) {
313
+ problem = `A second 2200 record for code ${code}.`;
314
+ }
315
+ seenCodes.add(code);
316
+ if (problem) {
317
+ issues.push(
318
+ parseIssue(
319
+ 'parse.fieldValue',
320
+ problem,
321
+ { ...at, column: field.position },
322
+ {
323
+ field: { position: field.position, length: field.length, nameHe: field.nameHe },
324
+ value: code,
325
+ },
326
+ ),
327
+ );
328
+ }
329
+ return issues;
330
+ }
331
+
332
+ /**
333
+ * The record-order issue of a record of `type` (SPEC §3.3), given the types of the records before
334
+ * it in its report. A record after the 9999 of its report is out of order (batches are split at
335
+ * each 9999 before this check, so only records that do not start a new report get here).
336
+ */
337
+ function orderProblem(type: RecordType, before: readonly RecordType[]): string | undefined {
338
+ const previous = before.at(-1);
339
+ if (previous === undefined) {
340
+ return type === '1000' ? undefined : 'The file does not start with a 1000 record.';
341
+ }
342
+ if (type === '1000' && before.includes('1000')) {
343
+ return 'A second 1000 record before a 9999 record.';
344
+ }
345
+ const latest = Math.max(...before.map(recordRank));
346
+ if (recordRank(type) < latest) {
347
+ return `Record ${type} comes after a record that must follow it (SPEC §3.3 order).`;
348
+ }
349
+ if (type === '2200' && !before.includes('2000')) {
350
+ return 'A 2200 record without a 2000 record before it: a country breakdown belongs to the foreign appendix.';
351
+ }
352
+ return undefined;
353
+ }
354
+
355
+ /** parse.control.counts issues of the 9999 record against the record types present. */
356
+ function controlIssues(
357
+ layout: YearLayout,
358
+ control: NormalizedRecord,
359
+ lineNumber: number,
360
+ present: readonly RecordType[],
361
+ ): Issue[] {
362
+ const expected = controlValues(layout, present);
363
+ const issues: Issue[] = [];
364
+ for (const field of layout.records['9999'].fields) {
365
+ const actual = control.values[field.key] ?? 0;
366
+ const wanted = expected[field.key] ?? 0;
367
+ if (actual !== wanted) {
368
+ issues.push(
369
+ parseIssue(
370
+ 'parse.control.counts',
371
+ `Control field ${field.key} is ${String(actual)}; the file has ${wanted}.`,
372
+ { line: lineNumber, column: field.position, recordType: '9999', recordIndex: 0 },
373
+ {
374
+ field: { position: field.position, length: field.length, nameHe: field.nameHe },
375
+ value: actual,
376
+ expected: String(wanted),
377
+ },
378
+ ),
379
+ );
380
+ }
381
+ }
382
+ return issues;
383
+ }
384
+
385
+ function byLine(a: Issue, b: Issue): number {
386
+ return (
387
+ (a.line ?? 0) - (b.line ?? 0) ||
388
+ (a.column ?? 0) - (b.column ?? 0) ||
389
+ (a.ruleId < b.ruleId ? -1 : Number(a.ruleId > b.ruleId))
390
+ );
391
+ }
392
+
393
+ /**
394
+ * The maximum number of records of `type` in one report (SPEC §3.2): RecordDef.maxOccurs, or for
395
+ * a capital-gains record the limit of all of them together; undefined for 1000 and 9999, which
396
+ * occur once per report (a second one is parse.order, or starts the next report of a batch).
397
+ */
398
+ function occurrenceLimit(layout: YearLayout, type: RecordType): number | undefined {
399
+ if (type === '1000' || type === '9999') {
400
+ return undefined;
401
+ }
402
+ return isCapitalGainsRecordType(type)
403
+ ? layout.limits.capitalGainsAppendices
404
+ : layout.records[type].maxOccurs;
405
+ }
406
+
407
+ /**
408
+ * The parse.occurrences issue of the record of `type` at `at` when it is the first one over its
409
+ * limit: `count` is the number of records of the type (of capital-gains records, for one of them)
410
+ * so far, this one included.
411
+ */
412
+ function occurrenceIssue(
413
+ layout: YearLayout,
414
+ type: RecordType,
415
+ count: number,
416
+ at: Location,
417
+ ): Issue | undefined {
418
+ const max = occurrenceLimit(layout, type);
419
+ if (max === undefined || count !== max + 1) {
420
+ return undefined;
421
+ }
422
+ const what = isCapitalGainsRecordType(type) ? 'capital-gains records (30xx)' : `${type} records`;
423
+ return parseIssue('parse.occurrences', `More than ${max} ${what} in one report.`, at, {
424
+ value: count,
425
+ expected: `at most ${max}`,
426
+ });
427
+ }
428
+
429
+ /**
430
+ * The NormalizedReport of one report given as its lines, which start at line `firstLine` of the
431
+ * file (issue lines are file lines): the layout comes from the tax year of the first 1000 record;
432
+ * each line is checked (SPEC §8.2 steps 2–3: line shape, order, maximum occurrences), decoded with
433
+ * the layout of its record type and indexed among the records of that type; 9999 is checked
434
+ * against the records present (step 6) but not kept. A report without structural problems is then
435
+ * checked for derived values that disagree with their source (parse.derivedMismatch). Throws
436
+ * Opcn1214ParseError with every structural problem of the report (an unsupported tax year
437
+ * included, as struct.taxYear.unsupported), sorted by line and column.
438
+ */
439
+ function parseReport(lines: readonly FileLine[], firstLine: number): NormalizedReport {
440
+ const issues: Issue[] = [];
441
+ const layout = selectLayout(lines, firstLine, issues);
442
+ const counts = new Map<RecordType, number>();
443
+ const types: RecordType[] = [];
444
+ const located: LocatedRecord[] = [];
445
+ let control: { record: NormalizedRecord; line: number } | undefined;
446
+ const breakdownCodes = new Set<string>();
447
+ // Capital-gains records are read back in file order, so their index among all of them is the
448
+ // index of their appendix in `capitalGains` (denormalize).
449
+ let capitalGainsCount = 0;
450
+ for (const [offset, line] of lines.entries()) {
451
+ const lineNumber = offset + firstLine;
452
+ const { type, decodable } = checkLine(line, lineNumber, layout, issues);
453
+ if (!type) {
454
+ continue;
455
+ }
456
+ const recordIndex = counts.get(type) ?? 0;
457
+ counts.set(type, recordIndex + 1);
458
+ const isCapitalGains = isCapitalGainsRecordType(type);
459
+ const inputIndex = isCapitalGains ? capitalGainsCount++ : recordIndex;
460
+ const at = { line: lineNumber, column: RECORD_TYPE.start + 1, recordType: type, recordIndex };
461
+ const order = orderProblem(type, types);
462
+ if (order) {
463
+ issues.push(parseIssue('parse.order', order, at));
464
+ }
465
+ const occurrences =
466
+ layout &&
467
+ occurrenceIssue(layout, type, isCapitalGains ? capitalGainsCount : recordIndex + 1, at);
468
+ if (occurrences) {
469
+ issues.push(occurrences);
470
+ }
471
+ types.push(type);
472
+ if (!layout || !decodable) {
473
+ continue;
474
+ }
475
+ const def = layout.records[type];
476
+ try {
477
+ const record = decodeRecord(def, line.text, recordIndex);
478
+ if (isCapitalGains) {
479
+ record.inputIndex = inputIndex;
480
+ }
481
+ if (type === '9999') {
482
+ control ||= { record, line: lineNumber };
483
+ } else {
484
+ if (type === '2200') {
485
+ issues.push(...breakdownIssues(layout, record, lineNumber, breakdownCodes));
486
+ }
487
+ located.push({ record: withPaths(record), line: lineNumber, text: line.text });
488
+ }
489
+ } catch (error) {
490
+ if (!(error instanceof CodecDecodeError)) {
491
+ throw error;
492
+ }
493
+ issues.push(decodeIssue(def, line.text, error, at, inputIndex));
494
+ }
495
+ }
496
+ if (!types.includes('9999')) {
497
+ issues.push(
498
+ parseIssue('parse.control.missing', 'The report is not terminated by a 9999 record.', {
499
+ line: lines.length + firstLine,
500
+ column: 1,
501
+ }),
502
+ );
503
+ }
504
+ if (layout && control) {
505
+ const present = types.filter(type => type !== '9999');
506
+ issues.push(...controlIssues(layout, control.record, control.line, present));
507
+ }
508
+ if (issues.length > 0 || !layout) {
509
+ throw new Opcn1214ParseError(issues.sort(byLine));
510
+ }
511
+ const report: NormalizedReport = {
512
+ taxYear: layout.taxYear,
513
+ layout,
514
+ records: located.map(({ record }) => record),
515
+ notices: [],
516
+ omittedZeroCodes: [],
517
+ };
518
+ const mismatches = derivedMismatches(report, located);
519
+ if (mismatches.length > 0) {
520
+ throw new Opcn1214ParseError(mismatches.sort(byLine));
521
+ }
522
+ return report;
523
+ }
524
+
525
+ function emptyFileError(): Opcn1214ParseError {
526
+ return new Opcn1214ParseError([
527
+ parseIssue('parse.empty', 'The file has no records.', { line: 1, column: 1 }),
528
+ ]);
529
+ }
530
+
531
+ function isControlLine(line: FileLine): boolean {
532
+ return recordTypeText(line) === '9999';
533
+ }
534
+
535
+ /**
536
+ * The NormalizedReport of a single-report file given as its lines (SPEC §8.2 steps 2–7). Throws
537
+ * Opcn1214ParseError for an empty file, for a file that holds more than one report (a 1000 record
538
+ * after a 9999 record: parse.multipleReports, at that record's line), and with every structural
539
+ * problem of the report (parseReport); records after the 9999 that do not start a report are
540
+ * parse.order.
541
+ */
542
+ export function parseLines(lines: readonly FileLine[]): NormalizedReport {
543
+ if (lines.length === 0) {
544
+ throw emptyFileError();
545
+ }
546
+ const control = lines.findIndex(isControlLine);
547
+ const next =
548
+ control === -1
549
+ ? -1
550
+ : lines.findIndex((line, index) => index > control && recordTypeText(line) === '1000');
551
+ if (next !== -1) {
552
+ throw new Opcn1214ParseError([
553
+ parseIssue(
554
+ 'parse.multipleReports',
555
+ `The file holds more than one report (a new report starts after the 9999 record on line ${control + 1}); parse it with parse1214Batch.`,
556
+ { line: next + 1, column: 1 },
557
+ ),
558
+ ]);
559
+ }
560
+ return parseReport(lines, 1);
561
+ }
562
+
563
+ /** The reports of a batch file: its lines split after each 9999 record, with their first line. */
564
+ function splitReports(lines: readonly FileLine[]): Array<{ lines: FileLine[]; firstLine: number }> {
565
+ const reports: Array<{ lines: FileLine[]; firstLine: number }> = [];
566
+ let current: FileLine[] = [];
567
+ let firstLine = 1;
568
+ for (const [offset, line] of lines.entries()) {
569
+ current.push(line);
570
+ if (isControlLine(line)) {
571
+ reports.push({ lines: current, firstLine });
572
+ current = [];
573
+ firstLine = offset + 2;
574
+ }
575
+ }
576
+ if (current.length > 0) {
577
+ reports.push({ lines: current, firstLine });
578
+ }
579
+ return reports;
580
+ }
581
+
582
+ /**
583
+ * The NormalizedReports of an OPCN batch file given as its lines (SPEC §3.1, §8.2 step 3): split
584
+ * after each 9999 record, each report parsed on its own (parseReport) with file line numbers. Throws
585
+ * Opcn1214ParseError for an empty file, or with every structural problem of every report, each
586
+ * issue carrying its 0-based `reportIndex`; a report after the 50th is parse.occurrences at its
587
+ * first line.
588
+ */
589
+ export function parseBatchLines(lines: readonly FileLine[]): NormalizedReport[] {
590
+ if (lines.length === 0) {
591
+ throw emptyFileError();
592
+ }
593
+ const issues: Issue[] = [];
594
+ const reports: NormalizedReport[] = [];
595
+ const max = REPORTS_PER_FILE;
596
+ for (const [reportIndex, { lines: reportLines, firstLine }] of splitReports(lines).entries()) {
597
+ if (reportIndex === max) {
598
+ issues.push(
599
+ parseIssue(
600
+ 'parse.occurrences',
601
+ `More than ${max} reports in one file.`,
602
+ { line: firstLine, column: 1 },
603
+ { reportIndex, expected: `at most ${max}` },
604
+ ),
605
+ );
606
+ }
607
+ try {
608
+ reports.push(parseReport(reportLines, firstLine));
609
+ } catch (error) {
610
+ if (!(error instanceof Opcn1214ParseError)) {
611
+ throw error;
612
+ }
613
+ issues.push(...error.issues.map(issue => ({ ...issue, reportIndex })));
614
+ }
615
+ }
616
+ if (issues.length > 0) {
617
+ throw new Opcn1214ParseError(issues.sort(byLine));
618
+ }
619
+ return reports;
620
+ }
@@ -0,0 +1,86 @@
1
+ /**
2
+ * File content to lines (SPEC §8.2 step 1): bytes decoded per `options.encoding`, a UTF-8 BOM
3
+ * stripped, CRLF or LF line endings, one trailing empty line ignored. Bytes that do not decode are
4
+ * located per line (parse.encoding).
5
+ */
6
+ import { decodeWindows1255 } from '../codec/windows1255.js';
7
+ import type { ParseOptions } from '../model/options.js';
8
+
9
+ /** One line of a file, without its line terminator. */
10
+ export interface FileLine {
11
+ text: string;
12
+ /**
13
+ * 0-based offset in `text` of the first character whose bytes do not decode in the selected
14
+ * encoding (they decode to U+FFFD); absent when the whole line decodes.
15
+ */
16
+ invalidAt?: number;
17
+ }
18
+
19
+ const UTF8_BOM_BYTES = [0xef, 0xbb, 0xbf] as const;
20
+ const BOM = String.fromCodePoint(0xfe_ff);
21
+ const LINE_BREAK = /\r?\n/;
22
+ const LF = 0x0a;
23
+ const CR = 0x0d;
24
+ const REPLACEMENT_CHARACTER = String.fromCodePoint(0xff_fd);
25
+
26
+ function startsWithUtf8Bom(bytes: Uint8Array): boolean {
27
+ return UTF8_BOM_BYTES.every((byte, offset) => bytes[offset] === byte);
28
+ }
29
+
30
+ /** The byte lines of `bytes`: split at LF, a CR before the LF removed. */
31
+ function byteLines(bytes: Uint8Array): Uint8Array[] {
32
+ const lines: Uint8Array[] = [];
33
+ let start = 0;
34
+ for (let end = bytes.indexOf(LF); end !== -1; end = bytes.indexOf(LF, start)) {
35
+ lines.push(bytes.subarray(start, end > start && bytes[end - 1] === CR ? end - 1 : end));
36
+ start = end + 1;
37
+ }
38
+ lines.push(bytes.subarray(start));
39
+ return lines;
40
+ }
41
+
42
+ /**
43
+ * Decodes one line. windows-1255 decodes its unassigned bytes to U+FFFD, which is not a character
44
+ * of the code page; UTF-8 is decoded strictly, so a U+FFFD only marks a problem when the line is not
45
+ * valid UTF-8.
46
+ */
47
+ function decodeLine(bytes: Uint8Array, encoding: ParseOptions['encoding']): FileLine {
48
+ if (encoding === 'utf-8') {
49
+ try {
50
+ return { text: new TextDecoder('utf-8', { fatal: true }).decode(bytes) };
51
+ } catch {
52
+ const text = new TextDecoder('utf-8').decode(bytes);
53
+ return { text, invalidAt: Math.max(0, text.indexOf(REPLACEMENT_CHARACTER)) };
54
+ }
55
+ }
56
+ const text = decodeWindows1255(bytes);
57
+ const invalidAt = text.indexOf(REPLACEMENT_CHARACTER);
58
+ return invalidAt === -1 ? { text } : { text, invalidAt };
59
+ }
60
+
61
+ function withoutTrailingEmptyLine<T>(lines: T[], isEmpty: (line: T) => boolean): T[] {
62
+ const last = lines.at(-1);
63
+ if (last !== undefined && isEmpty(last)) {
64
+ lines.pop();
65
+ }
66
+ return lines;
67
+ }
68
+
69
+ /**
70
+ * The lines of a file, without line terminators. A Uint8Array is decoded with windows-1255, or
71
+ * UTF-8 when `options.encoding` says so, line by line; a string is taken as already decoded. A
72
+ * leading UTF-8 BOM is stripped. Lines end with CRLF or LF; the empty line after the final
73
+ * terminator is not a line.
74
+ */
75
+ export function splitFile(content: string | Uint8Array, options: ParseOptions = {}): FileLine[] {
76
+ if (typeof content === 'string') {
77
+ const text = content.startsWith(BOM) ? content.slice(BOM.length) : content;
78
+ return withoutTrailingEmptyLine(text.split(LINE_BREAK), line => line === '').map(line => ({
79
+ text: line,
80
+ }));
81
+ }
82
+ const body = startsWithUtf8Bom(content) ? content.subarray(UTF8_BOM_BYTES.length) : content;
83
+ return withoutTrailingEmptyLine(byteLines(body), line => line.length === 0).map(line =>
84
+ decodeLine(line, options.encoding),
85
+ );
86
+ }