Files
luggage-list/src/utils/labels.js
Space-Banane 0a8444700e
Some checks failed
Luggage List Build / build-web (push) Successful in 31s
Luggage List Build / build-android (push) Failing after 1m24s
Luggage List Build / release (push) Has been skipped
Full UI 180 & Overall improvements
2026-04-19 00:12:16 +02:00

84 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export function toTitleWords(value) {
if (!value) return '';
return value
.toString()
.split('-')
.map((part) => (part ? part[0].toUpperCase() + part.slice(1) : part))
.join(' ');
}
export function formatStatusLabel(status, lentTo = '') {
if (!status) return '';
if (status === 'lent-to') {
const name = lentTo?.trim();
return name ? `Lent To ${name}` : 'Lent To';
}
return toTitleWords(status);
}
export function formatFilterLabel(value) {
if (!value) return '';
if (value === 'all') return 'All';
return toTitleWords(value);
}
export function normalizeQuantity(value, fallback = 1) {
const parsed = Number.parseInt(`${value ?? ''}`, 10);
if (Number.isFinite(parsed) && parsed > 0) return parsed;
return fallback;
}
export function extractLegacyQuantityFromName(rawName = '') {
const name = `${rawName || ''}`.trim();
if (!name) {
return { matched: false, name: '', quantity: 1 };
}
let matched = name.match(/^(\d+)\s*[xX×]\s*(.+)$/);
if (matched) {
const quantity = normalizeQuantity(matched[1], 1);
const cleanName = (matched[2] || '').trim() || name;
return { matched: true, name: cleanName, quantity };
}
matched = name.match(/^(.+?)\s+(\d+)\s*[xX×]$/);
if (matched) {
const quantity = normalizeQuantity(matched[2], 1);
const cleanName = (matched[1] || '').trim() || name;
return { matched: true, name: cleanName, quantity };
}
return { matched: false, name, quantity: 1 };
}
export function normalizeNameAndQuantity(rawName = '', rawQuantity = null) {
const explicitQuantity = normalizeQuantity(rawQuantity, 0);
const legacy = extractLegacyQuantityFromName(rawName);
if (legacy.matched && explicitQuantity <= 1) {
return {
name: legacy.name,
quantity: normalizeQuantity(legacy.quantity, 1),
};
}
const quantity = explicitQuantity > 0 ? explicitQuantity : legacy.quantity;
const cleanName = `${rawName || ''}`.trim() || legacy.name;
return {
name: cleanName,
quantity: normalizeQuantity(quantity, 1),
};
}
export function formatItemLabel(name = '', quantity = 1) {
const cleanName = `${name || ''}`.trim();
const normalizedQuantity = normalizeQuantity(quantity, 1);
if (!cleanName) {
return normalizedQuantity > 1 ? `${normalizedQuantity}x item` : 'item';
}
return normalizedQuantity > 1 ? `${normalizedQuantity}x ${cleanName}` : cleanName;
}