Skip to content

Commit 07d311a

Browse files
committed
fix environmental differences node10,12/4,5
1 parent da27d10 commit 07d311a

File tree

3 files changed

+96
-37
lines changed

3 files changed

+96
-37
lines changed

find-key.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
* @module object-loops/find-key
33
*/
44

5+
var isInteger = require('101/is-integer')
6+
7+
var castArrayKey = function (key) {
8+
var num = parseInt(key, 10)
9+
return isNaN(num) ? key : num
10+
}
11+
512
/**
613
* Find the key of the the object that passes the test implemented by the callback.
714
* @function module:object-loops/find-key
@@ -13,9 +20,12 @@
1320
module.exports = findKey
1421

1522
function findKey (obj, callback, thisArg) {
16-
if (Array.isArray(obj) && obj.findIndex) {
23+
var isArray = Array.isArray(obj)
24+
if (isArray && obj.findIndex) {
25+
/* $lab:coverage:off$ */ // not covered in envs that don't have `findIndex`
1726
var index = obj.findIndex(callback, thisArg)
1827
return ~index ? index : undefined
28+
/* $lab:coverage:on$ */
1929
}
2030
if (typeof obj !== 'object' && typeof obj !== 'function') {
2131
throw new TypeError(obj + ' must be an object')
@@ -26,6 +36,10 @@ function findKey (obj, callback, thisArg) {
2636
var ret
2737
var keys = Object.keys(obj)
2838

39+
if (isArray) {
40+
keys = Object.keys(obj).map(castArrayKey).filter(isInteger)
41+
}
42+
2943
for (var i = 0; i < keys.length; i++) {
3044
var key = keys[i]
3145
var val = obj[key]

test/test-find-key.js

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@ describe('findKey', function () {
9191
expect(ret).to.equal(1)
9292
done()
9393
})
94+
it('should break loop if item is found (weird array)', function (done) {
95+
var obj = [
96+
'foo',
97+
'bar',
98+
'baz'
99+
]
100+
obj['wut'] = 'hello'
101+
var callback = sinon.spy(equals('hello'))
102+
var thisArg = {}
103+
var ret = findKey(obj, callback, thisArg)
104+
// assertions
105+
expect(callback.callCount).to.equal(3)
106+
expect(ret).to.equal(undefined)
107+
done()
108+
})
94109
describe('errors', function () {
95110
it('should throw an error if obj must be an object', function (done) {
96111
var obj = 'notObject'
@@ -113,25 +128,53 @@ describe('findKey', function () {
113128
done()
114129
})
115130
})
116-
describe('use w/ array', function () {
117-
beforeEach(function (done) {
118-
sinon.spy(Array.prototype, 'findIndex')
119-
done()
120-
})
121-
afterEach(function (done) {
122-
Array.prototype.findIndex.restore()
123-
done()
131+
if (Array.prototype.findIndex) {
132+
describe('use w/ array', function () {
133+
beforeEach(function (done) {
134+
sinon.spy(Array.prototype, 'findIndex')
135+
done()
136+
})
137+
afterEach(function (done) {
138+
Array.prototype.findIndex.restore()
139+
done()
140+
})
141+
it('should use array findIndex', function (done) {
142+
var arr = [1, 2, 3]
143+
var callback = noop
144+
var index = arr.findIndex(callback, arr)
145+
expect(findKey(arr, callback))
146+
.to.equal(~index ? index : undefined)
147+
sinon.assert.calledOn(Array.prototype.findIndex, arr)
148+
sinon.assert.calledWith(Array.prototype.findIndex, callback)
149+
done()
150+
})
124151
})
125-
it('should use array findIndex', function (done) {
126-
var arr = [1, 2, 3]
127-
var callback = noop
128-
var index = arr.findIndex(callback, arr)
129-
expect(findKey(arr, callback))
130-
.to.equal(~index ? index : undefined)
131-
sinon.assert.calledOn(Array.prototype.findIndex, arr)
132-
sinon.assert.calledWith(Array.prototype.findIndex, callback)
133-
done()
152+
describe('use w/ array', function () {
153+
var findIndex = Array.prototype.findIndex
154+
beforeEach(function (done) {
155+
delete Array.prototype.findIndex
156+
done()
157+
})
158+
afterEach(function (done) {
159+
Array.prototype.findIndex = findIndex // eslint-disable-line no-extend-native
160+
done()
161+
})
162+
it('should break loop if item is found (weird array)', function (done) {
163+
var obj = [
164+
'foo',
165+
'bar',
166+
'baz'
167+
]
168+
obj['wut'] = 'hello'
169+
var callback = sinon.spy(equals('hello'))
170+
var thisArg = {}
171+
var ret = findKey(obj, callback, thisArg)
172+
// assertions
173+
expect(callback.callCount).to.equal(3)
174+
expect(ret).to.equal(undefined)
175+
done()
176+
})
134177
})
135-
})
178+
}
136179
})
137180
})

test/test-find.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,26 @@ describe('find', function () {
113113
done()
114114
})
115115
})
116-
describe('use w/ array', function () {
117-
beforeEach(function (done) {
118-
sinon.spy(Array.prototype, 'findIndex')
119-
done()
120-
})
121-
afterEach(function (done) {
122-
Array.prototype.findIndex.restore()
123-
done()
116+
if (Array.prototype.findIndex) {
117+
describe('use w/ array', function () {
118+
beforeEach(function (done) {
119+
sinon.spy(Array.prototype, 'findIndex')
120+
done()
121+
})
122+
afterEach(function (done) {
123+
Array.prototype.findIndex.restore()
124+
done()
125+
})
126+
it('should use array findIndex', function (done) {
127+
var arr = [1, 2, 3]
128+
var callback = noop
129+
expect(find(arr, callback))
130+
.to.equal(arr.find(callback, arr))
131+
sinon.assert.calledOn(Array.prototype.findIndex, arr)
132+
sinon.assert.calledWith(Array.prototype.findIndex, callback)
133+
done()
134+
})
124135
})
125-
it('should use array findIndex', function (done) {
126-
var arr = [1, 2, 3]
127-
var callback = noop
128-
expect(find(arr, callback))
129-
.to.equal(arr.find(callback, arr))
130-
sinon.assert.calledOn(Array.prototype.findIndex, arr)
131-
sinon.assert.calledWith(Array.prototype.findIndex, callback)
132-
done()
133-
})
134-
})
136+
}
135137
})
136138
})

0 commit comments

Comments
 (0)