smartunique/ts/index.ts

36 lines
1.0 KiB
TypeScript
Raw Normal View History

2023-08-15 09:32:46 +00:00
import * as plugins from './smartunique.plugins.js';
2017-07-14 16:53:10 +00:00
/**
* returns short strings that are unique to very high degree od certainty
*/
2023-08-15 09:32:46 +00:00
export const shortId = (sizeArg?: number): string => {
return plugins.nanoid.nanoid(sizeArg);
2018-10-06 13:38:55 +00:00
};
2017-07-14 16:53:10 +00:00
/**
* returns strings that are unique to a very high degree of certainty
*/
2020-03-05 20:39:44 +00:00
export const uuid4 = (): string => {
return plugins.uuid.v4();
2018-10-06 13:38:55 +00:00
};
2017-07-14 16:53:10 +00:00
2020-03-05 20:39:44 +00:00
export const uuid5 = (customStringArg: string, namespaceArg = plugins.uuid.v5.DNS): string => {
return plugins.uuid.v5(customStringArg, namespaceArg);
};
2024-02-29 12:49:59 +00:00
export const uni = (prefix: string = 'uni', lengthArg = 24) => {
return `${prefix}_${'x'.repeat(lengthArg).replace(/[xy]/g, (c) => {
2023-08-15 09:32:46 +00:00
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
})}`;
};
2024-02-29 12:31:33 +00:00
2024-02-29 12:49:59 +00:00
export const uniSimple = (prefix: string = 'uni', lengthArg = 8) => {
return `${prefix}${'x'.repeat(lengthArg).replace(/[xy]/g, (c) => {
2024-02-29 12:31:33 +00:00
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
})}`;
};