You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
1.8 KiB
JavaScript

"use strict";
const mergeByTemplate = require("./");
//----- Array merging ------//
// TODO: Document that the array-merging behaviour changes depending on whether the array in the template contains any items!
let mergeArray = mergeByTemplate.createMerger([ {
prop: (a, b) => a + b
} ]);
let arrayA = [
{ prop: 1 },
{ prop: 2 }
];
let arrayB = [
{ prop: 3 },
{ prop: 4 }
];
let arrayC = [
{ prop: 5 },
{ prop: 6 }
];
console.log(mergeArray([ arrayA, arrayB, arrayC ])); // [ { prop: 9 }, { prop: 12 } ]
// TODO: Document merge functions, also for arrays
//----- Object merging ------//
let mergeConfiguration = mergeByTemplate.createMerger({
// `null` makes it explicit that this property should be overridden as a single value, despite being an object - but leaving the property out entirely would have had the same result, so this is strictly for readability
database: null,
scripts: {},
accessList: [],
powerLevel: (a, b) => a + b
});
let defaultConfiguration = {
database: {
type: "socket",
path: "/default"
},
scripts: {
test: "echo 'no test configured'",
publish: "npm publish"
},
accessList: [
"maintainer-bot"
],
powerLevel: 8999
};
let customConfiguration = {
database: {
hostname: "localhost",
port: "1234",
username: "hello",
password: "world"
},
scripts: {
test: "node test.js",
build: "node build.js"
},
accessList: [
"real-person"
],
powerLevel: 2
};
console.log(mergeConfiguration([ defaultConfiguration, customConfiguration ])); /*
{
database: {
hostname: 'localhost',
port: '1234',
username: 'hello',
password: 'world'
},
scripts: {
test: 'node test.js',
publish: 'npm publish',
build: 'node build.js'
},
accessList: [ 'maintainer-bot', 'real-person' ],
powerLevel: 9001
}
*/