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

angular-ecmascript

v0.0.3

Published

Build an AngularJS app using ES6's class system

Downloads

23

Readme

Angular-Ecmascript

angular-ecmascript is a utility library which will help you write an AngularJS app using ES6's class system. As for now there is no official way to do so, however using ES6 syntax is recommended, hence this library was created.

In addition, angular-ecmascript provides us with some very handy features, like auto-injection without using any pre-processors like ng-annotate. For more information about angular-ecmascript's API and features please read the following docs.

Docs

angular-ecmascript provides us with some base module-helpers classes which we should inherit from while writing our helpers. These are the available helpers:

Each helper can be defined with a static $inject property which will be responsible for dependencies injection, and a static $name property, which is responsible for specifying the helper's name and defaults to the class'es name.

import { Service, Controller } from 'angular-ecmascript/module-helpers';

class DateService extends Service {
  static $name = '$date'

  now() {
    return new Date().getTime();
  }
}

class MyController extends Controller {
  static $inject = ['$date']

  constructor(...args) {
    super(...args);

    this.createdAt = this.$date.now();
  }
}

To interface with these helpers we will need to use a module-loader provided by angular-ecmascript. Just create a new AngularJS module wrapped by the loader and use it like so:

// libs
import Angular from 'angular';
import Loader from 'angular-ecmascript/module-loader';
// module-helpers
import MyCtrl from './controllers/my.ctrl';
import MyDirective from './directives/my.directive';
import MyService from './services/my.service';

// app
App = Angular.module('my-app', [
  'module1',
  'module2',
  'module3'
]);

// loader
new Loader(App)
  .load(MyCtrl)
  .load(MyDirective)
  .load(MyService);
  • Loader() can take a module name as the first argument and an optional dependencies array if you'd like to load a module by name.
  • Loader.load() can take an array of several module-helpers instead of chaining them one-by-one.
  • Loader.load() can take a string as the first argument representing the provider type and its value as the second argument, just like the $provide service.

Provider

Used to define a new provider.

import { Provider } from 'angular-ecmascript/module-helpers';

class MomentProvider extends Provider {
  static $name = '$now'

  $get() {
    return new Date().getTime();
  }
}

Service

Used to define a new service.

import { Service } from 'angular-ecmascript/module-helpers';

class DateService extends Service {
  static $name = '$date'

  now() {
    return new Date().getTime();
  }
}

Factory

Used to define a new factory.

  • Note that the create method must be implemented, otherwise an error will be thrown during load time.
import { Factory } from 'angular-ecmascript/module-helpers';

class MomentFactory extends Factory {
  static $name = 'now'

  create() {
    return new Date().getTime();
  }
}

Controller

Used to define a new controller.

  • $scope will be injected automatically so no need to specify it.
  • When using angular-meteor the controller will be set as the view model automatically.
import { Controller } from 'angular-ecmascript/module-helpers';

class MyController extends Controller {
  constructor(...args) {
    super(...args);

    this.createdAt = new Date().getTime();
  }

  logCreationTime() {
    console.log(`created at: ${this.createdAy}`);
  }
}

Directive

Used to define a new directive.

import { Directive } from 'angular-ecmascript/module-helpers';

class MyDirective extends Directive {
  templateUrl: 'my-template.html'
  restrict = 'E'
  transclude = true
  scope = {}

  link(scope) {
    scope.foo = 'foo';
    scope.bar = 'bar';
  }
}

Decorator

Used to define a new decorator.

  • $delegate will be injected automatically so no need to specify it.
  • Note that the decorate method must be implemented, otherwise an error will be thrown during load time.
  • No need to return the $delegate object, it should be handled automatically.
import { Decorator } from 'angular-ecmascript/module-helpers';

class MyDecorator extends Decorator {
  static $name = 'myService'

  helperFn() {
    // an additional fn to add to the service
  }

  decorate() {
    this.$delegate.aHelpfulAddition = this.helperFn;
  }
}

Filter

Used to define a new filter.

  • Note that the filter method must be implemented, otherwise an error will be thrown during load time.
import { Filter } from 'angular-ecmascript/module-helpers';

class MyFilter extends Filter {
  filter(input = '', uppercase) {
    let out = '';

    for (let i = 0; i < input.length; i++) {
      out = input.charAt(i) + out;
    }

    // conditional based on optional argument
    if (uppercase) {
      out = out.toUpperCase();
    }

    return out;
  }
}

Config

Used to define a new config.

  • Note that the configure method must be implemented, otherwise an error will be thrown during load time.
import { Config } from 'angular-ecmascript/module-helpers';

class RoutesCfg extends Config {
  static $inject = ['$routeProvider']

  constructor(...args) {
    super(...args);

    this.fetchUser = ['http', this::this.fetchUser];
  }

  configure() {
    this.$routeProvider
      .when('/', {
        template: '<home user="$resolve.user"></home>',
        resolve: {
          user: this.fetchUser
        }
      });
  }

  fetchUser($http) {
    return $http.get('...');
  }
}

Runner

Used to define a new run block.

  • Note that the run method must be implemented, otherwise an error will be thrown during load time.
import { Runner } from 'angular-meteor/module-helpers';

class RoutesRunner extends Runner {
  static $inject = ['$rootScope', '$state']

  run() {
    this.$rootScope.$on('$stateChangeError', (...args) => {
      const [,,, err] = args;

      if (err === 'AUTH_REQUIRED') {
        this.$state.go('login');
      }
    });
  }
}

Download

The source is available for download from GitHub. Alternatively, you can install using Node Package Manager (npm):

npm install angular-ecmascript