UNPKG

ng-injector

Version:

Injector decorator for writing angular 1.x with ES6/TypeScript

61 lines (55 loc) 1.67 kB
/*! * ng-injector v0.4.5 * (c) 2016 Yinan * Released under the MIT License. */ 'use strict'; function forEach(target, eachFunc) { if (!target) return; var length = void 0; if (target instanceof Array) { length = target.length; for (var i = length - 1; i >= 0; i--) { eachFunc(target[i], i); } } else { var keys = Object.keys(target); var key = void 0; length = keys.length; for (var _i = 0; _i < length; _i++) { key = keys[_i]; eachFunc(target[key], key); } } } var injector = function injector() { var deps = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0]; if (typeof deps === 'string') { deps = [deps]; } else if (!(deps instanceof Array)) { throw new Error('deps must be Array'); } return function (target) { var construct = function construct(args) { var Component = function Component() { var _this = this; forEach(args, function (arg, i) { return _this[deps[i]] = arg; }); return target.call(this); }; Component.prototype = target.prototype; return new Component(); }; var f = function f() { for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return construct(args); }; f.$inject = deps; f.prototype = target.prototype; return f; }; }; exports.injector = injector;