Created
May 11, 2019 06:15
-
-
Save fvilante/1e1d1038f9a8b0d391d9575311d2988e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Entry { | |
value: string | |
label: string | |
} | |
type Dictionary = Entry[] | |
const dictionary: Dictionary = [ | |
{ value: 'primeiro', label: '1 - Primeiro' }, | |
{ value: 'segundo', label: '2 - Segundo' }, | |
{ value: 'terceiro', label: '3 - Terceiro' }, | |
{ value: 'quarto', label: '4 - Quarto' }, | |
{ value: 'quinto', label: '5 - Quinto' }, | |
{ value: 'sexto', label: '6 - Sexto' }, | |
]; | |
type Data = string | |
type DataFormated = string | |
type Formatter = (_: Dictionary) => (_: Data[]) => DataFormated | |
const formatter: Formatter = dict => data => | |
[[...dict]] | |
.map( d => d | |
.filter( e => data.join(',').includes(e.value)) | |
.map( e => e.label ) | |
.join(', ') | |
) | |
.map( | |
s => s.length >= 25 | |
? s.substring(0, 25) + '...' | |
: s | |
) | |
.join('') | |
const StandardFormatter = formatter(dictionary) | |
const r = StandardFormatter(['primeiro', 'quinto', 'sexto']) | |
console.log(r) // "1 - Primeiro, 5 - Quinto,..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment