Comparing version 1.0.0 to 1.1.0
{ | ||
"name": "simplistic", | ||
"version": "1.0.0", | ||
"description": "A simple Javascript-based dependency injection module", | ||
"main": "src/container.js", | ||
"scripts": { | ||
"jscs": "jscs ./ --config=.jscsrc ", | ||
"test": "mocha './src/**/*.test.js'" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/slatkovic/simplistic.git" | ||
}, | ||
"keywords": [ | ||
"Dependency injection", | ||
"IoC" | ||
], | ||
"author": "Stipe Ivan Latkovic", | ||
"license": "Unlicense", | ||
"bugs": { | ||
"url": "https://github.com/slatkovic/simplistic/issues" | ||
}, | ||
"homepage": "https://github.com/slatkovic/simplistic#readme", | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"jscs": "^3.0.7", | ||
"mocha": "^3.0.2" | ||
}, | ||
"dependencies": { | ||
"lodash": "^4.15.0" | ||
} | ||
"name": "simplistic", | ||
"version": "1.1.0", | ||
"description": "A simple Javascript-based dependency injection module", | ||
"main": "src/container.js", | ||
"scripts": { | ||
"jscs": "jscs ./ --config=.jscsrc ", | ||
"test": "mocha './src/**/*.test.js'" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/slatkovic/simplistic.git" | ||
}, | ||
"keywords": [ | ||
"Dependency injection", | ||
"IoC" | ||
], | ||
"author": "Stipe", | ||
"license": "Unlicense", | ||
"bugs": { | ||
"url": "https://github.com/slatkovic/simplistic/issues" | ||
}, | ||
"homepage": "https://github.com/slatkovic/simplistic#readme", | ||
"devDependencies": { | ||
"chai": "^4.3.7", | ||
"jscs": "^3.0.7", | ||
"mocha": "^10.2.0" | ||
}, | ||
"dependencies": {} | ||
} |
# simplistic | ||
A simple Javascript-based dependency injection module | ||
### Motivation | ||
[Dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) leads to better component encapsulation, increases testability and promotes configurability. Components can expose clear interfaces, there's no need to override the NodeJS module resolution logic in unit tests and it's actually easy to write real unit tests. | ||
### Features | ||
- Supports pure components as components are not aware of the container | ||
- Easily isolates system dependencies | ||
- Caches dependencies | ||
- Allows for dependency expansion using multiple dependency files (think universal apps) | ||
### Example usage | ||
```javascript | ||
import containerFactory from 'simplistic'; | ||
const container = containerFactory(); | ||
var dependencies = { | ||
request: () => require('request'), | ||
apiClient: () => { | ||
const component = require('/some/api/client/implementation.js'); | ||
return component( | ||
container.wire('request') | ||
); | ||
} | ||
}; | ||
export default container.build(dependencies); | ||
``` | ||
@@ -1,27 +0,27 @@ | ||
var _ = require('lodash'); | ||
function containerFactory() { | ||
var cache = {}; | ||
var _dependencies = {}; | ||
var cache = {} | ||
var _dependencies = {} | ||
function wire(dependencyName) { | ||
if (dependencyName === undefined) throw new Error('dependencyName not defined'); | ||
if (dependencyName === undefined) { | ||
throw new Error('dependencyName not defined') | ||
} | ||
function dependencyIsCached() { | ||
return cache[dependencyName] !== undefined; | ||
return cache[dependencyName] !== undefined | ||
} | ||
function createDependency() { | ||
return _dependencies[dependencyName](); | ||
return _dependencies[dependencyName]() | ||
} | ||
function cacheDependency(dependency) { | ||
cache[dependencyName] = dependency; | ||
return dependency; | ||
cache[dependencyName] = dependency | ||
return dependency | ||
} | ||
function cachedDependency() { | ||
return cache[dependencyName]; | ||
return cache[dependencyName] | ||
} | ||
@@ -31,3 +31,3 @@ | ||
? cachedDependency() | ||
: cacheDependency(createDependency()); | ||
: cacheDependency(createDependency()) | ||
} | ||
@@ -37,13 +37,16 @@ | ||
if (dependencies === undefined) throw new Error('dependencies not defined'); | ||
if (dependencies === undefined) { | ||
throw new Error('dependencies not defined') | ||
} | ||
_dependencies = dependencies; | ||
_dependencies = dependencies | ||
function buildDependency(factory, name) { | ||
wire(name); | ||
function buildDependency(name) { | ||
wire(name) | ||
} | ||
_.forEach(dependencies, buildDependency); | ||
Object.keys(dependencies) | ||
.forEach(buildDependency) | ||
return cache; | ||
return cache | ||
} | ||
@@ -54,5 +57,5 @@ | ||
build: build | ||
}; | ||
} | ||
} | ||
module.exports = containerFactory; | ||
module.exports = containerFactory |
6448
0
108
41
6
- Removedlodash@^4.15.0
- Removedlodash@4.17.21(transitive)