npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

obrray

v1.1.0

Published

✨ Convert between objects and arrays in many different ways. Wololoooo!

Downloads

10

Readme

obrray Build status

Convert between objects and arrays in many different ways. Wololoooo!

Wololoooo!

Install

With npm:

$ npm install obrray

With Yarn:

$ yarn add obrray

Usage

toObject(arr[, options])

arr

Type: Array

Array to be converted.


const obrray = require('obrray');

obrray.toObject([]);
//=> {}

obrray.toObject([123]);
//=> { 0: 123 }

obrray.toObject([11, 22, 33]);
//=> { 0: 11, 1: 22, 2: 33 }

obrray.toObject([[11, 22], [33, 44], [55, 66]]);
//=> { 0: [11, 22], 1: [33, 44], 2: [55, 66] }

options

Type: Object

sameKeyAndValue

Type: boolean

Uses each array item both as key and value of the target object.

obrray.toObject([11, 22, 33], { sameKeyAndValue: true });
//=> { 11: 11, 22: 22, 33: 33 }
keyAndValuePairs

Type: boolean|object

Converts from a bidimensional array with key-value pairs.

// Standard key-value pairs array
obrray.toObject(
  [
    ['name', 'Ford Prefect'],
    ['planet', 'Betelgeuse Seven'],
    ['nickname', 'lx']
  ], { keyAndValuePairs: true });
//=> {
//     name: 'Ford Prefect',
//     planet: 'Betelgeuse Seven',
//     nickname: 'lx'
//   }


// Array with custom indexes for key and value
obrray.toObject(
  [
    [147, false, 'Ford Prefect', [], 'name'],
    [215, true, 'Betelgeuse Seven', [111], 'planet'],
    [12, false, 'lx', [27, 1], 'nickname']
  ], { keyAndValuePairs: { keyIndex: 4, valueIndex: 2 } });
//=> {
//     name: 'Ford Prefect',
//     planet: 'Betelgeuse Seven',
//     nickname: 'lx'
//   }
invertedKeyAndValuePairs

Type: boolean

Converts from a bidimensional array with inverted key-value pairs.


// Inverted key-value pairs array
obrray.toObject(
  [
    ['Ford Prefect', 'name'],
    ['Betelgeuse Seven', 'planet'],
    ['lx', 'nickname']
  ], { invertedKeyAndValuePairs: true });
//=> {
//     name: 'Ford Prefect',
//     planet: 'Betelgeuse Seven',
//     nickname: 'lx'
//   }
keyAndValueObjects

Type: boolean|object

Converts from an array of objects with key and value properties.

// Key-value properties with default names
obrray.toObject(
  [
    { key: 'name', value: 'Ford Prefect' },
    { key: 'planet', value: 'Betelgeuse Seven' },
    { key: 'nickname', value: 'lx' }
  ], { keyAndValueObjects: true });
//=> {
//     name: 'Ford Prefect',
//     planet: 'Betelgeuse Seven',
//     nickname: 'lx'
//   }


// Key-value properties with custom names
obrray.toObject(
  [
    { k: 'name', v: 'Ford Prefect' },
    { k: 'planet', v: 'Betelgeuse Seven' },
    { k: 'nickname', v: 'lx' }
  ], { keyAndValueObjects: { keyProperty: 'k', valueProperty: 'v' } });
//=> {
//     name: 'Ford Prefect',
//     planet: 'Betelgeuse Seven',
//     nickname: 'lx'
//   }
mapper

Type: function

Uses a callback function to map each array item in a custom way.

// Default approach, by returning the new value from the callback
obrray.toObject(
  [
    ['name', 147, 27, 42, false, 15, 'Ford Prefect', false],
    ['planet', 'Betelgeuse Seven', [111]],
    ['nickname', 12, false, 'lx', [27, 1], 147, 98]
  ], {
    mapper: (item) => {
      for (let i = 1; i < item.length; i++) {
        if (typeof item[i] !== 'string') continue;

        return { key: item[0], value: item[i] };
      }
    }
  });
//=> {
//     name: 'Ford Prefect',
//     planet: 'Betelgeuse Seven',
//     nickname: 'lx'
//   }

// Changing the target object from within the callback
obrray.toObject(
  [
    ['name', 147, 27, 42, false, 15, 'Ford Prefect', false],
    ['planet', 'Betelgeuse Seven', [111]],
    ['nickname', 12, false, 'lx', [27, 1], 147, 98]
  ], {
    mapper: (item, targetObj) => {
      for (let i = 1; i < item.length; i++) {
        if (typeof item[i] !== 'string') continue;

        targetObj[item[0]] = item[i];
        break;
      }
    }
  });
