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

layout

v2.2.0

Published

Organize and layout items based on various algorithms

Downloads

283,251

Readme

layout Build status

Organize and layout items based on various algorithms

Visualizations of output data:

| top-down | left-right | diagonal | alt-diagonal | binary-tree | |---------------------------|-------------------------------|---------------------------|-----------------------------------|---------------------------------| | top-down | left-right | diagonal | alt-diagonal | binary-tree |

Getting Started

Install the module with: npm install layout

// Load in layout
var layout = require('layout');

// Generate a new layer to organize items on
var layer = layout('top-down');

// Add items that you want to organize
layer.addItem({'height': 20, 'width': 10, 'meta': 'medium'});
layer.addItem({'height': 10, 'width': 10, 'meta': 'small'});
layer.addItem({'height': 50, 'width': 40, 'meta': 'large'});

// Export the info
var info = layer['export']();

// We get back the width and height of the pack as well as organized items
{
    height: 80,
    width: 40,
    items: [{
        height: 10,
        width: 10,
        meta: 'small',
        x: 0,
        y: 0
    }, {
        height: 20,
        width: 10,
        meta: 'medium',
        x: 0,
        y: 10
    }, {
        height: 50,
        width: 40,
        meta: 'large',
        x: 0,
        y: 30
    }]
}

Documentation

Layout is a constructor function

/**
 * Layout adds items in an algorithmic fashion
 * @constructor
 * @param {String|Object} [algorithm="top-down"] Name of algorithm or custom algorithm to use
 *   Available algorithms are listed in the Algorithms section
 * @param {Mixed} [options] Options to provide for the algorithm
 */

Items can be added via addItem which are required to have a height and width. Any additional info should be stored inside of meta.

/**
 * @param {Object} item Item to store -- this currently is mutated in-memory
 * @param {Number} item.width Width of the item
 * @param {Number} item.height Height of the item
 * @param {Mixed} [item.meta] Any meta data you would like to store related to the item
 */

export is how you take your items and organize them.

/**
 * @returns {Object} retObj
 * @returns {Number} retObj.height Height of the processed layout
 * @returns {Number} retObj.width Width of the processed layout
 * @returns {Mixed[]} retObj.items Organized items
 */

Algorithms

Currently layout supports 5 different layout types which are listed below.

top-down

The top-down algorithm places items vertically.

top-down image

By default, it sorts from smallest (top) to largest (bottom). However, this can be disabled via sort: false.

Options:

  • sort Boolean Flag to enable/disable sorting from smallest (top) to largest (bottom)
    • By default, this is enabled (true)

left-right

The left-right algorithm places items horizontally.

left-right image

By default, it sorts from smallest (left) to largest (right). However, this can be disabled via sort: false.

Options:

  • sort Boolean Flag to enable/disable sorting from smallest (left) to largest (right)
    • By default, this is enabled (true)

diagonal

The diagonal algorithm places items diagonally (top-left to bottom-right).

diagonal image

By default, it sorts from smallest (top-left) to largest (bottom-right). However, this can be disabled via sort: false.

Options:

  • sort Boolean Flag to enable/disable sorting from smallest (top-left) to largest (bottom-right)
    • By default, this is enabled (true)

alt-diagonal

The alt-diagonal algorithm places items diagonally (top-right to bottom-left).

alt-diagonal image

By default, it sorts from smallest (top-right) to largest (bottom-left). However, this can be disabled via sort: false.

Options:

  • sort Boolean Flag to enable/disable sorting from smallest (top-right) to largest (bottom-left)
    • By default, this is enabled (true)

binary-tree

The binary-tree algorithm packs items via the binary tree algorithm.

This is an efficient way to pack items into the smallest container possible.

binary-tree image

Custom algorithms

You can add your own algorithm via layout.addAlgorithm

/**
 * Method to add new algorithms via
 * @param {String} name Name of algorithm
 * @param {Object} algorithm Algorithm to bind under name
 * @param {Function} algorithm.sort Algorithm to sort object by
 * @param {Function} algorithm.placeItems Algorithm to place items by
 */

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via npm run lint and test via npm test.

Donating

Support this project and others by twolfson via gratipay.

Support via Gratipay

License

Copyright (c) 2012-2014 Todd Wolfson Licensed under the MIT license.