feat: parse aggregate commercial use values, see #708

Add support for parsing comma-separated and JSON-style commercial use permission values in both Python backend and JavaScript frontend. Implement helper functions to split aggregated values into individual permissions while preserving original values when no aggregation is detected.

Added comprehensive test coverage for the new parsing functionality to ensure correct handling of various input formats including strings, arrays, and iterable objects with aggregated commercial use values.
This commit is contained in:
Will Miao
2025-11-30 17:10:21 +08:00
parent f09224152a
commit 22ee37b817
4 changed files with 223 additions and 4 deletions

View File

@@ -77,28 +77,72 @@ function indentMarkup(markup, spaces) {
.join('\n');
}
function splitAggregateCommercialValue(value) {
const trimmed = String(value ?? '').trim();
const looksAggregate = trimmed.includes(',') || (trimmed.startsWith('{') && trimmed.endsWith('}'));
if (!looksAggregate) {
return [value];
}
let inner = trimmed;
if (inner.startsWith('{') && inner.endsWith('}')) {
inner = inner.slice(1, -1);
}
const parts = inner
.split(',')
.map(part => part.trim())
.filter(Boolean);
return parts.length ? parts : [value];
}
function normalizeCommercialValues(value) {
if (!value && value !== '') {
return ['Sell'];
}
if (Array.isArray(value)) {
return value.filter(item => item !== null && item !== undefined);
const flattened = [];
value.forEach(item => {
if (item === null || item === undefined) {
return;
}
if (typeof item === 'string') {
flattened.push(...splitAggregateCommercialValue(item));
return;
}
flattened.push(String(item));
});
if (flattened.length > 0) {
return flattened;
}
if (value.length === 0) {
return [];
}
}
if (typeof value === 'string') {
return [value];
return splitAggregateCommercialValue(value);
}
if (value && typeof value[Symbol.iterator] === 'function') {
const result = [];
for (const item of value) {
if (item === null || item === undefined) {
continue;
}
if (typeof item === 'string') {
result.push(...splitAggregateCommercialValue(item));
continue;
}
result.push(String(item));
}
if (result.length > 0) {
return result;
}
}
return ['Sell'];
}