Library to merge, extend and override JSON objects.
MIT License
Boys use JSON; Men use JFather.
JFather is a utility library to merge, extend and override JSON objects.
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 an object.
const overridden = await 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."
// }
JFather is published on npm (its CDN: esm.sh, jsDelivr, UNPKG) and Deno.
// Node.js and Bun (after `npm install jfather`):
import JFather from "jfather";
// 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";
// Deno:
import JFather from "https://deno.land/x/jfather/mod.js";
merge()
Merge and override parent
with child
.
JFather.merge(parent, child);
parent
: The parent object.child
: The child object.extend()
Extend obj
, merge and override.
JFather.extend(obj, [options]);
obj
: The object with any $extends
properties.options
:
request
: The function for getting a JSON object remotely. By default,fetch()
andResponse.json()
.load()
Load from a url
, extend, merge and override.
JFather.load(url, [options]);
url
: The string containing the URL of a JSON file.options
:
request
: The function for getting a JSON object remotely. By default,fetch()
andResponse.json()
.parse()
Parse a text
, extend, merge and override.
JFather.parse(text, [options]);
text
: The string containing a JSON object.options
:
request
: The function for getting a JSON object remotely. By default,fetch()
andResponse.json()
.