
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
@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 for updating the ordering of items when they are moved within an array. This package calculates new order values and applies minimal spacing adjustments to ensure consistency and avoid collisions.
step and minimumStep are now exposed for customization.batchIterator example.calculateInsert(items, newItem).This library offers two main functions for managing item order within an array: calculateInsert and calculateUpdateFromMove.
This is particularly useful when storing sorted arrays in a database where frequent insertions and moves occur. The library minimizes database writes by only updating order values that require adjustment.
pnpm install @sparkstone/id-order-spacing
# or
npm install @sparkstone/id-order-spacing
import { calculateInsert } from "@sparkstone/id-order-spacing";
interface Item {
id: string;
order: number;
}
const items: Item[] = [
{ id: "a", order: 100 },
{ id: "b", order: 200 },
{ id: "c", order: 300 },
];
const newItem: Item = { id: "d", order: 0 }; // order will be calculated
// Insert at index 1
const result1 = calculateInsert(items, newItem, 1);
// Or insert at the end
const result2 = calculateInsert(items, newItem);
console.log(result2.items); // d gets placed after c
// After insertion:
// 1. Insert result2.item into the database (order already assigned)
await db.create(result2.item);
// 2. Iterate over `result2.changes` and update affected items in the database.
for (const [id, order] of result2.changes.entries()) {
await db.update(id, { order });
}
import {
calculateUpdateFromMove,
batchIterator,
} from "@sparkstone/id-order-spacing";
const result = calculateUpdateFromMove(unwrap(executors()), fromIndex, toIndex);
for (const subset of batchIterator(result.changes.entries(), 10)) {
const batch = db.createBatch();
for (const [id, order] of subset) {
batch.update(id, { order });
}
await batch.send();
}
calculateInsert<T>(items: T[], item: T, index?: number, opts?: { step?: number, minimumStep?: number })items: sorted array of items with { id: string; order: number }item: the item to insert (its order will be assigned)index: optional index; if omitted, inserts at the endopts: optional object to override default step and minimumStep{ changes, items, item }calculateUpdateFromMove<T>(items: T[], fromIndex: number, toIndex: number, opts?: { step?: number, minimumStep?: number }){ changes, items }fromIndex === toIndex, returns empty changes.opts: optional object to override default step and minimumStepstep and minimumStepThese constants are exposed for advanced use:
import { step, minimumStep } from "@sparkstone/id-order-spacing";
console.log(step); // Default spacing step size (e.g. 100)
console.log(minimumStep); // Minimum gap before rebalance triggers (e.g. 5)
import {
calculateUpdateFromMove,
batchIterator,
} from "@sparkstone/id-order-spacing";
const result = calculateUpdateFromMove(unwrap(executors()), fromIndex, toIndex);
for (const subset of batchIterator(result.changes.entries(), 10)) {
const batch = db.createBatch();
for (const [id, order] of subset) {
batch.update(id, { order });
}
await batch.send();
}
By assigning order values and using these functions, you minimize write operations when inserting or moving items in database-backed lists. After each operation:
order (returned as result.item).changes map by updating affected records.batchIterator to efficiently batch updates if your database supports batch writes.This minimizes the number of writes required to keep your ordering stable and avoids unnecessary updates.
pnpm run build
MIT
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.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.