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.
import * as import _
_ from 'radashi'
// Using a property nameconst const getName: _.MappingFunction<"name">
getName = import _
_.castMapping<"name">(mapping: "name"): _.MappingFunction<"name">export castMapping
Cast the mapping value into a mapping function.
- If
mapping is a function, it's returned as is.
- If
mapping is a property name, the mapping uses it to retrieve
the property value from a given object.
- If
mapping is nullish, the mapping is (input: T) => input.
castMapping('name')const getName: <{ name: string;}>(input: { name: string;}) => string
getName({ name: string
name: 'Alice' }) // => 'Alice'
// Using a functionconst const getLength: _.MappingFunction<(str: string) => number>
getLength = import _
_.castMapping<(str: string) => number>(mapping: (str: string) => number): _.MappingFunction<(str: string) => number>export castMapping
Cast the mapping value into a mapping function.
- If
mapping is a function, it's returned as is.
- If
mapping is a property name, the mapping uses it to retrieve
the property value from a given object.
- If
mapping is nullish, the mapping is (input: T) => input.
castMapping((str: string
str: string) => str: string
str.String.length: number
Returns the length of a String object.
length)const getLength: <"Hello">(input: "Hello") => number
getLength('Hello') // => 5
// Using undefinedconst const identity: _.MappingFunction<undefined>
identity = import _
_.castMapping<undefined>(mapping: undefined): _.MappingFunction<undefined>export castMapping
Cast the mapping value into a mapping function.
- If
mapping is a function, it's returned as is.
- If
mapping is a property name, the mapping uses it to retrieve
the property value from a given object.
- If
mapping is nullish, the mapping is (input: T) => input.
castMapping(var undefined
undefined)const identity: <{ any: string;}>(input: { any: string;}) => { any: string;}
identity({ any: string
any: 'value' }) // => { any: 'value' }Types
CastMapping
This is the return type of castMapping.
import * as import _
_ from 'radashi'
import type { import CastMapping
CastMapping } from 'radashi'
const const data: { a: number; b: string;}
data = { a: number
a: 1, b: string
b: '2' }
const const mapper: CastMapping<{ a: number; b: string;}, number>
mapper: import CastMapping
CastMapping<typeof const data: { a: number; b: string;}
data, number> = import _
_.castMapping<(data: any) => any>(mapping: (data: any) => any): _.MappingFunction<(data: any) => any>export castMapping
Cast the mapping value into a mapping function.
- If
mapping is a function, it's returned as is.
- If
mapping is a property name, the mapping uses it to retrieve
the property value from a given object.
- If
mapping is nullish, the mapping is (input: T) => input.
castMapping(data: any
data => data: any
data.any
a)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.
import * as import _
_ from 'radashi'import type { type MappedOutput<TMapping, TInput = any> = TMapping extends (data: TInput) => infer Result ? [Result] extends [_.Any] ? unknown : Result : [TInput] extends [_.Any] ? unknown : TMapping extends null | undefined ? TInput : TMapping extends keyof TInput ? TInput[TMapping] : never
The return type of a mapping function created with castMapping.
MappedOutput } from 'radashi'
type type Data = { a: number; b: string;}
Data = { a: number
a: number; b: string
b: string }
const const test: <T>() => any
test = <function (type parameter) T in <T>(): any
T>() => any
T
const test: <never>() => any
test<type MappedOutput<TMapping, TInput = any> = TMapping extends (data: TInput) => infer Result ? [Result] extends [_.Any] ? unknown : Result : [TInput] extends [_.Any] ? unknown : TMapping extends null | undefined ? TInput : TMapping extends keyof TInput ? TInput[TMapping] : never
The return type of a mapping function created with castMapping.
MappedOutput<type Data = { a: number; b: string;}
Data, (data: Data
data: type Data = { a: number; b: string;}
Data) => number>>()// is number
const test: <unknown>() => any
test<type MappedOutput<TMapping, TInput = any> = TMapping extends (data: TInput) => infer Result ? [Result] extends [_.Any] ? unknown : Result : [TInput] extends [_.Any] ? unknown : TMapping extends null | undefined ? TInput : TMapping extends keyof TInput ? TInput[TMapping] : never
The return type of a mapping function created with castMapping.
MappedOutput<type CastMapping = /*unresolved*/ any
CastMapping<'a'>>>()// is number
const test: <unknown>() => any
test<type MappedOutput<TMapping, TInput = any> = TMapping extends (data: TInput) => infer Result ? [Result] extends [_.Any] ? unknown : Result : [TInput] extends [_.Any] ? unknown : TMapping extends null | undefined ? TInput : TMapping extends keyof TInput ? TInput[TMapping] : never
The return type of a mapping function created with castMapping.
MappedOutput<undefined>>()// is DataMapping
You can use the Mapping type to accept a value that can be passed into castMapping.
import * as import _
_ from 'radashi'import type { type Mapping<T = any, U = any> = ((arg: T) => U) | _.CompatibleProperty<T, U>
A value that can be casted with castMapping.
Mapping, type MappedOutput<TMapping, TInput = any> = TMapping extends (data: TInput) => infer Result ? [Result] extends [_.Any] ? unknown : Result : [TInput] extends [_.Any] ? unknown : TMapping extends null | undefined ? TInput : TMapping extends keyof TInput ? TInput[TMapping] : never
The return type of a mapping function created with castMapping.
MappedOutput } from 'radashi'
function function mapArray<T, TMapping extends Mapping<T>>(array: readonly T[], mapping: TMapping): MappedOutput<TMapping, T>[]
mapArray<function (type parameter) T in mapArray<T, TMapping extends Mapping<T>>(array: readonly T[], mapping: TMapping): MappedOutput<TMapping, T>[]
T, function (type parameter) TMapping in mapArray<T, TMapping extends Mapping<T>>(array: readonly T[], mapping: TMapping): MappedOutput<TMapping, T>[]
TMapping extends type Mapping<T = any, U = any> = ((arg: T) => U) | _.CompatibleProperty<T, U>
A value that can be casted with castMapping.
Mapping<function (type parameter) T in mapArray<T, TMapping extends Mapping<T>>(array: readonly T[], mapping: TMapping): MappedOutput<TMapping, T>[]
T>>( array: readonly T[]
array: readonly function (type parameter) T in mapArray<T, TMapping extends Mapping<T>>(array: readonly T[], mapping: TMapping): MappedOutput<TMapping, T>[]
T[], mapping: TMapping extends Mapping<T>
mapping: function (type parameter) TMapping in mapArray<T, TMapping extends Mapping<T>>(array: readonly T[], mapping: TMapping): MappedOutput<TMapping, T>[]
TMapping,): type MappedOutput<TMapping, TInput = any> = TMapping extends (data: TInput) => infer Result ? [Result] extends [_.Any] ? unknown : Result : [TInput] extends [_.Any] ? unknown : TMapping extends null | undefined ? TInput : TMapping extends keyof TInput ? TInput[TMapping] : never
The return type of a mapping function created with castMapping.
MappedOutput<function (type parameter) TMapping in mapArray<T, TMapping extends Mapping<T>>(array: readonly T[], mapping: TMapping): MappedOutput<TMapping, T>[]
TMapping, function (type parameter) T in mapArray<T, TMapping extends Mapping<T>>(array: readonly T[], mapping: TMapping): MappedOutput<TMapping, T>[]
T>[] { return array: readonly T[]
array.ReadonlyArray<T>.map<_.MappedOutput<TMapping, T>>(callbackfn: (value: T, index: number, array: readonly T[]) => _.MappedOutput<TMapping, T>, thisArg?: any): _.MappedOutput<TMapping, T>[]
Calls a defined callback function on each element of an array, and returns an array that contains the results.
map(import _
_.castMapping<TMapping>(mapping: TMapping): _.MappingFunction<TMapping>export castMapping
Cast the mapping value into a mapping function.
- If
mapping is a function, it's returned as is.
- If
mapping is a property name, the mapping uses it to retrieve
the property value from a given object.
- If
mapping is nullish, the mapping is (input: T) => input.
castMapping(mapping: TMapping extends Mapping<T>
mapping))}If you want a mapping to be optional, use the OptionalMapping type instead.
import * as import _
_ from 'radashi'import type { type OptionalMapping<T = any, U = any> = _.Mapping<T, U> | null | undefined
A value that can be casted with castMapping.
OptionalMapping, type MappedOutput<TMapping, TInput = any> = TMapping extends (data: TInput) => infer Result ? [Result] extends [_.Any] ? unknown : Result : [TInput] extends [_.Any] ? unknown : TMapping extends null | undefined ? TInput : TMapping extends keyof TInput ? TInput[TMapping] : never
The return type of a mapping function created with castMapping.
MappedOutput } from 'radashi'
function function mapArray<T, TMapping extends OptionalMapping<T>>(array: readonly T[], mapping?: TMapping): MappedOutput<TMapping, T>[]
mapArray<function (type parameter) T in mapArray<T, TMapping extends OptionalMapping<T>>(array: readonly T[], mapping?: TMapping): MappedOutput<TMapping, T>[]
T, function (type parameter) TMapping in mapArray<T, TMapping extends OptionalMapping<T>>(array: readonly T[], mapping?: TMapping): MappedOutput<TMapping, T>[]
TMapping extends type OptionalMapping<T = any, U = any> = _.Mapping<T, U> | null | undefined
A value that can be casted with castMapping.
OptionalMapping<function (type parameter) T in mapArray<T, TMapping extends OptionalMapping<T>>(array: readonly T[], mapping?: TMapping): MappedOutput<TMapping, T>[]
T>>( array: readonly T[]
array: readonly function (type parameter) T in mapArray<T, TMapping extends OptionalMapping<T>>(array: readonly T[], mapping?: TMapping): MappedOutput<TMapping, T>[]
T[], mapping: TMapping | undefined
mapping?: function (type parameter) TMapping in mapArray<T, TMapping extends OptionalMapping<T>>(array: readonly T[], mapping?: TMapping): MappedOutput<TMapping, T>[]
TMapping,): type MappedOutput<TMapping, TInput = any> = TMapping extends (data: TInput) => infer Result ? [Result] extends [_.Any] ? unknown : Result : [TInput] extends [_.Any] ? unknown : TMapping extends null | undefined ? TInput : TMapping extends keyof TInput ? TInput[TMapping] : never
The return type of a mapping function created with castMapping.
MappedOutput<function (type parameter) TMapping in mapArray<T, TMapping extends OptionalMapping<T>>(array: readonly T[], mapping?: TMapping): MappedOutput<TMapping, T>[]
TMapping, function (type parameter) T in mapArray<T, TMapping extends OptionalMapping<T>>(array: readonly T[], mapping?: TMapping): MappedOutput<TMapping, T>[]
T>[] { return array: readonly T[]
array.ReadonlyArray<T>.map<_.MappedOutput<TMapping, T> | ([T] extends [_.Any] ? unknown : T)>(callbackfn: (value: T, index: number, array: readonly T[]) => _.MappedOutput<TMapping, T> | ([...] extends [...] ? unknown : T), thisArg?: any): (_.MappedOutput<...> | ([...] extends [...] ? unknown : T))[]
Calls a defined callback function on each element of an array, and returns an array that contains the results.
map(import _
_.castMapping<TMapping | undefined>(mapping: TMapping | undefined): _.MappingFunction<TMapping | undefined>export castMapping
Cast the mapping value into a mapping function.
- If
mapping is a function, it's returned as is.
- If
mapping is a property name, the mapping uses it to retrieve
the property value from a given object.
- If
mapping is nullish, the mapping is (input: T) => input.
castMapping(mapping: TMapping | undefined
mapping))}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.