From 65b037b95e267a3119e2350ff956b64d2b27c9aa Mon Sep 17 00:00:00 2001 From: Alexandr Rodik Date: Tue, 8 Aug 2017 15:45:02 +0300 Subject: [PATCH] fix: crashes on IE11 and ReflectMetadata typings --- decorators/ng-module/metadata-handlers/import.ts | 12 +++++++++--- example/src/app/index.ts | 3 +++ example/src/index.ejs | 2 +- helpers/array.ts | 15 +++++++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 helpers/array.ts diff --git a/decorators/ng-module/metadata-handlers/import.ts b/decorators/ng-module/metadata-handlers/import.ts index 7bdea60..c1894a4 100644 --- a/decorators/ng-module/metadata-handlers/import.ts +++ b/decorators/ng-module/metadata-handlers/import.ts @@ -1,14 +1,20 @@ +import {findFirst} from "../../../helpers/array"; + type ng1Module = { $inject?: Array } | any; export default function importHandler(imports: Array) { let ng1ModuleIds: Array = []; const modules = imports.filter((mdl: ng1Module) => { - return !(mdl.$inject && mdl.$inject.find((i: any) => i === "$stateProvider")); + return !( + mdl.$inject + && findFirst(mdl.$inject, (i: any) => i === "$stateProvider") + ); }); - const ng1RouterConfig = imports.find((mdl: ng1Module) => { - return mdl.$inject && mdl.$inject.find((i: any) => i === "$stateProvider"); + const ng1RouterConfig = findFirst(imports, (mdl: ng1Module) => { + return mdl.$inject + && findFirst(mdl.$inject, (i: any) => i === "$stateProvider"); }); if (modules.length) { diff --git a/example/src/app/index.ts b/example/src/app/index.ts index 31d7c39..9739f27 100644 --- a/example/src/app/index.ts +++ b/example/src/app/index.ts @@ -1,11 +1,14 @@ const {NgModule, BrowserModule, UIRouterModule} = require("./export-switch"); +import "reflect-metadata"; + import {AppComponent} from "./component"; import {NgShiftModule} from "./module"; import {NgShiftComponentModule} from "./component/index"; import {NgShiftRouterTestModule} from "./router"; @NgModule({ + id: "app-module", imports: [ ...UIRouterModule, ...BrowserModule, diff --git a/example/src/index.ejs b/example/src/index.ejs index 0f9cd55..f6bf277 100644 --- a/example/src/index.ejs +++ b/example/src/index.ejs @@ -15,7 +15,7 @@ <% } else { %> - + NG1-shift Example diff --git a/helpers/array.ts b/helpers/array.ts new file mode 100644 index 0000000..98fc4d7 --- /dev/null +++ b/helpers/array.ts @@ -0,0 +1,15 @@ +export function findFirst(array: Array, expression: (value: T) => boolean): T { + if (!angular.isFunction(expression)) { + let first = array[0]; + if (!first) { + return null; + } + return first; + } + for (let i = 0; i < array.length; i++) { + if (expression(array[i])) { + return array[i]; + } + } + return null; +}