2.3.2 • Published 3 years ago

@dyrektrypt/container v2.3.2

Weekly downloads
178
License
MIT
Repository
github
Last release
3 years ago

container.js

Simple to learn and use, open-source project for emulating containers in Javascript using CommonJS.

See 'usage' for learning how to use the API.
See 'information' for more detailed information about the API.

Installation

Simply install the repository using npm.

#Assuming you are in your project's directory
npm i @dyrektrypt/container

And that's it! Now all you need to do is require it.

const container = require('@dyrektrypt/container')

Usage

The container emulates IoC by registering objects to it at runtime, which can simply be fetched by using the container.getObject(name) function. However the container also takes advantage of being weakly typed, allowing you to declare nodes with a name of type 'any' in which you can later retrieve.

Single Objects

To register a single object to the container, simply declare it as an export named 'node' with options:

  • name: any (required)
  • object: object (required)
  • singleton: boolean (required)

An example of this:

exports.node = {
    name: 123,
    object: new testObject(),
    singleton: true //It is singleton
}

Multiple Objects

To register multiple objects to the container, simply declare an export named 'node' in which exports an array of nodes. Just like a single node, it must contain objects with options:

  • name: any (required)
  • object: object (required)
  • singleton: boolean (required)

An example of this:

let myObjects = [{
    name: 123,
    object: new testObject(),
    singleton: false //It is not singleton
}]

exports.node = myObjects

And that's it! All objects will be individually registered to the container. Now all you need to do is fetch them from the container whenever you want.

container.getObject(123) //Fetches the testObject in either scenario

When a singleton is configured to equal false, a new object will be created upon every call.

Information

Due to the declarative approach of ES6, the container relies on CommonJS in order to dynamically run.

The container is good for emulating objects which have similiar functionality to statically typed languages such as module-wide IIFE's and classes.

The container makes sure that the objects logic is not touched by other ends of the program making it a good way to keep the program up-to-date and secure; as well as allowing for a singleton pattern for objects needed on a global level.

However it is not recommended that you just pass an object to the container for a singleton object, but instead you pass a new instance of one, for example:

exports.node = {
    name: 'badPractice',
    object: myBadPracticeObject, //This means that the objects logic can still be changed!
    singleton: true
}

exports.node = {
    name: 'goodPractice',
    object: new myGoodPracticeObject(), //This objects logic won't be touched elsewhere!
    singleton: true
}

This is because the 'badPractice' object can still be changed and updated elsewhere changing the logic of the emulated 'statically' typed object (which should not be done for most scenarios).

container.js is dependency-free.

2.3.2

3 years ago

2.3.0

3 years ago

2.3.1

3 years ago

2.2.18

3 years ago

2.2.17

3 years ago

2.2.15

3 years ago

2.2.16

3 years ago

2.2.13

3 years ago

2.2.14

3 years ago

2.2.11

3 years ago

2.2.12

3 years ago

2.2.10

3 years ago

2.2.9

3 years ago

2.2.8

3 years ago

2.2.7

3 years ago

2.2.5

3 years ago

2.2.6

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.2.3

3 years ago

2.2.2

3 years ago

2.2.4

3 years ago

2.1.3

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.0

3 years ago