### JFather Installation Examples Source: https://github.com/regseb/jfather/blob/main/README.md Shows how to import JFather in Node.js, browsers, and Deno environments. ```javascript // Node.js and Bun (after `npm install jfather`): import JFather from "jfather"; ``` ```javascript // Browsers: import JFather from "https://esm.sh/jfather@0"; import JFather from "https://cdn.jsdelivr.net/npm/jfather@0"; import JFather from "https://unpkg.com/jfather@0"; ``` ```javascript // Deno (after `deno add jsr:@regseb/jfather`): import JFather from "jsr:@regseb/jfather"; ``` -------------------------------- ### JFather Core Functionality Examples Source: https://github.com/regseb/jfather/blob/main/README.md Demonstrates merging, extending, and overriding JSON objects using JFather. Includes examples of deep merging, extending with remote URLs, and array manipulation. ```javascript import JFather from "jfather"; // Merge two objects. const merged = JFather.merge( { "foo": "a", "bar": "alpha" }, { "foo": "b", "baz": "beta" } ); console.log(merged); // { "foo": "b", "bar": "alpha", "baz": "beta" } // Extend an object. const extended = await JFather.extend({ "$extends": "https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json#members[1]", "age": 34, "quote": "With great fist comes great KO" }); console.log(extended); // { // "name": "Madame Uppercut", // "age": 34, // "secretIdentity": "Jane Wilson", // "powers": [ // "Million tonne punch", // "Damage resistance", // "Superhuman reflexes" // ], // "quote": "With great fist comes great KO" // } // Override arrays of an object. const overridden = JFather.merge( { "foo": ["a", "alpha"] }, { "$foo[0]": "A", "$foo[]": ["BETA"] } ); console.log(overridden); // { // "foo": ["A", "alpha", "BETA"] // } // Extend, merge and override an object. const allIn = await JFather.extend({ "$extends": "https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json#members[0]", "age": 27, "$powers[2]": "Atomic breath", "$powers[]": ["Matter Creation", "Reality Warping"], "quote": "I'm no God. I'm not even a man. I'm just Molecule Man." }); console.log(allIn); // { // "name": "Molecule Man", // "age": 27, // "secretIdentity": "Dan Jukes", // "powers": [ // "Radiation resistance", // "Turning tiny", // "Atomic breath", // "Matter Creation", // "Reality Warping // ], // "quote": "I'm no God. I'm not even a man. I'm just Molecule Man." // } ``` -------------------------------- ### Load JSON with Custom Request for Local Files Source: https://context7.com/regseb/jfather/llms.txt Use JFather.load with a custom `request` function to load JSON from local file paths. This example uses Node.js `readFile`. ```javascript // Custom request function for local files or custom fetching import { readFile } from "node:fs/promises"; const localOptions = { request: async (url) => { const filePath = new URL(url).pathname; const content = await readFile(filePath, "utf-8"); return JSON.parse(content); } }; const localConfig = await JFather.load("file:///app/config.json", localOptions); ``` -------------------------------- ### Combine Array Merge and Append Operations Source: https://github.com/regseb/jfather/blob/main/README.md You can combine index-based merging and appending within the same operation. This example merges the first element of the parent's 'foo' array and appends to the 'bar' sub-array. ```javascript const parent = { "foo": [{ "bar": ["a"] }] }; const child = { "$foo[0]": { "$bar[]": ["b", "c"] } }; console.log(JFather.merge(parent, child)); // { // "foo": [{ // "bar": ["a", "b", "c"] // }] // } ``` -------------------------------- ### Load JSON File from URL Source: https://context7.com/regseb/jfather/llms.txt Use JFather.load to fetch a JSON file from a URL, automatically extending and merging its contents. This is a shortcut for fetching and extending. ```javascript import JFather from "jfather"; // Load and process a JSON file const config = await JFather.load("https://example.com/app-config.json"); console.log(config); ``` -------------------------------- ### JFather.load(url, [options]) Source: https://context7.com/regseb/jfather/llms.txt Loads a JSON file from a URL, then extends, merges, and overrides it. Supports URL hash syntax for specific sub-object loading and custom request functions. ```APIDOC ## JFather.load(url, [options]) ### Description Loads a JSON file from a URL, then extends, merges, and overrides it. This is a convenience method that combines fetching and extending in one call. Supports URL hash syntax to load specific sub-objects. ### Parameters #### Path Parameters - **url** (string) - Required - The URL of the JSON file to load. - **options** (object) - Optional - Configuration options, including a custom `request` function. - **request** (function) - Optional - A function to handle fetching remote JSON. It should accept a URL and return a Promise that resolves with the JSON content. ### Request Example ```javascript import JFather from "jfather"; import { readFile } from "node:fs/promises"; // Load and process a JSON file const config = await JFather.load("https://example.com/app-config.json"); console.log(config); // Load a specific sub-object using hash const dbConfig = await JFather.load("https://example.com/config.json#database"); console.log(dbConfig); // Load with array index in hash const firstUser = await JFather.load("https://api.com/users.json#users[0]"); console.log(firstUser); // Load with nested path const nestedValue = await JFather.load("https://example.com/config.json#settings.theme.colors"); // Custom request function for local files or custom fetching const localOptions = { request: async (url) => { const filePath = new URL(url).pathname; const content = await readFile(filePath, "utf-8"); return JSON.parse(content); } }; const localConfig = await JFather.load("file:///app/config.json", localOptions); // Load with authentication headers const authOptions = { request: async (url) => { const response = await fetch(url, { headers: { "X-API-Key": "secret-key" } }); return response.json(); } }; const secureConfig = await JFather.load("https://secure-api.com/config.json", authOptions); ``` ### Response #### Success Response (200) - **object** (object) - The loaded and processed JSON object. ### Response Example ```json { "app": "MyApp", "version": "1.0", "debug": false } ``` ``` -------------------------------- ### URL Hash Path Syntax for Remote Resources Source: https://context7.com/regseb/jfather/llms.txt Enables precise extraction of specific values from remote JSON files using URL fragments (hashes) for property access and array indexing. ```APIDOC ## URL Hash Path Syntax ### Description The URL hash syntax allows extracting specific values from remote JSON files when using `$extends` or `load()`. Supports property access, array indexing, and nested paths. ### Method `JFather.load(url)` or used within `$extends` properties. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```javascript import JFather from "jfather"; // Assume https://example.com/data.json contains: // { // "config": { // "database": { "host": "localhost", "port": 5432 }, // "servers": [ // { "name": "web1", "ip": "10.0.0.1" }, // { "name": "web2", "ip": "10.0.0.2" } // ] // }, // "metadata": { "version": "2.0" } // } // Access property: #config const config = await JFather.load("https://example.com/data.json#config"); // Access nested property: #config.database const db = await JFather.load("https://example.com/data.json#config.database"); // Access array element: #config.servers[0] const server1 = await JFather.load("https://example.com/data.json#config.servers[0]"); // Access property within array element: #config.servers[1].name const serverName = await JFather.load("https://example.com/data.json#config.servers[1].name"); // Using hash in $extends const myConfig = await JFather.extend({ "$extends": "https://example.com/data.json#config.database", "port": 5433, "ssl": true }); ``` ### Response #### Success Response (200) - **any** - The extracted value from the specified URL and hash path. #### Response Example ```json { "host": "localhost", "port": 5432 } ``` ``` -------------------------------- ### Load JSON with Custom Request for Authentication Source: https://context7.com/regseb/jfather/llms.txt Configure JFather.load to include authentication headers in requests by providing a custom `request` function that utilizes `fetch` with specific headers. ```javascript // Load with authentication headers const authOptions = { request: async (url) => { const response = await fetch(url, { headers: { "X-API-Key": "secret-key" } }); return response.json(); } }; const secureConfig = await JFather.load( "https://secure-api.com/config.json", authOptions ); ``` -------------------------------- ### JFather.load Source: https://github.com/regseb/jfather/blob/main/README.md Loads a JSON object from a URL, then extends, merges, and overrides its properties. ```APIDOC ## JFather.load(url, [options]) ### Description Load from a `url`, extend, merge and override. ### Method `JFather.load` ### Parameters #### Path Parameters - **url** (`` | ``) - Required - The string containing the URL of a JSON file. - **options** (``) - Optional - Configuration options. - **request** (``) - Optional - The function for getting a JSON object remotely. By default, the object is got with `fetch()` and `Response.json()`. ### Response #### Success Response (200) - **loadedObject** (``) - A promise with the loaded object. ### Request Example ```json { "url": "https://example.com/data.json", "options": { "request": "(url) => fetch(url).then(res => res.json())" } } ``` ### Response Example ```json { "data": "some data" } ``` ``` -------------------------------- ### JFather.parse() - JSON Parsing and Extension Source: https://context7.com/regseb/jfather/llms.txt Parses a JSON string, applying any `$extends` properties to merge and override configurations from local or remote sources. ```APIDOC ## JFather.parse(text, [options]) ### Description Parses a JSON string and then extends, merges, and overrides any `$extends` properties found within. Combines `JSON.parse()` with the extend functionality. ### Method `JFather.parse(text, [options])` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **text** (string) - Required - The JSON string to parse. - **options** (object) - Optional - Configuration options, including a custom `request` function. - **request** (function) - Optional - A function to handle fetching remote JSON content. ### Request Example ```javascript import JFather from "jfather"; // Parse a simple JSON string const simpleResult = await JFather.parse('{ "name": "MyApp", "version": "1.0" }'); console.log(simpleResult); // Parse JSON with $extends property const jsonText = `{ "$extends": "https://example.com/base-config.json", "environment": "production", "debug": false }`; const extendedResult = await JFather.parse(jsonText); console.log(extendedResult); // Parse with override syntax const overrideText = `{ "$extends": "https://example.com/defaults.json", "$plugins[0]": "custom-plugin", "$plugins[]": ["extra-plugin"] }`; const overrideResult = await JFather.parse(overrideText); // Parse with custom request function const mockData = { "https://api.com/schema.json": { type: "object", required: ["id"] } }; const mockOptions = { request: async (url) => mockData[url] || {} }; const schemaText = `{ "$extends": "https://api.com/schema.json", "properties": { "id": { "type": "string" } } }`; const schema = await JFather.parse(schemaText, mockOptions); console.log(schema); ``` ### Response #### Success Response (200) - **object** - The parsed and extended JSON object. #### Response Example ```json { "name": "MyApp", "version": "1.0" } ``` ``` -------------------------------- ### JFather.merge: Array Replacement Behavior Source: https://context7.com/regseb/jfather/llms.txt Demonstrates that arrays are replaced entirely by default in JFather.merge unless override syntax is used. ```javascript // Arrays replace entirely without override syntax const arrayParent = { items: [1, 2, 3] }; const arrayChild = { items: [4, 5] }; console.log(JFather.merge(arrayParent, arrayChild)); // { items: [4, 5] } ``` -------------------------------- ### Load Specific Sub-object from URL with Hash Source: https://context7.com/regseb/jfather/llms.txt Fetch only a specific sub-object from a remote JSON file using URL hash syntax with JFather.load (e.g., #database). ```javascript // Load a specific sub-object using hash const dbConfig = await JFather.load("https://example.com/config.json#database"); console.log(dbConfig); // Returns only the "database" property from the JSON ``` -------------------------------- ### JFather.extend(obj, [options]) Source: https://context7.com/regseb/jfather/llms.txt Recursively extends an object by resolving `$extends` properties pointing to remote JSON files. Supports URL hash syntax for sub-object extraction and custom request functions. ```APIDOC ## JFather.extend(obj, [options]) ### Description Extends an object by resolving all `$extends` properties recursively. Each `$extends` property should contain a URL pointing to a remote JSON file. The remote file is fetched, and its contents are merged with the current object. Supports URL hash syntax to extract specific sub-objects from the remote file. ### Parameters #### Path Parameters - **obj** (object) - Required - The object to extend. - **options** (object) - Optional - Configuration options, including a custom `request` function. - **request** (function) - Optional - A function to handle fetching remote JSON. It should accept a URL and return a Promise that resolves with the JSON content. ### Request Example ```javascript import JFather from "jfather"; // Basic extend from remote JSON const config = { "$extends": "https://example.com/base.json", "version": "2.0", "debug": true }; const result = await JFather.extend(config); console.log(result); // Extend with URL hash to extract sub-object const adminConfig = { "$extends": "https://example.com/data.json#users.admin", "permissions": ["read", "write", "delete"] }; const adminResult = await JFather.extend(adminConfig); console.log(adminResult); // Extend array element by index const heroConfig = { "$extends": "https://api.com/heroes.json#members[1]", "power": "super strength", "active": true }; const heroResult = await JFather.extend(heroConfig); console.log(heroResult); // Nested extends within sub-objects const nestedConfig = { "app": "MyApp", "database": { "$extends": "https://example.com/db-defaults.json", "host": "custom-host.com" } }; const nestedResult = await JFather.extend(nestedConfig); // Custom request function for non-HTTP sources or authentication const customOptions = { request: async (url) => { const response = await fetch(url, { headers: { "Authorization": "Bearer token123" } }); return response.json(); } }; const authResult = await JFather.extend(config, customOptions); ``` ### Response #### Success Response (200) - **object** (object) - The extended JSON object. ### Response Example ```json { "name": "BaseApp", "version": "2.0", "debug": true } ``` ``` -------------------------------- ### JFather Merge with Simple Objects Source: https://github.com/regseb/jfather/blob/main/README.md Shows how JFather.merge handles properties in simple objects. Properties in the child object overwrite properties with the same name in the parent object. ```javascript const parent = { "foo": "alpha", "bar": "ALPHA" }; const child = { "foo": "beta", "baz": "BETA" }; console.log(JFather.merge(parent, child)); // { "foo": "beta", "bar": "ALPHA", "baz": "BETA" } ``` -------------------------------- ### Load remote JSON with URL Hash Path Syntax Source: https://context7.com/regseb/jfather/llms.txt Loads JSON data from a remote URL and extracts specific values using URL hash path syntax. Supports property access, array indexing, and nested paths. ```javascript import JFather from "jfather"; // Assume https://example.com/data.json contains: // { // "config": { // "database": { "host": "localhost", "port": 5432 }, // "servers": [ // { "name": "web1", "ip": "10.0.0.1" }, // { "name": "web2", "ip": "10.0.0.2" } // ] // }, // "metadata": { "version": "2.0" } // } // Access property: #config const config = await JFather.load("https://example.com/data.json#config"); // { database: {...}, servers: [...] } ``` ```javascript // Access nested property: #config.database const db = await JFather.load("https://example.com/data.json#config.database"); // { host: "localhost", port: 5432 } ``` ```javascript // Access array element: #config.servers[0] const server1 = await JFather.load( "https://example.com/data.json#config.servers[0]" ); // { name: "web1", ip: "10.0.0.1" } ``` ```javascript // Access property within array element: #config.servers[1].name const serverName = await JFather.load( "https://example.com/data.json#config.servers[1].name" ); // "web2" ``` ```javascript // Using hash in $extends const myConfig = await JFather.extend({ "$extends": "https://example.com/data.json#config.database", "port": 5433, "ssl": true }); // { host: "localhost", port: 5433, ssl: true } ``` -------------------------------- ### JFather Array Override: Combine Replace and Append Source: https://context7.com/regseb/jfather/llms.txt Shows how to simultaneously replace an element at a specific index and append new elements to an array in a single merge. ```javascript // Combine replace and append operations const parent3 = { items: ["a", "b", "c"] }; const child3 = { "$items[0]": "A", "$items[]": ["d", "e"] }; console.log(JFather.merge(parent3, child3)); // { items: ["A", "b", "c", "d", "e"] } ``` -------------------------------- ### Extend Object with Remote JSON Source: https://context7.com/regseb/jfather/llms.txt Use JFather.extend to merge a local configuration object with data fetched from a remote JSON URL. Supports basic URL extension and overriding properties. ```javascript import JFather from "jfather"; // Basic extend from remote JSON // Assume https://example.com/base.json contains: { "name": "BaseApp", "version": "1.0" } const config = { "$extends": "https://example.com/base.json", "version": "2.0", "debug": true }; const result = await JFather.extend(config); console.log(result); // { name: "BaseApp", version: "2.0", debug: true } ``` -------------------------------- ### JFather.merge: Basic Object Merging Source: https://context7.com/regseb/jfather/llms.txt Demonstrates basic deep merging of JSON objects where child properties override parent properties. ```javascript import JFather from "jfather"; // Basic object merge - child properties override parent const parent = { foo: "alpha", bar: "ALPHA" }; const child = { foo: "beta", baz: "BETA" }; const result = JFather.merge(parent, child); console.log(result); // { foo: "beta", bar: "ALPHA", baz: "BETA" } ``` -------------------------------- ### JFather.extend Source: https://github.com/regseb/jfather/blob/main/README.md Extends an object by merging and overriding its properties, potentially fetching extended objects remotely. ```APIDOC ## JFather.extend(obj, [options]) ### Description Extend `obj`, merge and override. ### Method `JFather.extend` ### Parameters #### Path Parameters - **obj** (``) - Required - The object with any `$extends` properties. - **options** (``) - Optional - Configuration options. - **request** (``) - Optional - The function for getting a JSON object remotely. By default, the object is got with `fetch()` and `Response.json()`. ### Response #### Success Response (200) - **extendedObject** (``) - A promise with the extended object. ### Request Example ```json { "obj": { "$extends": "./base.json", "name": "Jane" }, "options": { "request": "(url) => fetch(url).then(res => res.json())" } } ``` ### Response Example ```json { "name": "Jane", "version": "1.0" } ``` ``` -------------------------------- ### Extend Object with URL Hash for Sub-object Source: https://context7.com/regseb/jfather/llms.txt Fetch and merge a specific sub-object from a remote JSON file by appending a URL hash (e.g., #users.admin). This allows for targeted data inclusion. ```javascript // Extend with URL hash to extract sub-object // Assume https://example.com/data.json contains: // { "users": { "admin": { "role": "admin", "permissions": ["read", "write"] } } } const adminConfig = { "$extends": "https://example.com/data.json#users.admin", "permissions": ["read", "write", "delete"] }; const adminResult = await JFather.extend(adminConfig); console.log(adminResult); // { role: "admin", permissions: ["read", "write", "delete"] } ``` -------------------------------- ### Load Array Element by Index from URL Source: https://context7.com/regseb/jfather/llms.txt Retrieve a specific element from a JSON array at a remote URL by specifying its index in the URL hash with JFather.load (e.g., #users[0]). ```javascript // Load with array index in hash const firstUser = await JFather.load("https://api.com/users.json#users[0]"); console.log(firstUser); // Returns the first element of the "users" array ``` -------------------------------- ### JFather.parse Source: https://github.com/regseb/jfather/blob/main/README.md Parses a JSON string, then extends, merges, and overrides its properties. ```APIDOC ## JFather.parse(text, [options]) ### Description Parse a `text`, extend, merge and override. ### Method `JFather.parse` ### Parameters #### Path Parameters - **text** (``) - Required - The string containing a JSON object. - **options** (``) - Optional - Configuration options. - **request** (``) - Optional - The function for getting a JSON object remotely. By default, the object is got with `fetch()` and `Response.json()`. ### Response #### Success Response (200) - **parsedObject** (``) - A promise with the parsed object. ### Request Example ```json { "text": "{\"name\": \"Alice\", \"$extends\": \"./config.json\"}", "options": { "request": "(url) => fetch(url).then(res => res.json())" } } ``` ### Response Example ```json { "name": "Alice", "setting": "value" } ``` ``` -------------------------------- ### Extend Object with Remote JSON Source: https://github.com/regseb/jfather/blob/main/README.md Use the $extends property to link to a remote JSON file. The remote content will be merged with the current object. If the child object is empty, the result will be the parent object. ```javascript // https://example.com/parent.json // { "foo": 42 } const obj = { "$extends": "https://example.com/parent.json" }; console.log(await JFather.extend(obj)); // { "foo": 42 } ``` -------------------------------- ### JFather.merge: Non-Object Value Replacement Source: https://context7.com/regseb/jfather/llms.txt Shows how JFather.merge handles non-object types, where the child value replaces the parent value. ```javascript // Non-object values - child replaces parent console.log(JFather.merge(1, 2)); // 2 console.log(JFather.merge("foo", { bar: "baz" })); // { bar: "baz" } console.log(JFather.merge({ foo: "bar" }, "baz")); // "baz" ``` -------------------------------- ### Array Override with $property[index] and $property[] Source: https://context7.com/regseb/jfather/llms.txt Allows specific array element replacement using `$property[index]` or appending elements using `$property[]` during merge operations. ```APIDOC ## Array Override with $property[index] and $property[] ### Description Override specific array elements using `$property[index]` syntax or append values using `$property[]` syntax. This allows fine-grained control over array merging without replacing the entire array. ### Method `JFather.merge` (with override syntax in child object) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```javascript import JFather from "jfather"; // Replace specific array element at index const parent1 = { colors: ["red", "green", "blue"] }; const child1 = { "$colors[1]": "yellow" }; console.log(JFather.merge(parent1, child1)); // { colors: ["red", "yellow", "blue"] } // Append values to array const parent2 = { tags: ["javascript", "json"] }; const child2 = { "$tags[]": ["utility", "merge"] }; console.log(JFather.merge(parent2, child2)); // { tags: ["javascript", "json", "utility", "merge"] } // Combine replace and append operations const parent3 = { items: ["a", "b", "c"] }; const child3 = { "$items[0]": "A", "$items[]": ["d", "e"] }; console.log(JFather.merge(parent3, child3)); // { items: ["A", "b", "c", "d", "e"] } // Deep override with nested objects inside arrays const parent4 = { users: [ { name: "Alice", role: "admin" }, { name: "Bob", role: "user" } ] }; const child4 = { "$users[1]": { role: "moderator", verified: true } }; console.log(JFather.merge(parent4, child4)); // { // users: [ // { name: "Alice", role: "admin" }, // { name: "Bob", role: "moderator", verified: true } // ] // } // Nested array override const parent5 = { config: [{ plugins: ["plugin-a", "plugin-b"] }] }; const child5 = { "$config[0]": { "$plugins[]": ["plugin-c"] } }; console.log(JFather.merge(parent5, child5)); // { config: [{ plugins: ["plugin-a", "plugin-b", "plugin-c"] }] } ``` ### Response #### Success Response (200) - **mergedObject** (object) - The JSON object with specified array elements overridden or appended. #### Response Example ```json { "colors": ["red", "yellow", "blue"] } ``` ``` -------------------------------- ### JFather Merge with Arrays Source: https://github.com/regseb/jfather/blob/main/README.md Demonstrates JFather.merge with arrays. By default, arrays are treated as values, and the child array replaces the parent array. ```javascript const parent = { "foo": [1, 10, 11] }; const child = { "foo": [2, 20, 22] }; console.log(JFather.merge(parent, child)); // { "foo": [2, 20, 22] } ``` -------------------------------- ### Extend Object with Custom Request Function Source: https://context7.com/regseb/jfather/llms.txt Provide a custom `request` function in the options to JFather.extend for fetching data from non-HTTP sources or to include authentication headers. ```javascript // Custom request function for non-HTTP sources or authentication const customOptions = { request: async (url) => { const response = await fetch(url, { headers: { "Authorization": "Bearer token123" } }); return response.json(); } }; const authResult = await JFather.extend(config, customOptions); ``` -------------------------------- ### Load Nested Path from URL Source: https://context7.com/regseb/jfather/llms.txt Access deeply nested properties within a remote JSON file by providing a nested path in the URL hash when using JFather.load (e.g., #settings.theme.colors). ```javascript // Load with nested path const nestedValue = await JFather.load( "https://example.com/config.json#settings.theme.colors" ); ``` -------------------------------- ### JFather Array Override: Deep Override in Nested Objects Source: https://context7.com/regseb/jfather/llms.txt Demonstrates overriding properties of objects within an array using the `$property[index]` syntax. ```javascript // Deep override with nested objects inside arrays const parent4 = { users: [ { name: "Alice", role: "admin" }, { name: "Bob", role: "user" } ] }; const child4 = { "$users[1]": { role: "moderator", verified: true } }; console.log(JFather.merge(parent4, child4)); // { // users: [ // { name: "Alice", role: "admin" }, // { name: "Bob", role: "moderator", verified: true } // ] // } ``` -------------------------------- ### Parse JSON with $extends Source: https://context7.com/regseb/jfather/llms.txt Parses a JSON string, merging it with content from a remote URL specified in the $extends property. Handles basic JSON parsing and inheritance. ```javascript import JFather from "jfather"; // Parse a simple JSON string const simpleResult = await JFather.parse('{ "name": "MyApp", "version": "1.0" }'); console.log(simpleResult); // { name: "MyApp", version: "1.0" } ``` ```javascript const jsonText = `{ "$extends": "https://example.com/base-config.json", "environment": "production", "debug": false }`; const extendedResult = await JFather.parse(jsonText); console.log(extendedResult); // Merges base-config.json with the local properties ``` ```javascript const overrideText = `{ "$extends": "https://example.com/defaults.json", "$plugins[0]": "custom-plugin", "$plugins[]": ["extra-plugin"] }`; const overrideResult = await JFather.parse(overrideText); ``` ```javascript const mockData = { "https://api.com/schema.json": { type: "object", required: ["id"] } }; const mockOptions = { request: async (url) => mockData[url] || {} }; const schemaText = `{ "$extends": "https://api.com/schema.json", "properties": { "id": { "type": "string" } } }`; const schema = await JFather.parse(schemaText, mockOptions); console.log(schema); // { type: "object", required: ["id"], properties: { id: { type: "string" } } } ``` -------------------------------- ### Append to Arrays Source: https://github.com/regseb/jfather/blob/main/README.md Use the $propertyName[] syntax to append values from the child object to an existing array in the parent object. This is useful for adding multiple items to an array. ```javascript const parent = { "foo": ["a", "Alpha"] }; const child = { "$foo[]": ["b", "Beta"] }; console.log(JFather.merge(parent, child)); // { "foo": ["a", "Alpha", "b", "Beta"] } ``` -------------------------------- ### Extend Object with Specific Parent Sub-Object Source: https://github.com/regseb/jfather/blob/main/README.md Specify a path in the URL hash to retrieve a specific sub-object from the parent JSON for merging. This allows targeted extension of nested properties. ```javascript // https://example.com/parent.json // { // "foo": { "bar": [1, 2], "baz": "a" }, // "qux": true // } const obj = { "$extends": "https://example.com/parent.json#foo" }; console.log(await JFather.extend(obj)); // { "bar": [1, 2], "baz": "a" } ``` -------------------------------- ### Extend Object with Array Index in URL Hash Source: https://context7.com/regseb/jfather/llms.txt Extend an object using a specific element from a remote JSON array by specifying its index in the URL hash (e.g., #members[1]). ```javascript // Extend array element by index // Assume https://api.com/heroes.json contains: // { "members": [{ "name": "Hero1" }, { "name": "Hero2", "power": "strength" }] } const heroConfig = { "$extends": "https://api.com/heroes.json#members[1]", "power": "super strength", "active": true }; const heroResult = await JFather.extend(heroConfig); console.log(heroResult); // { name: "Hero2", power: "super strength", active: true } ``` -------------------------------- ### Merge Arrays by Index Source: https://github.com/regseb/jfather/blob/main/README.md To merge specific elements of an array, use the $propertyName[index] syntax. This allows you to update an element at a particular index without overwriting the entire array. ```javascript const parent = { "foo": ["a", "Alpha"] }; const child = { "$foo[0]": "B" }; console.log(JFather.merge(parent, child)); // { "foo": ["B", "Alpha"] } ``` -------------------------------- ### JFather Array Override: Append Elements Source: https://context7.com/regseb/jfather/llms.txt Uses `$property[]` syntax to append new values to an existing array during a merge operation. ```javascript // Append values to array const parent2 = { tags: ["javascript", "json"] }; const child2 = { "$tags[]": ["utility", "merge"] }; console.log(JFather.merge(parent2, child2)); // { tags: ["javascript", "json", "utility", "merge"] } ``` -------------------------------- ### JFather Recursive Merge Source: https://github.com/regseb/jfather/blob/main/README.md Illustrates JFather.merge with nested objects. The merge operation is performed recursively on sub-objects. ```javascript const parent = { "foo": { "bar": 1, "baz": 2 }, "qux": "a" }; const child = { "foo": { "bar": 10, "quux": 20 }, "corge": "b" }; console.log(JFather.merge(parent, child)); // { // "foo": { "bar": 10, "baz": 2, "quux": 20 }, // "qux": "a", // "corge": "b" // } ``` -------------------------------- ### JFather Array Override: Replace Element by Index Source: https://context7.com/regseb/jfather/llms.txt Uses `$property[index]` syntax to replace a specific element within an array during a merge operation. ```javascript import JFather from "jfather"; // Replace specific array element at index const parent1 = { colors: ["red", "green", "blue"] }; const child1 = { "$colors[1]": "yellow" }; console.log(JFather.merge(parent1, child1)); // { colors: ["red", "yellow", "blue"] } ``` -------------------------------- ### Nested Extends in Sub-objects Source: https://context7.com/regseb/jfather/llms.txt JFather.extend can be used within nested objects to extend specific sub-configurations recursively. Only the targeted sub-object is extended. ```javascript // Nested extends within sub-objects const nestedConfig = { "app": "MyApp", "database": { "$extends": "https://example.com/db-defaults.json", "host": "custom-host.com" } }; const nestedResult = await JFather.extend(nestedConfig); // Extends only the database sub-object ``` -------------------------------- ### JFather Array Override: Nested Array Operations Source: https://context7.com/regseb/jfather/llms.txt Illustrates performing array append operations on nested arrays within objects inside arrays. ```javascript // Nested array override const parent5 = { config: [{ plugins: ["plugin-a", "plugin-b"] }] }; const child5 = { "$config[0]": { "$plugins[]": ["plugin-c"] } }; console.log(JFather.merge(parent5, child5)); // { config: [{ plugins: ["plugin-a", "plugin-b", "plugin-c"] }] } ``` -------------------------------- ### JFather.merge(parent, child) Source: https://context7.com/regseb/jfather/llms.txt Deeply merges two JSON objects recursively. Child values take precedence. Arrays are replaced by default unless override syntax is used. ```APIDOC ## JFather.merge(parent, child) ### Description Deeply merges two JSON objects recursively, with the child values taking precedence over parent values. When both arguments are objects, their properties are merged recursively. For non-object types, the child value simply replaces the parent. Arrays are not merged by default—the child array replaces the parent array entirely (use override syntax for array merging). ### Method `JFather.merge` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```javascript import JFather from "jfather"; const parent = { foo: "alpha", bar: "ALPHA" }; const child = { foo: "beta", baz: "BETA" }; const result = JFather.merge(parent, child); console.log(result); // { foo: "beta", bar: "ALPHA", baz: "BETA" } const deepParent = { database: { host: "localhost", port: 5432 }, cache: { enabled: true } }; const deepChild = { database: { host: "production.db.com", ssl: true }, logging: { level: "info" } }; const deepResult = JFather.merge(deepParent, deepChild); console.log(deepResult); // { // database: { host: "production.db.com", port: 5432, ssl: true }, // cache: { enabled: true }, // logging: { level: "info" } // } // Non-object values - child replaces parent console.log(JFather.merge(1, 2)); // 2 console.log(JFather.merge("foo", { bar: "baz" })); // { bar: "baz" } console.log(JFather.merge({ foo: "bar" }, "baz")); // "baz" // Arrays replace entirely without override syntax const arrayParent = { items: [1, 2, 3] }; const arrayChild = { items: [4, 5] }; console.log(JFather.merge(arrayParent, arrayChild)); // { items: [4, 5] } ``` ### Response #### Success Response (200) - **mergedObject** (object) - The deeply merged JSON object. #### Response Example ```json { "foo": "beta", "bar": "ALPHA", "baz": "BETA" } ``` ``` -------------------------------- ### JFather.merge Source: https://github.com/regseb/jfather/blob/main/README.md Merges a child object into a parent object, overriding properties in the parent with those from the child. ```APIDOC ## JFather.merge(parent, child) ### Description Merge and override `parent` with `child`. ### Method `JFather.merge` ### Parameters #### Path Parameters - **parent** (``) - Required - The parent object. - **child** (``) - Required - The child object. ### Response #### Success Response (200) - **mergedObject** (``) - The merged object. ### Request Example ```json { "parent": { "name": "John", "age": 30 }, "child": { "age": 31, "city": "New York" } } ``` ### Response Example ```json { "name": "John", "age": 31, "city": "New York" } ``` ``` -------------------------------- ### Extend Nested Object with Remote JSON Source: https://github.com/regseb/jfather/blob/main/README.md You can extend a sub-object within a child object by specifying the $extends property within that sub-object. This merges the remote parent with the child's sub-object. ```javascript // https://example.com/parent.json // { "foo": 42 } const obj = { "bar": { "$extends": "https://example.com/parent.json", "baz": 3.14 } }; console.log(await JFather.extend(obj)); // { // "bar": { "foo": 42, "baz": 3.14 } // } ``` -------------------------------- ### Extend Object with Property Overrides Source: https://github.com/regseb/jfather/blob/main/README.md When extending, if a property exists in both the parent and child, the child's value takes precedence. Properties unique to either parent or child are included in the result. ```javascript // https://example.com/parent.json // { "foo": "A", "bar": "Alpha" } const obj = { "$extends": "https://example.com/parent.json", "foo": "B", "baz": "Beta" }; console.log(await JFather.extend(obj)); // { "foo": "B", "bar": "Alpha", "baz": "Beta" } ``` -------------------------------- ### Ignored Array Overrides Source: https://github.com/regseb/jfather/blob/main/README.md Array override syntaxes ($propertyName[index] or $propertyName[]) are ignored if the target property in the parent is not an array or does not exist. This prevents unintended modifications. ```javascript const parent = { "foo": ["a", "A"], "bar": 42 }; const child = { "$bar[0]": 3.14, "$baz[]": ["beta"] }; console.log(JFather.merge(parent, child)); // { "foo": ["a", "A"], "bar": 42 } ``` -------------------------------- ### JFather Merge with Numbers Source: https://github.com/regseb/jfather/blob/main/README.md Demonstrates JFather.merge with non-object types. When merging two variables that are not objects, the child's value is returned. ```javascript const parent = 1; const child = 2; console.log(JFather.merge(parent, child)); // 2 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.