@salesforce/core
Version:
Core libraries to interact with SFDX projects, orgs, and APIs.
56 lines • 2.39 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.uniqid = uniqid;
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
const node_crypto_1 = require("node:crypto");
const util = __importStar(require("node:util"));
/**
* A function to generate a unique id and return it in the context of a template, if supplied.
*
* A template is a string that can contain `${%s}` to be replaced with a unique id.
* If the template contains the "%s" placeholder, it will be replaced with the unique id otherwise the id will be appended to the template.
*
* @param options an object with the following properties:
* - template: a template string.
* - length: the length of the unique id as presented in hexadecimal.
*/
function uniqid(options) {
const uniqueString = (0, node_crypto_1.randomBytes)(Math.ceil((options?.length ?? 32) / 2))
.toString('hex')
.slice(0, options?.length ?? 32);
if (!options?.template) {
return uniqueString;
}
return options.template.includes('%s')
? util.format(options.template, uniqueString)
: `${options.template}${uniqueString}`;
}
//# sourceMappingURL=uniqid.js.map
;