Generar uuid en js
Crear o generar un id único usando js
En React Native he tenido un problema con el paquete react-native-uuid al ejecutar mi app en android.
npm install react-native-uuid
Tenía que encontrar alguna función alternativa, en stackoverflow la solución no me gustaba mucho por la longitud del alfanumérico obtenido.
Esta función también se basa en Math.random()
Mi propuesta se basa en dos funciones, una de ellas es un wrapper get_rnd del Math.random que devuelve un número y la otra get_uuid es la que devuelve el string alfanumérico con la peculiaridad que permite pasarle una longitud del id a obtener.
//función que devuelve un número aleatorio entre min y max ambos inclusive
export const get_rnd = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)
export const get_uuid = (length=14) => {
const ar = [
"abcdefghijklmnopqrstuvxyz", //0: letras
"0123456789" //1: números
]
const r = new Array(length).fill(0).map(()=>{
//indica de modo aleatorio si se usará letras o números
let i = get_rnd(0,1)
//si i=0 => letras, sino (i=1) numeros
let str = ar[i]
//si es 1 se pasará a mayusculas el string obtenido, evidentemente para los números
//no hay mayor efecto
str = get_rnd(0,1) ? str.toUpperCase() : str
//se obtendrá un valor entre 0 y la longitud del string anterior 10 o 25
const max = str.length - 1
i = get_rnd(0, max)
return str.split("")[i]
}).join("")
return r
}
Prueba
const r = []
for(let i=0; i<500; i++){r.push(get_uuid())}
console.log(r)
Resultado
[
"18kOe43095552y", "Mx1378f8xK435N", "8uzinq3gcRt8t3", "0Jj79L1L0a9Pi8", "kI918x0E2y3332", "l436t1fsQQGL0J",
"1g1e8Q6o9ahYd7", "9CpY1O6k9S35Ma", "U80SY015Fcc8fc", "064459GHz5C342", "62baA218C7g6MS", "266vCl7MO60L35",
"s01X76r13g921x", "A3SHI86111e9cE", "3Pz8M3mOjR3O47", "7a16F80U28u702", "GclT581d68x4J6", "3H33q8Q5iB914x",
"a437aATLntN742", "siU7QZ7A75E84x", "0yA44Hb0kJ48o5", "68xa8Qr7SVO439", "HD7bvFRz2P7xf2", "74yO0266R96ALu",
"t4O44775JcrDc3", "3v2r421830TO1Y", "n096Ij02Ztrkt2", "5DsQUnf1xD4171", "FY57Y6j070X1EH", "u5LP46163fi7q3",
"31UitaUy3dJ4Y2", "y7Bf29J9H4DY5c", "nS8l701Uj185pM", "If9kA0286SRy0n", "n84mv4n8e3P54J", "Nm4Uj18A87m3o7",
"BasZVgNbto244s", "6jJracU14Hv613", "Jpf8A4A94zZixm", "08HqM636k735P8", "5p2yYQ21zU375x", "138N0D9gOim56N",
"290X5Mt6it8d6O", "992v2c98603368", "98452DYh079h3s", "37CV967O1Odsst", "oFb2E4jV7F281I", "77M0h87520xJ6g",
"Uu22i3c483g3Ek", "j09SR343u36z14", "zvv94Vk338X8nv", "97389o392e8202", "0316tX3INzH0Be", "BaLl9krh8Gd79a",
"7EqjEETa6O7mmO", "316R4mH31bt5XV", "5UHnU4lUQ7xV33", "fd99K207QKQqr2", "F5n4C7qIo53e6N", "27Q316mvvx40Qs",
"6325352AU4B4xb", "51B3RiY86mIXe7", "e79775V757Ej8R", "qYd25Qpl1698fQ", "7l29ic5iSC7VEN", "L108555s7x8kO6",
"RJ5j277s72n9aV", "856g3EAe88T40E", "2GMYpHx605dl31", "6x804qBhxG23k2", "2tA09m369t3037", "lGK3b36y8XK8t4",
"5QMy3Ql51A089Y", "r6SFe899maAbt5", "7U4800DdGd7084", "765cP916x9Z2q1", "G3Ll5A49y31j8o", "8d14E47777E3NV",
"5F311E3551Z54c", "U2S855QNSa54LX", "61rQUD04CnvN7Y", "ena40lV1J05890", "01vVbE8Oj24pXg", "75Ki84m89Fa3fp",
"20y9o699m1TN91", "OM104gf03vIN52", "92e2om93ss61mu", "f7x7A5JB54j43R", "7462Mt3x0j15Qy", "82p5la7s557344",
"6xlg3Zqh705J0Y", "1R50iZ1k351q25", "975057RZO2jF6u", "K17UZP8B0576B2", "9351530l1hM2Qn", "z1170IL4Z3Td75",
"7QuKyh8nE2JS8R", "23O9Rb9089mY79", "Pr68P2sf0Obx71", "9DS69oOq01649x", …
]
Autor: Eduardo A. F.
Publicado: 27-12-2020 12:29
Actualizado: 27-12-2020 12:30