1 | # depject
|
2 |
|
3 | minimal dependency injection
|
4 |
|
5 | ## module api
|
6 |
|
7 | a is an object with named functions and arrays.
|
8 | functions are represent _plugs_ and arrays represent _sockets_.
|
9 | plugs are pluged into sockets. modules may expose plugs,
|
10 | and may create new sockets. The creator of a socket gets to choose
|
11 | the signature of that socket, i.e. what arguments it's called with,
|
12 | and also how multiple plugs are combined together.
|
13 |
|
14 | plugs are connected to sockets with the same name, so be sure to name wisely.
|
15 | (we can improve this, once more experience with this sort of system is gained)
|
16 |
|
17 | ## example
|
18 |
|
19 | ``` js
|
20 | var combine = require('depject')
|
21 |
|
22 | var hi = {
|
23 | hello: function (name) {
|
24 | console.log(
|
25 | hi.decorate_hello.reduce(function (name, dec) {
|
26 | return dec(name)
|
27 | }, name)
|
28 | )
|
29 | },
|
30 | decorate_hello: []
|
31 | }
|
32 |
|
33 |
|
34 | function toCapitalized(word) {
|
35 | return word[0].toUpperCase() + word.substring(1).toLowerCase()
|
36 | }
|
37 |
|
38 | var capitalize = {
|
39 | decorate_hello: toCapitalized
|
40 | }
|
41 |
|
42 | var greet = {
|
43 | decorate_hello: function (name) {
|
44 | return 'Hello, '+name
|
45 | }
|
46 | }
|
47 | combine([hi, capitalize, greet])
|
48 |
|
49 | hi.hello('dominic')
|
50 | ```
|
51 |
|
52 | in this case, hello becomes:
|
53 |
|
54 | ``` js
|
55 | function (name) {
|
56 | return console.log('Hello, '+toCapitalized(name))
|
57 | }
|
58 | ```
|
59 |
|
60 | except that it's complete decoupled from hello, and you didn't need
|
61 | to bother the maintainer of `hello` because you wanted it capitalized.
|
62 |
|
63 | ## api
|
64 |
|
65 | ### combine ([modules...])
|
66 |
|
67 | takes an array of modules and plugs every plug into the relavant socket.
|
68 |
|
69 | ## License
|
70 |
|
71 | MIT
|
72 |
|
73 |
|
74 |
|