get-mapped-value
v0.1.6
Published
strongly typed utility function for mapping
Downloads
4
Readme
Get-mapped-value
Strongly typed utility function for mapping
Installation
npm install get-mapped-value
yarn add get-mapped-value
Basic Usage
import { getMappedValue } from 'get-mapped-value';
const map = {
one: 1,
two: 2,
three: 3,
};
var key = 'one';
// ^? var key: string
const resultOne = getMappedValue(map, key); // 1
// ^? const resultOne: number | undefined
const resultTwo = getMappedValue(map, key, { defaultKey: 'two' }); // 2
// ^? const resultTwo: number
const resultThree = getMappedValue(map, 'THREE', { normalizingMethod: 'toLowerCase' }); // 3
// ^? const resultThree: number
const resultUndefined = getMappedValue(map, 'THREE'); // undefined
// ^? const resultUndefined: undefined
But it becomes more powerful when you use const types
const map = {
one: 1,
two: 2,
three: 3,
} as const;
const resultOne = getMappedValue(map, 'one'); // 1
// ^? const resultOne: 1
const resultTwo = getMappedValue(map, 'four', { defaultKey: 'two' }); // 2
// ^? const resultTwo: 2
const resultThree = getMappedValue(map, 'THREE', { normalizingMethod: 'toLowerCase' }); // 3
// ^? const resultThree: 3
const resultUndefined = getMappedValue(map, 'four'); // undefined
// ^? const resultUndefined: undefined