//=> {
//     name: 'Ford Prefect',
//     planet: 'Betelgeuse Seven',
//     nickname: 'lx'
//   }

toArray(obj[, options])

obj

Type: Object

Object to be converted.

obrray.toArray({});
//=> []

obrray.toArray({ whatever: 123 });
//=> [123]

obrray.toArray({ name: 'Vito', surname: 'Corleone' });
//=> ['Vito', 'Corleone']

obrray.toArray({ 2: 'Corleone', 1: 'Andolini', 0: 'Vito' });
//=> ['Vito', 'Andolini', 'Corleone']

obrray.toArray({ foo: 'bar', 2: 'Corleone', 1: 'Andolini', 0: 'Vito', bar: 'foo' });
//=> ['Vito', 'Andolini', 'Corleone', 'bar', 'foo']
useKeys

Type: boolean

Uses property keys instead of values for each item of the mapped array

obrray.toArray({ name: 'Vito', surname: 'Corleone' }, { useKeys: true });
//=> ['name', 'surname']
toKeyAndValuePairs

Type: boolean

Converts to an array of key and value pairs

obrray.toArray(
  {
    name: 'Vito',
    middleName: 'Andolini',
    surname: 'Corleone'
  }, { toKeyAndValuePairs: true });
//=> [['name', 'Vito'], ['middleName', 'Andolini'], ['surname', 'Corleone']]
toInvertedKeyAndValuePairs

Type: boolean

Converts to an array of inverted key and value pairs

obrray.toArray(
  {
    name: 'Vito',
    middleName: 'Andolini',
    surname: 'Corleone'
  }, { toInvertedKeyAndValuePairs: true });
//=> [['Vito', 'name'], ['Andolini', 'middleName'], ['Corleone', 'surname']]
toKeyAndValueObjects

Type: boolean|object

Converts to an array of key and value objects

// Using default key-value property names
obrray.toArray(
  {
    name: 'Vito',
    middleName: 'Andolini',
    surname: 'Corleone'
  }, { toKeyAndValueObjects: true });
//=> [{ key: 'name', value: 'Vito' }, { key: 'middleName', value: 'Andolini' }, { key: 'surname', value: 'Corleone' }]

// Using custom key-value property names
obrray.toArray(
  {
    name: 'Vito',
    middleName: 'Andolini',
    surname: 'Corleone'
  }, { toKeyAndValueObjects: { keyProperty: 'k', valueProperty: 'v' } });
//=>  [{ k: 'name', v: 'Vito' }, { k: 'middleName', v: 'Andolini' }, { k: 'surname', v: 'Corleone' }]
sorter

Type: function

Sorts the output array

obrray.toArray(
  {
    name: 'Vito',
    middleName: 'Andolini',
    surname: 'Corleone'
  }, { sorter: (a, b) => a.value.localeCompare(b.value) });
//=>  ['Andolini', 'Corleone', 'Vito']

// Using a sorter along with other option
obrray.toArray(
  {
    name: 'Vito',
    middleName: 'Andolini',
    surname: 'Corleone'
  }, { sorter: (a, b) => a.value.localeCompare(b.value), toKeyAndValuePairs: true });
//=>  [['middleName', 'Andolini'], ['surname', 'Corleone'], ['name', 'Vito']]
mapper

Type: function

Uses a callback function to map each object property in a custom way.

// Default approach, by returning the new value from the callback
obrray.toArray(
  {
    name: 'Vito',
    middleName: 'Andolini',
    surname: 'Corleone'
  }, { mapper: (item) => `The ${item.key} is "${item.value}"` });
//=>  ['The name is "Vito"', 'The middleName is "Andolini"', 'The surname is "Corleone"']

// Changing the target array from within the callback
obrray.toArray(
  {
    name: 'Vito',
    middleName: 'Andolini',
    surname: 'Corleone'
  },
  {
    mapper: (item, targetArr) => {
      targetArr.push(`The ${item.key} is "${item.value}"`);
      targetArr.push('...an additional item...');
    }
  });
//=>  [
//      'The name is "Vito"',
//      '...an additional item...',
//      'The middleName is "Andolini"',
//      '...an additional item...',
//      'The surname is "Corleone"',
//      '...an additional item...'
//    ]

Author

Alcides Queiroz Aguiar

License

This code is free to use under the terms of the MIT License.