0

I want to generate 100 unique 10-char strings using chance.js. I use the code below, but it returns an error. Anyone knows how to make it work?

let res = chance.unique(chance.string({ length: 10, casing: 'upper', alpha: true, numeric: true }), 100);
<script src="https://cdnjs.cloudflare.com/ajax/libs/chance/1.1.6/chance.min.js"></script>

The following works, but it doesn't returns fixed-length strings.

let res = chance.unique(chance.string, 100);
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/chance/1.1.6/chance.min.js"></script>
trincot
  • 317,000
  • 35
  • 244
  • 286
Test
  • 41
  • 4

1 Answers1

2

The problem with your attempt is that you are not passing a function to chance.unique, but instead execute chance.string.

In order to pass a function, which will be called with the options you want, you can pass an anonymous function, or let bind do that for you:

let res = chance.unique(chance.string.bind(chance, { length: 10, casing: 'upper', alpha: true, numeric: true }), 100);

console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/chance/1.1.6/chance.min.js"></script>
trincot
  • 317,000
  • 35
  • 244
  • 286