deep-merge-lite-ts
v1.0.0
Published
Fast, zero-dependency, prototype-pollution safe deep object merger with configurable array strategies.
Maintainers
Readme
🔀 deep-merge-lite-ts
Ultra-fast, zero-dependency, prototype-pollution safe deep object merger with configurable array strategies and TypeScript generics.
💡 Why deep-merge-lite-ts?
Merging JavaScript objects is ubiquitous, but popular utilities often bring major downsides:
Object.assign()and spread syntax ({ ...a, ...b }) are shallow, accidentally replacing entire nested configurations.- Popular libraries like
lodash.mergeor heavy merge packages weigh 20 KB+ and frequently suffer from prototype pollution security vulnerabilities.
deep-merge-lite-ts provides:
- ⚡ Zero Dependencies — Pure native TypeScript, minified to under 1.2 KB.
- 🛡️ Prototype Pollution Protection — Blocks
__proto__,constructor, andprototypeproperty injections out of the box. - 🎛️ Configurable Array Strategies — Easily toggle between
replace,concat, and deduplicatedunion. - 🧬 Non-Mutating Deep Cloning — Deep clones nested structures, Dates, and RegExps so your original input objects stay untouched.
- 📦 Dual ESM & CommonJS — Compatible with Node.js, Next.js, Vite, Remix, React Native, and browsers.
📥 Installation
# npm
npm install deep-merge-lite-ts
# pnpm
pnpm add deep-merge-lite-ts
# yarn
yarn add deep-merge-lite-ts
# bun
bun add deep-merge-lite-ts🚀 Usage & Real-World Examples
1. Basic Deep Merge
import { deepMerge } from 'deep-merge-lite-ts';
const defaultSettings = {
theme: 'dark',
server: {
host: 'localhost',
port: 3000,
},
logging: {
level: 'info',
colorize: true,
},
};
const userSettings = {
server: {
port: 8080, // Overrides port without losing host!
},
logging: {
level: 'debug',
},
};
const config = deepMerge(defaultSettings, userSettings);
console.log(config);
// Output:
// {
// theme: 'dark',
// server: { host: 'localhost', port: 8080 },
// logging: { level: 'debug', colorize: true }
// }2. Configurable Array Merging (Union & Concat)
By default, arrays are replaced. Use createMerger() to concatenate or deduplicate arrays:
import { createMerger } from 'deep-merge-lite-ts';
const baseConfig = { roles: ['viewer', 'editor'], permissions: [1, 2] };
const adminConfig = { roles: ['admin', 'editor'], permissions: [3] };
// Deduplicate arrays with 'union'
const unionMerge = createMerger({ arrayMerge: 'union' });
const result = unionMerge(baseConfig, adminConfig);
console.log(result.roles);
// ['viewer', 'editor', 'admin'] (no duplicate 'editor')
// Concatenate all items with 'concat'
const concatMerge = createMerger({ arrayMerge: 'concat' });
console.log(concatMerge(baseConfig, adminConfig).roles);
// ['viewer', 'editor', 'admin', 'editor']3. Merging Multiple Objects in Sequence
import { deepMerge } from 'deep-merge-lite-ts';
const envDefaults = { env: 'development', debug: true };
const fileConfig = { debug: false, db: 'sqlite' };
const cliOverrides = { db: 'postgres' };
const finalAppConfig = deepMerge(envDefaults, fileConfig, cliOverrides);
console.log(finalAppConfig);
// { env: 'development', debug: false, db: 'postgres' }📖 API Reference
deepMerge(target, ...sources)
Deeply merges multiple source objects into a cloned target object.
- Parameters:
target(Record<string, any>): The initial object....sources(Record<string, any>[]): One or more objects to merge overtarget.
- Returns: Cloned, deeply merged object.
createMerger(options)
Factory creating a configured merge function.
Options (MergeOptions):
| Option | Type | Default | Description |
|---|---|---|---|
| arrayMerge | 'replace' \| 'concat' \| 'union' | 'replace' | Array merge algorithm |
| clone | boolean | true | Deep clone nested objects/arrays to prevent mutations |
🛡️ Security: Prototype Pollution Guard
deep-merge-lite-ts guards against malicious property injections:
const maliciousPayload = JSON.parse('{"__proto__": {"isAdmin": true}}');
deepMerge({}, maliciousPayload);
console.log(({} as any).isAdmin); // undefined (safe!)💖 Support the Project
If deep-merge-lite-ts keeps your data structures tidy and safe, please support continued development:
👉 Sponsor @Piratha97 on GitHub Sponsors
📄 License
MIT © 2026 Pirapaakaran Pirathapan
