sec-edgar-api 1.0.1 → 1.0.2

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.
@@ -14,19 +14,20 @@ var XBRLParser = /** @class */ (function () {
14
14
  this.schemaParser = new SchemaParser_1.default();
15
15
  }
16
16
  XBRLParser.prototype.filterDocuments = function (documents) {
17
- var _a, _b, _c, _d, _e, _f;
17
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
18
18
  var xmlDocs = documents.filter(function (doc) { return doc.fileName.endsWith('.xml') || doc.fileName.endsWith('.xsd'); });
19
+ var instanceDoc = (_a = xmlDocs.find(function (doc) {
20
+ return doc.description.toLowerCase().includes('instance doc') ||
21
+ doc.fileName.endsWith('_htm.xml') ||
22
+ (doc.content.includes('<us-gaap') && doc.content.includes('<context'));
23
+ })) !== null && _a !== void 0 ? _a : null;
19
24
  return {
20
- instanceDoc: (_a = xmlDocs.find(function (doc) {
21
- return doc.description.toLowerCase().includes('instance doc') ||
22
- doc.fileName.endsWith('_htm.xml') ||
23
- (doc.content.includes('<us-gaap') && doc.content.includes('<context'));
24
- })) !== null && _a !== void 0 ? _a : null,
25
+ instanceDoc: instanceDoc,
25
26
  schemaDoc: (_b = xmlDocs.find(function (doc) { return doc.fileName.endsWith('.xsd'); })) !== null && _b !== void 0 ? _b : null,
26
- linkbasePresentationDoc: (_c = xmlDocs.find(function (doc) { return doc.fileName.endsWith('_pre.xml'); })) !== null && _c !== void 0 ? _c : null,
27
- linkbaseDefinitionDoc: (_d = xmlDocs.find(function (doc) { return doc.fileName.endsWith('_def.xml'); })) !== null && _d !== void 0 ? _d : null,
28
- linkbaseCalculationDoc: (_e = xmlDocs.find(function (doc) { return doc.fileName.endsWith('_cal.xml'); })) !== null && _e !== void 0 ? _e : null,
29
- linkbaseLabelDoc: (_f = xmlDocs.find(function (doc) { return doc.fileName.endsWith('_lab.xml'); })) !== null && _f !== void 0 ? _f : null,
27
+ linkbasePresentationDoc: (_d = (_c = xmlDocs.find(function (doc) { return doc.fileName.endsWith('_pre.xml'); })) !== null && _c !== void 0 ? _c : xmlDocs.find(function (doc) { return doc !== instanceDoc && doc.content.includes('link:presentationLink>'); })) !== null && _d !== void 0 ? _d : null,
28
+ linkbaseDefinitionDoc: (_f = (_e = xmlDocs.find(function (doc) { return doc.fileName.endsWith('_def.xml'); })) !== null && _e !== void 0 ? _e : xmlDocs.find(function (doc) { return doc !== instanceDoc && doc.content.includes('link:definitionLink>'); })) !== null && _f !== void 0 ? _f : null,
29
+ linkbaseCalculationDoc: (_h = (_g = xmlDocs.find(function (doc) { return doc.fileName.endsWith('_cal.xml'); })) !== null && _g !== void 0 ? _g : xmlDocs.find(function (doc) { return doc !== instanceDoc && doc.content.includes('link:calculationLink>'); })) !== null && _h !== void 0 ? _h : null,
30
+ linkbaseLabelDoc: (_k = (_j = xmlDocs.find(function (doc) { return doc.fileName.endsWith('_lab.xml'); })) !== null && _j !== void 0 ? _j : xmlDocs.find(function (doc) { return doc !== instanceDoc && doc.content.includes('link:labelLink>'); })) !== null && _k !== void 0 ? _k : null,
30
31
  };
31
32
  };
32
33
  XBRLParser.prototype.createXbrlDocument = function (doc, xbrl) {
@@ -1,3 +1,6 @@
1
1
  import { XbrlLinkbase } from '../types/xbrl.type';
2
+ /**
3
+ * Gets labels by taxonomy from the label linkbase. priority level: verboseLabel > terseLabel > label.
4
+ */
2
5
  export declare function getLabelByTaxonomy(labelLinkbase: XbrlLinkbase): Record<string, string>;
3
6
  export declare function getTaxonomyFromId(id: string): string;
@@ -1,16 +1,29 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getTaxonomyFromId = exports.getLabelByTaxonomy = void 0;
4
+ /**
5
+ * Gets labels by taxonomy from the label linkbase. priority level: verboseLabel > terseLabel > label.
6
+ */
4
7
  function getLabelByTaxonomy(labelLinkbase) {
5
8
  var _a;
6
9
  var labelByTaxonomy = {};
10
+ var taxonomyWithVerboseLabels = new Set();
7
11
  (_a = labelLinkbase.labelLink) === null || _a === void 0 ? void 0 : _a.forEach(function (link) {
8
12
  var _a;
9
13
  (_a = link.label) === null || _a === void 0 ? void 0 : _a.forEach(function (_a) {
10
14
  var _b = _a.label, label = _b === void 0 ? '' : _b, _c = _a.text, text = _c === void 0 ? '' : _c, _d = _a.role, role = _d === void 0 ? '' : _d;
11
15
  var taxonomy = getTaxonomyFromId(label);
16
+ // skip if verbose label already exists for this taxonomy
17
+ if (taxonomyWithVerboseLabels.has(taxonomy)) {
18
+ return;
19
+ }
20
+ // label, terseLabel, or verboseLabel
21
+ var labelType = role.substring(role.lastIndexOf('/') + 1);
22
+ if (labelType === 'verboseLabel') {
23
+ taxonomyWithVerboseLabels.add(taxonomy);
24
+ }
12
25
  // prefer terseLabel over regular label
13
- if (role.endsWith('terseLabel') || !labelByTaxonomy[taxonomy]) {
26
+ if (!labelByTaxonomy[taxonomy] || labelType === 'terseLabel' || labelType === 'verboseLabel') {
14
27
  labelByTaxonomy[taxonomy] = text;
15
28
  }
16
29
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sec-edgar-api",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Fetch and parse SEC earnings reports and other filings. Useful for financial analysis.",
5
5
  "main": "build/index.js",
6
6
  "author": "Andrew Evers (https://github.com/andyevers)",
@@ -1,97 +0,0 @@
1
- /**
2
- * Practical examples of working with XBRL relationships using your existing types
3
- */
4
- import type { XbrlInstance, XbrlLinkbase } from '../types';
5
- /**
6
- * Example 1: Extract presentation hierarchy from your XBRL data
7
- */
8
- export declare function extractPresentationHierarchy(presentationLinkbase: XbrlLinkbase): Record<string, ConceptNode>;
9
- /**
10
- * Example 2: Extract calculation relationships
11
- */
12
- export declare function extractCalculationRelationships(calculationLinkbase: XbrlLinkbase): Record<string, CalculationRule>;
13
- /**
14
- * Example 3: Extract dimensional relationships (axis/member mappings)
15
- */
16
- export declare function extractDimensionalRelationships(definitionLinkbase: XbrlLinkbase): Record<string, DimensionStructure>;
17
- /**
18
- * Example 4: Find facts with specific dimensional context
19
- */
20
- export declare function findFactsWithDimensions(instance: XbrlInstance, conceptName: string, dimensionFilters?: {
21
- dimension: string;
22
- member?: string;
23
- }[]): {
24
- fact: import("../types").XbrlElement;
25
- context: import("../types").XbrlContext;
26
- dimensions: {
27
- dimension: string;
28
- member: string;
29
- }[];
30
- }[];
31
- /**
32
- * Example 5: Build a complete concept map with all relationships
33
- */
34
- export declare function buildCompleteConceptMap(linkbases: {
35
- presentation: XbrlLinkbase;
36
- calculation: XbrlLinkbase;
37
- definition: XbrlLinkbase;
38
- label: XbrlLinkbase;
39
- }): Map<string, CompleteConcept>;
40
- interface ConceptNode {
41
- concept: string;
42
- children: ConceptChild[];
43
- role: string;
44
- order: number;
45
- }
46
- interface ConceptChild {
47
- concept: string;
48
- order: number;
49
- preferredLabel?: string;
50
- }
51
- interface CalculationRule {
52
- summationItem: string;
53
- contributingItems: CalculationItem[];
54
- }
55
- interface CalculationItem {
56
- concept: string;
57
- weight: number;
58
- order: number;
59
- }
60
- interface DimensionStructure {
61
- concept: string;
62
- type: string;
63
- dimensions?: string[];
64
- domains?: string[];
65
- members?: string[];
66
- }
67
- interface ConceptLabel {
68
- text: string;
69
- role: string;
70
- lang: string;
71
- }
72
- interface CompleteConcept {
73
- name: string;
74
- labels: ConceptLabel[];
75
- presentation: ConceptNode | null;
76
- calculation: CalculationRule | null;
77
- dimensions: DimensionStructure | null;
78
- }
79
- export declare function usageExamples(): {
80
- segmentRevenue: {
81
- fact: import("../types").XbrlElement;
82
- context: import("../types").XbrlContext;
83
- dimensions: {
84
- dimension: string;
85
- member: string;
86
- }[];
87
- }[];
88
- northAmericaRevenue: {
89
- fact: import("../types").XbrlElement;
90
- context: import("../types").XbrlContext;
91
- dimensions: {
92
- dimension: string;
93
- member: string;
94
- }[];
95
- }[];
96
- };
97
- export {};
@@ -1,277 +0,0 @@
1
- "use strict";
2
- /**
3
- * Practical examples of working with XBRL relationships using your existing types
4
- */
5
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
6
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
7
- if (ar || !(i in from)) {
8
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
9
- ar[i] = from[i];
10
- }
11
- }
12
- return to.concat(ar || Array.prototype.slice.call(from));
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.usageExamples = exports.buildCompleteConceptMap = exports.findFactsWithDimensions = exports.extractDimensionalRelationships = exports.extractCalculationRelationships = exports.extractPresentationHierarchy = void 0;
16
- /**
17
- * Example 1: Extract presentation hierarchy from your XBRL data
18
- */
19
- function extractPresentationHierarchy(presentationLinkbase) {
20
- var _a;
21
- var hierarchy = {};
22
- // Process each presentation link (usually one per role/statement)
23
- (_a = presentationLinkbase.presentationLink) === null || _a === void 0 ? void 0 : _a.forEach(function (link) {
24
- var roleUri = link.role || 'default';
25
- // Get all concept locators and relationship arcs
26
- var locators = (link.loc || []);
27
- var arcs = (link.presentationArc || []);
28
- // Build concept reference map
29
- var conceptMap = new Map();
30
- locators.forEach(function (loc) {
31
- var concept = extractConceptFromHref(loc.href);
32
- conceptMap.set(loc.label, concept);
33
- });
34
- // Build parent-child relationships
35
- arcs.forEach(function (arc) {
36
- var parent = conceptMap.get(arc.from);
37
- var child = conceptMap.get(arc.to);
38
- if (parent && child) {
39
- // Initialize parent node if it doesn't exist
40
- if (!hierarchy[parent]) {
41
- hierarchy[parent] = {
42
- concept: parent,
43
- children: [],
44
- role: roleUri,
45
- order: 0,
46
- };
47
- }
48
- // Add child with ordering information
49
- hierarchy[parent].children.push({
50
- concept: child,
51
- order: arc.order || 0,
52
- preferredLabel: arc.preferredLabel,
53
- });
54
- // Sort children by order
55
- hierarchy[parent].children.sort(function (a, b) { return a.order - b.order; });
56
- }
57
- });
58
- });
59
- return hierarchy;
60
- }
61
- exports.extractPresentationHierarchy = extractPresentationHierarchy;
62
- /**
63
- * Example 2: Extract calculation relationships
64
- */
65
- function extractCalculationRelationships(calculationLinkbase) {
66
- var _a;
67
- var calculations = {};
68
- (_a = calculationLinkbase.calculationLink) === null || _a === void 0 ? void 0 : _a.forEach(function (link) {
69
- var locators = (link.loc || []);
70
- var arcs = (link.calculationArc || []);
71
- // Build concept reference map
72
- var conceptMap = new Map();
73
- locators.forEach(function (loc) {
74
- var concept = extractConceptFromHref(loc.href);
75
- conceptMap.set(loc.label, concept);
76
- });
77
- // Group arcs by summation item (the 'to' end of the arc)
78
- var summationGroups = new Map();
79
- arcs.forEach(function (arc) {
80
- var summationConcept = conceptMap.get(arc.to);
81
- if (summationConcept) {
82
- if (!summationGroups.has(summationConcept)) {
83
- summationGroups.set(summationConcept, []);
84
- }
85
- summationGroups.get(summationConcept).push(arc);
86
- }
87
- });
88
- // Build calculation rules
89
- summationGroups.forEach(function (contributingArcs, summationConcept) {
90
- calculations[summationConcept] = {
91
- summationItem: summationConcept,
92
- contributingItems: contributingArcs.map(function (arc) { return ({
93
- concept: conceptMap.get(arc.from) || '',
94
- weight: arc.weight || 1,
95
- order: arc.order || 0,
96
- }); }),
97
- };
98
- });
99
- });
100
- return calculations;
101
- }
102
- exports.extractCalculationRelationships = extractCalculationRelationships;
103
- /**
104
- * Example 3: Extract dimensional relationships (axis/member mappings)
105
- */
106
- function extractDimensionalRelationships(definitionLinkbase) {
107
- var _a;
108
- var dimensions = {};
109
- (_a = definitionLinkbase.definitionLink) === null || _a === void 0 ? void 0 : _a.forEach(function (link) {
110
- var locators = (link.loc || []);
111
- var arcs = (link.definitionArc || []);
112
- // Build concept reference map
113
- var conceptMap = new Map();
114
- locators.forEach(function (loc) {
115
- var concept = extractConceptFromHref(loc.href);
116
- conceptMap.set(loc.label, concept);
117
- });
118
- // Process dimensional arcs based on their arcrole
119
- arcs.forEach(function (arc) {
120
- var fromConcept = conceptMap.get(arc.from);
121
- var toConcept = conceptMap.get(arc.to);
122
- var arcrole = arc.arcrole || '';
123
- if (fromConcept && toConcept) {
124
- switch (arcrole) {
125
- case 'http://xbrl.org/int/dim/arcrole/hypercube-dimension':
126
- // Hypercube contains dimension
127
- initializeDimension(dimensions, fromConcept, 'hypercube');
128
- dimensions[fromConcept].dimensions = dimensions[fromConcept].dimensions || [];
129
- dimensions[fromConcept].dimensions.push(toConcept);
130
- break;
131
- case 'http://xbrl.org/int/dim/arcrole/dimension-domain':
132
- // Dimension has domain
133
- initializeDimension(dimensions, fromConcept, 'dimension');
134
- dimensions[fromConcept].domains = dimensions[fromConcept].domains || [];
135
- dimensions[fromConcept].domains.push(toConcept);
136
- break;
137
- case 'http://xbrl.org/int/dim/arcrole/domain-member':
138
- // Domain has member
139
- initializeDimension(dimensions, fromConcept, 'domain');
140
- dimensions[fromConcept].members = dimensions[fromConcept].members || [];
141
- dimensions[fromConcept].members.push(toConcept);
142
- break;
143
- }
144
- }
145
- });
146
- });
147
- return dimensions;
148
- }
149
- exports.extractDimensionalRelationships = extractDimensionalRelationships;
150
- /**
151
- * Example 4: Find facts with specific dimensional context
152
- */
153
- function findFactsWithDimensions(instance, conceptName, dimensionFilters) {
154
- var matchingFacts = [];
155
- // Filter facts by concept name
156
- var conceptFacts = instance.factElements.filter(function (fact) { return fact.name === conceptName; });
157
- var _loop_1 = function (fact) {
158
- // Get the context for this fact
159
- var context = instance.contexts.find(function (ctx) { return ctx.id === fact.contextRef; });
160
- if (!context)
161
- return "continue";
162
- // Check if this fact matches our dimensional filters
163
- if (dimensionFilters) {
164
- var matchesFilters = dimensionFilters.every(function (filter) {
165
- var matchingSegment = context.entity.segment.find(function (seg) { return seg.dimension === filter.dimension; });
166
- if (!matchingSegment)
167
- return false;
168
- // If specific member is required, check for it
169
- if (filter.member && matchingSegment.value !== filter.member) {
170
- return false;
171
- }
172
- return true;
173
- });
174
- if (!matchesFilters)
175
- return "continue";
176
- }
177
- matchingFacts.push({
178
- fact: fact,
179
- context: context,
180
- dimensions: context.entity.segment.map(function (seg) { return ({
181
- dimension: seg.dimension,
182
- member: seg.value,
183
- }); }),
184
- });
185
- };
186
- for (var _i = 0, conceptFacts_1 = conceptFacts; _i < conceptFacts_1.length; _i++) {
187
- var fact = conceptFacts_1[_i];
188
- _loop_1(fact);
189
- }
190
- return matchingFacts;
191
- }
192
- exports.findFactsWithDimensions = findFactsWithDimensions;
193
- /**
194
- * Example 5: Build a complete concept map with all relationships
195
- */
196
- function buildCompleteConceptMap(linkbases) {
197
- var conceptMap = new Map();
198
- // Extract all relationships
199
- var presentationHierarchy = extractPresentationHierarchy(linkbases.presentation);
200
- var calculationRelationships = extractCalculationRelationships(linkbases.calculation);
201
- var dimensionalRelationships = extractDimensionalRelationships(linkbases.definition);
202
- var labels = extractLabels(linkbases.label);
203
- // Combine all concepts
204
- var allConcepts = new Set(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], Object.keys(presentationHierarchy), true), Object.keys(calculationRelationships), true), Object.keys(dimensionalRelationships), true), Object.keys(labels), true));
205
- // Build complete concept entries
206
- allConcepts.forEach(function (concept) {
207
- conceptMap.set(concept, {
208
- name: concept,
209
- labels: labels[concept] || [],
210
- presentation: presentationHierarchy[concept] || null,
211
- calculation: calculationRelationships[concept] || null,
212
- dimensions: dimensionalRelationships[concept] || null,
213
- });
214
- });
215
- return conceptMap;
216
- }
217
- exports.buildCompleteConceptMap = buildCompleteConceptMap;
218
- // Helper functions and types
219
- function extractConceptFromHref(href) {
220
- var match = href.match(/#(.+)$/);
221
- return match ? match[1] : '';
222
- }
223
- function initializeDimension(dimensions, concept, type) {
224
- if (!dimensions[concept]) {
225
- dimensions[concept] = {
226
- concept: concept,
227
- type: type,
228
- dimensions: [],
229
- domains: [],
230
- members: [],
231
- };
232
- }
233
- }
234
- function extractLabels(labelLinkbase) {
235
- var _a;
236
- var labels = {};
237
- (_a = labelLinkbase.labelLink) === null || _a === void 0 ? void 0 : _a.forEach(function (link) {
238
- var locators = (link.loc || []);
239
- var arcs = (link.labelArc || []);
240
- var labelResources = link.label || [];
241
- // Build concept reference map
242
- var conceptMap = new Map();
243
- locators.forEach(function (loc) {
244
- var concept = extractConceptFromHref(loc.href);
245
- conceptMap.set(loc.label, concept);
246
- });
247
- // Connect labels to concepts
248
- arcs.forEach(function (arc) {
249
- var concept = conceptMap.get(arc.from);
250
- var labelResource = labelResources.find(function (r) { return r.label === arc.to; });
251
- if (concept && labelResource) {
252
- labels[concept] = labels[concept] || [];
253
- labels[concept].push({
254
- text: labelResource.text || '',
255
- role: labelResource.role || '',
256
- lang: labelResource.lang || 'en',
257
- });
258
- }
259
- });
260
- });
261
- return labels;
262
- }
263
- // Usage examples:
264
- function usageExamples() {
265
- // Example: Get all revenue facts broken down by segment
266
- var segmentRevenue = findFactsWithDimensions({}, // Your XBRL instance
267
- 'us-gaap:Revenues', [{ dimension: 'us-gaap:SegmentDomain' }]);
268
- // Example: Get specific segment revenue
269
- var northAmericaRevenue = findFactsWithDimensions({}, 'us-gaap:Revenues', [
270
- {
271
- dimension: 'us-gaap:SegmentDomain',
272
- member: 'us-gaap:NorthAmericaSegmentMember',
273
- },
274
- ]);
275
- return { segmentRevenue: segmentRevenue, northAmericaRevenue: northAmericaRevenue };
276
- }
277
- exports.usageExamples = usageExamples;
@@ -1,145 +0,0 @@
1
- import type { XbrlInstance, XbrlLinkbase, XbrlSchema } from '../types';
2
- export declare class XbrlRelationshipBuilder {
3
- private instance;
4
- private schema;
5
- private presentationLinkbase;
6
- private calculationLinkbase;
7
- private definitionLinkbase;
8
- private labelLinkbase;
9
- constructor(data: {
10
- instance: XbrlInstance;
11
- schema: XbrlSchema;
12
- presentationLinkbase: XbrlLinkbase;
13
- calculationLinkbase: XbrlLinkbase;
14
- definitionLinkbase: XbrlLinkbase;
15
- labelLinkbase: XbrlLinkbase;
16
- });
17
- /**
18
- * 1. PRESENTATION LINKBASE - Defines hierarchical structure
19
- * Shows how concepts are organized in financial statements
20
- */
21
- buildPresentationHierarchy(): Record<string, ConceptHierarchy>;
22
- /**
23
- * 2. CALCULATION LINKBASE - Defines mathematical relationships
24
- * Shows how concepts add up to totals
25
- */
26
- buildCalculationRelationships(): Record<string, CalculationRelationship>;
27
- /**
28
- * 3. DEFINITION LINKBASE - Defines dimensional relationships
29
- * Shows axis, members, domains, and hypercubes
30
- */
31
- buildDimensionalRelationships(): Record<string, DimensionalRelationship>;
32
- /**
33
- * 4. LABEL LINKBASE - Provides human-readable labels
34
- * Maps concepts to their display labels
35
- */
36
- buildLabelMappings(): Record<string, ConceptLabel[]>;
37
- /**
38
- * 5. RELATING FACTS TO DIMENSIONS
39
- * Shows how actual data values relate to dimensional context
40
- */
41
- buildFactDimensionalMappings(): FactDimensionalMapping[];
42
- /**
43
- * 6. COMPREHENSIVE TAXONOMY MAPPING
44
- * Combines all relationships into a unified structure
45
- */
46
- buildComprehensiveTaxonomyMap(): {
47
- presentation: Record<string, ConceptHierarchy>;
48
- calculations: Record<string, CalculationRelationship>;
49
- dimensions: Record<string, DimensionalRelationship>;
50
- labels: Record<string, ConceptLabel[]>;
51
- factMappings: FactDimensionalMapping[];
52
- getConceptChildren: (concept: string) => ConceptChild[];
53
- getConceptCalculation: (concept: string) => CalculationRelationship;
54
- getConceptLabels: (concept: string) => ConceptLabel[];
55
- getFactsForConcept: (concept: string) => FactDimensionalMapping[];
56
- getDimensionsForConcept: (concept: string) => DimensionalRelationship;
57
- };
58
- private getLocators;
59
- private getPresentationArcs;
60
- private getCalculationArcs;
61
- private getDefinitionArcs;
62
- private getLabelArcs;
63
- private getLabelResources;
64
- private getConceptFromLocator;
65
- private addHypercubeDimension;
66
- private addDimensionDomain;
67
- private addDomainMember;
68
- private addAllRelationship;
69
- }
70
- interface ConceptHierarchy {
71
- concept: string;
72
- children: ConceptChild[];
73
- order: number;
74
- role: string;
75
- }
76
- interface ConceptChild {
77
- concept: string;
78
- order: number;
79
- preferredLabel?: string;
80
- }
81
- interface CalculationRelationship {
82
- summationItem: string;
83
- contributingItems: CalculationItem[];
84
- }
85
- interface CalculationItem {
86
- concept: string;
87
- weight: number;
88
- order: number;
89
- }
90
- interface DimensionalRelationship {
91
- type: 'hypercube' | 'dimension' | 'domain' | 'member';
92
- concept: string;
93
- dimensions?: DimensionalItem[];
94
- domains?: DimensionalItem[];
95
- members?: DimensionalItem[];
96
- all?: DimensionalItem[];
97
- }
98
- interface DimensionalItem {
99
- concept: string;
100
- type: string;
101
- }
102
- interface ConceptLabel {
103
- text: string;
104
- role: string;
105
- lang: string;
106
- }
107
- interface FactDimensionalMapping {
108
- factName: string;
109
- value: string;
110
- unit: string;
111
- context: DimensionalContext;
112
- contextRef: string;
113
- }
114
- interface DimensionalContext {
115
- entity: string;
116
- period: {
117
- startDate?: string;
118
- endDate?: string;
119
- instant?: string;
120
- };
121
- dimensions: {
122
- dimension: string;
123
- member: string;
124
- }[];
125
- }
126
- export declare function exampleUsage(xbrlData: {
127
- instance: XbrlInstance;
128
- schema: XbrlSchema;
129
- presentationLinkbase: XbrlLinkbase;
130
- calculationLinkbase: XbrlLinkbase;
131
- definitionLinkbase: XbrlLinkbase;
132
- labelLinkbase: XbrlLinkbase;
133
- }): {
134
- presentation: Record<string, ConceptHierarchy>;
135
- calculations: Record<string, CalculationRelationship>;
136
- dimensions: Record<string, DimensionalRelationship>;
137
- labels: Record<string, ConceptLabel[]>;
138
- factMappings: FactDimensionalMapping[];
139
- getConceptChildren: (concept: string) => ConceptChild[];
140
- getConceptCalculation: (concept: string) => CalculationRelationship;
141
- getConceptLabels: (concept: string) => ConceptLabel[];
142
- getFactsForConcept: (concept: string) => FactDimensionalMapping[];
143
- getDimensionsForConcept: (concept: string) => DimensionalRelationship;
144
- };
145
- export {};
@@ -1,337 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.exampleUsage = exports.XbrlRelationshipBuilder = void 0;
40
- /**
41
- * Example of how to use XBRL types to relate taxonomies, axis, members, and facts
42
- * using linkbases (calculation, presentation, definition, etc.)
43
- */
44
- var fs = require("fs");
45
- var __1 = require("..");
46
- // Example: Building a comprehensive XBRL relationship map
47
- var XbrlRelationshipBuilder = /** @class */ (function () {
48
- function XbrlRelationshipBuilder(data) {
49
- this.instance = data.instance;
50
- this.schema = data.schema;
51
- this.presentationLinkbase = data.presentationLinkbase;
52
- this.calculationLinkbase = data.calculationLinkbase;
53
- this.definitionLinkbase = data.definitionLinkbase;
54
- this.labelLinkbase = data.labelLinkbase;
55
- }
56
- /**
57
- * 1. PRESENTATION LINKBASE - Defines hierarchical structure
58
- * Shows how concepts are organized in financial statements
59
- */
60
- XbrlRelationshipBuilder.prototype.buildPresentationHierarchy = function () {
61
- var _this = this;
62
- var _a;
63
- var hierarchy = {};
64
- // Process presentation links
65
- (_a = this.presentationLinkbase.presentationLink) === null || _a === void 0 ? void 0 : _a.forEach(function (link) {
66
- var roleUri = link.role || '';
67
- // Get locators (concept references)
68
- var locators = _this.getLocators(link);
69
- var arcs = _this.getPresentationArcs(link);
70
- // Build parent-child relationships
71
- arcs.forEach(function (arc) {
72
- var parentConcept = _this.getConceptFromLocator(locators, arc.from);
73
- var childConcept = _this.getConceptFromLocator(locators, arc.to);
74
- if (parentConcept && childConcept) {
75
- hierarchy[parentConcept] = hierarchy[parentConcept] || {
76
- concept: parentConcept,
77
- children: [],
78
- order: arc.order || 0,
79
- role: roleUri,
80
- };
81
- hierarchy[parentConcept].children.push({
82
- concept: childConcept,
83
- order: arc.order || 0,
84
- preferredLabel: arc.preferredLabel,
85
- });
86
- }
87
- });
88
- });
89
- return hierarchy;
90
- };
91
- /**
92
- * 2. CALCULATION LINKBASE - Defines mathematical relationships
93
- * Shows how concepts add up to totals
94
- */
95
- XbrlRelationshipBuilder.prototype.buildCalculationRelationships = function () {
96
- var _this = this;
97
- var _a;
98
- var calculations = {};
99
- (_a = this.calculationLinkbase.calculationLink) === null || _a === void 0 ? void 0 : _a.forEach(function (link) {
100
- var locators = _this.getLocators(link);
101
- var arcs = _this.getCalculationArcs(link);
102
- arcs.forEach(function (arc) {
103
- var summationConcept = _this.getConceptFromLocator(locators, arc.to);
104
- var itemConcept = _this.getConceptFromLocator(locators, arc.from);
105
- if (summationConcept && itemConcept) {
106
- calculations[summationConcept] = calculations[summationConcept] || {
107
- summationItem: summationConcept,
108
- contributingItems: [],
109
- };
110
- calculations[summationConcept].contributingItems.push({
111
- concept: itemConcept,
112
- weight: arc.weight || 1,
113
- order: arc.order || 0,
114
- });
115
- }
116
- });
117
- });
118
- return calculations;
119
- };
120
- /**
121
- * 3. DEFINITION LINKBASE - Defines dimensional relationships
122
- * Shows axis, members, domains, and hypercubes
123
- */
124
- XbrlRelationshipBuilder.prototype.buildDimensionalRelationships = function () {
125
- var _this = this;
126
- var _a;
127
- var dimensions = {};
128
- (_a = this.definitionLinkbase.definitionLink) === null || _a === void 0 ? void 0 : _a.forEach(function (link) {
129
- var locators = _this.getLocators(link);
130
- var arcs = _this.getDefinitionArcs(link);
131
- arcs.forEach(function (arc) {
132
- var arcrole = arc.arcrole || '';
133
- var fromConcept = _this.getConceptFromLocator(locators, arc.from);
134
- var toConcept = _this.getConceptFromLocator(locators, arc.to);
135
- if (fromConcept && toConcept) {
136
- // Different arcroles define different relationship types
137
- switch (arcrole) {
138
- case 'http://xbrl.org/int/dim/arcrole/hypercube-dimension':
139
- // Hypercube to dimension relationship
140
- _this.addHypercubeDimension(dimensions, fromConcept, toConcept);
141
- break;
142
- case 'http://xbrl.org/int/dim/arcrole/dimension-domain':
143
- // Dimension to domain relationship
144
- _this.addDimensionDomain(dimensions, fromConcept, toConcept);
145
- break;
146
- case 'http://xbrl.org/int/dim/arcrole/domain-member':
147
- // Domain to member relationship
148
- _this.addDomainMember(dimensions, fromConcept, toConcept);
149
- break;
150
- case 'http://xbrl.org/int/dim/arcrole/all':
151
- // All relationship (for hypercubes)
152
- _this.addAllRelationship(dimensions, fromConcept, toConcept);
153
- break;
154
- }
155
- }
156
- });
157
- });
158
- return dimensions;
159
- };
160
- /**
161
- * 4. LABEL LINKBASE - Provides human-readable labels
162
- * Maps concepts to their display labels
163
- */
164
- XbrlRelationshipBuilder.prototype.buildLabelMappings = function () {
165
- var _this = this;
166
- var _a;
167
- var labels = {};
168
- (_a = this.labelLinkbase.labelLink) === null || _a === void 0 ? void 0 : _a.forEach(function (link) {
169
- var locators = _this.getLocators(link);
170
- var arcs = _this.getLabelArcs(link);
171
- var labelResources = _this.getLabelResources(link);
172
- arcs.forEach(function (arc) {
173
- var concept = _this.getConceptFromLocator(locators, arc.from);
174
- var labelResource = labelResources.find(function (r) { return r.label === arc.to; });
175
- if (concept && labelResource) {
176
- labels[concept] = labels[concept] || [];
177
- labels[concept].push({
178
- text: labelResource.text || '',
179
- role: labelResource.role || '',
180
- lang: labelResource.lang || 'en',
181
- });
182
- }
183
- });
184
- });
185
- return labels;
186
- };
187
- /**
188
- * 5. RELATING FACTS TO DIMENSIONS
189
- * Shows how actual data values relate to dimensional context
190
- */
191
- XbrlRelationshipBuilder.prototype.buildFactDimensionalMappings = function () {
192
- var _this = this;
193
- var factMappings = [];
194
- this.instance.factElements.forEach(function (fact) {
195
- var context = _this.instance.contexts.find(function (c) { return c.id === fact.contextRef; });
196
- if (context) {
197
- var dimensionalContext = {
198
- entity: context.entity.identifier.value,
199
- period: context.period,
200
- dimensions: context.entity.segment.map(function (seg) { return ({
201
- dimension: seg.dimension,
202
- member: seg.value,
203
- }); }),
204
- };
205
- factMappings.push({
206
- factName: fact.name,
207
- value: fact.text || '',
208
- unit: fact.unitRef || '',
209
- context: dimensionalContext,
210
- contextRef: fact.contextRef,
211
- });
212
- }
213
- });
214
- return factMappings;
215
- };
216
- /**
217
- * 6. COMPREHENSIVE TAXONOMY MAPPING
218
- * Combines all relationships into a unified structure
219
- */
220
- XbrlRelationshipBuilder.prototype.buildComprehensiveTaxonomyMap = function () {
221
- var presentation = this.buildPresentationHierarchy();
222
- var calculations = this.buildCalculationRelationships();
223
- var dimensions = this.buildDimensionalRelationships();
224
- var labels = this.buildLabelMappings();
225
- var factMappings = this.buildFactDimensionalMappings();
226
- return {
227
- presentation: presentation,
228
- calculations: calculations,
229
- dimensions: dimensions,
230
- labels: labels,
231
- factMappings: factMappings,
232
- // Helper methods to navigate relationships
233
- getConceptChildren: function (concept) { var _a; return ((_a = presentation[concept]) === null || _a === void 0 ? void 0 : _a.children) || []; },
234
- getConceptCalculation: function (concept) { return calculations[concept]; },
235
- getConceptLabels: function (concept) { return labels[concept] || []; },
236
- getFactsForConcept: function (concept) { return factMappings.filter(function (f) { return f.factName === concept; }); },
237
- getDimensionsForConcept: function (concept) { return dimensions[concept]; },
238
- };
239
- };
240
- // Helper methods
241
- XbrlRelationshipBuilder.prototype.getLocators = function (link) {
242
- return (link.loc || []).filter(function (item) { return item.type === 'locator'; });
243
- };
244
- XbrlRelationshipBuilder.prototype.getPresentationArcs = function (link) {
245
- return (link.presentationArc || []).filter(function (item) { return item.type === 'arc'; });
246
- };
247
- XbrlRelationshipBuilder.prototype.getCalculationArcs = function (link) {
248
- return (link.calculationArc || []).filter(function (item) { return item.type === 'arc'; });
249
- };
250
- XbrlRelationshipBuilder.prototype.getDefinitionArcs = function (link) {
251
- return (link.definitionArc || []).filter(function (item) { return item.type === 'arc'; });
252
- };
253
- XbrlRelationshipBuilder.prototype.getLabelArcs = function (link) {
254
- return (link.labelArc || []).filter(function (item) { return item.type === 'arc'; });
255
- };
256
- XbrlRelationshipBuilder.prototype.getLabelResources = function (link) {
257
- return (link.label || []).filter(function (item) { return item.type === 'resource'; });
258
- };
259
- XbrlRelationshipBuilder.prototype.getConceptFromLocator = function (locators, label) {
260
- var locator = locators.find(function (loc) { return loc.label === label; });
261
- if (!locator)
262
- return null;
263
- // Extract concept name from href (e.g., "concept.xsd#us-gaap_Assets")
264
- var match = locator.href.match(/#(.+)$/);
265
- return match ? match[1] : null;
266
- };
267
- XbrlRelationshipBuilder.prototype.addHypercubeDimension = function (dimensions, hypercube, dimension) {
268
- dimensions[hypercube] = dimensions[hypercube] || { type: 'hypercube', concept: hypercube, dimensions: [] };
269
- dimensions[hypercube].dimensions = dimensions[hypercube].dimensions || [];
270
- dimensions[hypercube].dimensions.push({ concept: dimension, type: 'dimension' });
271
- };
272
- XbrlRelationshipBuilder.prototype.addDimensionDomain = function (dimensions, dimension, domain) {
273
- dimensions[dimension] = dimensions[dimension] || { type: 'dimension', concept: dimension, domains: [] };
274
- dimensions[dimension].domains = dimensions[dimension].domains || [];
275
- dimensions[dimension].domains.push({ concept: domain, type: 'domain' });
276
- };
277
- XbrlRelationshipBuilder.prototype.addDomainMember = function (dimensions, domain, member) {
278
- dimensions[domain] = dimensions[domain] || { type: 'domain', concept: domain, members: [] };
279
- dimensions[domain].members = dimensions[domain].members || [];
280
- dimensions[domain].members.push({ concept: member, type: 'member' });
281
- };
282
- XbrlRelationshipBuilder.prototype.addAllRelationship = function (dimensions, from, to) {
283
- // Handle 'all' relationships for hypercubes
284
- dimensions[from] = dimensions[from] || { type: 'hypercube', concept: from, all: [] };
285
- dimensions[from].all = dimensions[from].all || [];
286
- dimensions[from].all.push({ concept: to, type: 'all' });
287
- };
288
- return XbrlRelationshipBuilder;
289
- }());
290
- exports.XbrlRelationshipBuilder = XbrlRelationshipBuilder;
291
- // Example usage:
292
- function exampleUsage(xbrlData) {
293
- var builder = new XbrlRelationshipBuilder(xbrlData);
294
- var taxonomyMap = builder.buildComprehensiveTaxonomyMap();
295
- // Example: Find all children of "Assets" concept
296
- var assetChildren = taxonomyMap.getConceptChildren('us-gaap:Assets');
297
- console.log('Asset children:', assetChildren);
298
- // Example: Get calculation for "Assets" (what adds up to total assets)
299
- var assetCalculation = taxonomyMap.getConceptCalculation('us-gaap:Assets');
300
- console.log('Asset calculation:', assetCalculation);
301
- // Example: Get all labels for "Assets" concept
302
- var assetLabels = taxonomyMap.getConceptLabels('us-gaap:Assets');
303
- console.log('Asset labels:', assetLabels);
304
- // Example: Get all facts for "Assets" concept
305
- var assetFacts = taxonomyMap.getFactsForConcept('us-gaap:Assets');
306
- console.log('Asset facts:', assetFacts);
307
- // Example: Get dimensional information for a concept
308
- var assetDimensions = taxonomyMap.getDimensionsForConcept('us-gaap:Assets');
309
- console.log('Asset dimensions:', assetDimensions);
310
- return taxonomyMap;
311
- }
312
- exports.exampleUsage = exampleUsage;
313
- function runExample() {
314
- return __awaiter(this, void 0, void 0, function () {
315
- var res, builder;
316
- return __generator(this, function (_a) {
317
- switch (_a.label) {
318
- case 0: return [4 /*yield*/, __1.secEdgarApi.getDocumentXbrl({
319
- url: 'https://www.sec.gov/Archives/edgar/data/320193/000032019324000123/0000320193-24-000123.txt',
320
- })];
321
- case 1:
322
- res = _a.sent();
323
- builder = new XbrlRelationshipBuilder({
324
- calculationLinkbase: res.linkbaseCalculation.xbrl,
325
- definitionLinkbase: res.linkbaseDefinition.xbrl,
326
- instance: res.instance.xbrl,
327
- labelLinkbase: res.linkbaseLabel.xbrl,
328
- presentationLinkbase: res.linkbasePresentation.xbrl,
329
- schema: res.schema.xbrl,
330
- });
331
- fs.writeFileSync('fact-dimensional-mappings.json', JSON.stringify(builder.buildFactDimensionalMappings(), null, 2));
332
- return [2 /*return*/];
333
- }
334
- });
335
- });
336
- }
337
- runExample();