-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(extendPropsWithContext): [internal] rewrite to TypeScript
- Loading branch information
1 parent
99629c4
commit a27bcf7
Showing
6 changed files
with
63 additions
and
54 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
16 changes: 16 additions & 0 deletions
16
packages/dnb-eufemia/src/shared/helpers/__tests__/extendPropsWithContext.test.ts
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,16 @@ | ||
import { extendPropsWithContext } from '../extendPropsWithContext' | ||
|
||
describe('"extendPropsWithContext" should', () => { | ||
it('extend prop from other context object', () => { | ||
expect( | ||
extendPropsWithContext( | ||
{ key: { x: 'y' }, foo: null }, // given props | ||
{ key: { x: 'y' }, foo: null }, // default props | ||
{ key: 'I can’t replace You', foo: 'bar' } | ||
) | ||
).toEqual({ | ||
key: { x: 'y' }, | ||
foo: 'bar', // because the prop was null, we get bar | ||
}) | ||
}) | ||
}) |
40 changes: 40 additions & 0 deletions
40
packages/dnb-eufemia/src/shared/helpers/extendPropsWithContext.ts
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,40 @@ | ||
export type DefaultsProps = Record<string, unknown> | ||
export type Contexts = Array<Record<string, unknown>> | ||
|
||
/** | ||
* Extends props from a given context | ||
* but give the context second priority only | ||
* | ||
* @param props object of component properties | ||
* @param defaults object of potential default values | ||
* @param contexts the rest of all context to merge | ||
* @returns merged properties | ||
*/ | ||
export function extendPropsWithContext<Props>( | ||
props: Props, | ||
defaults: DefaultsProps = {}, | ||
...contexts: Contexts | ||
) { | ||
const context = contexts.reduce((acc, cur) => { | ||
if (cur) { | ||
acc = { ...acc, ...cur } | ||
} | ||
return acc | ||
}, {}) | ||
|
||
return { | ||
...props, | ||
...Object.entries(context).reduce((acc, [key, value]) => { | ||
if ( | ||
// check if a prop of the same name exists | ||
typeof props[key] !== 'undefined' && | ||
// and if it was NOT defined as a component prop, because its still the same as the defaults | ||
props[key] === defaults[key] | ||
) { | ||
// then we use the context value | ||
acc[key] = value | ||
} | ||
return acc | ||
}, {}), | ||
} | ||
} |