Generate short name a to z number avoided

This commit is contained in:
unknown 2025-11-11 10:36:03 +05:30
parent 3692b7bf4c
commit aee1d48171

View File

@ -150,10 +150,9 @@ exports.uploadUnitMasterFromCSV = async (req, res) => {
const seenShortNames = new Set();
const fileDuplicates = [];
//generate smart short name (letters + numbers)
const generateShortName = (unitName, existingShorts = new Set()) => {
const cleaned = unitName.replace(/[^a-zA-Z0-9]/g, "").toUpperCase();
const cleaned = unitName.replace(/[^a-zA-Z]/g, "").toUpperCase();
// Predefined abbreviations
const abbreviationMap = {
METER: "MT",
METRE: "MT",
@ -174,14 +173,22 @@ exports.uploadUnitMasterFromCSV = async (req, res) => {
UNIT: "UNT",
};
//if Unit Name is ≤ 5 characters, just use it in uppercase
if (cleaned.length <= 5) return cleaned;
// Otherwise, use abbreviation map or generate automatically
let shortName = abbreviationMap[cleaned] || cleaned.substring(0, 3);
if (shortName.length < 2) shortName = shortName.padEnd(2, "X");
// Random 2-letter suffix for uniqueness
const randomLetters = () =>
Array.from({ length: 2 }, () =>
String.fromCharCode(65 + Math.floor(Math.random() * 26))
).join("");
let finalShort = `${shortName}${Math.floor(Math.random() * 100)}`;
let finalShort = `${shortName}${randomLetters()}`;
while (existingShorts.has(finalShort.toLowerCase())) {
finalShort = `${shortName}${Math.floor(Math.random() * 100)}`;
finalShort = `${shortName}${randomLetters()}`;
}
return finalShort.substring(0, 5);
};