-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69be946
commit acc6a98
Showing
2 changed files
with
35 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 { describe, expect, it } from 'vitest'; | ||
import { asTransientProps } from './asTransientProps'; | ||
|
||
describe('asTransientProps()', () => { | ||
it('converts all properties to have a `$` prefix', () => { | ||
const props = { | ||
size: 'lg', | ||
color: 'red', | ||
}; | ||
|
||
const expected = { | ||
$size: 'lg', | ||
$color: 'red', | ||
}; | ||
|
||
expect(asTransientProps(props)).toEqual(expected); | ||
}); | ||
}); |
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,17 @@ | ||
// Thanks to https://stackoverflow.com/a/65278278/974981 for the prefixer | ||
// utility types | ||
type Prefix<K extends string, T extends string> = `${K}${T}`; | ||
|
||
type Prefixer<K, T extends string> = { | ||
[P in keyof K as Prefix<T, string & P>]: K[P]; | ||
}; | ||
|
||
export type AsTransientProps<T extends Record<string, unknown>> = Prefixer<T, '$'>; | ||
|
||
export const asTransientProps = <T extends Record<string, unknown>>( | ||
props: T, | ||
): AsTransientProps<T> => | ||
Object.entries(props).reduce<Partial<AsTransientProps<T>>>((prev, curr) => { | ||
const [key, value] = curr; | ||
return { ...prev, [`$${key}`]: value }; | ||
}, {}) as unknown as AsTransientProps<T>; |