### Install Mergician via NPM Source: https://github.com/jhildenbiddle/mergician/blob/main/README.md Installs the Mergician library using the Node Package Manager (NPM). This is the standard method for adding JavaScript packages to a project. ```bash npm install mergician ``` -------------------------------- ### Advanced Object Merging with Custom Options (JavaScript) Source: https://github.com/jhildenbiddle/mergician/blob/main/README.md Illustrates advanced object merging using Mergician with custom options. This example shows how to skip specific keys, append and deduplicate arrays, and use a filter function to modify properties during the merge process, resulting in a customized merged object. ```javascript import { mergician } from 'mergician'; const obj1 = { a: [1, 1], b: { c: 1, d: 1 } }; const obj2 = { a: [2, 2], b: { c: 2 } }; const obj3 = { e: 3 }; const mergedObj = mergician({ skipKeys: ['d'], appendArrays: true, dedupArrays: true, filter({ depth, key, srcObj, srcVal, targetObj, targetVal }) { if (key === 'e') { targetObj['hello'] = 'world'; return false; } } })(obj1, obj2, obj3); // Result console.log(mergedObj); ``` -------------------------------- ### Mergician Option: onlyKeys (JavaScript) Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md Example of using the `onlyKeys` option to specify an exclusive list of keys to be merged. Keys not present in the `onlyKeys` array are skipped, both at the top level and within nested objects. ```javascript const obj1 = { a: 1 }; const obj2 = { b: { c: 2 } }; const obj3 = { b: { d: 3 } }; const mergedObj = mergician({ onlyKeys: ['a', 'b', 'c'] })(obj1, obj2, obj3); console.log(mergedObj); // { a: 1, b: { c: 2 } } ``` -------------------------------- ### Initialize Google Analytics with Docsify Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.html This snippet initializes the Google Analytics dataLayer and configures the gtag function for tracking. It's typically used in the head of an HTML document or within a Docsify configuration to set up analytics. ```javascript window.dataLayer = window.dataLayer || []; function gtag() { dataLayer.push(arguments); } gtag('js', new Date()); gtag('config', 'G-V42RNSFL2N'); ``` -------------------------------- ### Key Filtering with onlyKeys and skipKeys in Mergician Source: https://context7.com/jhildenbiddle/mergician/llms.txt Explains and demonstrates the `onlyKeys` and `skipKeys` options in Mergician for precise control over which properties are included or excluded during the merge process. These filters can apply to both top-level and nested object properties. ```javascript import { mergician } from 'mergician'; const obj1 = { a: 1, x: 'skip me' }; const obj2 = { b: { c: 2, y: 'skip nested' } }; const obj3 = { b: { d: 3 } }; // Only merge specific keys const onlyResult = mergician({ onlyKeys: ['a', 'b', 'c'] })(obj1, obj2, obj3); console.log(onlyResult); // { a: 1, b: { c: 2 } } // Skip specific keys const skipResult = mergician({ skipKeys: ['x', 'y'] })(obj1, obj2, obj3); console.log(skipResult); // { a: 1, b: { c: 2, d: 3 } } ``` -------------------------------- ### Basic Cloning and Merging with Mergician Source: https://context7.com/jhildenbiddle/mergician/llms.txt Illustrates the fundamental usage of the `mergician()` function for deep cloning objects and merging multiple objects. It highlights that Mergician creates new objects and performs deep copies of nested structures, ensuring immutability of source objects. ```javascript import { mergician } from 'mergician'; // Clone an object (pass empty object as first argument) const obj1 = { a: [1, 1], b: { c: 1, d: 1 } }; const clonedObj = mergician({}, obj1); console.log(clonedObj); // { a: [1, 1], b: { c: 1, d: 1 } } console.log(clonedObj === obj1); // false (new object) console.log(clonedObj.a === obj1.a); // false (deep clone) console.log(clonedObj.b === obj1.b); // false (deep clone) // Merge multiple objects const obj2 = { b: [2, 2], c: { d: 2 } }; const obj3 = { b: [3, 3], c: { e: 3 } }; const mergedObj = mergician(obj1, obj2, obj3); console.log(mergedObj); // { a: [1, 1], b: [3, 3], c: { d: 2, e: 3 } } ``` -------------------------------- ### Skip Common and Universal Keys in Mergician Source: https://context7.com/jhildenbiddle/mergician/llms.txt Demonstrates how to use `skipCommonKeys` to merge only unique keys and `skipUniversalKeys` to skip keys present in all objects during a Mergician merge. Requires the 'mergician' library. ```javascript import { mergician } from 'mergician'; const obj1 = { a: 1 }; const obj2 = { a: 2, b: { c: 2 } }; const obj3 = { a: 3, b: { c: 3, d: 3 }, e: 3 }; // Skip keys found in multiple objects (merge only unique) const skipCommonResult = mergician({ skipCommonKeys: true })(obj1, obj2, obj3); console.log(skipCommonResult); // { e: 3 } // Skip keys found in ALL objects const skipUniversalResult = mergician({ skipUniversalKeys: true })(obj1, obj2, obj3); console.log(skipUniversalResult); // { b: { d: 3 }, e: 3 } ``` -------------------------------- ### Merge Objects with Default Options (JavaScript) Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md Demonstrates merging multiple objects using Mergician with its default options. The function returns a new merged object, leaving the original objects unmodified. It also shows how to clone an object. ```javascript const obj1 = { a: 1 }; const obj2 = { b: [2, 2], c: { d: 2 } }; const obj3 = { b: [3, 3], c: { e: 3 } }; const clonedObj = mergician({}, obj1); const mergedObj = mergician(obj1, obj2, obj3); console.log(clonedObj); // { a: 1 } console.log(clonedObj === obj1); // false console.log(mergedObj); // { a: 1, b: [3, 3], c: { d: 2, e: 3 } } ``` -------------------------------- ### Import Mergician in JavaScript (ESM, CJS, CDN) Source: https://context7.com/jhildenbiddle/mergician/llms.txt Shows various ways to import the Mergician utility into your JavaScript project. It covers ES module imports, CommonJS module imports, and usage via a Content Delivery Network (CDN) for browser environments. ```javascript // ES module import { mergician } from 'mergician'; // CommonJS module const { mergician } = require('mergician'); // CDN (ES module) import { mergician } from 'https://cdn.jsdelivr.net/npm/mergician@2'; ``` -------------------------------- ### Getter and Setter Handling in Mergician Source: https://context7.com/jhildenbiddle/mergician/llms.txt Explains Mergician's options for handling accessor properties: `invokeGetters` converts getters to static values, and `skipSetters` removes setter functions. Requires the 'mergician' library. ```javascript import { mergician } from 'mergician'; const obj = { a: 1, get b() { return this.a + 1; }, set c(val) { this._c = val; } }; // Default: preserves accessor functions const defaultClone = mergician({}, obj); console.log(defaultClone); // { a: 1, b: [Getter], c: [Setter] } // Invoke getters to get static values const invokedClone = mergician({ invokeGetters: true })({}, obj); console.log(invokedClone); // { a: 1, b: 2, c: [Setter] } // Skip setters entirely const noSetterClone = mergician({ skipSetters: true })({}, obj); console.log(noSetterClone); // { a: 1, b: [Getter] } ``` -------------------------------- ### Basic Object Cloning with Mergician (JavaScript) Source: https://github.com/jhildenbiddle/mergician/blob/main/README.md Demonstrates how to perform a basic deep clone of a JavaScript object using Mergician with default options. It highlights that the cloned object and its nested properties are new instances, ensuring immutability of the original object. ```javascript import { mergician } from 'mergician'; const obj1 = { a: [1, 1], b: { c: 1, d: 1 } }; const clonedObj = mergician({}, obj1); // Results console.log(clonedObj); console.log(clonedObj === obj1); console.log(clonedObj.a === obj1.a); console.log(clonedObj.b === obj1.b); ``` -------------------------------- ### Custom Merging with Mergician Options Source: https://context7.com/jhildenbiddle/mergician/llms.txt Demonstrates how to configure Mergician with specific options to create a custom merge function. This allows for tailored merging behavior, such as skipping keys, appending arrays, and deduplicating array elements. The custom function can be used immediately or stored for reuse. ```javascript import { mergician } from 'mergician'; const obj1 = { a: [1, 1], b: { c: 1, d: 1 } }; const obj2 = { a: [2, 2], b: { c: 2 } }; const obj3 = { e: 3 }; // Immediate invocation with options const mergedObj = mergician({ skipKeys: ['d'], appendArrays: true, dedupArrays: true })(obj1, obj2, obj3); console.log(mergedObj); // { a: [1, 2], b: { c: 2 }, e: 3 } // Create reusable custom merge function const customMerge = mergician({ appendArrays: true, dedupArrays: true }); const result1 = customMerge(obj1, obj2); console.log(result1); // { a: [1, 2], b: { c: 2, d: 1 } } // Custom merge function can also accept new options const result2 = customMerge({ skipKeys: ['c'] })(obj1, obj2); console.log(result2); // { a: [1, 2], b: { d: 1 } } ``` -------------------------------- ### Merge Objects with Custom Options (JavaScript) Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md Shows how to merge objects using Mergician with custom options like `skipKeys` and array manipulation options (`appendArrays`, `dedupArrays`). A custom merge function is created first, then invoked. ```javascript const obj1 = { a: 1 }; const obj2 = { b: [2, 2], c: { d: 2 } }; const obj3 = { b: [3, 3], c: { e: 3 } }; const clonedObj = mergician({ skipKeys: ['c'] })({}, obj2); const mergedObj = mergician({ appendArrays: true, dedupArrays: true })(obj1, obj2, obj3); console.log(clonedObj); // { b: [2, 2] }; console.log(mergedObj); // { a: 1, b: [2, 3], c: { d: 2, e: 3 } } ``` -------------------------------- ### Docsify Configuration for Mergician Project Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.html This JavaScript object configures the Docsify documentation generator for the Mergician project. It sets general options like name and repository, navigation settings, and enables various plugins including search and a custom title persistence function. ```javascript window.$docsify = { // GENERAL // ----------------------------------------------------------------- name: 'Mergician', repo: 'https://github.com/jhildenbiddle/mergician', homepage: 'index.md', loadSidebar: 'sidebar.md', // NAVIGATION // ----------------------------------------------------------------- alias: { '.*?/changelog': 'https://raw.githubusercontent.com/jhildenbiddle/mergician/main/CHANGELOG.md' }, auto2top: true, maxLevel: 2, subMaxLevel: 2, // PLUGINS // ----------------------------------------------------------------- ethicalAds: { eaPublisher: 'jhildenbiddle-github-io' }, executeScript: true, search: { depth: 3, noData: 'No results!', placeholder: 'Search...' }, plugins: [ // Restores initial function persistTitle(hook, vm) { var titleElm = document.querySelector('title'); var rootTitle = titleElm && titleElm.textContent; var pageTitlePrefix = window.$docsify.name ? window.$docsify.name + ' - ' : ''; if (rootTitle) { hook.doneEach(function () { var currentTitle = titleElm.textContent; var isRoot = !/\/[a-z]/.test(location.hash); var newTitle = isRoot ? rootTitle : pageTitlePrefix + currentTitle; titleElm.textContent = newTitle; }); } } ] }; ``` -------------------------------- ### Transform values before merge with beforeEach (JavaScript) Source: https://context7.com/jhildenbiddle/mergician/llms.txt The `beforeEach` callback in Mergician allows inspection and transformation of values before they are merged. You can return a new value to replace the source value, or return nothing to keep the original. This is useful for normalizing data or applying prefixes to strings. ```javascript import { mergician } from 'mergician'; const obj1 = { a: null }; const obj2 = { b: undefined }; const obj3 = { c: { d: '', e: 0 } }; // Normalize non-zero falsy values to false const result = mergician({ beforeEach({ depth, key, srcObj, srcVal, targetObj, targetVal }) { if (srcVal !== 0 && !Boolean(srcVal)) { return false; } } })(obj1, obj2, obj3); console.log(result); // { a: false, b: false, c: { d: false, e: 0 } } // Add prefix to all string values const prefixResult = mergician({ beforeEach({ srcVal }) { if (typeof srcVal === 'string') { return `prefix_${srcVal}`; } } })({ name: 'test', count: 5 }); console.log(prefixResult); // { name: 'prefix_test', count: 5 } ``` -------------------------------- ### Create Reusable Custom Merge Function (JavaScript) Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md Demonstrates creating a named, reusable custom merge function with specific options. This function can then be used multiple times with different objects, applying the predefined options. ```javascript const obj1 = { a: 1 }; const obj2 = { b: [2, 2], c: { d: 2 } }; const obj3 = { b: [3, 3], c: { e: 3 } }; const customMerge = mergician({ appendArrays: true, dedupArrays: true }); const clonedObj = customMerge({}, obj2); const mergedObj1 = customMerge(obj1, obj2, obj3); const mergedObj2 = customMerge({ skipKeys: ['c'] })(obj1, obj2, obj3); console.log(clonedObj); // { b: [2], c: { d: 2 } } console.log(mergedObj1); // { a: 1, b: [2, 3], c: { d: 2, e: 3 } } console.log(mergedObj2); // { a: 1, b: [2, 3] } ``` -------------------------------- ### Mergician Option: skipKeys (JavaScript) Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md Demonstrates the `skipKeys` option, which allows specifying keys to be excluded from the merge process. All other keys are merged, including those in nested objects. ```javascript const obj1 = { a: 1 }; const obj2 = { b: { c: 2 } }; const obj3 = { b: { d: 3 } }; const mergedObj = mergician({ skipKeys: ['c'] })(obj1, obj2, obj3); console.log(mergedObj); // { a: 1, b: { d: 3 } } ``` -------------------------------- ### Define property descriptors with callbacks (JavaScript) Source: https://context7.com/jhildenbiddle/mergician/llms.txt Mergician supports property descriptor merging, enabling you to define properties with specific attributes like writability, enumerability, and configurability using callbacks. You can return descriptor objects from `afterEach` to create read-only properties or from `beforeEach` to define getters and setters. ```javascript import { mergician } from 'mergician'; const obj = { a: 1, b: 2 }; // Create read-only properties using afterEach const result = mergician({ afterEach({ key, mergeVal }) { if (key === 'a') { return { value: mergeVal, writable: false, enumerable: true, configurable: false }; } } })({}, obj); console.log(result.a); // 1 result.a = 100; // Silently fails in non-strict mode console.log(result.a); // 1 (unchanged, property is read-only) // Define getter/setter via descriptor const accessorResult = mergician({ beforeEach({ key, srcVal }) { if (key === 'b') { let _value = srcVal; return { get() { return _value * 2; }, set(v) { _value = v; }, enumerable: true, configurable: true }; } } })({}, obj); console.log(accessorResult.b); // 4 (2 * 2) accessorResult.b = 5; console.log(accessorResult.b); // 10 (5 * 2) ``` -------------------------------- ### Import Mergician in ES Modules Source: https://github.com/jhildenbiddle/mergician/blob/main/README.md Imports the Mergician library using ES module syntax. This is compatible with modern JavaScript environments and bundlers. ```javascript import { mergician } from 'mergician'; ``` -------------------------------- ### Modifying Properties Before Merge with Mergician's beforeEach() Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md The `beforeEach` callback function allows inspection and modification of properties before they are merged. It can return a new value to be merged, a property descriptor for advanced usage with `Object.defineProperty()`, or nothing to use the original value. This is useful for data normalization or transformation. ```javascript const obj1 = { a: null }; const obj2 = { b: undefined }; const obj3 = { c: { d: '', e: 0 } }; const mergedObj = mergician({ // Normalize non-zero "falsy" values to be false beforeEach({ depth, key, srcObj, srcVal, targetObj, targetVal }) { if (srcVal !== 0 && !Boolean(srcVal)) { return false; } } })(obj1, obj2, obj3); console.log(mergedObj); // { a: false, b: false, c: { d: false, e: 0 } } ``` -------------------------------- ### Import Mergician via CDN (ES Module) Source: https://github.com/jhildenbiddle/mergician/blob/main/README.md Imports the Mergician library from a CDN using ES module syntax, with version locking to a specific major version (v2.x.x). This method is useful for direct browser inclusion. ```javascript import { mergician } from 'https://cdn.jsdelivr.net/npm/mergician@2'; ``` -------------------------------- ### Prototype Handling Options in Mergician Source: https://context7.com/jhildenbiddle/mergician/llms.txt Details Mergician's prototype merging options: `hoistProto` moves prototype methods to own properties, `hoistEnumerable` does the same for enumerable prototype properties, and `skipProto` ignores prototypes. Requires the 'mergician' library. ```javascript import { mergician } from 'mergician'; class Person { constructor(name) { this.name = name; } greeting() { return `My name is ${this.name}.`; } } const john = new Person('John'); // Default: prototype is preserved const defaultClone = mergician({}, john); console.log(defaultClone.greeting()); // "My name is John." console.log(defaultClone.hasOwnProperty('greeting')); // false // Hoist prototype methods to own properties const hoistedClone = mergician({ hoistProto: true })({}, john); console.log(hoistedClone.greeting()); // "My name is John." console.log(hoistedClone.hasOwnProperty('greeting')); // true // Skip prototype properties entirely const noProtoClone = mergician({ skipProto: true })({}, john); console.log(noProtoClone); // { name: 'John' } console.log(noProtoClone.greeting); // undefined ``` -------------------------------- ### Filter Callback for Conditional Merging in Mergician Source: https://context7.com/jhildenbiddle/mergician/llms.txt Explains the `filter` callback in Mergician, which allows conditional merging or skipping of properties based on provided criteria. The callback receives detailed information about the property and its context. Requires the 'mergician' library. ```javascript import { mergician } from 'mergician'; const obj1 = { a: true }; const obj2 = { a: false, b: true }; const obj3 = { a: null, b: undefined, c: { d: 0, e: '' } }; // Skip properties with non-zero falsy values const result = mergician({ filter({ depth, key, srcObj, srcVal, targetObj, targetVal }) { // Keep value if truthy OR if it's exactly 0 return Boolean(srcVal) || srcVal === 0; } })(obj1, obj2, obj3); console.log(result); // { a: true, b: true, c: { d: 0 } } // Skip nested properties beyond depth 0 const shallowResult = mergician({ filter({ depth }) { return depth === 0; } })({ a: { b: 1 } }, { a: { c: 2 }, d: 3 }); console.log(shallowResult); // { a: { c: 2 }, d: 3 } ``` -------------------------------- ### afterEach() Callback Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md The `afterEach` callback allows inspection and modification of properties after a merge operation. It can return a new value, a property descriptor, or nothing to use the unmodified value. ```APIDOC ## afterEach() ### Description Callback used for inspecting/modifying properties after merge. If a value is returned, that value will be used as the new merged value. If an `Object` with the shape of a [property descriptor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty) is returned, the object will be used to define the property using [`Object.defineProperty()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty). If no value is returned, the unmodified `mergeVal` will be used. ### Method Callback function ### Parameters #### Arguments - **data** (object) - Object containing merge data - **depth** (number) - The nesting level of the key being processed - **key** (string) - The object key being processed - **mergeVal** (any) - The new merged value - **srcObj** (object) - The object containing the source value - **targetObj** (object) - The new merged object ### Request Example ```javascript const obj1 = { a: 1 }; const obj2 = { b: 2 }; const obj3 = { c: { d: 3, e: true } }; const mergedObj = mergician({ // Add 1 to all numbers afterEach({ depth, key, mergeVal, srcObj, targetObj }) { if (typeof mergeVal === 'number') { return mergeVal + 1; } } })(obj1, obj2, obj3); console.log(mergedObj); // { a: 2, b: 3, c: { d: 4, e: true } } ``` ### Response #### Success Response (Callback Return Value) - **Return Value** (any) - The new merged value, a property descriptor, or undefined. #### Response Example ```json { "a": 2, "b": 3, "c": { "d": 4, "e": true } } ``` ``` -------------------------------- ### Import Mergician ES Module Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md Imports the mergician function using ES module syntax. This is suitable for modern JavaScript environments and projects using module bundlers like Webpack or Rollup. ```javascript // ES module import { mergician } from 'mergician'; ``` -------------------------------- ### Hoist Prototype Properties with Mergician Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md The `hoistProto` option merges custom prototype properties as direct properties of the new merge object. This allows methods and other prototype-defined properties to be directly accessible on the merged object, rather than requiring prototype chain lookup. ```javascript class Person { constructor(name) { this.name = name; // <= Own property } greeting() { // <= Prototype property return `My name is ${this.name}.`; } } const John = new Person('John'); console.log(John.greeting()); // My name is John. console.log(John.hasOwnProperty('greeting')); // false console.log(Object.getPrototypeOf(John).hasOwnProperty('greeting')); // true const cloneObj = mergician({ hoistProto: true })({}, John); console.log(cloneObj.greeting()); // My name is John. console.log(John.hasOwnProperty('greeting')); // true console.log(Object.getPrototypeOf(John).hasOwnProperty('greeting')); // false ``` -------------------------------- ### Import Mergician CommonJS Module Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md Imports the mergician function using CommonJS module syntax. This is typically used in Node.js environments or older JavaScript projects. ```javascript // CommonJS module const { mergician } = require('mergician'); ``` -------------------------------- ### Array Manipulation Options in Mergician Source: https://context7.com/jhildenbiddle/mergician/llms.txt Illustrates Mergician's array manipulation options: `appendArrays` to concatenate arrays, `prependArrays` to add to the beginning, `dedupArrays` to remove duplicates, and `sortArrays` for custom sorting. Requires the 'mergician' library. ```javascript import { mergician } from 'mergician'; const obj1 = { a: [1, 1] }; const obj2 = { a: [2, 2] }; const obj3 = { a: [3, 3] }; // Append arrays (default replaces) const appendResult = mergician({ appendArrays: true })(obj1, obj2, obj3); console.log(appendResult); // { a: [1, 1, 2, 2, 3, 3] } // Prepend arrays const prependResult = mergician({ prependArrays: true })(obj1, obj2, obj3); console.log(prependResult); // { a: [3, 3, 2, 2, 1, 1] } // Combine with dedup and sort const combinedResult = mergician({ appendArrays: true, dedupArrays: true, sortArrays: true })(obj1, obj2, obj3); console.log(combinedResult); // { a: [1, 2, 3] } // Custom sort function (descending) const descResult = mergician({ appendArrays: true, dedupArrays: true, sortArrays: (a, b) => b - a })(obj1, obj2, obj3); console.log(descResult); // { a: [3, 2, 1] } ``` -------------------------------- ### Key Intersection Options (onlyCommonKeys, onlyUniversalKeys) in Mergician Source: https://context7.com/jhildenbiddle/mergician/llms.txt Illustrates the use of `onlyCommonKeys` and `onlyUniversalKeys` options in Mergician to merge properties based on their presence across multiple objects. `onlyCommonKeys` merges properties present in at least two objects, while `onlyUniversalKeys` merges properties present in all provided objects. ```javascript import { mergician } from 'mergician'; const obj1 = { a: 1 }; const obj2 = { a: 2, b: { c: 2 } }; const obj3 = { a: 3, b: { c: 3, d: 3 }, e: 3 }; // Merge only keys found in multiple (2+) objects const commonResult = mergician({ onlyCommonKeys: true })(obj1, obj2, obj3); console.log(commonResult); // { a: 3, b: { c: 3 } } // Merge only keys found in ALL objects const universalResult = mergician({ onlyUniversalKeys: true })(obj1, obj2, obj3); console.log(universalResult); // { a: 3 } ``` -------------------------------- ### Prepend arrays at the beginning when merging with Mergician Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md The `prependArrays` option merges array values by prepending them to the beginning of existing arrays in the target object. Like `appendArrays`, this does not affect nested arrays. This is useful for prioritizing newer data or maintaining a specific order. ```javascript const obj1 = { a: [1, 1] }; const obj2 = { a: [2, 2] }; const obj3 = { a: [3, 3] }; const mergedObj = mergician({ prependArrays: true })(obj1, obj2, obj3); console.log(mergedObj); // { a: [3, 3, 2, 2, 1, 1] } ``` -------------------------------- ### Import Mergician in CommonJS Modules Source: https://github.com/jhildenbiddle/mergician/blob/main/README.md Imports the Mergician library using CommonJS module syntax. This is typically used in Node.js environments. ```javascript const { mergician } = require('mergician'); ``` -------------------------------- ### Skip universal keys during object merging with Mergician Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md The `skipUniversalKeys` option merges keys that are not present in all objects, effectively skipping keys common to all. This option complements `onlyUniversalKeys` by focusing on unique or partially shared properties. Nested object comparisons follow the same rules as other key-based options. ```javascript const obj1 = { a: 1 }; const obj2 = { a: 2, b: { c: 2 } }; const obj3 = { a: 3, b: { c: 3, d: 3 }, e: 3 }; const mergedObj = mergician({ skipUniversalKeys: true })(obj1, obj2, obj3); console.log(mergedObj); // { b: { d: 3 }, e: 3 } ``` -------------------------------- ### Mergician Option: onlyCommonKeys (JavaScript) Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md Illustrates the `onlyCommonKeys` option, which merges only those keys that are present in multiple source objects. Keys appearing in only one object are skipped. This applies recursively to nested objects. ```javascript const obj1 = { a: 1 }; const obj2 = { a: 2, b: { c: 2 } }; const obj3 = { a: 3, b: { c: 3, d: 3 }, e: 3 }; const mergedObj = mergician({ onlyCommonKeys: true })(obj1, obj2, obj3); console.log(mergedObj); // { a: 3, b: { c: 3 } } ``` -------------------------------- ### Skip Prototype Properties with Mergician Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md The `skipProto` option prevents the merging of custom prototype properties. This is useful when you only want to merge the own properties of an object and exclude any methods or properties defined on its prototype chain. ```javascript class Person { constructor(name) { this.name = name; // <= Own property } greeting() { // <= Prototype property return `My name is ${this.name}.`; } } const John = new Person('John'); console.log(John); // { name: 'John' }; console.log(John.greeting()); // My name is John. console.log(Object.getPrototypeOf(John).hasOwnProperty('greeting')); // true const cloneObj = mergician({ skipProto: true })({}, John); console.log(cloneObj); // { name: 'John' }; console.log(cloneObj.greeting()); // undefined console.log(Object.getPrototypeOf(John).hasOwnProperty('greeting')); // false ``` -------------------------------- ### Skip setters during object merging with Mergician Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md The `skipSetters` option, when `true`, prevents Mergician from processing setter methods during the merge operation. This means that properties defined with setters in the source objects will not be included in the merged output. Setters remain accessible to callback functions for direct invocation or modification. ```javascript const obj1 = { firstname: 'John', lastname: 'Smith', set fullname() { return this.fullname = `${this.firstname} ${this.lastname}`; } }; const clonedObj = mergician({ skipSetters: true })({}, obj1); console.log(obj1); // { firstname: 'John', lastname: 'Smith', fullname: [Setter] } console.log(clonedObj); // { firstname: 'John', lastname: 'Smith' } ``` -------------------------------- ### onCircular() Callback Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md The `onCircular` callback handles circular object references during a merge. It can return a new value, a property descriptor, or nothing to use the unmodified source value. ```APIDOC ## onCircular() ### Description Callback used for handling circular object references during merge. If a value is returned, that value will be used as the new value to merge. If an `Object` with the shape of a [property descriptor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty) is returned, the object will be used to define the property using [`Object.defineProperty()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty). If no value is returned, the unmodified `srcVal` will be used. ### Method Callback function ### Parameters #### Arguments - **data** (object) - Object containing merge data - **depth** (number) - The nesting level of the key being processed - **key** (string) - The object key being processed - **srcObj** (object) - The object containing the source value - **srcVal** (any) - The source object's property value - **targetObj** (object) - The new merged object - **targetVal** (any) - The new merged object's current property value ### Request Example ```javascript const circularObj = { a: 1, get b() { return this; } }; const clonedObj = mergician({ // Replace circular object reference with '[Circular]' string onCircular({ depth, key, srcObj, srcVal, targetObj, targetVal }) { return '[Circular]'; } })({}, circularObj); console.log(clonedObj); // { a: 1, b: '[Circular]' } ``` ### Response #### Success Response (Callback Return Value) - **Return Value** (any) - The new value to merge, a property descriptor, or undefined. #### Response Example ```json { "a": 1, "b": "[Circular]" } ``` ``` -------------------------------- ### Handle circular references with onCircular (JavaScript) Source: https://context7.com/jhildenbiddle/mergician/llms.txt The `onCircular` callback in Mergician is designed to manage circular object references during the merge process. It allows you to define how these circularities should be handled, either by returning a replacement value or by allowing Mergician's default behavior to create the circular reference. ```javascript import { mergician } from 'mergician'; // Object with circular reference const circularObj = { a: 1, b: null }; circularObj.b = circularObj; // circular reference // Replace circular references with a string marker const result = mergician({ onCircular({ depth, key, srcObj, srcVal, targetObj, targetVal }) { return '[Circular]'; } })({}, circularObj); console.log(result); // { a: 1, b: '[Circular]' } // Using getter for circular reference const getterCircular = { a: 1, get self() { return this; } }; const getterResult = mergician({ onCircular() { return { type: 'circular-ref' }; } })({}, getterCircular); console.log(getterResult); // { a: 1, self: { type: 'circular-ref' } } ``` -------------------------------- ### Merge only universal keys in objects using Mergician Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md The `onlyUniversalKeys` option merges only those keys that are present in all provided objects. For nested objects, comparisons are made at the same depth and parent key. This is useful for ensuring that only common properties across all objects are included in the final merged object. ```javascript const obj1 = { a: 1 }; const obj2 = { a: 2, b: { c: 2 } }; const obj3 = { a: 3, b: { c: 3, d: 3 }, e: 3 }; const mergedObj = mergician({ onlyUniversalKeys: true })(obj1, obj2, obj3); console.log(mergedObj); // { a: 3 } ``` -------------------------------- ### Invoke getters during object merging with Mergician Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md The `invokeGetters` option, when set to `true`, causes Mergician to invoke any getter methods found in the source objects and merge their returned values. This differs from the default behavior where getters are treated as properties and their descriptor is copied. ```javascript const obj1 = { a: 1, get b() { return this.a + 1; } }; const clonedObj1 = mergician({}, obj1); const clonedObj2 = mergician({ invokeGetters: true })({}, obj1); console.log(obj1); // { a: 1, b: [Getter] } console.log(clonedObj1); // { a: 1, b: [Getter] } console.log(clonedObj2); // { a: 1, b: 2 } ``` -------------------------------- ### Inspect/Modify Properties After Merge with afterEach() Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md The afterEach callback allows inspection and modification of properties after a merge operation. It can return a new value, a property descriptor for Object.defineProperty, or nothing to keep the original merged value. This function is useful for transforming values based on merge context. ```javascript const obj1 = { a: 1 }; const obj2 = { b: 2 }; const obj3 = { c: { d: 3, e: true } }; const mergedObj = mergician({ // Add 1 to all numbers afterEach({ depth, key, mergeVal, srcObj, targetObj }) { if (typeof mergeVal === 'number') { return mergeVal + 1; } } })(obj1, obj2, obj3); console.log(mergedObj); // { a: 2, b: 3, c: { d: 4, e: true } } ``` -------------------------------- ### Conditional Merging with Mergician's filter() Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md The `filter` callback function allows for conditional merging of properties. It receives detailed information about the property being processed and should return a truthy value to merge or a falsy value to skip. This provides fine-grained control over which properties are included in the final merged object. ```javascript const obj1 = { a: true }; const obj2 = { a: false, b: true }; const obj3 = { a: null, b: undefined, c: { d: 0, e: '' } }; const mergedObj = mergician({ // Skip properties with non-zero "falsy" values filter({ depth, key, srcObj, srcVal, targetObj, targetVal }) { return Boolean(srcVal) || srcVal === 0; } })(obj1, obj2, obj3); console.log(mergedObj); // { a: true, b: true, c: { d: 0 } } ``` -------------------------------- ### Skip common keys when merging objects with Mergician Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md The `skipCommonKeys` option merges only keys that appear in a single object, effectively skipping keys present in multiple objects. Similar to `onlyUniversalKeys`, nested object comparisons are depth and parent key specific. This is useful for merging unique properties from different objects. ```javascript const obj1 = { a: 1 }; const obj2 = { a: 2, b: { c: 2 } }; const obj3 = { a: 3, b: { c: 3, d: 3 }, e: 3 }; const mergedObj = mergician({ skipCommonKeys: true })(obj1, obj2, obj3); console.log(mergedObj); // { e: 3 } ``` -------------------------------- ### Modify values after merge with afterEach (JavaScript) Source: https://context7.com/jhildenbiddle/mergician/llms.txt The `afterEach` callback in Mergician is executed after values have been merged, allowing for post-processing. It's useful for modifying merged values, such as incrementing numbers or logging merged keys. The callback receives the merged value and can return a modified value. ```javascript import { mergician } from 'mergician'; const obj1 = { a: 1 }; const obj2 = { b: 2 }; const obj3 = { c: { d: 3, e: true } }; // Add 1 to all numbers after merge const result = mergician({ afterEach({ depth, key, mergeVal, srcObj, targetObj }) { if (typeof mergeVal === 'number') { return mergeVal + 1; } } })(obj1, obj2, obj3); console.log(result); // { a: 2, b: 3, c: { d: 4, e: true } } // Log all merged keys const loggedResult = mergician({ afterEach({ depth, key, mergeVal }) { console.log(`Merged: ${' '.repeat(depth)}${key} = ${JSON.stringify(mergeVal)}`); } })({ a: 1, b: { c: 2 } }); // Output: // Merged: a = 1 // Merged: c = 2 // Merged: b = {"c":2} ``` -------------------------------- ### Hoist Enumerable Properties with Mergician Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md The `hoistEnumerable` option merges enumerable prototype properties as direct properties of the new merge object. This is useful when you want to include properties defined on the prototype chain directly in the merged object, rather than inheriting them. ```javascript const obj = { a: 1 }; console.log(obj); // { a: 1 } console.log(Object.getPrototypeOf(obj)); // { b: 2 } const clonedObj = mergician({ hoistEnumerable: true })({}, obj); console.log(clonedObj); // { a: 1, b: 2 } console.log(Object.getPrototypeOf(clonedObj)); // {} ``` -------------------------------- ### Deduplicate arrays during merge with Mergician Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md The `dedupArrays` option removes duplicate values from arrays during the merge process. When used with `appendArrays`, it ensures that the final merged array contains only unique elements. This option does not modify nested arrays. ```javascript const obj1 = { a: [1, 1] }; const obj2 = { a: [2, 2] }; const obj3 = { a: [3, 3] }; const clonedObj = mergician({ dedupArrays: true })({}, obj1); const mergedObj = mergician({ appendArrays: true, dedupArrays: true })(obj1, obj2, obj3); console.log(clonedObj); // { a: [1] } console.log(mergedObj); // { a: [1, 2, 3] } ``` -------------------------------- ### Sort arrays during merge with Mergician Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md The `sortArrays` option sorts array values in the merged object. By default, it uses the standard JavaScript `Array.prototype.sort()` method. An optional comparison function can be provided to customize the sorting logic, such as for natural or descending order. Nested arrays are not affected. ```javascript const obj1 = { a: [1, 4] }; const obj2 = { a: [2, 5] }; const obj3 = { a: [3, 6] }; const mergedAscending = mergician({ appendArrays: true, sortArrays: true })(obj1, obj2, obj3); const mergedDescending = mergician({ appendArrays: true, sortArrays(a, b) { return b - a; } })(obj1, obj2, obj3); console.log(mergedAscending); // { a: [1, 2, 3, 4, 5, 6] } console.log(mergedDescending); // { a: [6, 5, 4, 3, 2, 1] } ``` -------------------------------- ### Append arrays at the end when merging with Mergician Source: https://github.com/jhildenbiddle/mergician/blob/main/docs/index.md The `appendArrays` option merges array values by appending them to the end of existing arrays in the target object. This option does not modify arrays nested within other arrays. It's useful for combining sequential data from multiple sources. ```javascript const obj1 = { a: [1, 1] }; const obj2 = { a: [2, 2] }; const obj3 = { a: [3, 3] }; const mergedObj = mergician({ appendArrays: true })(obj1, obj2, obj3); console.log(mergedObj); // { a: [1, 1, 2, 2, 3, 3] } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.