Clean seller metadata display names
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 40s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 40s
This commit is contained in:
26
src/displayFormatters.ts
Normal file
26
src/displayFormatters.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
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(' ');
|
||||
};
|
||||
Reference in New Issue
Block a user