This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add arrayFind (implementation of Array.prototype.find)
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Usage: import arrayFind = require("tsutil/arrayFind"); | ||
* | ||
* arrayFind([1,2,3,4,5], (x) => x === 3); | ||
* | ||
* @param {Array<T>} list | ||
* @param {(x: T) => boolean} predicate | ||
* @param {Object} thisArg? | ||
* @returns {T?} | ||
*/ | ||
function arrayFind<T>( | ||
list: T[], | ||
predicate: (x: T, index: number, list: T[]) => boolean, | ||
thisArg?: {}): T { | ||
|
||
var length = list.length; | ||
var value: T; | ||
for (var i = 0; i < length; i++) { | ||
value = list[i]; | ||
if (predicate.call(thisArg, value, i, list)) { | ||
return value; | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
export = arrayFind; | ||
|
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,49 @@ | ||
import chai = require("chai"); | ||
import tsutil = require("../src/index"); | ||
|
||
var arrayFind = tsutil.arrayFind; | ||
var assert = chai.assert; | ||
|
||
describe("arrayFind", () => { | ||
|
||
var list: number[] = [5, 10, 15, 20]; | ||
|
||
it("should find item by predicate", function() { | ||
var result = arrayFind(list, (x) => x === 15); | ||
assert.equal(result, 15); | ||
}); | ||
|
||
it("should return undefined when nothing matched", function() { | ||
var result = arrayFind(list, (x) => x === -1); | ||
assert.equal(result, undefined); | ||
}); | ||
|
||
it("should receive all three parameters", function() { | ||
arrayFind(list, (value, index, arr) => { | ||
assert.equal(list[index], value); | ||
assert.equal(list, arr); | ||
return false; | ||
}); | ||
}); | ||
|
||
it("should work with the context argument", function() { | ||
var context = {}; | ||
arrayFind([1], function() { | ||
assert.equal(this, context); | ||
return false; | ||
}, context); | ||
}); | ||
|
||
it("should work with a sparse array", function() { | ||
var obj = [1, , undefined]; | ||
assert.isFalse(1 in obj); | ||
var seen: any[] = []; | ||
var found = arrayFind(obj, (x) => { | ||
seen.push(x); | ||
return false; | ||
}); | ||
assert.equal(found, undefined); | ||
assert.sameMembers(seen, [1, undefined, undefined]); | ||
}); | ||
}); | ||
|