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

group-array-items

v1.0.6

Published

Group json items in array

Downloads

3

Readme

Group Array Items


This library helps you group json items in array. It has zero-dependency.

How to install


Using npm

$ npm i --save group-array-items
Using yarn

 $ yarn add group-array-items
Compiling source code

  • Download src code from github
  • Open your cli and type
$ npm run build

or

$ yarn run build
  • Get file in dist folder and referance it in your application

How to use


This helper function is configurable with a configuration parameter. However, if you want to use with default configuration, your json model must have a type property for grouping and children property is you have child objects needed to be grouped. But dont worry, each of them can be configured easily.

With default configuration file

import groupBy from 'group-array-items'

let data = [
  {
    name: "My item 1",
    type: "group-1",
    children: [
      {
        name: "My item 1.1",
        type: "group-1"
      }, {
        name: "My item 1.2",
        type: "group-2"
      }, {
        name: "My item 1.3",
        type: "group-1"
      }
    ]
  }, {
    name: "My item 2",
    type: "group-2"
  }, {
    name: "My item 3",
    type: "group-1",
    children: [
      {
        name: "My item 3.1",
        type: "group-1",
        children: [
          {
            name: "My item 3.1.1",
            type: "group-3"
          }, {
            name: "My item 3.1.2",
            type: "group-3"
          }, {
            name: "My item 3.1.3",
            type: "group-4"
          }
        ]
      }, {
        name: "My item 3.2",
        type: "group-2"
      }, {
        name: "My item 3.3",
        type: "group-1"
      }
    ]
  }
]

const result = groupBy(data);

With configuration file

To configure groupBy function, send a second json object as parameter.

const config = {
    aliases: [
        {
            alias: "Group Name 1 Alias",
            name: "group_name_1"
        },
        {
            alias: "Group Name 2 Alias",
            name: "group_name_2"
        }
    ],
    groupBy: "type",
    childElement: "children",
    prefix: "",
    generateId: true
}
const result = groupBy(data,config);

When you grouped items, groupBy function generates a new item which has a boolean property groupItem to indicate that item is an grouping-header-item. By default, that element's name is given to its group name however you can override that behaviour by giving to alias by configuration file.

Output of groupBy

You will have an output as below format

[
  {
    "name": "type-name-alias",
    "groupItem": true, // shows that this json is an header generated by groupBy function
    "type": "type-name",
    "children": [
      {
        "name": "My item 1", // grouped item
        "type": "group-1"
      },
      {
        "name": "My item 1", // second grouped item
        "type": "group-1"
      }
    ]
  }
]

Configuration Description


You can configure groupBy function by sending any of configuration parameter to fit your needs.

| Property | Type | Default Value | Description | |:------------: |:------: |:-------------: |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | aliases | array | empty | an array which holds alias for grouped headers. Format of that array item is: { alias: "A title which will be used for instead of group-type", name: "group-type" } Assume that you have group types gold,silver and rock in your json type and you want to see in your group-header titles as My Gold, My Silver,My Rock. { alias: "My Gold", name: "gold } | | groupBy | string | "type" | property which will be looked for grouping in json object | | childElement | string | children | property which will be looked for child elements in json object for deep grouping | | prefix | string | empty | string which will be appended to generated grouped header's groupBy propery | | generateId | boolean | true | unique id generate for each group header |

Change log


  • v1.0.3 Webpack configuration fixed
  • v.1.0.2 Package.json configuration fixed
  • v.1.0.0 Initial release

Contributing


I'd be very happy if you feedback any issue