Browse packages

Package · v0.13.0

fn

Standard library functions

Runtime support

  • Node
  • Deno
  • Bun
  • Browser

This module provides typed predicates, identity transforms, and concurrent asynchronous collection helpers.

Installation#

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

Usage#

defined#

A predicate function that checks if a value is not undefined.

js
import fn from "@rcompat/fn";

fn.defined("hello");    // true
fn.defined(0);          // true
fn.defined(null);       // true
fn.defined(false);      // true
fn.defined(undefined);  // false

Useful for filtering out undefined values:

js
import fn from "@rcompat/fn";

const values = [1, undefined, 2, undefined, 3];
const filtered = values.filter(fn.defined);
// [1, 2, 3]

identity#

The identity function — returns its input unchanged.

js
import fn from "@rcompat/fn";

fn.identity(42);        // 42
fn.identity("hello");   // "hello"
fn.identity({ a: 1 });  // { a: 1 }

Useful as a default callback or no-op transformer:

js
import fn from "@rcompat/fn";

function transform(data, transformer = fn.identity) {
  return transformer(data);
}

transform({ name: "Bob" });  // { name: "Bob" }
transform({ name: "Bob" }, x => ({ ...x, id: 1 }));  // { name: "Bob", id: 1 }

async.map#

Async map over an array. With no options, it retains the original Promise.all behavior: every callback starts immediately and results preserve input order.

js
import fn from "@rcompat/fn";

const urls = ["/api/users/1", "/api/users/2", "/api/users/3"];

// fetch all users in parallel
const users = await fn.async.map(urls, async (url) => {
  const response = await fetch(url);
  return response.json();
});
// [{ id: 1, ... }, { id: 2, ... }, { id: 3, ... }]
js
import fn from "@rcompat/fn";

// with index parameter
const results = await fn.async.map([10, 20, 30], async (value, index) => {
  return { index, doubled: value * 2 };
});
// [{ index: 0, doubled: 20 }, { index: 1, doubled: 40 }, { index: 2, doubled: 60 }]

Use concurrency to cap callbacks running at once, and pass a signal to stop queued callbacks:

js
const controller = new AbortController();
const results = await fn.async.map(urls, async (url, index, items) => {
  const response = await fetch(url, { signal: controller.signal });
  return { index, total: items.length, data: await response.json() };
}, {
  concurrency: 4,
  signal: controller.signal,
});

concurrency must be a finite positive integer. Empty arrays resolve to [] unless their signal is already aborted. Rejection or abort prevents additional queued callbacks from starting. Abort rejects the map promptly, but callbacks already running are not forcibly stopped; pass the same signal to cooperative operations such as fetch when they also need cancellation.

Examples#

Filter undefined from array#

js
import fn from "@rcompat/fn";

const users = [
  { name: "John" },
  undefined,
  { name: "Bob" },
  undefined,
];

users.filter(fn.defined);
// [{ name: "John" }, { name: "Bob" }]

Optional transformation pipeline#

js
import fn from "@rcompat/fn";

function processData(data, options = {}) {
  const {
    preProcess = fn.identity,
    postProcess = fn.identity,
  } = options;

  const prepared = preProcess(data);
  const result = doWork(prepared);
  return postProcess(result);
}

// use with default (no-op) transformations
processData(myData);

// use with custom transformations
processData(myData, {
  preProcess: data => data.map(x => x * 2),
  postProcess: result => result.filter(x => x > 10),
});

Conditional mapping#

js
import fn from "@rcompat/fn";

function conditionalMap(array, condition, mapper) {
  return array.map(condition ? mapper : fn.identity);
}

const numbers = [1, 2, 3];

conditionalMap(numbers, true, x => x * 2);   // [2, 4, 6]
conditionalMap(numbers, false, x => x * 2);  // [1, 2, 3]

Parallel API requests#

js
import fn from "@rcompat/fn";

async function fetchProducts(ids) {
  return fn.async.map(ids, async id => {
    const response = await fetch(`/api/products/${id}`);
    return response.json();
  });
}

const products = await fetchProducts([1, 2, 3, 4, 5]);

Cross-Runtime Compatibility#

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

No configuration required — just import and use.

License#

MIT

Contributing#

See CONTRIBUTING.md in the repository root.