
Research
/Security News
Shai Hulud Strikes Again (v2)
Another wave of Shai-Hulud campaign has hit npm with more than 500 packages and 700+ versions affected.
@sparkstone/id-order-spacing
Advanced tools
A lightweight utility library for calculating minimal spacing changes when reordering items in a sorted array.
A lightweight utility library to update the ordering of items when they are moved within an array. This package calculates new order values with minimal spacing adjustments to ensure consistency and avoid collisions.
The package provides a main function, calculateUpdateFromMove, which takes an array of ordered items (each with an id and an order number) along with indices for a move operation. It returns an updated ordering along with any necessary changes to maintain proper spacing. Internally, the package leverages utility functions to compute a new order value, detect collisions, and apply minimal spacing changes.
This solution is particularly useful when storing a sorted array inside a database. By assigning each record an order value, you can simply query the database and sort by this field to retrieve the items in the correct order. Furthermore, when reordering items, the function returns only the minimal set of record changes needed, updating only the records that have been affected by the reordering operation—improving efficiency and reducing write operations.
When adding new items to the end of your list, it is recommended to add 10,000 to the current highest order value. Although the library does not enforce this spacing, using a larger gap between order values can help minimize the number of modifications needed in the long term when items are reordered. Essentially, providing more "room" between numbers reduces the likelihood of collisions and the need for widespread updates.
Note: The items passed into the functions are expected to be pre-sorted in ascending order based on their order property. This assumption is critical for the correct calculation of new order values.
You can install the package using your favorite package manager. For example, using pnpm:
pnpm install @sparkstone/id-order-spacing
Or with npm:
npm install @sparkstone/id-order-spacing
Below is a basic example of how to use the package:
import { calculateUpdateFromMove } from "@sparkstone/id-order-spacing";
interface Item {
id: string;
order: number;
}
// Items must be sorted by the order property in ascending order.
const items: Item[] = [
{ id: "a", order: 100 },
{ id: "b", order: 200 },
{ id: "c", order: 300 },
];
const fromIndex = 0;
const toIndex = 2;
const result = calculateUpdateFromMove(items, fromIndex, toIndex);
console.log(result);
/*
Expected output:
{
changes: Map { 'a' => <new order> },
newOrder: [ <updated items array> ]
}
*/
calculateUpdateFromMove<T>(items: T[], fromIndex: number, toIndex: number)items: An array of items where each item is an object with an id (string) and an order (number). Note: The items must be sorted in ascending order based on the order property.fromIndex: The current index of the item being moved.toIndex: The target index where the item should be placed.changes: A Map<string, number> mapping item IDs to their new order values.newOrder: The updated array of items sorted by the new order values.If fromIndex equals toIndex, the function returns an empty array, indicating that no update is needed.
This library is especially beneficial when you need to store a sorted array of items in a database. By maintaining an order field on each record, you can perform a simple sort in your query to retrieve the items in the correct order. When an item is moved, calculateUpdateFromMove computes the new order for only the affected items. This results in minimal record changes, meaning you only update the records that have changed. This efficiency can be crucial for applications with large data sets or frequent reordering operations.
This package uses microbundle for bundling. To build the package, run:
pnpm run build
This will compile the TypeScript source (located at src/main.ts) and produce the bundled output (typically in dist/index.js).
The source code is hosted on GitHub. Feel free to browse the repository, open issues, or contribute improvements.
This project is licensed under the MIT License.
FAQs
A lightweight utility library for calculating minimal spacing changes when reordering items in a sorted array.
We found that @sparkstone/id-order-spacing demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Research
/Security News
Another wave of Shai-Hulud campaign has hit npm with more than 500 packages and 700+ versions affected.

Product
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.

Security News
ENISA has become a CVE Program Root, giving the EU a central authority for coordinating vulnerability reporting, disclosure, and cross-border response.