Lightweight utility library for extending JavaScript objects with immutable and mutable variants
npx @tessl/cli install tessl/npm-xtend@4.0.0xtend 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.
npm install xtendconst extend = require("xtend");For the mutable variant:
const mutableExtend = require("xtend/mutable");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" }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:
arguments objectUsage 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" }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:
arguments objectUsage 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); // trueBoth variants handle edge cases gracefully:
The functions do not throw exceptions and handle invalid inputs by skipping them silently.