Skip to content

Instantly share code, notes, and snippets.

@fvilante
Created May 11, 2019 06:15
Show Gist options
  • Save fvilante/1e1d1038f9a8b0d391d9575311d2988e to your computer and use it in GitHub Desktop.
Save fvilante/1e1d1038f9a8b0d391d9575311d2988e to your computer and use it in GitHub Desktop.
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