npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ioc-library-example-1

v1.0.13

Published

Inversion of control/ Dependency injection library

Downloads

10

Readme

IOC (Inversion of Control) library

npm version contributions welcome

Aim

The aim of this library is to provide a simple and easy way to use dependency injection with minimal setup and or configuration that can be used specifically with TypeScript projects.

How to use this library

Step: 1 Install the library

> npm install ioc-library-example-1

Step: 2 Import the Inject decorator into your application and use it as a class decorator to inject class and or objects into your main class as dependencies.

// app.ts

import { Inject } from "ioc-library-example-1";

@Inject([class, object])
class Car {}

Full Example

// app.ts

import { Inject } from "ioc-library-example-1";

class Engine {
  engineCapacity: string;
  constructor() {
    this.engineCapacity = "2.0L";
  }
  getEngineCapacity() {
    return this.engineCapacity;
  }
}

class Wheels {
  wheelCount: number | undefined;
  constructor() {
    this.wheelCount = 4;
  }
  getWheels() {
    return this.wheelCount;
  }
}

@Inject([Engine, new Wheels()])
class Car {
  Engine: any;
  Wheels: any;
  model: string | undefined;
  constructor(model: string) {
    this.model = model;
  }
}

const mercedes = new Car("mercedes");

console.log(
  mercedes.model,
  mercedes.Engine.engineCapacity,
  mercedes.Wheels.wheelCount
);

See the Examples subdirectory for examples you can run immediately.

Problem we're trying to solve

To avoid tightly coupled modules, functions and classes ensuring code maintainablilty and ease of testing.

Important Concepts

What is a JavaScript decorator (Experimental)

JavaScript decorators are currently in "stage 2 Draft" which means they are a proposed change that's likely to go through and be implemented, but are still considered experimental. Decorators are made use of in this library to allow you to inject your dependencies into a class.

What is IOC "Inversion of control"?

Inversion of control is a programming concept where flow of control (execution of statements) is handled externally to your application code.

What does this IOC library do exactly?

This Ioc (Inversion of Control) library provides a way to pass (inject) dependencies (classes, objects, etc...) through a function instead of declaring them inside your class.

Why dependency injection?

Dependency injection (a form of "inversion of control") is a way to decouple your JavaScript dependencies from your app code and configure them separately. If you need to "rewire" or completely change these dependencies you can do so away from your application logic rather than directly in what can become very complex application code.

It also supports the "separation of concerns" principle meaning you separate the resolution (e.g., instantiating a class) of your dependencies from the actual "use" of them in your app.

Currently Supports

  • Transient Classes
  • Transient Objects

Planned Support

  • Circular dependencies - dependencies that rely on one another to work
  • Constants, functions and other types.

Potential Issues

  • Error: "exports is not defined in ES module scope" - make sure you remove the "type": "module" from the package.json file.
  • error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option in your 'tsconfig' or 'jsconfig' to remove this warning. - Just a warning that decorators are still experimental. Add/ uncomment the option in your tsconfig file called "experimentalDecorators: true"