0.1.6 • Published 2 years ago

get-mapped-value v0.1.6

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years 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

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago