63 lines
2.0 KiB
JavaScript
63 lines
2.0 KiB
JavaScript
import { CATEGORY_ALIASES, normalizeCategoryToken } from './category_aliases.generated.js';
|
|
|
|
export function normalizeCategoryValue(
|
|
category = '',
|
|
{
|
|
aliases = CATEGORY_ALIASES,
|
|
defaultValue = '',
|
|
emptyValues = ['unknown', 'none', 'null'],
|
|
} = {},
|
|
) {
|
|
const token = normalizeCategoryToken(category);
|
|
const categoryAliases = aliases && typeof aliases === 'object'
|
|
? aliases
|
|
: CATEGORY_ALIASES;
|
|
const emptyTokens = new Set(emptyValues);
|
|
if (!token || emptyTokens.has(token)) return defaultValue;
|
|
return categoryAliases[token] || token;
|
|
}
|
|
|
|
export function normalizeDownloadCategoryValue(
|
|
category = '',
|
|
aliases = CATEGORY_ALIASES,
|
|
) {
|
|
const token = normalizeCategoryToken(category);
|
|
const categoryAliases = aliases && typeof aliases === 'object'
|
|
? aliases
|
|
: CATEGORY_ALIASES;
|
|
return categoryAliases[token] || token || 'checkpoints';
|
|
}
|
|
|
|
export function collectNormalizedCategoryValues(
|
|
values = [],
|
|
normalizeCategory,
|
|
{ allowedCategories = null, excludedCategories = [] } = {},
|
|
) {
|
|
const categories = [];
|
|
const allowed = allowedCategories instanceof Set ? allowedCategories : null;
|
|
const excluded = new Set(excludedCategories);
|
|
const addCategory = (value) => {
|
|
if (value === undefined || value === null || String(value).trim() === '') return;
|
|
if (Array.isArray(value)) {
|
|
value.forEach(addCategory);
|
|
return;
|
|
}
|
|
String(value).split(/[,|;]/).forEach(part => {
|
|
if (String(part || '').trim() === '') return;
|
|
const normalized = normalizeCategory(part);
|
|
if (
|
|
!normalized
|
|
|| excluded.has(normalized)
|
|
|| (allowed && !allowed.has(normalized))
|
|
|| categories.includes(normalized)
|
|
) {
|
|
return;
|
|
}
|
|
categories.push(normalized);
|
|
});
|
|
};
|
|
|
|
values.forEach(addCategory);
|
|
return categories;
|
|
}
|