simply-pattern
v0.2.0
Published
Create and use pattern without knowledge. The purpose is to bring the javascript design pattern closer to a real OO design pattern.
Downloads
5
Maintainers
Readme
simply-pattern
Create and use pattern without knowledge. The purpose is to bring the javascript design pattern closer to a real OO design pattern.
Index
Injection pattern
injection(entity
, where_inject
[, ...dependencies
])
entity
<function> | <object>where_inject
<object> | <function> ifdependencies
is presentdependencies
[<any>] eg
const {injection} = require('simply-pattern')
const performsCallback = function(callback, ...number){return callback(...number)}
// example with sum callback
const sum = (a, b) => {return a+b}
const where_inject_sum = {apply(...args){return [sum, ...args]}}
const entity_with_dep_injected_sum = injection(performsCallback, where_inject)
console.log(entity_with_dep_injected(2,3)) // = 5
//example with multiplication callback
const mult = (a, b) => {return a*b}
const where_inject_multiplication = {apply(...args){return [mult, ...args]}}
const entity_with_dep_injected_multiplication = injection(performsCallback, where_inject)
console.log(entity_with_dep_injected(2,3)) // = 6
where_inject as a function
It must to be return an object like previously.
const where_inject = function(...dep){
return {apply(...args){return [dep[0], dep[1], ...args}}
}
const entity_with_dep_injected = injection(entity, where_inject, depA, depB)
// or, once again
const where_inject = function(dep){
return {apply(...args){return dep.dependencyA, ...args}}
}
const entity_with_dep_injected = injection(entity, where_inject, {dependencyA: /* any */})