All files randkey.ts

100% Statements 203/203
100% Branches 18/18
100% Functions 12/12
100% Lines 203/203

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 2041x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 93x 93x 93x 93x 93x 93x 93x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 23x 23x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { getRandomItem } from '@jsweb/utils/modules/array'
import { isBetween } from '@jsweb/utils/modules/number'
 
/**
 * Generates a random number as string with optional radix and size limit.
 * The `radix` parameter can be a number from 2 to 36. Default is 10, which generates base 10 random numbers.
 * The `limit` size parameter can be a number from 1 to 40. Default is 40, which is the bigger possible.
 * Keep in mind that `radix` can automatic limit the result max size.
 *
 * @export {function}
 * @param {number} [radix=10] - Optional radix used to convert the number to string.
 * @param {number} [limit=40] - Optional size limit used to cut the string result.
 * @returns {string}
 * @example
 * import { rand } from '@jsweb/randkey'
 *
 * rand()      // '555847878175'
 * rand(2)     // '1001010000010000000001111111010100000110'
 * rand(8)     // '2036103777231'
 * rand(16)    // '12a82628ee7'
 * rand(32)    // '18vq5b2hq'
 * rand(36)    // 'fdqlsnvb'
 */
export function rand(radix: number = 10, limit: number = 40): string {
  const rnd = Date.now() * Math.random()
  const rdx = isBetween(radix, 2, 36) ? radix : 10
  const cut = isBetween(limit, 1, 40) ? limit : 40
  const result = Math.round(rnd).toString(rdx).substring(0, cut)
 
  return result
}
 
/**
 * Generates a random hexadecimal number as string with optional custom size limit.
 * Size limit must be from 1 to 11. Default is 11, which is the bigger possible.
 * It is just an alias for `rand(16, limit)`.
 *
 * @export {function}
 * @param {number} [limit=11] - Optional size limit used to cut the string result.
 * @returns {string}
 * @example
 * import { hex } from '@jsweb/randkey'
 *
 * hex(6)    // 'f9c1d0'
 */
export function hex(limit: number = 11): string {
  return rand(16, limit)
}
 
/**
 * Generates a random ID with 4 hexadecimal digits. It is just an alias for `hex(4)`.
 *
 * @export {function}
 * @returns {string}
 * @example
 * import { id4 } from '@jsweb/randkey'
 *
 * id4()    // 'f9c1'
 */
export function id4(): string {
  return hex(4)
}
 
/**
 * Generates a random ID with 8 hexadecimal digits. It is just an alias for `hex(8)`.
 *
 * @export {function}
 * @returns {string}
 * @example
 * import { id8 } from '@jsweb/randkey'
 *
 * id8()    // 'd74b8ed'
 */
export function id8(): string {
  return hex(8)
}
 
/**
 * Generates a random ID with 16 hexadecimal digits.
 *
 * @export {function}
 * @returns {string}
 * @example
 * import { id16 } from '@jsweb/randkey'
 *
 * id16()    // '6c1f3ac8e0ba611d'
 */
export function id16(): string {
  return `${id8()}${id8()}`
}
 
/**
 * Generates a random ID with 32 hexadecimal digits.
 *
 * @export {function}
 * @returns {string}
 * @example
 * import { id32 } from '@jsweb/randkey'
 *
 * id32()    // 'f17e3ac8e0ba925a61ed19016c1f2eb0'
 */
export function id32(): string {
  return `${id16()}${id16()}`
}
 
/**
 * Generates a random ID with 64 hexadecimal digits.
 *
 * @export {function}
 * @returns {string}
 * @example
 * import { id64 } from '@jsweb/randkey'
 *
 * id64()    // 'f17e3ac8e0ba925a61ed19016c1f2eb0f17e3ac8e0ba925a61ed19016c1f2eb0'
 */
export function id64(): string {
  return `${id32()}${id32()}`
}
 
/**
 * Generates a valid UUID v4 (RFC 4122 compliant).
 *
 * @export {function}
 * @returns {string}
 * @example
 * import { uuid } from '@jsweb/randkey'
 *
 * uuid()    // 'c30663ff-a2d3-4e5d-b377-9e561e8e599b'
 */
export function uuid(): string {
  const rfc = [8, 9, 'a', 'b']
 
  return [
    id8(),
    id4(),
    `4${hex(3)}`,
    `${getRandomItem(rfc)}${hex(3)}`,
    `${hex(6)}${hex(6)}`,
  ].join('-')
}
 
/**
 * Generates a random 5x5 ID with base2 progressive radix per block.
 *
 * @export {function}
 * @returns {string}
 * @example
 * import { puid } from '@jsweb/randkey'
 *
 * puid()    // 10100-13110-42720-98222-13prn
 */
export function puid(): string {
  return [2, 4, 6, 8, 16].map((i) => rand(i, 5)).join('-')
}
 
/**
 * Generates a random 5x5 ID using a common radix for all blocks using `rand(radix, 5)`.
 * The `radix` parameter can be a number from 2 to 36. Default is 10, which generates base 10 random blocks.
 *
 * @export {function}
 * @param {number} [radix=10] - Optional radix used to convert block numbers to string.
 * @returns {string}
 * @example
 * import { ruid } from '@jsweb/randkey'
 *
 * ruid(8)    // 15124-22432-17325-45517-15522
 */
export function ruid(radix: number = 10): string {
  return Array(5)
    .fill(radix)
    .map((i) => rand(i, 5))
    .join('-')
}
 
/**
 * Generates a random 5x5 ID with hexadecimal blocks.
 * It is just an alias for `ruid(16)`.
 *
 * @export {function}
 * @returns {string}
 * @example
 * import { huid } from '@jsweb/randkey'
 *
 * huid()    // d74b8-124e7-15854-15c73-82909
 */
export function huid(): string {
  return ruid(16)
}
 
/**
 * Generates a random 5x5 ID with alphanumerical blocks, like Windows Product Key.
 * It is just an alias for `ruid(36)`.
 *
 * @export {function}
 * @returns {string}
 * @example
 * import { wuid } from '@jsweb/randkey'
 *
 * wuid()    // 7wuiu-e4fw7-ari12-3z50r-iv04x
 */
export function wuid(): string {
  return ruid(36)
}