@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
53 lines (52 loc) • 2.6 kB
TypeScript
import React from 'react';
/**
* withCamelCaseProps is a HOC for function components
* it will return a React Component where all snake_case props gets converted to camelCase
*
* Use the same for TypeScript types by using: ToCamelCase
*
* @param Base the original function or class
* @returns extended function or class
*/
export declare function withCamelCaseProps<TBase, P>(Base: ((props: P) => JSX.Element) & TBase): typeof Base;
/**
* withCamelCaseProps is a HOC for classes
* it will return a React Component where all snake_case props gets converted to camelCase
*
* Use the same for TypeScript types by using: ToCamelCase
*
* @param Base the original function or class
* @returns extended function or class
*/
export declare function classWithCamelCaseProps<TBase extends React.ComponentClass>(Base: TBase): typeof Base;
/**
* Converts camel case props to snake case props.
*/
export declare function convertCamelCasePropsToSnakeCase<P>(props: P, validProperties?: Array<string> | ReadonlyArray<string>): P;
export type AssertNoMissing<T extends readonly string[], P extends Record<string, unknown>> = Exclude<Extract<keyof P, `${string}_${string}`>, T[number]> extends never ? T : never;
type KeysMatching<T, P extends string> = Extract<keyof T, P>;
export type KeysWithUnderscore<T> = KeysMatching<T, `${string}_${string}`>;
/**
* Convert recursively Types from snake_case to camelCase
*
* Use it like so:
* OriginalProps & ToCamelCase<OriginalProps>
*
* Disclaimer: Be careful using these with required props
* - ToCamelCase makes the required snake_case props also required in camelCase
* - ToCamelCasePartial removes required for the camelCase props
* - ToCamelCaseFlat is like ToCamelCase, but it will not convert nested objects for performance boost
* - ToCamelCasePartialFlat is like ToCamelCasePartial, but it will not convert nested objects for performance boost
*/
export type ToCamelCasePartial<T> = Partial<ToCamelCase<T>>;
export type ToCamelCase<T> = T extends object ? {
[K in keyof T as ConvertSnakeToCamelCase<K & string>]: T[K] | ToCamelCase<T[K]>;
} : T;
export type IncludeCamelCase<T> = Partial<T> & ToCamelCasePartial<T>;
type ConvertSnakeToCamelCase<S extends string> = S extends `${infer T}_${infer U}` ? `${T}${Capitalize<ConvertSnakeToCamelCase<U>>}` : S;
type ConvertObjectKeysToCamelCase<T> = {
[K in keyof T as ConvertSnakeToCamelCase<K & string>]: T[K];
};
export type ToCamelCaseFlat<T> = T extends object ? ConvertObjectKeysToCamelCase<T> : T;
export type ToCamelCasePartialFlat<T> = Partial<ToCamelCaseFlat<T>>;
export {};