oberknecht-utils
Advanced tools
Comparing version 1.5.2 to 1.5.3
@@ -1,1 +0,1 @@ | ||
export declare function createID(length?: number): string; | ||
export declare function createID(length_?: number, preventDuplicates_?: boolean): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createID = void 0; | ||
function createID(length) { | ||
let length_ = length ?? 5; | ||
const _1 = require("."); | ||
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"; | ||
const usedIDs = []; | ||
function createID(length_, preventDuplicates_) { | ||
let preventDuplicates = !(0, _1.isNullUndefined)(preventDuplicates_) | ||
? preventDuplicates_ | ||
: true; | ||
let length = length_ ?? 5; | ||
let r = ""; | ||
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"; | ||
for (let i = 0; i < length_; i++) { | ||
r += chars.charAt(Math.floor(Math.random() * chars.length)); | ||
function actualCreateID() { | ||
if (usedIDs.length >= (chars.length ^ length)) | ||
length++; | ||
let r2 = ""; | ||
for (let i = 0; i < length; i++) { | ||
r2 += chars.charAt(Math.floor(Math.random() * chars.length)); | ||
} | ||
if (preventDuplicates && usedIDs.includes(r2)) | ||
return actualCreateID(); | ||
usedIDs.push(r2); | ||
r = r2; | ||
} | ||
actualCreateID(); | ||
return r; | ||
} | ||
exports.createID = createID; |
@@ -1,11 +0,29 @@ | ||
export function createID(length?: number) { | ||
let length_ = length ?? 5; | ||
import { isNullUndefined } from "."; | ||
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"; | ||
const usedIDs = []; | ||
export function createID(length_?: number, preventDuplicates_?: boolean) { | ||
let preventDuplicates = !isNullUndefined(preventDuplicates_) | ||
? preventDuplicates_ | ||
: true; | ||
let length = length_ ?? 5; | ||
let r = ""; | ||
const chars = | ||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"; | ||
for (let i = 0; i < length_; i++) { | ||
r += chars.charAt(Math.floor(Math.random() * chars.length)); | ||
function actualCreateID() { | ||
if (usedIDs.length >= (chars.length ^ length)) length++; | ||
let r2 = ""; | ||
for (let i = 0; i < length; i++) { | ||
r2 += chars.charAt(Math.floor(Math.random() * chars.length)); | ||
} | ||
if (preventDuplicates && usedIDs.includes(r2)) return actualCreateID(); | ||
usedIDs.push(r2); | ||
r = r2; | ||
} | ||
actualCreateID(); | ||
return r; | ||
} |
{ | ||
"name": "oberknecht-utils", | ||
"version": "1.5.2", | ||
"version": "1.5.3", | ||
"description": "Utils for oberknecht packages", | ||
@@ -5,0 +5,0 @@ "main": "./lib-ts/utils/index", |
126026
3418