Skip to content
Replaces destructuring with a simple curried function
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
README.md
index.js
index.test.js
package.json

README.md

array-to-object-helper

Install with yarn add array-to-object-helper or npm i array-to-object-helper.

Ever had a situation when conversion from array to object looked way too verbose?

Promise.resolve([1,2,3,4,5,6,7,8])
.then(([one, two, three, four, five, six, seven, eight]) => 
  ({one, two, three, four, five, six, seven, eight}))

With this utility you can turn in into less verbose version:

const AtoO = require('array-to-object-helper');

Promise.resolve([1,2,3,4,5,6,7,8])
.then(AtoO(['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight']))

Or even less verbose:

const {to} = require('array-to-object-helper');

Promise.resolve([1,2,3,4,5,6,7,8])
.then(to`{one, two, three, four, five, six, seven, eight}`)

This basically looks like a syntax sugar. Maybe one day js will have something similar built in.

This is also valid:

Promise.resolve([1,2]).then(to`{one, x-two,}`);
Promise.resolve([1,2,3]).then(to`{
  $,2,
  x-three
}`);

api

  • AtoO(keysArray)(valuesArray): Object
  • AtoO(valuesArray, keysArray): Object
  • AtoO throws nice error if keysArray.length !== valuesArray.length or when length is 0
  • AtoO.to`objectShapeFormat`(valuesArray): Object
  • AtoO.to validates objectShapeFormat and throws nice error if format is not valid
You can’t perform that action at this time.