-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Add findOrUndefined function. (#583)
This allows us to avoid using Array.prototype.find, which is not available on Internet Explorer 11.
- Loading branch information
Showing
3 changed files
with
60 additions
and
1 deletion.
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,12 @@ | ||
/** | ||
* @hidden | ||
* Find first element matching the predicate in the array, or null if none match | ||
* Equivalent to Array.prototype.find, but works on Internet Explorer 11. | ||
*/ | ||
export function findOrUndefined<T>(ts: ArrayLike<T>, f: (t: T) => boolean): T | undefined { | ||
for (let i = 0; i < ts.length; i++) { | ||
const t = ts[i]; | ||
if (f(t)) return t; | ||
} | ||
return undefined; | ||
} |
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,46 @@ | ||
import * as fc from '../../../../../lib/fast-check'; | ||
import { findOrUndefined } from '../../../../../src/check/arbitrary/helpers/ArrayHelper'; | ||
|
||
describe('ArrayHelper', () => { | ||
describe('findOrUndefined', () => { | ||
it('should return undefined for empty array', () => { | ||
expect( | ||
findOrUndefined<number>([], () => { | ||
throw new Error('⊥'); | ||
}) | ||
).toBe(undefined); | ||
}); | ||
it('should return a matching element', () => { | ||
fc.assert( | ||
fc.property(fc.array(fc.integer()), fc.integer(), fc.array(fc.integer()), (prefix, e, suffix) => { | ||
expect(findOrUndefined<number>([...prefix, e, ...suffix], x => x === e)).toBe(e); | ||
}) | ||
); | ||
}); | ||
it('should return undefined when not present', () => { | ||
fc.assert( | ||
fc.property( | ||
fc.array(fc.integer(0, 100)), | ||
fc.integer(-100, -1), | ||
fc.array(fc.integer(0, 100)), | ||
(prefix, e, suffix) => { | ||
expect(findOrUndefined<number>([...prefix, ...suffix], x => x === e)).toBe(undefined); | ||
} | ||
) | ||
); | ||
}); | ||
it('should pass some simple examples', () => { | ||
expect(findOrUndefined<number>([7, 8, 2, 1], x => x < 3)).toBe(2); | ||
expect(findOrUndefined<number>([12, 8, -1], x => x < 3)).toBe(-1); | ||
expect(findOrUndefined<number>([3], x => x > 10)).toBe(undefined); | ||
expect(findOrUndefined<number>([], x => x > 10)).toBe(undefined); | ||
}); | ||
it('should be consistent with Array.prototype.indexOf', () => { | ||
fc.assert( | ||
fc.property(fc.array(fc.integer(1, 200)), fc.integer(1, 200), (xs, x) => { | ||
expect(findOrUndefined<number>(xs, a => a === x) === undefined).toBe(xs.indexOf(x) === -1); | ||
}) | ||
); | ||
}); | ||
}); | ||
}); |