Skip to content

Instantly share code, notes, and snippets.

@VitorLuizC
Last active July 16, 2018 21:02
Show Gist options
  • Select an option

  • Save VitorLuizC/b74289dfa94e1138147c1fd383c087f4 to your computer and use it in GitHub Desktop.

Select an option

Save VitorLuizC/b74289dfa94e1138147c1fd383c087f4 to your computer and use it in GitHub Desktop.
Collection library
import * as object from './object'
export const toObject = (collection) =>
[ ...collection ].reduce(object.increase, {})
export const tail = <T> (array: Array<T>): T => array[array.length - 1];
export const exceptTail = <T> (array: Array<T>): Array<T> => array.slice(0, array.length - 1);
/**
* Convert collection to PT-BR statement.
*/
type toStatementΛ = <T> (value: T) => string;
const toStatement = <T> (
array: Array<T>,
λ: toStatementΛ = (value) => String(value)
): string =>
array.length === 0 ? '' :
array.length === 1 ? λ(array[0])
: exceptTail(array).map(λ).join(', ') + ' e ' + λ(tail(array));
export const increase = (object, [ property, value ]) => ({
...object,
[property]: value
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment