Registry / devops / merge-k-sorted-arrays

merge-k-sorted-arrays

JSON →
library3.0.0jsnpmunverified

Efficiently merges 2 or more sorted arrays using a priority queue (min-heap). Version 3.0.0 requires Node.js >=20. Ships TypeScript types. Key differentiators: supports custom comparator, output metadata (array index and value index), and deduplication via unique option. Uses a heap-based approach for O(n log k) time complexity. No external runtime dependencies.

npm install merge-k-sorted-arrays
INSTALL
IMPORT
SIG · MERGE-K-SORTED-ARR
M
merge-k-sorted-arrays
devopsjavascriptv3.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import merge from 'merge-k-sorted-arrays'
const merge = require('merge-k-sorted-arrays')
ESM-only since v3; CommonJS require is not supported.
type MergeOptions
import type { MergeOptions } from 'merge-k-sorted-arrays'
import { MergeOptions } from 'merge-k-sorted-arrays'
MergeOptions is a TypeScript type; use 'import type' to avoid runtime import.
type OutputMetadata
import type { OutputMetadata } from 'merge-k-sorted-arrays'
OutputMetadata is a type; use 'import type'.

Shows basic merge, custom comparator, output metadata, and unique/dedupe usage with TypeScript.

import merge from 'merge-k-sorted-arrays'; import type { MergeOptions, OutputMetadata } from 'merge-k-sorted-arrays'; const arrays = [ [1, 3, 5, 7], [2, 4, 6, 8], [0, 9, 10, 11] ]; // Basic merge const result: number[] = merge(arrays); console.log(result); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] // With custom comparator (descending) const desc: number[] = merge( [ [7, 5, 3, 1], [8, 6, 4, 2], [11, 10, 9, 0] ], { comparator: (a: number, b: number) => b - a } ); console.log(desc); // [11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1] // With output metadata const times: number[][] = [ [0, 2, 4, 6], [1, 3, 5, 7] ]; const values: number[][] = [ [10, 20, 50, 40], [80, 70, 30, 60] ]; const merged: OutputMetadata<number>[] = merge(times, { outputMetadata: true }); const mergedTimes: number[] = merged.map(([arrIdx, valIdx, time]) => time); const mergedValues: number[] = merged.map(([arrIdx, valIdx]) => values[arrIdx][valIdx]); console.log(mergedTimes); // [0, 1, 2, 3, 4, 5, 6, 7] console.log(mergedValues); // [10, 80, 20, 70, 50, 30, 40, 60] // Unique / dedupe const deduped: number[] = merge( [ [1, 3, 3, 6, 6], [2, 2, 3, 5] ], { unique: true } ); console.log(deduped); // [1, 2, 3, 5, 6]
Debug
Known issues
breakingv3.0.0 drops CommonJS support; package is now ESM-only.
fix
Use 'import' syntax instead of 'require()'. Ensure your project is configured for ESM.
affects: >=3.0.0
breakingv3.0.0 drops Node.js <20 support.
fix
Upgrade Node.js to version 20 or later.
affects: >=3.0.0
deprecatedv2.x allowed passing arrays as separate arguments; v3 requires an array as the first argument.
fix
Wrap arrays in an array: merge([arr1, arr2, ...]) instead of merge(arr1, arr2, ...).
affects: >=3.0.0
gotchaoutputMetadata option returns tuples of [arrayIndex, valueIndex, value]; the third element is the value, not any metadata.
fix
Destructure as [arrIdx, valIdx, value] to access the actual value.
affects: all
gotchaWhen using unique: true, the last occurrence of a value determines which metadata (arrayIndex, valueIndex) is kept.
fix
If you need a specific occurrence, preprocess arrays to ensure uniqueness.
affects: all
Errors
Common errors & fixes
SyntaxError: Cannot use import statement outside a module
Using ESM import in a CommonJS project or Node.js version <13.2 (with default module system).
fix
Add "type": "module" to package.json, use .mjs extension, or use dynamic import(). For Node.js <20, upgrade to Node >=20.
TypeError: merge is not a function
Trying to use CommonJS require on v3 (ESM-only) or passing arrays as separate arguments instead of an array.
fix
Use ESM import: import merge from 'merge-k-sorted-arrays'. For v3, call merge([arr1, arr2, ...]).
TypeError: Cannot read properties of undefined (reading 'length')
Passing an empty array or non-array argument.
fix
Ensure first argument is an array of arrays, e.g., merge([[1,2], [3,4]]).
Error: Comparator must be a function
Providing comparator as a non-function value in options.
fix
Pass a function as comparator: e.g., { comparator: (a, b) => a - b }.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
merge-k-sorted-arrays — npm install merge-k-sorted-arrays · libregistry