This package has been deprecated

Author message:

code

architect.js

0.0.1 • Public • Published

architect.js

A small and simple injection lib. You can build an architecture based on the parameter names.

Install

npm install architect.js

Usage

Require the

const architect = require('architect.js');

const inject = architect({
    $b: 100,
    $a: 50,
    $c: 30,
    $d: 10
}, true);

API

architect

restrictions

Type: object A object full of predefined arguments

injectorScoope

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!

Example


Simple functions

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

Updating a inner argument

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

Adding dynamically a parameter

If you need to dynamically inject a parameter use the $add function:

function addParameter ($add) {
    $add('$o', 1000);
}

inject(addParameter);

Get the mutate architect.js scope

You can get the mutated architect.js scope just by invoking the getRestrictions function:

console.log(inject.getRestrictions());

restrictions types

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

Author

Oliver Jessner @oliverj_net

Copyright © 2016

Package Sidebar

Install

npm i architect.js

Weekly Downloads

0

Version

0.0.1

License

MIT

Last publish

Collaborators

  • oliverj