forked from renovatebot/renovate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
assignKeys
utility function (renovatebot#23483)
- Loading branch information
Sergei Zharinov
authored
Jul 20, 2023
1 parent
3fb7cb1
commit 8845247
Showing
2 changed files
with
36 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,18 @@ | ||
import { assignKeys } from './assign-keys'; | ||
|
||
describe('util/assign-keys', () => { | ||
it('should assign values from right to left for specified keys', () => { | ||
type Left = { a: number; b: number }; | ||
const left: Left = { a: 1, b: 2 }; | ||
|
||
type Right = { a?: number; b?: number; c?: number }; | ||
const right: Right = { a: 3, c: 4 }; | ||
|
||
const result = assignKeys(left, right, ['a', 'b']); | ||
expect(result).toEqual({ | ||
a: 3, | ||
b: 2, | ||
}); | ||
expect(result).toBe(left); | ||
}); | ||
}); |
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,18 @@ | ||
import is from '@sindresorhus/is'; | ||
|
||
/** | ||
* Assigns non-nullish values from `right` to `left` for the given `keys`. | ||
*/ | ||
export function assignKeys< | ||
Left extends { [key in K]?: Right[key] }, | ||
Right extends { [key in K]?: any }, | ||
K extends keyof Right | ||
>(left: Left, right: Right, keys: K[]): Left { | ||
for (const key of keys) { | ||
const val = right[key]; | ||
if (!is.nullOrUndefined(val)) { | ||
left[key] = val; | ||
} | ||
} | ||
return left; | ||
} |