1.0.1 • Published 8 years ago

to-object-reducer v1.0.1

Weekly downloads
81
License
MIT
Repository
github
Last release
8 years ago

to-object-reducer

Build Status

Simple JavaScript/Node.js module which exports a toObject reducer that can be used with arr.reduce(toObject) to convert an array [[key,value],...] to an object { key: value, ... }.

Example:

[['username', 'olalonde'], ['id',1337]].reduce(toObj, {})
// => { username: 'olalonde', id: 1337 }

Install

npm install --save to-object-reducer

Usage

It is meant to facilitate the pattern of converting an object to an array of [key,val] for easier manipulation with Array methods and back to an object, e.g.:

const toObject = require('to-object-reducer')

// Lower case object keys
const lowerCaseHeaders = (headers = {}) => Object
  .keys(headers)
  .map(key => [key, headers[key]])
  .map([key, val] => [key.toLowerCase(), val])
  .reduce(toObject, {})

// Or with ES2017 Object.entries
const lowerCaseHeaders = (headers = {}) => Object
  .entries(headers)
  .map([key, val] => [key.toLowerCase(), val])
  .reduce(toObject, {})

License

MIT