Browse packages

Package · v0.22.0 · changelog

array

Standard library arrays

Runtime support

  • Node
  • Deno
  • Bun
  • Browser

This module provides common array operations for computing differences, checking emptiness, and normalizing values to arrays.

Installation#

bash
npm install @rcompat/array
bash
pnpm add @rcompat/array
bash
yarn add @rcompat/array
bash
bun add @rcompat/array

Usage#

difference#

Returns elements from the first array that are not present in the second array (set difference).

js
import array from "@rcompat/array";

array.difference([1, 2, 3, 4], [2, 4]);
// [1, 3]

array.difference(["a", "b", "c"], ["b"]);
// ["a", "c"]

array.difference([1, 2], [1, 2]);
// []

empty#

Checks if an array is empty.

js
import array from "@rcompat/array";

array.empty([]);
// true

array.empty(["foo"]);
// false

array.empty([[]]);
// false (contains one element, an empty array)

to#

Normalizes a value to an array. If the value is already an array, it is returned as-is. Otherwise, the value is wrapped in an array.

js
import array from "@rcompat/array";

array.to("hello");
// ["hello"]

array.to(["hello"]);
// ["hello"]

array.to(42);
// [42]

array.to([1, 2, 3]);
// [1, 2, 3]

Examples#

Processing user input#

js
import array from "@rcompat/array";

function processIds(ids) {
  const idArray = array.to(ids);

  if (array.empty(idArray)) {
    return "No IDs provided";
  }

  return `Processing ${idArray.length} ID(s)`;
}

processIds("user-1");        // "Processing 1 ID(s)"
processIds(["user-1", "user-2"]); // "Processing 2 ID(s)"
processIds([]);              // "No IDs provided"

Finding new items#

js
import array from "@rcompat/array";

const previousTags = ["javascript", "typescript", "node"];
const currentTags = ["javascript", "typescript", "deno", "bun"];

const newTags = array.difference(currentTags, previousTags);
// ["deno", "bun"]

const removedTags = array.difference(previousTags, currentTags);
// ["node"]

Cross-Runtime Compatibility#

Runtime Supported
Node.js ✓
Deno ✓
Bun ✓
Browser ✓

No configuration required — just import and use.

License#

MIT

Contributing#

See CONTRIBUTING.md in the repository root.