nope/lib/helpers/idMethods.ts
2021-12-04 08:25:26 +01:00

52 lines
970 B
TypeScript

/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-11-06 08:53:48
* @modify date 2021-11-11 11:11:09
* @desc [description]
*/
import { v4 } from "uuid";
import { replaceAll } from "./stringMethods";
/**
* Generates an ID.
*
* @author M.Karkowski
* @export
* @param {string} [prestring] additional PreString for the Id.
* @return {*} {string} the Id.
*/
/**
*
*
* @author M.Karkowski
* @export
* @param {{
* prestring?: string,
* useAsVar?: boolean
* }} [options={}]
* @return {*} {string}
*/
export function generateId(
options: {
// PreString for the Var.
prestring?: string;
useAsVar?: boolean;
} = {}
): string {
let id = v4();
if (options.useAsVar) {
id = replaceAll(id, "-", "");
options.prestring =
typeof options.prestring === "string" ? options.prestring : "_";
}
if (typeof options.prestring === "string") {
id = options.prestring + id;
}
return id;
}