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

js-inject

v0.3.2

Published

Angular liked dependency injection as a module

Downloads

4

Readme

js-inject

Angular liked dependency injection as a module.

Install

npm install js-inject --save

Getting started

This library works well with es7 annotation and translate to es3/es5 by Babel or Typescript. You can also use this library without es7 annotation by using Registry.

Basic usage

Register service

You can register service by using @Name annotation.

@Named
class ArmBuilder { }

With above annotation, ArmBuilder will be registered as armBuilder (camel case).

You can rename by passing name to @Name annotation.

@Named('arm')
class ArmBuilder { }

With above annotation, ArmBuilder will be registered as just arm.

Constructor injection

To inject service to another class, you could specify parameter name to match with registered service.

@Named
class RobotBuilder {
  constructor(armBuilder: ArmBuilder) { }
} 

Above code, js-inject will match parameter name with registered services and inject the service when it create RobotBuilder.

TIP: If you need to uglify your code, you can use /* @ngInject */ from ng-annotate to prevent parameter name to be mangled.

Property Injection

With js-inject, you can also inject service to property by using @Inject annotation.

@Named
class RobotBuilder {
  @Inject
  armBuilder: ArmBuilder;
}

If service name is different to property name, you can pass the name to @Inject annotation.

@Named
class RobotBuilder {
  @Inject('arm')
  armBuilder: ArmBuilder;
}

Perform post construct action

You can perform action after class has been created by @PostConstruct annotation.

@Named
class RobotBuilder {
  @PostConstruct
  init() { }
} 

With above @PostConstruct annotated, init method will be run after RobotBuilder has be created.

Registry

Register and replace service

To register service manually, you can register and replace service by Registry.registerService.

Registry.registerService('robotBuilder', MockRobotBuilder);

Register factory

Sometimes, value you would like to inject is not the class. You can register service factory by Registry.registerFactory.

Registry.registerFactory('robotVersion', app => app.version);

In factory function, you can pass registered services. In above example, app will be injected to factory function.

TIP: If you need to uglify your code, you can use /* @ngInject */ from ng-annotate to prevent parameter name to be mangled.

Register value

You can use Registry.registerValue to register a value.

Registry.registerValue('robotVersion', '1.0.0');

Get all names

You can get names of all registered services by GetNames.

let names = Registry.getNames();

Injector

injector can be created from Registry.

let injector = Registry.createInjector();

Singleton

You can create singleton service through Injector.

let robotBuilder = injector.get<RobotBuilder>('robotBuilder');

With above call, RobotBuilder will be created with ArmBuilder injected. Please note that, services will be singleton for the same Injector instance.

Transient

If you would like to just create transient instance, you could use instantiate.

let robotBuilder = injector.instantiate(RobotBuilder);

With instantiate, you can also pass local services to the function.

let robotBuilder = injector.instantiate(RobotBuilder, { armBuilder: mockArmBuilder });

Factory

You could invoke a function and inject registered services with invoke.

let robotBuilder = injector.invoke(armBuilder => RobotBuilder.create(armBuilder));

You can also pass this and local services to the function.

let robotBuilder = injector.invoke(
  armBuilder => this.create(armBuilder),
  FakeRobotBuilder,
  { armBuilder: mockArmBuilder });

Check service existed

You can check service is existed by has function.

let hasRobotBuilder = injector.has('robotBuilder');

Plug to AngularJS

This library can be used in NodeJS or on browser without AngularJS. If you would like registered services to be available on AngularJS app, you can do following.

let injector = Registry.createInjector();
let names = Registry.getNames();
let app = angular.module('your-module');

for (let name of names) {
  app.value(name, injector.get(name));
}

//now you can inject your registered service to AngularJS
app.run(robotBuilder => {
  ...
});