Skip to content

Commit e64d083

Browse files
committed
Enhance MetadataRuleSet class with additional properties management and improved documentation
1 parent 0d5626f commit e64d083

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

packages/tsurlfilter/src/rules/declarative-converter/metadata-ruleset.ts

+50-7
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,22 @@ type ByteRangeMapCollection = z.infer<typeof byteRangeMapCollectionValidator>;
4646
* Metadata validator.
4747
*/
4848
const metadataValidator = z.object({
49+
/**
50+
* Byte range maps collection for all rulesets.
51+
*/
4952
byteRangeMapsCollection: byteRangeMapCollectionValidator,
53+
54+
/**
55+
* Checksums for all rulesets.
56+
*/
5057
checksums: checksumMapValidator,
58+
59+
/**
60+
* Additional properties.
61+
* This field stores any extra information not covered by the other fields.
62+
* The content of this field is not validated, but it must be JSON serializable.
63+
* Validation should be performed by users.
64+
*/
5165
additionalProperties: z.record(z.unknown()),
5266
});
5367

@@ -66,20 +80,29 @@ const metadataRuleValidator = z.object({
6680
type Metadata = z.infer<typeof metadataValidator>;
6781

6882
/**
69-
* Metadata rule set.
70-
* Its a special rule set that contains only one rule - metadata rule, that holds metadata for all other rulesets.
83+
* Represents a specialized metadata ruleset for managing and validating metadata associated
84+
* with various rulesets.
85+
*
86+
* This class handles byte range maps, checksums, and additional properties,
87+
* providing methods to manipulate and query this metadata.
7188
*/
7289
export class MetadataRuleSet {
7390
private metadataRule: MetadataRule<Metadata>;
7491

7592
/**
76-
* Constructor.
93+
* Creates an instance of the MetadataRuleSet class.
7794
*
78-
* @param byteRangeMapsCollection Byte range maps collection. Default is an empty object.
79-
* @param checksums Checksums. Default is an empty object.
80-
* @param additionalProperties Additional properties. Default is an empty object.
95+
* @param byteRangeMapsCollection A map of byte range maps, where each key corresponds to a ruleset ID
96+
* and each value is a map of byte ranges for that ruleset. Defaults to an empty object.
97+
* @param checksums A map of checksums, where each key corresponds to a rule set ID and each value is the checksum
98+
* for that ruleset. Defaults to an empty object.
99+
* @param additionalProperties A collection of additional properties, where keys are property names and values are
100+
* their associated data. These properties are JSON serializable but not validated by the class.
101+
* Defaults to an empty object.
81102
*
82-
* @note It takes values by reference. If ever needed, clone them before passing.
103+
* @note
104+
* This constructor uses references for the provided arguments. If immutability is required, ensure to clone the
105+
* inputs before passing them.
83106
*/
84107
constructor(
85108
byteRangeMapsCollection: ByteRangeMapCollection = {},
@@ -203,6 +226,26 @@ export class MetadataRuleSet {
203226
this.metadataRule.metadata.additionalProperties[key] = value;
204227
}
205228

229+
/**
230+
* Checks whether additional property exists.
231+
*
232+
* @param key Property key.
233+
*
234+
* @returns Whether the property exists.
235+
*/
236+
public hasAdditionalProperty(key: string): boolean {
237+
return key in this.metadataRule.metadata.additionalProperties;
238+
}
239+
240+
/**
241+
* Removes additional property.
242+
*
243+
* @param key Property key.
244+
*/
245+
public removeAdditionalProperty(key: string): void {
246+
delete this.metadataRule.metadata.additionalProperties[key];
247+
}
248+
206249
/**
207250
* Serializes the ruleset to a string.
208251
*

0 commit comments

Comments
 (0)