@sachfolio/cas-reader 0.0.0-stage → 0.2.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/LICENSE +71 -0
- package/NOTICE +41 -0
- package/README.md +198 -2
- package/dist/analytics/contract.d.ts +110 -0
- package/dist/analytics/contract.js +79 -0
- package/dist/analytics/eligibility.d.ts +66 -0
- package/dist/analytics/eligibility.js +93 -0
- package/dist/analytics/gains-schema.d.ts +898 -0
- package/dist/analytics/gains-schema.js +170 -0
- package/dist/analytics/gains.d.ts +319 -0
- package/dist/analytics/gains.js +649 -0
- package/dist/analytics/modules.d.ts +33 -0
- package/dist/analytics/modules.js +74 -0
- package/dist/analytics/returns-schema.d.ts +931 -0
- package/dist/analytics/returns-schema.js +152 -0
- package/dist/analytics/returns.d.ts +126 -0
- package/dist/analytics/returns.js +453 -0
- package/dist/analytics/schedule112a.d.ts +77 -0
- package/dist/analytics/schedule112a.js +134 -0
- package/dist/analytics/xirr.d.ts +75 -0
- package/dist/analytics/xirr.js +314 -0
- package/dist/bin/cas.d.ts +2 -0
- package/dist/bin/cas.js +137 -0
- package/dist/checks.d.ts +49 -0
- package/dist/checks.js +227 -0
- package/dist/compat/casparser.d.ts +121 -0
- package/dist/compat/casparser.js +193 -0
- package/dist/detect.d.ts +10 -0
- package/dist/detect.js +69 -0
- package/dist/errors.d.ts +26 -0
- package/dist/errors.js +95 -0
- package/dist/export/csv.d.ts +21 -0
- package/dist/export/csv.js +49 -0
- package/dist/extract/browser.d.ts +17 -0
- package/dist/extract/browser.js +34 -0
- package/dist/extract/budget.d.ts +46 -0
- package/dist/extract/budget.js +88 -0
- package/dist/extract/node.d.ts +11 -0
- package/dist/extract/node.js +31 -0
- package/dist/extract/pdfjs.d.ts +71 -0
- package/dist/extract/pdfjs.js +244 -0
- package/dist/extract/types.d.ts +106 -0
- package/dist/extract/types.js +11 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +26 -0
- package/dist/layout/lines.d.ts +51 -0
- package/dist/layout/lines.js +115 -0
- package/dist/licence/app-licence.d.ts +53 -0
- package/dist/licence/app-licence.js +139 -0
- package/dist/licence/ed25519.d.ts +2 -0
- package/dist/licence/ed25519.js +103 -0
- package/dist/model.d.ts +148 -0
- package/dist/model.js +1 -0
- package/dist/num.d.ts +19 -0
- package/dist/num.js +27 -0
- package/dist/parse.d.ts +4 -0
- package/dist/parse.js +13 -0
- package/dist/parsers/cams/classify.d.ts +25 -0
- package/dist/parsers/cams/classify.js +103 -0
- package/dist/parsers/cams/detailed.d.ts +15 -0
- package/dist/parsers/cams/detailed.js +343 -0
- package/dist/parsers/cams/header.d.ts +47 -0
- package/dist/parsers/cams/header.js +170 -0
- package/dist/parsers/cams/investor.d.ts +28 -0
- package/dist/parsers/cams/investor.js +139 -0
- package/dist/parsers/cams/summary.d.ts +29 -0
- package/dist/parsers/cams/summary.js +413 -0
- package/dist/parsers/cams/table.d.ts +55 -0
- package/dist/parsers/cams/table.js +144 -0
- package/dist/parsers/cams/total.d.ts +17 -0
- package/dist/parsers/cams/total.js +105 -0
- package/dist/parsers/depository/mf.d.ts +22 -0
- package/dist/parsers/depository/mf.js +438 -0
- package/dist/privacy.d.ts +243 -0
- package/dist/privacy.js +256 -0
- package/dist/schema.d.ts +779 -0
- package/dist/schema.js +214 -0
- package/examples/nextjs/app/page.jsx +34 -0
- package/examples/node/server.mjs +45 -0
- package/examples/python/read_cas.py +67 -0
- package/examples/vite/index.html +14 -0
- package/examples/vite/main.js +22 -0
- package/package.json +54 -4
package/dist/privacy.js
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
const nested = (name) => ({ nested: name });
|
|
2
|
+
/** Every model.ts interface, by name, with a class for each field. */
|
|
3
|
+
export const PRIVACY = {
|
|
4
|
+
CasDocument: {
|
|
5
|
+
schemaVersion: "P0",
|
|
6
|
+
source: nested("CasSource"),
|
|
7
|
+
provenance: nested("Provenance"),
|
|
8
|
+
statementPeriod: "P1",
|
|
9
|
+
addressee: nested("Addressee"),
|
|
10
|
+
folios: nested("Folio"),
|
|
11
|
+
coverage: nested("Coverage"),
|
|
12
|
+
printedTotal: nested("PrintedTotal"),
|
|
13
|
+
// Statement-level checks (the printed portfolio total).
|
|
14
|
+
checks: nested("Check"),
|
|
15
|
+
// Parser notes are free text; some embed statement text.
|
|
16
|
+
warnings: "PX",
|
|
17
|
+
},
|
|
18
|
+
CasSource: { issuer: "P0", variant: "P0", engineVersion: "P0" },
|
|
19
|
+
Provenance: {
|
|
20
|
+
// The file hash identifies the file: never outside the device's own use.
|
|
21
|
+
sha256: "P2",
|
|
22
|
+
pageCount: "P0",
|
|
23
|
+
producer: "PX",
|
|
24
|
+
creator: "PX",
|
|
25
|
+
pdfVersion: "P0",
|
|
26
|
+
encrypted: "P0",
|
|
27
|
+
extractor: "P0",
|
|
28
|
+
},
|
|
29
|
+
Addressee: { name: "P3", email: "P3", mobile: "P3", address: "P3" },
|
|
30
|
+
Folio: {
|
|
31
|
+
folio: "P2",
|
|
32
|
+
amc: "P0",
|
|
33
|
+
pan: "P2",
|
|
34
|
+
// Printed status text, verbatim: may be anything.
|
|
35
|
+
kyc: "PX",
|
|
36
|
+
panKyc: "PX",
|
|
37
|
+
holderName: "P3",
|
|
38
|
+
schemes: nested("Scheme"),
|
|
39
|
+
},
|
|
40
|
+
Scheme: {
|
|
41
|
+
// Scheme and registrar names, codes and ISINs are public (AMFI lists them).
|
|
42
|
+
// Renderers still treat them as statement text (escaped, screened; see apps/web report).
|
|
43
|
+
name: "P0",
|
|
44
|
+
rtaCode: "P0",
|
|
45
|
+
rta: "P0",
|
|
46
|
+
isin: "P0",
|
|
47
|
+
// The ARN or RIA code printed on the folio identifies a person.
|
|
48
|
+
advisor: "P2",
|
|
49
|
+
plan: "P0",
|
|
50
|
+
option: "P0",
|
|
51
|
+
nominees: "P3",
|
|
52
|
+
open: "P1",
|
|
53
|
+
close: "P1",
|
|
54
|
+
closeCalculated: "P1",
|
|
55
|
+
nav: "P1",
|
|
56
|
+
valuation: "P1",
|
|
57
|
+
cost: "P1",
|
|
58
|
+
transactions: nested("Transaction"),
|
|
59
|
+
checks: nested("Check"),
|
|
60
|
+
source: nested("Source"),
|
|
61
|
+
footerSource: nested("Source"),
|
|
62
|
+
},
|
|
63
|
+
Transaction: {
|
|
64
|
+
order: "P0",
|
|
65
|
+
date: "P1",
|
|
66
|
+
description: "PX",
|
|
67
|
+
amount: "P1",
|
|
68
|
+
units: "P1",
|
|
69
|
+
price: "P1",
|
|
70
|
+
balance: "P1",
|
|
71
|
+
type: "P1",
|
|
72
|
+
dividendRate: "P1",
|
|
73
|
+
giftFolio: "P2",
|
|
74
|
+
signCorrected: "P1",
|
|
75
|
+
source: nested("Source"),
|
|
76
|
+
},
|
|
77
|
+
Check: {
|
|
78
|
+
id: "P0",
|
|
79
|
+
status: "P1",
|
|
80
|
+
expected: "PX",
|
|
81
|
+
observed: "PX",
|
|
82
|
+
tolerance: "P1",
|
|
83
|
+
detail: "PX",
|
|
84
|
+
source: nested("Source"),
|
|
85
|
+
},
|
|
86
|
+
Source: { page: "P0", y: "P0" },
|
|
87
|
+
// The row's label is printed text, so it is treated as free text; the totals are amounts.
|
|
88
|
+
PrintedTotal: { label: "PX", cost: "P1", marketValue: "P1", source: nested("Source") },
|
|
89
|
+
Coverage: {
|
|
90
|
+
pages: "P0",
|
|
91
|
+
datedRowsSeen: "P0",
|
|
92
|
+
transactionsParsed: "P0",
|
|
93
|
+
datedRowsSkipped: nested("CoverageEntry"),
|
|
94
|
+
ambiguousCells: nested("AmbiguousCell"),
|
|
95
|
+
schemeRegionsUnbuilt: nested("CoverageEntry"),
|
|
96
|
+
},
|
|
97
|
+
// Reasons can quote the skipped text (and on summary statements, the folio).
|
|
98
|
+
CoverageEntry: { source: nested("Source"), reason: "PX" },
|
|
99
|
+
AmbiguousCell: { source: nested("Source"), text: "PX", reason: "PX" },
|
|
100
|
+
};
|
|
101
|
+
export const isNested = (f) => typeof f === "object";
|
|
102
|
+
/** Every leaf field and its class, as "Map.field" (for inventories and tests). */
|
|
103
|
+
export function privacyInventory() {
|
|
104
|
+
const out = [];
|
|
105
|
+
for (const [map, fields] of Object.entries(PRIVACY)) {
|
|
106
|
+
for (const [field, c] of Object.entries(fields))
|
|
107
|
+
if (!isNested(c))
|
|
108
|
+
out.push({ map, field, class: c });
|
|
109
|
+
}
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
113
|
+
/** JSON Schema $defs that are values, not records: their parts take the class of the field holding them. */
|
|
114
|
+
const VALUE_DEFS = new Set(["Num", "IsoDate"]);
|
|
115
|
+
/**
|
|
116
|
+
* Writes `"x-privacy": "<class>"` on every leaf property of the CasDocument
|
|
117
|
+
* JSON Schema (generated from schema.ts) and returns the same object.
|
|
118
|
+
* Throws when a property has no class, when a class names a property the
|
|
119
|
+
* schema doesn't have, or when an object schema can't be matched to a map.
|
|
120
|
+
*/
|
|
121
|
+
export function annotatePrivacy(schema) {
|
|
122
|
+
const defs = isObject(schema.$defs) ? schema.$defs : {};
|
|
123
|
+
const problems = [];
|
|
124
|
+
const done = new Set();
|
|
125
|
+
/** The object schema behind a property: through $ref, nullable anyOf and arrays. */
|
|
126
|
+
const objectBehind = (node) => {
|
|
127
|
+
if (!isObject(node))
|
|
128
|
+
return null;
|
|
129
|
+
if (typeof node.$ref === "string") {
|
|
130
|
+
const name = node.$ref.replace(/^#\/\$defs\//, "");
|
|
131
|
+
const hit = objectBehind(defs[name]);
|
|
132
|
+
return hit ? { schema: hit.schema, def: name } : null;
|
|
133
|
+
}
|
|
134
|
+
if (Array.isArray(node.anyOf)) {
|
|
135
|
+
for (const branch of node.anyOf) {
|
|
136
|
+
const hit = objectBehind(branch);
|
|
137
|
+
if (hit)
|
|
138
|
+
return hit;
|
|
139
|
+
}
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
if (node.type === "array")
|
|
143
|
+
return objectBehind(node.items);
|
|
144
|
+
if (isObject(node.properties))
|
|
145
|
+
return { schema: node, def: null };
|
|
146
|
+
return null;
|
|
147
|
+
};
|
|
148
|
+
const walk = (node, mapName, where) => {
|
|
149
|
+
const map = PRIVACY[mapName];
|
|
150
|
+
const props = isObject(node.properties) ? node.properties : {};
|
|
151
|
+
for (const key of Object.keys(props)) {
|
|
152
|
+
const c = map[key];
|
|
153
|
+
const prop = props[key];
|
|
154
|
+
if (c === undefined) {
|
|
155
|
+
problems.push(`${where}.${key} has no privacy class (packages/engine/src/privacy.ts, ${mapName})`);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (!isObject(prop))
|
|
159
|
+
continue;
|
|
160
|
+
if (!isNested(c)) {
|
|
161
|
+
prop["x-privacy"] = c;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
const inner = objectBehind(prop);
|
|
165
|
+
if (!inner) {
|
|
166
|
+
problems.push(`${where}.${key} is classed as nested ${c.nested} but its schema has no object`);
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
const id = inner.def ? `def:${inner.def}` : `${where}.${key}`;
|
|
170
|
+
if (done.has(id))
|
|
171
|
+
continue;
|
|
172
|
+
done.add(id);
|
|
173
|
+
walk(inner.schema, c.nested, inner.def ?? `${where}.${key}`);
|
|
174
|
+
}
|
|
175
|
+
for (const key of Object.keys(map)) {
|
|
176
|
+
if (!(key in props))
|
|
177
|
+
problems.push(`${where}.${key} is classed in ${mapName} but not in the schema`);
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
walk(schema, "CasDocument", "CasDocument");
|
|
181
|
+
// Every record-like $def must have been reached through a classed field.
|
|
182
|
+
for (const [name, def] of Object.entries(defs)) {
|
|
183
|
+
if (VALUE_DEFS.has(name) || !isObject(def) || !isObject(def.properties))
|
|
184
|
+
continue;
|
|
185
|
+
if (!done.has(`def:${name}`))
|
|
186
|
+
problems.push(`$defs/${name} is not reachable through a classed field`);
|
|
187
|
+
}
|
|
188
|
+
if (problems.length)
|
|
189
|
+
throw new Error(`privacy classes:\n ${problems.join("\n ")}`);
|
|
190
|
+
return schema;
|
|
191
|
+
}
|
|
192
|
+
const num = (n) => (n ? { value: n.value, scale: n.scale } : null);
|
|
193
|
+
const src = (s) => ({ page: s.page, y: s.y });
|
|
194
|
+
const dated = (d) => (d ? { date: d.date, value: num(d.value) } : null);
|
|
195
|
+
/** The P0/P1 projection of a CasDocument. Builds new objects; never shares references with the input. */
|
|
196
|
+
export function redactCas(cas) {
|
|
197
|
+
return {
|
|
198
|
+
schemaVersion: cas.schemaVersion,
|
|
199
|
+
source: { issuer: cas.source.issuer, variant: cas.source.variant, engineVersion: cas.source.engineVersion },
|
|
200
|
+
provenance: {
|
|
201
|
+
pageCount: cas.provenance.pageCount,
|
|
202
|
+
pdfVersion: cas.provenance.pdfVersion,
|
|
203
|
+
encrypted: cas.provenance.encrypted,
|
|
204
|
+
extractor: cas.provenance.extractor,
|
|
205
|
+
},
|
|
206
|
+
statementPeriod: cas.statementPeriod ? { from: cas.statementPeriod.from, to: cas.statementPeriod.to } : null,
|
|
207
|
+
folios: cas.folios.map((f) => ({
|
|
208
|
+
amc: f.amc,
|
|
209
|
+
schemes: f.schemes.map((s) => ({
|
|
210
|
+
name: s.name,
|
|
211
|
+
rtaCode: s.rtaCode,
|
|
212
|
+
rta: s.rta,
|
|
213
|
+
isin: s.isin,
|
|
214
|
+
plan: s.plan,
|
|
215
|
+
option: s.option,
|
|
216
|
+
open: num(s.open),
|
|
217
|
+
close: num(s.close),
|
|
218
|
+
closeCalculated: s.closeCalculated,
|
|
219
|
+
nav: dated(s.nav),
|
|
220
|
+
valuation: dated(s.valuation),
|
|
221
|
+
cost: num(s.cost),
|
|
222
|
+
transactions: s.transactions.map((t) => ({
|
|
223
|
+
order: t.order,
|
|
224
|
+
date: t.date,
|
|
225
|
+
amount: num(t.amount),
|
|
226
|
+
units: num(t.units),
|
|
227
|
+
price: num(t.price),
|
|
228
|
+
balance: num(t.balance),
|
|
229
|
+
type: t.type,
|
|
230
|
+
dividendRate: t.dividendRate,
|
|
231
|
+
signCorrected: t.signCorrected,
|
|
232
|
+
source: src(t.source),
|
|
233
|
+
})),
|
|
234
|
+
checks: s.checks.map((c) => {
|
|
235
|
+
const out = { id: c.id, status: c.status };
|
|
236
|
+
if (c.tolerance !== undefined)
|
|
237
|
+
out.tolerance = c.tolerance;
|
|
238
|
+
if (c.source)
|
|
239
|
+
out.source = src(c.source);
|
|
240
|
+
return out;
|
|
241
|
+
}),
|
|
242
|
+
source: src(s.source),
|
|
243
|
+
footerSource: s.footerSource ? src(s.footerSource) : null,
|
|
244
|
+
})),
|
|
245
|
+
})),
|
|
246
|
+
coverage: {
|
|
247
|
+
pages: cas.coverage.pages,
|
|
248
|
+
datedRowsSeen: cas.coverage.datedRowsSeen,
|
|
249
|
+
transactionsParsed: cas.coverage.transactionsParsed,
|
|
250
|
+
datedRowsSkipped: cas.coverage.datedRowsSkipped.map((e) => ({ source: src(e.source) })),
|
|
251
|
+
ambiguousCells: cas.coverage.ambiguousCells.map((e) => ({ source: src(e.source) })),
|
|
252
|
+
schemeRegionsUnbuilt: cas.coverage.schemeRegionsUnbuilt.map((e) => ({ source: src(e.source) })),
|
|
253
|
+
},
|
|
254
|
+
warningCount: cas.warnings.length,
|
|
255
|
+
};
|
|
256
|
+
}
|