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

respacer

v1.1.0

Published

Respacer is a library for adjusting the position of points or line-segments to avoid overlaps.

Downloads

18

Readme

Respacer

Respacer is a library for adjusting the position of points or line-segments to avoid overlaps.

It accepts an array of objects, each of which has a property storing its 1D position, and returns a new array containing the objects with an added property storing the adjusted position. The array is sorted by this position.

It is similar to labella.js, but differs both in method used (it solves a Quadratic Program rather than doing a force based simulation), and in API (it accepts an array of arbitrary objects, rather than an array of labella.Node objects).

Usage

For points

The repositionPoints(data, options) function is used to reposition objects which do not have individual widths.

The optional argument options is an object in which the following properties can be set:

| Name | Type | Default | Description | | ----------------- | ------ | ------- | -------------------------------------------------------------------------------------------------------------------- | | width | number | 1000 | The maximum position value that can be assigned. Note that if this is too small, the problem may not be satisfiable. | | minSpacing | number | 10 | The minimum distance between adjacent points after repositioning. | | oldPositionName | string | x | The name of the property which contains the original position. | | newPositionName | string | newX | The name of the property in which the updated position will be stored. |

For example, the input array

const data = [
    {x: 4, label: "A"},
    {x: 9, label: "B"},
    {x: 2, label: "C"},
    {x: 13, label: "D"},
    {x: 9, label: "E"},
    {x: 9.1, label: "F"}
];

const newPositions = repositionPoints(data);

returns:

[
    {
        "x": 2,
        "label": "C",
        "newX": 0
    },
    {
        "x": 4,
        "label": "A",
        "newX": 10.000000000000005
    },
    {
        "x": 9,
        "label": "B",
        "newX": 20
    },
    {
        "x": 9,
        "label": "E",
        "newX": 30
    },
    {
        "x": 9.1,
        "label": "F",
        "newX": 39.999999999999986
    },
    {
        "x": 13,
        "label": "D",
        "newX": 49.999999999999986
    }
]

For line segments

The repositionLineSegments(data, options) function is used to reposition objects which do have individual widths.

The optional argument options is an object in which the following properties can be set:

| Name | Type | Default | Description | | ----------------- | ------ | ------- | ----------------------------------------------------------------------------------------------------------------------------- | | width | number | 1000 | The maximum position value that can be assigned. Note that if this is too small, the problem may not be satisfiable. | | minSpacing | number | 10 | The minimum distance between adjacent the end of one object (position + width) and the start of the next after repositioning. | | oldPositionName | string | x | The name of the property which contains the original position of each object. | | widthName | string | width | The name of the property which contains the width of each object. | | newPositionName | string | newX | The name of the property in which the updated position will be stored. |

For example,

const data = [
    {x: 4, width: 3},
    {x: 5, width: 4},
    {x: 2, width: 5},
    {x: 9, width: 6},
    {x: 5, width: 7}
];


const repositionedData = repositionLineSegments(data);

returns:

[
    {
        "x": 2,
        "width": 5,
        "newX": 0
    },
    {
        "x": 4,
        "width": 3,
        "newX": 4.999999999999999
    },
    {
        "x": 5,
        "width": 4,
        "newX": 7.999999999999998
    },
    {
        "x": 5,
        "width": 7,
        "newX": 12
    },
    {
        "x": 9,
        "width": 6,
        "newX": 18.999999999999996
    }
]

Importing

Using script tag

You can make respacer available in the global scope using a <script> tag:


<script src="../dist/bundle.js"></script>

You can then use the respacer.repositionPoints() and respacer.repositionLineSegments() methods from within JavaScript on that page.

The files in examples/ provides complete working examples.

You can generate this bundle file from the source code in this repository by running npm install and then npm build.

Alternatively, you can use a copy of the file served by various CDNs, including:

As a commonjs module

You can use respacer as a CommonJS module:


const {repositionPoints, repositionLineSegments} = require('./reposition');

const data = [
    {x: 4, label: "A"},
    {x: 9, label: "B"},
    {x: 2, label: "C"},
    {x: 13, label: "D"},
    {x: 9, label: "E"},
    {x: 9.1, label: "F"}
];

const repositionedData = repositionPoints(data);