Files
graphs/src/displayFormatters.ts
2026-07-09 13:58:48 -03:00

42 lines
1.4 KiB
TypeScript

const SMALL_WORDS = new Set(['da', 'de', 'do', 'das', 'dos', 'e']);
export const removeTrailingSellerId = (value: string) => {
return value.replace(/\s*#\d+\s*$/, '').trim();
};
export const formatDisplayName = (value: string) => {
const normalized = String(value || '').replace(/\s+/g, ' ').trim();
if (!normalized) return '';
const withoutSellerId = removeTrailingSellerId(normalized);
const withoutNumericPrefix = withoutSellerId.replace(/^\[\d+\]\s*/, '').trim();
const isUppercaseName = /[A-ZÁÀÂÃÉÈÊÍÏÓÔÕÖÚÇÑ]/.test(withoutNumericPrefix) &&
withoutNumericPrefix === withoutNumericPrefix.toUpperCase();
if (!isUppercaseName) return withoutNumericPrefix;
return withoutNumericPrefix
.toLocaleLowerCase('pt-BR')
.split(' ')
.map((word, index) => {
if (index > 0 && SMALL_WORDS.has(word)) return word;
return word.charAt(0).toLocaleUpperCase('pt-BR') + word.slice(1);
})
.join(' ');
};
export const formatColorLabel = (value: string) => {
const normalized = String(value || '').replace(/\s+/g, ' ').trim();
if (!normalized) return '';
if (normalized.toLocaleLowerCase('pt-BR') === 'sem cor') return 'Sem cor';
return normalized
.toLocaleLowerCase('pt-BR')
.split(' ')
.map((word, index) => {
if (index > 0 && SMALL_WORDS.has(word)) return word;
return word.charAt(0).toLocaleUpperCase('pt-BR') + word.slice(1);
})
.join(' ');
};