-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDFRepairRegistry.ts
More file actions
107 lines (96 loc) · 3.26 KB
/
PDFRepairRegistry.ts
File metadata and controls
107 lines (96 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { PDFDocument } from "@peculiarventures/pdf-doc";
import { PDFRepairStatus } from "./PDFRepairStatus";
/**
* An interface representing a repair rule for a PDF document.
*/
export interface IRepairRule {
/**
* The ID of the repair rule.
*/
id: string;
/**
* A description of the repair rule.
*/
description: string;
/**
* Applies the repair rule to a PDF document.
* @param doc The PDF document to repair.
* @returns A string representing the repair note generated by the rule, or null if no repair was performed.
*/
apply: (doc: PDFDocument) => Promise<string[]>;
/**
* Checks if the repair rule is applicable to a PDF document.
* @param doc
* @returns An enum value indicating the repair status of the PDF document.
*/
check: (doc: PDFDocument) => Promise<PDFRepairStatus>;
}
/**
* A registry for PDF repair rules.
*/
export class PDFRepairRegistry {
private rules: Map<string, IRepairRule> = new Map();
/**
* Checks if a rule with the given ID exists in the registry.
* @param id The ID of the rule to check for.
* @returns True if a rule with the given ID exists in the registry, false otherwise.
*/
public hasRule(id: string): boolean {
return this.rules.has(id);
}
/**
* Adds a repair rule to the registry.
* @param rule - The repair rule to add.
* @throws An error if a repair rule with the same ID already exists in the registry.
*/
public addRule(rule: IRepairRule): void {
if (this.hasRule(rule.id)) {
throw new Error(`A repair rule with id '${rule.id}' already exists.`);
}
this.rules.set(rule.id, rule);
}
/**
* Retrieves a repair rule from the registry by its ID.
* @param id The ID of the repair rule to retrieve.
* @returns The repair rule with the specified ID, or undefined if no such rule exists.
*/
public getRule(id: string): IRepairRule | undefined {
return this.rules.get(id);
}
/**
* Returns an array of repair rules. If `ids` is provided, only the rules with the specified IDs are returned.
* @param ids An optional array of rule IDs to filter the results by.
* @returns An array of repair rules.
*/
public getRules(ids?: string[]): IRepairRule[] {
if (ids) {
return ids.map(id => this.rules.get(id)).filter(rule => rule) as IRepairRule[];
}
return Array.from(this.rules.values());
}
/**
* Returns an iterator of all the keys in the registry.
* @returns An iterator of all the keys in the registry.
*/
public keys(): IterableIterator<string> {
return this.rules.keys();
}
/**
* Returns a new PDFRepairRegistry instance containing only the rules that satisfy
* the provided testing function.
* @param cb A function that accepts a repair rule and returns a boolean
* indicating whether the rule should be included in the filtered registry.
* @returns A new PDFRepairRegistry instance containing only the rules that
* satisfy the provided testing function.
*/
public filter(cb: (rule: IRepairRule) => boolean): PDFRepairRegistry {
const registry = new PDFRepairRegistry();
for (const rule of this.rules.values()) {
if (cb(rule)) {
registry.addRule(rule);
}
}
return registry;
}
}
export const globalRepairRegistry = new PDFRepairRegistry();