UNPKG

find-insert-index

Version:

Find the index to insert an element in array keeping the sort order.

42 lines (35 loc) 929 B
/* global describe, it */ var chai, assert, findInsertIndex; chai = require('chai'); findInsertIndex = require('..'); assert = chai.assert; chai.Assertion.includeStack = true; describe('findInsertIndex', function () { 'use strict'; it('should get the index to insert the element in the array according to the comparator', function () { var targetArray, comparatorFn; comparatorFn = function (a, b) { return a.id - b.id; }; targetArray = [ {id: 0}, {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}, {id: 8}, {id: 9}, {id: 10}, {id: 11}, {id: 12}, {id: 13}, {id: 14}, {id: 15} ]; assert.equal(findInsertIndex(comparatorFn, [], {id: 7}), 0); assert.equal(findInsertIndex(comparatorFn, [{id: 7}], {id: 7}), 1); assert.equal(findInsertIndex(comparatorFn, targetArray, {id: 7}), 7); }); });