(size: number)
| 20 | // (256) evenly. Such bias is acceptable for most cases when the output |
| 21 | // length is long enough and doesn't need to be uniform. |
| 22 | export function randomString(size: number): string { |
| 23 | if (size === 0) { |
| 24 | throw new Error('Zero-length randomString is useless.'); |
| 25 | } |
| 26 | const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789'; |
| 27 | let objectId = ''; |
| 28 | const bytes = randomBytes(size); |
| 29 | for (let i = 0; i < bytes.length; ++i) { |
| 30 | objectId += chars[bytes.readUInt8(i) % chars.length]; |
| 31 | } |
| 32 | return objectId; |
| 33 | } |
| 34 | |
| 35 | // Returns a new random alphanumeric string suitable for object ID. |
| 36 | export function newObjectId(size: number = 10): string { |
no outgoing calls
no test coverage detected