-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(findKey): add findKey to compat layer (#828)
* feat(findKey): add findKey to compat layer * Add docs --------- Co-authored-by: Sojin Park <[email protected]>
- Loading branch information
1 parent
976d509
commit 917aff0
Showing
8 changed files
with
378 additions
and
4 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
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
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
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,61 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { findKey } from './findKey'; | ||
|
||
/** | ||
* @see https://lodash.com/docs/4.17.15#findKey | ||
*/ | ||
describe('findKey', () => { | ||
const users = { | ||
barney: { age: 36, active: true }, | ||
fred: { age: 40, active: false }, | ||
pebbles: { age: 1, active: true }, | ||
}; | ||
|
||
it('should find key with a function predicate', function () { | ||
const actual = findKey(users, function (o) { | ||
return o.age < 40; | ||
}); | ||
expect(actual).toBe('barney'); | ||
}); | ||
|
||
it('should work with `_.matches` shorthands', function () { | ||
const actual = findKey(users, { age: 1, active: true }); | ||
expect(actual).toBe('pebbles'); | ||
}); | ||
|
||
it('should work with `_.matchesProperty` shorthands', function () { | ||
const actual = findKey(users, ['active', false]); | ||
expect(actual).toBe('fred'); | ||
}); | ||
|
||
it('should work with `_.property` shorthands', function () { | ||
const actual = findKey(users, 'active'); | ||
expect(actual).toBe('barney'); | ||
}); | ||
|
||
it('should return undefined for an empty object', function () { | ||
// @ts-expect-error - invalid argument | ||
const actual = findKey({}, { age: 36 }); | ||
expect(actual).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined for null input', function () { | ||
const actual = findKey(null, { age: 36 }); | ||
expect(actual).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined for undefined input', function () { | ||
const actual = findKey(undefined, { age: 36 }); | ||
expect(actual).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined if no matching key is found', function () { | ||
const actual = findKey(users, { age: 100 }); | ||
expect(actual).toBeUndefined(); | ||
}); | ||
|
||
it('should handle partial matches with `Partial<T[keyof T]>`', function () { | ||
const actual = findKey(users, { active: true }); | ||
expect(actual).toBe('barney'); | ||
}); | ||
}); |
Oops, something went wrong.