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

grid-sort

v2.1.3

Published

Sort objects in a two dimensional array to compose grids based on a condition

Downloads

7

Readme

Introduction

In CSS there are multiple ways to achieve stunning layouts, the most popular systems to achieve this are Grid and Flex layout systems. These systems provide you with a set of properties to define layouts, however they depend on the order of the elements in the dom.

For simple binary choices this is great, however we may want a grid layout where elements fill the horizontal space based on their item size. The ideal concept would be that grid or flex had properties to lay out items respecting their intrinsic size from greatest to lowest size, and filling out the gaps with items with smaller size. I've tried to achieve this with only css but haven't come up with a way, so I decided to reach out to JavsScript and sort the items in that same manner.

Visualization

To better understand the case above let's introduce an example. Imagine a list of numbers which represent the columns they would take in a grid of four columns:

const list = [3, 2, 1, 2, 3, 1, 1, 1];

The numbers are totally random, because they may come from any dataset you'd have and these may not be sorted for a grid, perhaps they are sorted by insertion order in your database, size, date created, or whatever. Therefore to have them sorted for a grid the sorting algorithm should respect a few rules:

  1. Move largest objects to the top of the grid
  2. Fill the gaps they leave in the current row with whatever largest object we can take
  3. Always respect the number of columns
  4. If two rows are visually the same swap position of items to create a better space distribution
const sortedList = [
  [3, 1],
  [1, 3],
  [2, 2],
  [1, 1],
];

The sorted list is a two dimensional array that represents a grid where rows are filled using the rules above. The fourth rule can be seen on the second row, where if the second row didn't follow the fourth rule it would have the 3 column item first and the 1 column item last, but as we have an identical row before this one lets swap the items.

Installation

This module is distributed via npm and should be installed as one of your project's dependencies:

# with yarn
yarn add grid-sort

# with npm
npm install grid-sort

Usage

With a list of primitive numbers

import { gridSort } from "grid-sort";

const items = [1, 1, 1, 2, 3, 3];

const sortedItems = gridSort({ items });

/*
  sortedItems -> [
    [3, 1],
    [1, 3],
    [2, 1]
  ]
*/

With a list of objects we need to provide another option to our gridSort function. An accessor option is required for list of objects because we can compare objects we need you to provide a way to access a number within the objects that represents the column an object would take.

import { gridSort } from "grid-sort";

const images = [
  { id: 1, columns: 3 },
  { id: 2, columns: 2 },
  { id: 3, columns: 1 },
  { id: 4, columns: 1 },
  { id: 5, columns: 1 },
];

// define an accessor to get the property to check against
const accessor = (image: typeof items[number]) => image.columns;

const sortedItems = gridSort({ items, accessor });

/*
  sortedItems -> [
    [
      { id: 1, columns: 3 },
      { id: 3, columns: 1 }
    ],
    [
      { id: 2, columns: 2 },
      { id: 4, columns: 1 }
      { id: 5, columns: 1 }
    ]
  ]
*/

Options

The exported function gridSort accepts an object that has three properties which represent required data or options.

items (required)

It represents the list of items you want to sort

accessor

If you give an array of objects as items you'll have to pass this option too, as I described above, this option is necessary to obtain a number that represents the number of columns an item in your list might take

columns

Defines the number of columns your grid would have

Contributions

You are more than welcome to report any issues or suggest new features. Just create an issue with the bug or enhancement label and we'll discuss further actions. Thank you in advance for even considering contributing to this.

License

MIT