UNPKG

cloud-region-shortener

Version:

Shorten region names of cloud providers for use in resource names

143 lines (142 loc) 4.92 kB
"use strict"; // _ _ _ _ _ // ___| | ___ _ _ __| | _ __ ___ __ _(_) ___ _ __ ___| |__ ___ _ __| |_ ___ _ __ ___ _ __ // / __| |/ _ \| | | |/ _` |_____| '__/ _ \/ _` | |/ _ \| '_ \ _____/ __| '_ \ / _ \| '__| __/ _ \ '_ \ / _ \ '__| // | (__| | (_) | |_| | (_| |_____| | | __/ (_| | | (_) | | | |_____\__ \ | | | (_) | | | || __/ | | | __/ | // \___|_|\___/ \__,_|\__,_| |_| \___|\__, |_|\___/|_| |_| |___/_| |_|\___/|_| \__\___|_| |_|\___|_| // |___/ Object.defineProperty(exports, "__esModule", { value: true }); exports.getShortRegion = exports.getMaxLength = exports.setMaxLength = exports.setDefaultStyle = exports.getDefaultStyle = exports.Styles = void 0; // ############################################################################ // Structures // ############################################################################ var Styles; (function (Styles) { Styles[Styles["standard"] = 0] = "standard"; // iso3166 = 1, })(Styles = exports.Styles || (exports.Styles = {})); // ############################################################################ // Consts & Variables // ############################################################################ var defaultStyle = Styles.standard; var maxLength = 0; var regionAbbreviations = { eastus: ['eus'], eastus2: ['eus2'], southcentralus: ['scus'], westus2: ['wus2'], westus3: ['wus3'], australiaeast: ['aue'], southeastasia: ['seas'], northeurope: ['neu'], swedencentral: ['swc'], uksouth: ['uks'], westeurope: ['weu'], centralus: ['cus'], northcentralus: ['ncus'], westus: ['wus'], southafricanorth: ['san'], centralindia: ['cin'], eastasia: ['eas'], japaneast: ['jae'], jioindiawest: ['jinw'], koreacentral: ['koc'], canadacentral: ['cac'], francecentral: ['frc'], germanywestcentral: ['gewc'], norwayeast: ['noe'], switzerlandnorth: ['swn'], uaenorth: ['uan'], brazilsouth: ['brs'], centralusstage: ['cuss'], eastusstage: ['euss'], eastus2stage: ['eus2s'], northcentralusstage: ['ncuss'], southcentralusstage: ['scuss'], westusstage: ['wuss'], westus2stage: ['wus2s'], asia: ['as'], asiapacific: ['asp'], australia: ['au'], brazil: ['br'], canada: ['ca'], europe: ['eu'], france: ['fr'], germany: ['ge'], global: ['gl'], india: ['in'], japan: ['ja'], korea: ['ko'], norway: ['no'], southafrica: ['saf'], switzerland: ['sw'], uae: ['ua'], uk: ['uk'], unitedstates: ['us'], eastasiastage: ['eass'], southeastasiastage: ['seass'], centraluseuap: ['cuse'], eastus2euap: ['eus2e'], westcentralus: ['wcus'], southafricawest: ['safw'], australiacentral: ['auc'], australiacentral2: ['auc2'], australiasoutheast: ['ause'], japanwest: ['jaw'], jioindiacentral: ['jinc'], koreasouth: ['kos'], southindia: ['sin'], westindia: ['win'], canadaeast: ['cae'], francesouth: ['frs'], germanynorth: ['gen'], norwaywest: ['now'], switzerlandwest: ['swe'], ukwest: ['ukw'], uaecentral: ['uac'], brazilsoutheast: ['brse'], }; // ############################################################################ // Get / Set // ############################################################################ function getDefaultStyle() { return defaultStyle; } exports.getDefaultStyle = getDefaultStyle; function setDefaultStyle(style) { if (style === void 0) { style = Styles.standard; } defaultStyle = style; return defaultStyle; } exports.setDefaultStyle = setDefaultStyle; function setMaxLength(length) { if (length === void 0) { length = 0; } if (length >= 0) { maxLength = length; return maxLength; } throw new Error("Can't set maximum length for abbreviation to a negative value!"); } exports.setMaxLength = setMaxLength; function getMaxLength() { return maxLength; } exports.getMaxLength = getMaxLength; // ############################################################################ // Functions // ############################################################################ function truncate(input, len) { if (len > 0) { return input.substr(0, len); } return input; } function getShortRegion(region, style, pMaxLength) { if (style === void 0) { style = defaultStyle; } if (pMaxLength === void 0) { pMaxLength = maxLength; } if (Object.keys(regionAbbreviations).includes(region)) { return truncate(regionAbbreviations[region][style.valueOf()], pMaxLength); } throw new Error("Region '" + region + "' not recognized as a known region. Shortening not possible!'"); } exports.getShortRegion = getShortRegion;