@mysten/sui
Version:
Sui TypeScript API(Work in Progress)
58 lines (57 loc) • 2.14 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var suins_exports = {};
__export(suins_exports, {
isValidSuiNSName: () => isValidSuiNSName,
normalizeSuiNSName: () => normalizeSuiNSName
});
module.exports = __toCommonJS(suins_exports);
const SUI_NS_NAME_REGEX = /^(?!.*(^(?!@)|[-.@])($|[-.@]))(?:[a-z0-9-]{0,63}(?:\.[a-z0-9-]{0,63})*)?@[a-z0-9-]{0,63}$/i;
const SUI_NS_DOMAIN_REGEX = /^(?!.*(^|[-.])($|[-.]))(?:[a-z0-9-]{0,63}\.)+sui$/i;
const MAX_SUI_NS_NAME_LENGTH = 235;
function isValidSuiNSName(name) {
if (name.length > MAX_SUI_NS_NAME_LENGTH) {
return false;
}
if (name.includes("@")) {
return SUI_NS_NAME_REGEX.test(name);
}
return SUI_NS_DOMAIN_REGEX.test(name);
}
function normalizeSuiNSName(name, format = "at") {
const lowerCase = name.toLowerCase();
let parts;
if (lowerCase.includes("@")) {
if (!SUI_NS_NAME_REGEX.test(lowerCase)) {
throw new Error(`Invalid SuiNS name ${name}`);
}
const [labels, domain] = lowerCase.split("@");
parts = [...labels ? labels.split(".") : [], domain];
} else {
if (!SUI_NS_DOMAIN_REGEX.test(lowerCase)) {
throw new Error(`Invalid SuiNS name ${name}`);
}
parts = lowerCase.split(".").slice(0, -1);
}
if (format === "dot") {
return `${parts.join(".")}.sui`;
}
return `${parts.slice(0, -1).join(".")}@${parts[parts.length - 1]}`;
}
//# sourceMappingURL=suins.js.map
;