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

data_structure_tool

v1.1.2

Published

Helper for working with data structures as Map, Set and Object in JavaScript

Downloads

17

Readme

Data Structure Tool

There is some examples of usage DataStructureTool

const DataStructureTool = require('data_structure_tool')

structsFromArrayByKey method

const users = []

for (let i = 0; i < 3; i++) {
  users.push({ id: i, name: `User ${i}`, branchId: (i % 100) + 3 })
}

const [nameById, branchIdByName, allBranchIds] = DataStructureTool.structsFromArrayByKey({
  arr: users,
  structs: [
    {
      key: 'id',
      valKey: 'name',
      type: 'map'
    },
    {
      key: 'name',
      valKey: 'branchId',
      type: 'map'
    },
    {
      key: 'branchId',
      type: 'set'
    }
  ]
})

nameById === Map(3) { 11 => 'Michael', 12 => 'Karen', 13 => 'Tony' }
branchIdByName === Map(3) { 'Michael' => 1, 'Karen' => 2, 'Tony' => 3 }
allBranchIds === Set(3) { 1, 2, 3 }

mapFromArrayByKey method

const persIdByName = DataStructureTool.mapFromArrayByKey({
  arr: users,
  key: 'name',
  valKey: 'id'
})

result will be following:

Map(3) { 'Michael' => 11, 'Karen' => 12, 'Tony' => 13 }

addToArrayInMap method

const users = [
  {
    name: 'Jim',
    clientNumber: '+77071126486'
  },
  {
    name: 'Jim',
    clientNumber: '+77072126486'
  },
  {
    name: 'Dwight',
    clientNumber: '+77073126486'
  },
  {
    name: 'Dwight',
    clientNumber: '+77073126486'
  }
]

const clientsNumbersByName = new Map()

for (const user of users) {
  // ... you do something, for example
  const error = await validateNumber(user.clientNumber)

  if (!error) {
   // and you want gather valid numbers into groups by names of users
    DataStructureTool.addToArrayInMap({
      map: clientsNumbersByName,
      key: user.name,
      val: user.clientNumber
    })
  }
}

In this case result will be following:

Map(2) {
  'Jim' => [ '+77071126486', '+77072126486' ],
  'Dwight' => [ '+77073126486', '+77073126486' ]
}

But you can improve your collection. Imagine you're gonna add another information about your users in future. It would be nice gather user's numbers in certain key in object as value of your map. To reach this you should just add key "valKey" with name of your key. Example:

DataStructureTool.addToArrayInMap({
  map: clientsNumbersByName,
  key: user.name,
  val: user.clientNumber,
  valKey: 'clientsNumbers'
})

And this is the output:

Map(2) {
  'Jim' => { clientsNumbers: [ '+77071126486', '+77072126486' ] },
  'Dwight' => { clientsNumbers: [ '+77073126486', '+77073126486' ] }
}

You can gather data not only from certain primitive key of object in array(as number or string). It is possible to collect data from keys containing arrays as well.

There is example:

const users = [
  {
    name: 'Jim',
    clientNumbers: ['+77071126486', '+77771125647']
  }
]

const clientsNumbersByName = new Map()

for (const user of users) {
  DataStructureTool.addToArrayInMap({
    map: clientsNumbersByName,
    key: user.name,
    val: user.clientNumbers
    // you can use "valKey" here as well
  })
}

In this case result will be following:

Map(2) {
  'Jim' => ['+77071126486', '+77771125647'],
}

setFromArrayByKey method

const salesmen = [
  {
    name: 'Jim',
    clientNumber: '+77071126486',
    orderNumbers: ['353', '743']
  },
  {
    name: 'Dwight',
    clientNumber: '+77072126486',
    orderNumbers: ['453', '843']
  },
  {
    name: 'Andy',
    clientNumber: '+77073126486',
    orderNumbers: ['953', '234']
  },
]

You can use setFromArrayByKey method to gather set of certain values from any array

Examples: 1 way of usage:

const salesmenNames = DataStructureTool.setFromArrayByKey(salesmen, 'name')

This is result:

Set(3) { 'Jim', 'Dwight', 'Andy' }

2 way of usage: If necessary you can pass to setFromArrayByKey previously created Set. It can be helpful if you're collecting data from different sources

const salesmenNames = new Set()

DataStructureTool.setFromArrayByKey(salesmen, 'orderNumbers', salesmenNames)

If you've noticed in this example we've used as key 'orderNumbers' which contains array of items. In this case all items from each object by this keys will be gathered to resulting set.

This is result:

Set(3) { '353', '743', '453', '843', '953', '234' }