Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

flacon

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flacon - npm Package Compare versions

Comparing version 0.3.1 to 0.3.2

40

index.js
'use strict';
// A dependency injection container, holding all modules, mocks and dependencies.
var Flacon = function () {
function Flacon () {
var modules = {}, notCached = {}; // `{} is …` is never true.
var modules = {},
notCached = {}; // `{} is …` is never true.
var load = function (id, mocks) { // `mocks` is optional
var module, deps, hasMocks;
var load = function (id, mocks) {
var module, deps;
if ('string' !== typeof id) throw new Error('`id` must be a string.');
// `mocks` is optional
if (Array.isArray(mocks) || 'object' !== typeof mocks) mocks = {};

@@ -21,7 +18,8 @@

var hasMocks = Object.keys(mocks).length > 0;
if (module.cache === notCached || hasMocks) {
hasMocks = Object.keys(mocks).length > 0;
if (module.cache === notCached || mocks[id]) {
// merge dependencies and mocks
deps = module.deps.map(function (id) {
var dep = load(id);
var dep = load(id, mocks);
// For greater flexibility, the mocks are being called with the

@@ -32,6 +30,6 @@ // dependency. They can then manipulate it or return something entirely new.

});
if (hasMocks) return module.factory.apply({}, deps);
module.cache = module.factory.apply({}, deps);
}
return module.cache;

@@ -42,3 +40,3 @@ };

var flush = function (id) {
load.flush = function (id) {
if ('string' !== typeof id) throw new Error('`id` must be a string.');

@@ -48,3 +46,2 @@ if (!modules[id]) throw new Error(id + ' has not been registered.');

modules[id].cache = notCached;
return this; // for method chaining

@@ -55,3 +52,3 @@ };

var publish = function (id, deps, factory) {
load.publish = function (id, deps, factory) {
if ('string' !== typeof id) throw new Error('`id` must be a string.');

@@ -65,10 +62,5 @@ if (arguments.length === 2) { // `deps` is optional

if (modules[id]) throw new Error(id + ' has already been registered.');
else modules[id] = {
deps: deps,
factory: factory,
cache: notCached
};
else modules[id] = {deps: deps, factory: factory, cache: notCached};
// To make publishing *and* exporting a factory easier, we return it here.
return factory;
return factory; // To make publishing *and* exporting a factory easier.
};

@@ -78,9 +70,5 @@

load.flush = flush;
load.publish = publish;
return load;
};
}
if (module) module.exports = Flacon;
{
"name": "flacon",
"description": "A hyperminimal dependency injection framework.",
"version": "0.3.1",
"version": "0.3.2",
"main": "index.js",

@@ -6,0 +6,0 @@ "files": ["index.js", "readme.md"],

@@ -40,16 +40,16 @@ # flacon

### `publish`
### create a container
First, we create a new *flacon* container in `container.js`.
First, we create a new container in `container.js`. **On a container, you can [publish](#flaconpublishid-deps-factory) and [load](#flaconid-mocks) modules.**
```js
var Flacon = require('flacon');
module.exports = new Flacon();
```
You can do two things with a container: `publish` a module and `load` a module.
Let's publish `foo` first. We call the `publish` method with an **id** and a **factory function**.
### publish modules
Let's start with `foo.js`. We call the `publish` method with an **id** and a **factory function**.
```js

@@ -65,3 +65,3 @@ var container = require('./container');

Moving on to `bar`, we define `foo` as a **dependency**. All dependencies get passed into the factory function.
Moving on to `bar.js`, we define `foo` as a **dependency**. The result of `foo`'s factory will be passed into `bar`'s factory.

@@ -73,3 +73,3 @@ ```js

return {
value: function () { return foo.value() + ' bar' }
value: function () { return foo.value() + 'bar' }
};

@@ -80,3 +80,3 @@ });

### `load`
### load modules

@@ -89,7 +89,10 @@ By simply calling the container with a module id, you will get the **return value of the factory function**.

var bar = container('bar');
bar.value(); // -> 'foo bar'
bar.value(); // -> 'foobar'
```
During testing, we can easily **manipulate or mock a dependency**. Note that this loads the module without caching.
### mock dependencies
During testing, we can easily **manipulate or mock a dependency**. This will load every mocked module without caching.
```js

@@ -99,8 +102,8 @@ var container = require('./container');

var bar = container('bar', {
'a': function (a) {
a.value = function () { return 'baz' }
return a;
foo: function (foo) {
foo.value = function () { return 'baz' };
return foo;
}
});
bar.value(); // -> 'baz bar'
bar.value(); // -> 'bazbar'
```

@@ -114,5 +117,5 @@

```js
container.load('a'); // factory creates module
container.flush('a');
container.load('a'); // factory creates module again
container.load('foo'); // factory creates module
container.flush('foo');
container.load('foo'); // factory creates module again
```

@@ -126,5 +129,5 @@

Loads a module by `id`. Returns the module.
Loads a module by `id`. Caches and returns the module.
If `mocks` are passed, they will be used to instead of its dependencies. Otherwise the module will be cached.
`mocks` is an object of mocking functions by id. Mocked dependencies will not be cached.

@@ -131,0 +134,0 @@ - `id`: The identifier, unique to the container.

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc