Skip to content

Commit

Permalink
fix environmental differences node10,12/4,5
Browse files Browse the repository at this point in the history
  • Loading branch information
tjmehta committed Mar 17, 2016
1 parent da27d10 commit 07d311a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 37 deletions.
16 changes: 15 additions & 1 deletion find-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
* @module object-loops/find-key
*/

var isInteger = require('101/is-integer')

var castArrayKey = function (key) {
var num = parseInt(key, 10)
return isNaN(num) ? key : num
}

/**
* Find the key of the the object that passes the test implemented by the callback.
* @function module:object-loops/find-key
Expand All @@ -13,9 +20,12 @@
module.exports = findKey

function findKey (obj, callback, thisArg) {
if (Array.isArray(obj) && obj.findIndex) {
var isArray = Array.isArray(obj)
if (isArray && obj.findIndex) {
/* $lab:coverage:off$ */ // not covered in envs that don't have `findIndex`
var index = obj.findIndex(callback, thisArg)
return ~index ? index : undefined
/* $lab:coverage:on$ */
}
if (typeof obj !== 'object' && typeof obj !== 'function') {
throw new TypeError(obj + ' must be an object')
Expand All @@ -26,6 +36,10 @@ function findKey (obj, callback, thisArg) {
var ret
var keys = Object.keys(obj)

if (isArray) {
keys = Object.keys(obj).map(castArrayKey).filter(isInteger)
}

for (var i = 0; i < keys.length; i++) {
var key = keys[i]
var val = obj[key]
Expand Down
79 changes: 61 additions & 18 deletions test/test-find-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ describe('findKey', function () {
expect(ret).to.equal(1)
done()
})
it('should break loop if item is found (weird array)', function (done) {
var obj = [
'foo',
'bar',
'baz'
]
obj['wut'] = 'hello'
var callback = sinon.spy(equals('hello'))
var thisArg = {}
var ret = findKey(obj, callback, thisArg)
// assertions
expect(callback.callCount).to.equal(3)
expect(ret).to.equal(undefined)
done()
})
describe('errors', function () {
it('should throw an error if obj must be an object', function (done) {
var obj = 'notObject'
Expand All @@ -113,25 +128,53 @@ describe('findKey', function () {
done()
})
})
describe('use w/ array', function () {
beforeEach(function (done) {
sinon.spy(Array.prototype, 'findIndex')
done()
})
afterEach(function (done) {
Array.prototype.findIndex.restore()
done()
if (Array.prototype.findIndex) {
describe('use w/ array', function () {
beforeEach(function (done) {
sinon.spy(Array.prototype, 'findIndex')
done()
})
afterEach(function (done) {
Array.prototype.findIndex.restore()
done()
})
it('should use array findIndex', function (done) {
var arr = [1, 2, 3]
var callback = noop
var index = arr.findIndex(callback, arr)
expect(findKey(arr, callback))
.to.equal(~index ? index : undefined)
sinon.assert.calledOn(Array.prototype.findIndex, arr)
sinon.assert.calledWith(Array.prototype.findIndex, callback)
done()
})
})
it('should use array findIndex', function (done) {
var arr = [1, 2, 3]
var callback = noop
var index = arr.findIndex(callback, arr)
expect(findKey(arr, callback))
.to.equal(~index ? index : undefined)
sinon.assert.calledOn(Array.prototype.findIndex, arr)
sinon.assert.calledWith(Array.prototype.findIndex, callback)
done()
describe('use w/ array', function () {
var findIndex = Array.prototype.findIndex
beforeEach(function (done) {
delete Array.prototype.findIndex
done()
})
afterEach(function (done) {
Array.prototype.findIndex = findIndex // eslint-disable-line no-extend-native
done()
})
it('should break loop if item is found (weird array)', function (done) {
var obj = [
'foo',
'bar',
'baz'
]
obj['wut'] = 'hello'
var callback = sinon.spy(equals('hello'))
var thisArg = {}
var ret = findKey(obj, callback, thisArg)
// assertions
expect(callback.callCount).to.equal(3)
expect(ret).to.equal(undefined)
done()
})
})
})
}
})
})
38 changes: 20 additions & 18 deletions test/test-find.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,26 @@ describe('find', function () {
done()
})
})
describe('use w/ array', function () {
beforeEach(function (done) {
sinon.spy(Array.prototype, 'findIndex')
done()
})
afterEach(function (done) {
Array.prototype.findIndex.restore()
done()
if (Array.prototype.findIndex) {
describe('use w/ array', function () {
beforeEach(function (done) {
sinon.spy(Array.prototype, 'findIndex')
done()
})
afterEach(function (done) {
Array.prototype.findIndex.restore()
done()
})
it('should use array findIndex', function (done) {
var arr = [1, 2, 3]
var callback = noop
expect(find(arr, callback))
.to.equal(arr.find(callback, arr))
sinon.assert.calledOn(Array.prototype.findIndex, arr)
sinon.assert.calledWith(Array.prototype.findIndex, callback)
done()
})
})
it('should use array findIndex', function (done) {
var arr = [1, 2, 3]
var callback = noop
expect(find(arr, callback))
.to.equal(arr.find(callback, arr))
sinon.assert.calledOn(Array.prototype.findIndex, arr)
sinon.assert.calledWith(Array.prototype.findIndex, callback)
done()
})
})
}
})
})

0 comments on commit 07d311a

Please sign in to comment.