43 lines
793 B
TypeScript
43 lines
793 B
TypeScript
|
|
namespace formgen.helpers.map
|
||
|
|
{
|
||
|
|
|
||
|
|
/**
|
||
|
|
*/
|
||
|
|
export function read<type_value>(
|
||
|
|
map : Record<string, type_value>,
|
||
|
|
key : string,
|
||
|
|
fallback : type_value
|
||
|
|
) : type_value
|
||
|
|
{
|
||
|
|
return ((key in map) ? map[key] : fallback);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
*/
|
||
|
|
export function transform<type_value_from, type_value_to>(
|
||
|
|
map : Record<string, type_value_from>,
|
||
|
|
function_ : ((string, type_value_from) => type_value_to)
|
||
|
|
) : Record<string, type_value_to>
|
||
|
|
{
|
||
|
|
return Object.fromEntries(
|
||
|
|
Object.entries(map)
|
||
|
|
.map(([key, value]) => ([key, function_(key, value)]))
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
*/
|
||
|
|
export function to_pairs<type_value>(
|
||
|
|
map : Record<string, type_value>,
|
||
|
|
) : Array<{key : string; value : type_value;}>
|
||
|
|
{
|
||
|
|
return (
|
||
|
|
Object.entries(map)
|
||
|
|
.map(([key, value]) => ({"key": key, "value": value}))
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|