Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.unique() requires the options #253

Closed
franciscop opened this issue May 1, 2016 · 1 comment
Closed

.unique() requires the options #253

franciscop opened this issue May 1, 2016 · 1 comment

Comments

@franciscop
Copy link

When I try the example in Node.js:

chance.unique(chance.state, 5);

It gives me the following error:

Uncaught TypeError: Cannot read property 'comparator' of undefined
      at Chance.unique (node_modules/chance/chance.js:306:33)
      at generateUsers (test/fixtures.js:43:31)
      [many more, but irrelevant]

Going to the source:

Chance.prototype.unique = function(fn, num, options) {
    testRange(
        typeof fn !== "function",
        "Chance: The first argument must be a function."
    );

    var comparator = options.comparator || function(arr, val) { return arr.indexOf(val) !== -1; };

    // ...

It tries to access comparator of the object options, but options might not be defined. So, a proposed fix:

Chance.prototype.unique = function(fn, num, options) {
    testRange(
        typeof fn !== "function",
        "Chance: The first argument must be a function."
    );

    // NEW
    options = options || {};

    var comparator = options.comparator || function(arr, val) { return arr.indexOf(val) !== -1; };

    // ...

And my temporary fix is just doing this:

chance.unique(chance.state, 5, {});

Sorry I cannot make the PR right now, I'm super tight on a deadline to read and follow any process in place. Might try to do so in a couple of days (or if anyone else wants, sure, go ahead 😄 ).

@franciscop franciscop changed the title .unique() forces the options .unique() requires the options May 1, 2016
@victorquinn
Copy link
Member

Good catch @franciscop !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants