or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-xtend

Lightweight utility library for extending JavaScript objects with immutable and mutable variants

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/xtend@4.0.x

To install, run

npx @tessl/cli install tessl/npm-xtend@4.0.0

index.mddocs/

xtend

xtend is a lightweight utility library for extending JavaScript objects by merging properties from multiple source objects. It provides both immutable and mutable variants of object extension, implementing safe property copying with hasOwnProperty checks to prevent prototype pollution.

Package Information

  • Package Name: xtend
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install xtend

Core Imports

const extend = require("xtend");

For the mutable variant:

const mutableExtend = require("xtend/mutable");

Basic Usage

const extend = require("xtend");

// Immutable extension - creates new object
const obj1 = { a: "foo", b: "bar" };
const obj2 = { b: "baz", c: "qux" };
const result = extend(obj1, obj2);
// result: { a: "foo", b: "baz", c: "qux" }
// obj1 and obj2 remain unchanged

// Mutable extension - modifies target object
const mutableExtend = require("xtend/mutable");
const target = { a: "foo" };
mutableExtend(target, { b: "bar" });
// target is now: { a: "foo", b: "bar" }

Capabilities

Immutable Object Extension

Creates a new object by merging properties from source objects without modifying the original objects.

/**
 * Extends objects immutably by creating a new object with merged properties
 * Takes any number of objects as arguments and merges them into a new object
 * @param {...Object} sources - Objects to merge properties from (variadic arguments)
 * @returns {Object} New object with merged properties from all sources
 */
function extend() {}

Key features:

  • Creates a new target object (immutable operation)
  • Accepts any number of source objects as arguments using arguments object
  • Right-most property takes precedence on conflicts
  • Handles null/undefined sources gracefully
  • Uses hasOwnProperty checks to prevent prototype pollution
  • Copies only own enumerable properties

Usage Examples:

const extend = require("xtend");

// Basic merging
const result = extend({ a: "foo" }, { b: "bar" });
// { a: "foo", b: "bar" }

// Property precedence (rightmost wins)
const result = extend({ a: "foo" }, { a: "bar" });
// { a: "bar" }

// Multiple objects
const result = extend({ a: 1 }, { b: 2 }, { c: 3 });
// { a: 1, b: 2, c: 3 }

// Handles special values
const result = extend({ a: undefined }, { b: 0 }, { c: null });
// { a: undefined, b: 0, c: null }

// Graceful handling of null/undefined
const result = extend(null, { a: "foo" }, undefined, { b: "bar" });
// { a: "foo", b: "bar" }

Mutable Object Extension

Modifies the target object in place by copying properties from source objects.

/**
 * Extends the target object mutably by copying properties from source objects
 * Takes target as first argument, then any number of source objects
 * @param {Object} target - Object to modify (receives new properties)
 * @param {...Object} sources - Objects to copy properties from (variadic arguments)
 * @returns {Object} The modified target object
 */
function extend(target) {}

Key features:

  • Modifies the first argument (target) in place
  • Accepts target object as first parameter, sources as remaining parameters using arguments object
  • Right-most property takes precedence on conflicts
  • Handles null/undefined sources gracefully
  • Uses hasOwnProperty checks to prevent prototype pollution
  • Copies only own enumerable properties
  • Returns the modified target object

Usage Examples:

const mutableExtend = require("xtend/mutable");

// Basic mutable extension
const target = { a: "foo" };
mutableExtend(target, { b: "bar" });
console.log(target); // { a: "foo", b: "bar" }

// Property overwriting
const target = { a: "foo" };
mutableExtend(target, { a: "bar" });
console.log(target); // { a: "bar" }

// Multiple sources
const target = { a: 1 };
mutableExtend(target, { b: 2 }, { c: 3 });
console.log(target); // { a: 1, b: 2, c: 3 }

// Return value is the modified target
const target = { a: "foo" };
const result = mutableExtend(target, { b: "bar" });
console.log(result === target); // true

Error Handling

Both variants handle edge cases gracefully:

  • Null/undefined sources: Skipped silently without errors
  • Non-object sources: Properties enumerated if present, skipped if not
  • Prototype pollution: Prevented by hasOwnProperty checks
  • Special values: undefined, null, and 0 values are copied correctly

The functions do not throw exceptions and handle invalid inputs by skipping them silently.