castMapping
Cast a value into a mapping function
Usage
Improve your own utility function by adding a flexible value-mapping option, using castMapping
to retrieve a mapping function.
The following types can be casted into a mapping function:
- Function: If the input is a function, it returns the function as is.
- Property Name: If the input is a property name, it returns a function that retrieves the value of that property from an object.
- Nullish: If the input is nullish (null or undefined), it returns a function that simply returns the input object itself.
Types
CastMapping
This is the return type of castMapping
.
MappedOutput
As you may have noticed in the previous example, the MappedOutput
type is used to infer the type of the value returned by the mapping function.
Mapping
You can use the Mapping
type to accept a value that can be passed into castMapping
.
If you want a mapping to be optional, use the OptionalMapping
type instead.
Etymology
Origin
The term “castMapping” combines two key concepts from programming:
-
“Cast” originates from type casting in programming, which involves converting a value from one data type to another. This process ensures that data is in the correct format for a specific operation.
-
”Mapping” as a noun refers to a correspondence between elements of two sets, or a function that defines such a correspondence. In programming, it often represents a data structure that associates keys with values, or a function that transforms one set of data into another.
Together, “castMapping” describes a function that takes a value (which could be a function, a property name, or undefined) and converts or “casts” it into a standardized mapping. This resulting mapping can then be used to transform data consistently, regardless of the initial input type. The process enhances flexibility in data manipulation by allowing various input types to be treated uniformly as mappings for data transformation.