0.1.6 • Published 10 months ago

get-mapped-value v0.1.6

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Get-mapped-value

Strongly typed utility function for mapping

NPM License GitHub Workflow Status npm bundle size GitHub release codecov

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
0.1.6

10 months ago

0.1.5

10 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago

0.0.9

10 months ago

0.0.8

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago