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

conditional-injector

v1.0.16

Published

Typescript dependency injection based on conditional expressions

Downloads

21

Readme

conditional-injector

npm version Build Status Maintainability Greenkeeper badge codecov

It's a mix of dependency injection and factory method. It gives you an instance of one subclasses of a class based on an optional predicate.

    import {Container, Injectable, Scope} from "conditional-injector";
        
    class ParentClass {
        public argumentReceived: string = "";
        constructor(argument: string) {
            this.argumentReceived = argument;
        }
    };
    
    @Injectable({predicate: (argument: string) => argument == "first"})
    class FirstSubClass extends ParentClass {
        constructor(argument: string) {
            super(argument)
        }
    }
    
    @Injectable({predicate: (argument: string) => argument == "second"})
    class SecondSubClass extends ParentClass {
        constructor(argument: string) {
            super(argument)
        }
    }
    
    console.log(Container.subclassesOf(ParentClass).create("first").argumentReceived); //"first"
    console.log(Container.subclassesOf(ParentClass).create("second").argumentReceived); //"second" 

'predicate' value it's not required and the class will be instantiated every time that no other 'predicate' is satisfied. It fits like a glove to achieve NullObject implementation:

    class NoPredicateParentClass {}
    
    @Injectable()
    class NoPredicateSubClass extends NoPredicateParentClass {
        constructor() {
            super();
            console.log("NoPredicateSubClass was instantiated");
        }
    }
    
    @Injectable({predicate: () => false})
    class UnsatisfiedPredicateSubClass extends NoPredicateParentClass {}
    
    Container.subclassesOf(NoPredicateParentClass).create(); // "NoPredicateSubClass was instantiated"

Other than this, you can define instantiation scope. You can get a new instance in every Request or use the same instance throughout the entire Application.

    class ScopeParentClass {
        public scopeDemonstrationAttribute = "originalValue";
    }
    
    @Injectable({scope: Scope.Request, predicate: (value) => value.name == "request"})
    class RequestScopeSubClass extends ScopeParentClass {}
    
    Container.subclassesOf(ScopeParentClass).create({name: "request"}).scopeDemonstrationAttribute = "requestValue";
    console.log(Container.subclassesOf(ScopeParentClass).create({name: "request"}).scopeDemonstrationAttribute); // "originalValue"
    
    @Injectable({scope: Scope.Application, predicate: (value) => value.name == "application"})
    class ApplicationScopeSubClass extends ScopeParentClass {}
    
    Container.subclassesOf(ScopeParentClass).create({name: "application"}).scopeDemonstrationAttribute = "applicationValue";
    console.log(Container.subclassesOf(ScopeParentClass).create({name: "application"}).scopeDemonstrationAttribute); // "applicationValue"

If you want to, you can get every subclass instance of a given class: It includes those with no predicate.

    Container.subclassesOf(ScopeParentClass)
             .createAll()
             .map(scopeSubclass => console.log(scopeSubclass.scopeDemonstrationAttribute));
     // "originalValue" 
     // "applicationValue"
Notes:
  • You need to enable typescript flags 'experimentalDecorators' and 'emitDecoratorMetadata'.
  • Due to one decoration property, if a decorated class never gets imported, the decoration function never gets called. Therefore the class never gets registered to the container. You have to explicitly import the file that contains the class at least one time in order to make sure that that class is able to be injected. That's the reason for having a script. Although it's not mandatory, it makes your life easier creating an auto-generated source file from this script. You can use it like this:
    generate-injectables-list.sh src/injectable-files-list.ts src

And then using the auto-generated file src/injectable-files-list.ts in your project.