@adguard/agtree
Version:
Tool set for working with adblock filter lists
158 lines (157 loc) • 5.47 kB
TypeScript
/**
* @file Provides common compatibility table methods.
*/
import { type BaseCompatibilityDataSchema } from './schemas/index.js';
import { GenericPlatform, type SpecificPlatform } from './platforms.js';
import { type CompatibilityTable } from './types.js';
import { AdblockSyntax } from '../utils/adblockers.js';
/**
* Lists all supported entity records by a product.
*
* Keys are compatibility flags, values are compatibility data records.
*
* @template T Compatibility data schema.
*/
export type ProductRecords<T> = {
[key: string]: T;
};
/**
* Defines a compatibility table row by product.
*
* Keys are Adblock syntaxes, values are product records.
*
* @template T Compatibility data schema.
*/
export type RowByProduct<T> = {
[AdblockSyntax.Adg]: ProductRecords<T>;
[AdblockSyntax.Ubo]: ProductRecords<T>;
[AdblockSyntax.Abp]: ProductRecords<T>;
};
/**
* Defines multiple compatibility table rows by product.
*
* @template T Compatibility data schema.
*/
export type RowsByProduct<T> = RowByProduct<T>[];
/**
* Single platform records type.
*
* Keys are platform enums values, values are compatibility data records.
*
* @template T Compatibility data schema.
*/
type SinglePlatformRecords<T> = {
[key: string]: T;
};
/**
* Name transformer function type. This function is used to normalize compatibility data names before processing them,
* e.g. converting to lowercase, remove unnecessary prefixes, etc.
*
* @param name Compatibility data name.
*
* @returns Normalized name.
*/
type NameTransformer = (name: string) => string;
/**
* Base compatibility table class which provides common methods to work with compatibility data.
*
* @template T Compatibility data schema.
*/
export declare abstract class CompatibilityTableBase<T extends BaseCompatibilityDataSchema> {
/**
* Compatibility table data.
*/
private data;
/**
* Optional name transformer function. If provided,
* it will be called in all methods before processing compatibility data names.
*/
private readonly nameTransformer;
/**
* Creates a new instance of the common compatibility table.
*
* @param data Compatibility table data.
* @param nameTransformer Optional name transformer function.
*/
constructor(data: CompatibilityTable<T>, nameTransformer?: NameTransformer | null);
/**
* Helper method to get a 'row' from the compatibility table data by name.
*
* @param name Compatibility data name.
* @returns Compatibility table row storage or `null` if not found.
*/
private getRowStorage;
/**
* Checks whether a compatibility data `name` exists for any platform.
*
* @note Technically, do the same as `exists()` method with generic platform _any_
* but it is faster because it does not apply complex logic.
*
* @param name Compatibility data name.
*
* @returns True if the compatibility data exists, false otherwise.
*/
existsAny(name: string): boolean;
/**
* Checks whether a compatibility data `name` exists for a specified platform.
*
* @param name Compatibility data name.
* @param platform Specific or generic platform.
*
* @returns True if the compatibility data exists, false otherwise.
*/
exists(name: string, platform: SpecificPlatform | GenericPlatform): boolean;
/**
* Returns a compatibility data by name and specific platform.
*
* @param name The name of the compatibility data.
* @param platform The specific platform.
*
* @returns A single compatibility data or `null` if not found.
*/
getSingle(name: string, platform: SpecificPlatform): T | null;
/**
* Returns all compatibility data records for name and specified platform.
*
* @param name Compatibility data name.
* @param platform Specific or generic platform.
*
* @returns Multiple records grouped by platforms.
* Technically, it is an object where keys are platform enums values and values are compatibility data records.
*
* @note Platform enum values can be converted to string names using {@link getSpecificPlatformName} on demand.
*/
getMultiple(name: string, platform: SpecificPlatform | GenericPlatform): SinglePlatformRecords<T> | null;
/**
* Returns all compatibility data records for the specified platform.
*
* @param platform Specific or generic platform.
*
* @returns Array of multiple records grouped by platforms.
*/
getAllMultiple(platform: SpecificPlatform | GenericPlatform): SinglePlatformRecords<T>[];
/**
* Returns the first compatibility data record for name and specified platform.
*
* @param name Compatibility data name.
* @param platform Specific or generic platform.
*
* @returns First found compatibility data record or `null` if not found.
*/
getFirst(name: string, platform: SpecificPlatform | GenericPlatform): T | null;
/**
* Returns all compatibility data records for the specified name.
*
* @param name Compatibility data name.
*
* @returns Array of multiple records grouped by platforms.
*/
getRow(name: string): T[];
/**
* Returns all compatibility data grouped by products.
*
* @returns Array of multiple records grouped by products.
*/
getRowsByProduct(): RowsByProduct<T>;
}
export {};