-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: replace indexOf with typedArrayIndexOf for IE11 support (#417)
- Loading branch information
Showing
3 changed files
with
84 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// IE11 doesn't support indexOf for TypedArrays. | ||
// Once IE11 support is dropped, this function should be removed. | ||
var typedArrayIndexOf = (typedArray, element, fromIndex) => { | ||
if (!typedArray) { | ||
return -1; | ||
} | ||
|
||
var currentIndex = fromIndex; | ||
|
||
for (; currentIndex < typedArray.length; currentIndex++) { | ||
if (typedArray[currentIndex] === element) { | ||
return currentIndex; | ||
} | ||
} | ||
|
||
return -1; | ||
}; | ||
|
||
module.exports = { typedArrayIndexOf }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
'use strict'; | ||
|
||
var | ||
QUnit = require('qunit'), | ||
typedArrayIndexOf = require('../lib/utils/typed-array').typedArrayIndexOf; | ||
|
||
QUnit.module('typedArrayIndexOf'); | ||
|
||
QUnit.test('returns -1 when no typed array', function(assert) { | ||
assert.equal(typedArrayIndexOf(null, 5, 0), -1, 'returned -1'); | ||
}); | ||
|
||
QUnit.test('returns -1 when element not found', function(assert) { | ||
assert.equal(typedArrayIndexOf(new Uint8Array([2, 3]), 5, 0), -1, 'returned -1'); | ||
}); | ||
|
||
QUnit.test('returns -1 when element not found starting from index', function(assert) { | ||
assert.equal(typedArrayIndexOf(new Uint8Array([3, 5, 6, 7]), 5, 2), -1, 'returned -1'); | ||
}); | ||
|
||
QUnit.test('returns index when element found', function(assert) { | ||
assert.equal(typedArrayIndexOf(new Uint8Array([2, 3, 5]), 5, 0), 2, 'returned 2'); | ||
}); | ||
|
||
QUnit.test('returns index when element found starting from index', function(assert) { | ||
assert.equal(typedArrayIndexOf(new Uint8Array([2, 3, 5]), 5, 2), 2, 'returned 2'); | ||
}); |