Skip to content

permutationOf is not reliable for arrays containing non-primitive values #38

@radekn

Description

@radekn

Example case:

a = function () {}
b = function () {}

demand([a, b]).be.permutationOf([a, b]) // this passes
demand([b, a]).be.permutationOf([a, b]) // this fails

The second assertion fails with AssertionError: [null,null] must be a permutation of [null,null].
This problem is caused by array sort function converting values to strings by default and using them for deciding what goes first.

[rant]
Unfortunately, in JavaScript, for most types of values (like e.g. functions, objects, arrays, and recently symbols, maybe also regexps) there is no defined order. IMO, this is one of the reasons JS sucks, because it unnecessarily forces us into linear time complexity (or some dirty tricks), where otherwise logarithmic would be possible, or quadratic instead of linear. At least there is Map that helps alleviate the problem a bit.
[/rant]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions