@adguard/agtree
Version:
Tool set for working with adblock filter lists
62 lines (59 loc) • 2.13 kB
JavaScript
/*
* AGTree v3.2.2 (build date: Tue, 08 Jul 2025 13:39:47 GMT)
* (c) 2025 Adguard Software Ltd.
* Released under the MIT license
* https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree#readme
*/
import { isNull } from '../../utils/type-guards.js';
import { modifiersCompatibilityTable } from '../modifiers.js';
import { ResourceType } from '../schemas/resource-type.js';
/**
* Map of resource types to their corresponding adblock modifier names.
*
* @note Record type is used to ensure that all resource types are present in the map.
*/
const RESOURCE_TYPE_MODIFIER_MAP = Object.freeze({
[]: 'document',
[]: 'subdocument',
[]: 'stylesheet',
[]: 'script',
[]: 'image',
[]: 'font',
[]: 'object',
[]: 'xmlhttprequest',
[]: 'ping',
[]: 'media',
[]: 'websocket',
[]: 'other',
});
/**
* Gets the adblock modifier name for the given resource type.
*
* @param resourceType Resource type to get the modifier name for.
* @param platform Platform to get the modifier for.
*
* @returns A string containing the adblock modifier name for the given resource type
* or `null` if the modifier could not be found.
*/
const getResourceTypeModifier = (resourceType, platform) => {
const modifierName = RESOURCE_TYPE_MODIFIER_MAP[resourceType];
if (!modifierName) {
return null;
}
const modifierData = modifiersCompatibilityTable.getFirst(modifierName, platform);
if (isNull(modifierData)) {
return null;
}
return modifierData.name;
};
/**
* Checks if the given resource type is valid.
*
* @param resourceType Resource type to check.
*
* @returns `true` if the resource type is valid, `false` otherwise.
*/
const isValidResourceType = (resourceType) => {
return Object.values(ResourceType).includes(resourceType);
};
export { getResourceTypeModifier, isValidResourceType };