@vectorx/cloud-toolkit
Version:
VectorX Cloud Toolkit
67 lines (66 loc) • 2.57 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toHump = exports.getFixedFormatTime = void 0;
exports.getNonce = getNonce;
exports.getMd5 = getMd5;
exports.guid = guid;
exports.retry = retry;
const crypto_1 = __importDefault(require("crypto"));
const dayjs_1 = __importDefault(require("dayjs"));
const uuid_1 = require("uuid");
const getFixedFormatTime = (format, timeDistance = 0) => {
const current = (0, dayjs_1.default)();
const time = current.add(timeDistance, "millisecond").format(format);
return time;
};
exports.getFixedFormatTime = getFixedFormatTime;
function getNonce() {
return "xxxxxxxxxxxxxxxx".replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
function getMd5(str) {
const hash = crypto_1.default.createHash("md5");
hash.update(str);
const md5 = hash.digest("hex");
return md5;
}
function guid() {
return (0, uuid_1.v4)();
}
function retry(fn_1) {
return __awaiter(this, arguments, void 0, function* (fn, maxRetries = 5, delay = 0) {
let lastError;
for (let i = 0; i < maxRetries; i++) {
try {
if (delay > 0 && i > 0) {
yield new Promise((resolve) => setTimeout(resolve, delay));
}
return yield fn();
}
catch (error) {
lastError = error;
if (i === maxRetries - 1) {
throw lastError;
}
}
}
throw lastError;
});
}
const toHump = (str) => str.replace(/\-(\w)/g, (letter) => letter[1].toUpperCase());
exports.toHump = toHump;