A small and simple injection lib. You can build an architecture based on the parameter names.
npm install architect.js
Require the
const architect = require('architect.js');
const inject = architect({
$b: 100,
$a: 50,
$c: 30,
$d: 10
}, true);
Type: object
A object full of predefined arguments
Type: boolean
Defines if architect.js clones the passed restrictions
object or not.
In case of cloning architect.js does not mutate the object on the outside.
I highly recommend this!
You can inject any function which has the same parameter names as the passed restrictions
object.
function addAB ($a, $b) {
return $a + $b;
}
function subAC ($a, $c) {
return $a + $c;
}
const sum = inject(addAB);
console.log(sum); // -> 150
const dif = inject(subAC);
console.log(dif); // -> 80
The following function does not mutate the $c
argument:
function logC ($c) {
console.log('log C: ' + $c);
}
function addAB_to_C ($a, $b) {
$c = $a + $b;
}
inject(addAB_to_C);
inject(logC); // -> 30
In case of mutating use the $update
function as parameter:
function addAB_to_C_update ($a, $b, $update) {
$update('$c', $a + $b);
}
inject(addAB_to_C_update);
inject(logC); // -> 150
If you need to dynamically inject a parameter use the $add
function:
function addParameter ($add) {
$add('$o', 1000);
}
inject(addParameter);
You can get the mutated architect.js scope just by invoking the getRestrictions
function:
console.log(inject.getRestrictions());
You can pass everything to the restrictions
object, like functions or other objects:
const inject = architect({
$rick: {
fname: 'Rick',
lname: 'Sanchez',
age: 70
},
$printName: function (obj) {
return 'Name: ' + obj.fname + ' ' + obj.lname + ' age: ' + obj.age;
}
}, true);
function printName ($rick, $printName) {
const out = $printName($rick);
console.log(out);
}
inject(printName); // -> Name: Rick Sanchez age: 70
Oliver Jessner @oliverj_net
Copyright © 2016