-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f61bfc9
commit a51323a
Showing
4 changed files
with
34 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (c) 2022, 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 | ||
*/ | ||
import { randomBytes } from 'crypto'; | ||
import * as util from '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. | ||
*/ | ||
export function uniqid(options?: { template?: string; length?: number }): string { | ||
const uniqueString = randomBytes(Math.ceil((options?.length ?? 32) / 2.0)) | ||
.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}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters