nope/lib/helpers/idMethods.ts

50 lines
898 B
TypeScript
Raw Normal View History

2020-11-06 08:10:30 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @desc [description]
*/
2021-11-12 07:57:03 +00:00
import { v4 } from "uuid";
import { replaceAll } from "./stringMethods";
2020-08-25 22:11:26 +00:00
2021-11-12 07:57:03 +00:00
/**
* 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}
*/
2021-12-04 07:25:26 +00:00
export function generateId(
options: {
// PreString for the Var.
prestring?: string;
useAsVar?: boolean;
} = {}
): string {
2021-11-12 07:57:03 +00:00
let id = v4();
if (options.useAsVar) {
id = replaceAll(id, "-", "");
2021-12-04 07:25:26 +00:00
options.prestring =
typeof options.prestring === "string" ? options.prestring : "_";
2021-11-12 07:57:03 +00:00
}
if (typeof options.prestring === "string") {
id = options.prestring + id;
2020-09-08 14:59:06 +00:00
}
2021-11-12 07:57:03 +00:00
return id;
2021-12-04 07:25:26 +00:00
}