Skip to content

Commit

Permalink
fix(*): allow factory/service/constant/value to pass objects
Browse files Browse the repository at this point in the history
Fixes #23
  • Loading branch information
Jason Bedard committed Apr 5, 2017
1 parent 1350bb6 commit 1d93647
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 28 deletions.
36 changes: 36 additions & 0 deletions src/facade.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,13 @@ describe("facade", function() {

expect(fetched).toBe(42);
});

it("should support {key: value} Objects", function() {
mod.constant({foo: 42});
mod.run(["foo", function(foo) {
expect(foo).toBe(42);
}]);
});
});

describe("value", function() {
Expand All @@ -844,6 +851,13 @@ describe("facade", function() {

expect(fetched).toBe(42);
});

it("should support {key: value} Objects", function() {
mod.value({foo: 42});
mod.run(["foo", function(foo) {
expect(foo).toBe(42);
}]);
});
});

describe("service", function() {
Expand All @@ -866,6 +880,14 @@ describe("facade", function() {

expect(fetched).toEqual(jasmine.any(TheType));
});

it("should support {key: value} Objects", function() {
class TheType {}
mod.service({foo: TheType});
mod.run(["foo", function(foo) {
expect(foo).toEqual(jasmine.any(TheType));
}]);
});
});

describe("factory", function() {
Expand All @@ -886,6 +908,13 @@ describe("facade", function() {

expect(fetched).toBe(42);
});

it("should support {key: value} Objects", function() {
mod.factory({foo() { return 42; }});
mod.run(["foo", function(foo) {
expect(foo).toBe(42);
}]);
});
});

describe("decorator", function() {
Expand Down Expand Up @@ -942,6 +971,13 @@ describe("facade", function() {

expect(fetched).toBe(42);
});

it("should support {key: value} Objects", function() {
mod.provider({foo: {$get() { return 42; }}});
mod.run(["foo", function(foo) {
expect(foo).toBe(42);
}]);
});
});
});

Expand Down
48 changes: 20 additions & 28 deletions src/facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {extend, identity, module, noop} from "angular";

function valueFn<T>(v: T): () => T { return () => v; };


//https://github.com/angular/angular/blob/2.4.8/modules/%40angular/core/src/type.ts
const Type = Function;
export interface Type<T> extends Function { new (...args: any[]): T; }
Expand Down Expand Up @@ -793,39 +792,32 @@ module("ng").decorator("$injector", ["$delegate", function(injector: angular.aut
// Decorate (at config) the AngularJS $provide to allow non-string IDs.
// Follow the Types + arguments declared in @types definition + the ng-facade overrides
module("ng").config(["$provide", function(provide: angular.auto.IProvideService): void {
const {constant, decorator, factory, provider, service, value} = provide;
["constant", "value", "factory", "provider", "service"].forEach(function(method) {
const delegate = provide[method];

// constant(type: Type<any>, value: any): void;
provide.constant = function diConstant(this: angular.auto.IProvideService, type: Type<any> | string, v: any): void {
constant.call(this, toTypeName(type), v);
};
function diProvideWrapper(this: angular.auto.IProvideService, key: string | Type<any>, value: Function | any | any[]): angular.IServiceProvider;
function diProvideWrapper(this: angular.auto.IProvideService, key: string | Type<any>, value: angular.IServiceProvider): angular.IServiceProvider;
function diProvideWrapper(this: angular.auto.IProvideService, multi: {key: string, value: any}): void;

// value(type: Type<any>, value: any): angular.IServiceProvider;
provide.value = function diValue(this: angular.auto.IProvideService, type: Type<any> | string, v: any): angular.IServiceProvider {
return value.call(this, toTypeName(type), v);
};
function diProvideWrapper(this: angular.auto.IProvideService, key: string | Type<any> | {key: string, value: any}, value?: Function | angular.IServiceProvider | any[]): angular.IServiceProvider | void {
if (arguments.length === 1) {
for (const objKey in <Object>key) {
delegate(objKey, key[objKey]);
}
}
else {
return delegate(toTypeName(<string | Type<any>>key), <Function | any | any[]>value);
}
}

provide[method] = diProvideWrapper;
});

const decorator = provide.decorator;

// decorator(type: Type<any>, decorator: Function): void;
// decorator(type: Type<any>, inlineAnnotatedFunction: any[]): void;
provide.decorator = function diDecorator(this: angular.auto.IProvideService, type: Type<any> | string, dec: Function | any[]): void {
decorator.call(this, toTypeName(type), dec);
};

// factory(type: Type<any>, serviceFactoryFunction: Function): angular.IServiceProvider;
// factory(type: Type<any>, inlineAnnotatedFunction: any[]): angular.IServiceProvider;
provide.factory = function diFactory(this: angular.auto.IProvideService, type: Type<any> | string, f: Function | any[]): angular.IServiceProvider {
return factory.call(this, toTypeName(type), f);
};

// provider(type: Type<any>, provider: angular.IServiceProvider): angular.IServiceProvider;
// provider(type: Type<any>, serviceProviderConstructor: Function): angular.IServiceProvider;
provide.provider = function diProvider(this: angular.auto.IProvideService, type: Type<any> | string, p: angular.IServiceProvider | Function): angular.IServiceProvider {
return provider.call(this, toTypeName(type), p);
};

// service(type: Type<any>, constructor: Function): angular.IServiceProvider;
// service(type: Type<any>, inlineAnnotatedFunction: any[]): angular.IServiceProvider;
provide.service = function diService(this: angular.auto.IProvideService, type: Type<any> | string, s: Function | any[]): angular.IServiceProvider {
return service.call(this, toTypeName(type), s);
};
}]);

0 comments on commit 1d93647

Please sign in to comment.