 
      
    JavaScript
- 670 ответов
- 0 вопросов
    673
    Вклад в тег
    
      
      
    
  
  
 
  
  const, то всегда let. Потому как let используется в уникальном scope. А вообще неплохо рассказано тут (в новой редакции).var:{
	var value = 1;
}
console.log(value); // 1let (const):{
	let value = 1;
}
console.log(value); // ReferenceError: value is not defined 
  
   
  
  const createKeyGenerator = (groupSize, groupCount) => {
    const dictionary = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
    const length = groupSize * groupCount;
    return () => {
        const values = new Uint32Array(length);
        crypto.getRandomValues(values);
        const chars = [...values].map(value => dictionary[value % dictionary.length]);
        const key = new Array(groupCount)
            .fill(null)
            .map((_, index) => {
                const offset = index * groupSize;
                return chars
                    .slice(offset, offset + groupSize)
                    .join('');
            })
            .join('-');
        return key;
    };
};
const createKey = createKeyGenerator(4, 3);
console.log(createKey()); // FO4V-P2ZV-JYH4
console.log(createKey()); // TTMR-EBVC-8TUWconst createKeyGenerator = (groupSize, groupCount) => {
    const dictionary = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
    const length = groupSize * groupCount;
    const group = new RegExp(`.{${groupSize}}`, 'g');
    return () => {
        const values = new Uint32Array(length);
        crypto.getRandomValues(values);
        const chars = [...values].map(value => dictionary[value % dictionary.length]);
        const key = '_'
            .repeat(length)
            .replace(/\w/g, (match, index) => chars[index])
            .match(group)
            .join('-');
        return key;
    };
};Set коллекции, по итогу размер коллекции превысил (16^6 значений) и получил исключение. При этом не было ни одного повтора.