From aee1d481714d4fbd7a4b4009a8ede4295ab2fadb Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 11 Nov 2025 10:36:03 +0530 Subject: [PATCH] Generate short name a to z number avoided --- app/controllers/unitMasterController.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/app/controllers/unitMasterController.js b/app/controllers/unitMasterController.js index 1adcbb4..f48eaf6 100644 --- a/app/controllers/unitMasterController.js +++ b/app/controllers/unitMasterController.js @@ -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); };