ngraph.merge

1.0.0 · abandoned · verified Sun Apr 19

ngraph.merge is a minimalist JavaScript utility designed to extend target objects with properties from source objects without introducing external dependencies. It primarily operates as a shallow merge. Its key differentiator is a specific, somewhat unconventional, conflict resolution strategy: properties from the source will overwrite existing target properties only if their types differ. If the target already contains a property with the same name and type, it is *not* overwritten by the source property. The package is currently at version 1.0.0, with its last commit in 2017, indicating it is no longer actively maintained and has an abandoned release cadence. Its zero-dependency footprint makes it suitable for environments where bundle size and dependency chain control are critical, though its unique merge logic requires careful consideration.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic usage and highlights the specific shallow merge logic and overwrite behavior based on type matching.

const merge = require('ngraph.merge');

const defaults = {
  name: 'Guest',
  age: 30,
  isActive: true,
  settings: { theme: 'dark' }
};

const userOptions = {
  name: 'Alice',       // Same type as default, will not overwrite
  age: 'thirty',      // Different type (string vs number), will overwrite
  isAdmin: false,
  settings: { debug: true } // New property for settings, will overwrite entire settings object if it's not a deep merge
};

const finalOptions = {};
merge(finalOptions, defaults);
console.log('1. After merging with defaults:', finalOptions);
// Expected: { name: 'Guest', age: 30, isActive: true, settings: { theme: 'dark' } }

merge(finalOptions, userOptions);
console.log('2. After merging with user options:', finalOptions);
// Expected: { name: 'Guest', age: 'thirty', isActive: true, isAdmin: false, settings: { theme: 'dark' } }
// Note: `name` remains 'Guest' because types match. `age` becomes 'thirty' due to type mismatch.
// `settings` in `userOptions` will not overwrite because the `settings` property in `finalOptions` already exists and has the same object type.

console.log('Final Name:', finalOptions.name);     // Expected: 'Guest'
console.log('Final Age:', finalOptions.age);       // Expected: 'thirty'
console.log('Final Is Admin:', finalOptions.isAdmin); // Expected: false
console.log('Final Settings:', finalOptions.settings); // Expected: { theme: 'dark' } - not affected by userOptions.settings because it's a shallow merge and type match.

view raw JSON →