UNPKG

1.49 kBMarkdownView Raw
1# depject
2
3minimal dependency injection
4
5## module api
6
7a is an object with named functions and arrays.
8functions are represent _plugs_ and arrays represent _sockets_.
9plugs are pluged into sockets. modules may expose plugs,
10and may create new sockets. The creator of a socket gets to choose
11the signature of that socket, i.e. what arguments it's called with,
12and also how multiple plugs are combined together.
13
14plugs 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
20var combine = require('depject')
21
22var 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
34function toCapitalized(word) {
35 return word[0].toUpperCase() + word.substring(1).toLowerCase()
36 }
37
38var capitalize = {
39 decorate_hello: toCapitalized
40}
41
42var greet = {
43 decorate_hello: function (name) {
44 return 'Hello, '+name
45 }
46}
47combine([hi, capitalize, greet])
48
49hi.hello('dominic')
50```
51
52in this case, hello becomes:
53
54``` js
55function (name) {
56 return console.log('Hello, '+toCapitalized(name))
57}
58```
59
60except that it's complete decoupled from hello, and you didn't need
61to bother the maintainer of `hello` because you wanted it capitalized.
62
63## api
64
65### combine ([modules...])
66
67takes an array of modules and plugs every plug into the relavant socket.
68
69## License
70
71MIT
72
73
74