Library to experiment using template literals for creating a switch
/case
expression returning the respective value.
npm install --save tagged-switch
yarn add tagged-switch
The library can be used to create case-matching statements which return their respective values:
import taggedSwitch from 'tagged-switch';
const currencySymbolToName = taggedSwitch`
${'€'} -> ${'Euro'}
${'$'} -> ${'Dollar'}
${'£'} -> ${'Pound'}
_ -> ${'Unknown'}
`;
console.log(currencySymbolToName('€')); // Euro
The following checks will be performed and will throw an error in the case that one check fails:
- There must be at least one specific case and one default case.
- The cases must use a
->
-string to separate the key of the case from its value. - The default value must be the last one, declared by a
_
-string followed by the->
-string. - Keys can only occur once.
- useful tests
- support of ES5
Licensed under GNU General Public License v3.0. See LICENSE.