puiutucutu / bearly-functional

a repo of functions, some functional, some bearly 🐻
https://puiutucutu.github.io/bearly-functional/
GNU General Public License v3.0
1 stars 0 forks source link

add fn `itemsToDict` #2

Open puiutucutu opened 4 years ago

puiutucutu commented 4 years ago
/**
 * Creates an object where each string from the array of strings is the object
 * key set to some default value.
 *
 * @param {*} defaultValue
 * @return {function(xs: string[]): Object}
 * @example
 *
 * itemsToDict(null)(["a","b","c"]); //=> { a: null, b: null, c: null }
 *
 */
export const itemsToDict = defaultValue => xs => {
  return xs.reduce((acc, x) => ((acc[x] = defaultValue), acc), {});
};