Strictness of equality in DOJO array utils indexOf

by steveukx

HTML

<p>Using DOJO's array indexOf uses a non-strict equality check, meaning that checking for entities that are falsy can lead to false positives:</p>

<div class="testBench bad"></div>

<p>Anything that doesn't coerce to equal will return the correct index as expected</p>

<div class="testBench good"></div>

<p>This is the expected behaviour according to <a href="http://dojotoolkit.org/api/1.9/dojo/_base/array" target="_blank">dojotoolkit.org</a> which links to the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FArray%2FindexOf" target="_blank">developer.mozilla.org</a> documentation that says it should be strict equality.</p>

CSS

html { font: 10pt/1.5 Verdana, Helvetica; }
.search:before,
.search:after { content: '"'; color: #999; }

.testBench {
    border: 1px solid #999;
    background: #eee;
    padding: 0.5rem;
    margin: 0.5rem;
    font: 8pt/1.5 monospace;
}

JavaScript

require(['dojo/_base/array'], function(arrayUtils) {

    function doIndexOf(container, src, search) {
        document.querySelector(container).appendChild(
            document.createElement('div')
        )
        .innerHTML = 'Index of <span class="search">' +
            search + '</span> in <span class="source">' +
            src + '</span> is <output>' +
            arrayUtils.indexOf(src, search);
    }

    doIndexOf('.testBench.bad', [2,3,1], true);
    doIndexOf('.testBench.bad', [3,4,5], false);
    doIndexOf('.testBench.bad', [0,1,2], false);
    doIndexOf('.testBench.bad', [0,1,2], "");

    doIndexOf('.testBench.good', [0,1,2], null);
    doIndexOf('.testBench.good', [1,2,3,4], 2);    
})