From 5e4f7aa852d3b1e9000b088fda6f2e8e1bfe70f4 Mon Sep 17 00:00:00 2001 From: Tobias Frey Date: Wed, 28 Feb 2018 14:31:34 +0100 Subject: [PATCH 1/3] Replace usage of find & findIndex with other methods to support IE & PhantomJS --- src/MethodStubCollection.ts | 12 +++++++++--- src/Mock.ts | 4 ++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/MethodStubCollection.ts b/src/MethodStubCollection.ts index 3f8aa6f..6acaeb1 100644 --- a/src/MethodStubCollection.ts +++ b/src/MethodStubCollection.ts @@ -9,7 +9,7 @@ export class MethodStubCollection { } public getLastMatchingGroupIndex(args): number { - const matchingGroup = _.clone(this.items).reverse().find((item: MethodStub) => item.isApplicable(args)); + const matchingGroup = _.clone(this.items).reverse().filter((item: MethodStub) => item.isApplicable(args))[0]; return matchingGroup ? matchingGroup.getGroupIndex() : -1; } @@ -31,11 +31,17 @@ export class MethodStubCollection { } private getFirstMatchingFromGroup(groupIndex: number, args: any[]): MethodStub { - return this.items.find((item: MethodStub) => item.getGroupIndex() === groupIndex && item.isApplicable(args)); + return this.items.filter((item: MethodStub) => item.getGroupIndex() === groupIndex && item.isApplicable(args))[0]; } private getFirstMatchingIndexFromGroup(groupIndex: number, args: any[]): number { - return this.items.findIndex((item: MethodStub) => item.getGroupIndex() === groupIndex && item.isApplicable(args)); + for (let i = 0; i < this.items.length; i++) { + const item = this.items[i]; + if (item.getGroupIndex() === groupIndex && item.isApplicable(args)) { + return i; + } + } + return -1; } private getItemsCountInGroup(groupIndex: number): number { diff --git a/src/Mock.ts b/src/Mock.ts index d9f68f2..a17cb42 100644 --- a/src/Mock.ts +++ b/src/Mock.ts @@ -133,7 +133,7 @@ export class Mocker { private processClassCode(clazz: any): void { const classCode = typeof clazz.toString !== "undefined" ? clazz.toString() : ""; - const functionNames = this.mockableFunctionsFinder.find(classCode); + const functionNames = this.mockableFunctionsFinder.filter(classCode)[0]; functionNames.forEach((functionName: string) => { this.createMethodStub(functionName); this.createInstanceActionListener(functionName, this.clazz.prototype); @@ -143,7 +143,7 @@ export class Mocker { private processFunctionsCode(object: any): void { this.objectInspector.getObjectPrototypes(object).forEach((obj: any) => { this.objectInspector.getObjectOwnPropertyNames(obj).forEach((propertyName: string) => { - const functionNames = this.mockableFunctionsFinder.find(this.objectPropertyCodeRetriever.get(obj, propertyName)); + const functionNames = this.mockableFunctionsFinder.filter(this.objectPropertyCodeRetriever.get(obj, propertyName))[0]; functionNames.forEach((functionName: string) => { this.createMethodStub(functionName); this.createInstanceActionListener(functionName, this.clazz.prototype); From 5661adea11f4dcec9a7ff22ea02ff6931cbf24d9 Mon Sep 17 00:00:00 2001 From: MasterCassim Date: Wed, 28 Feb 2018 14:46:36 +0100 Subject: [PATCH 2/3] Revered changes in Mock.ts --- src/Mock.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Mock.ts b/src/Mock.ts index a17cb42..d9f68f2 100644 --- a/src/Mock.ts +++ b/src/Mock.ts @@ -133,7 +133,7 @@ export class Mocker { private processClassCode(clazz: any): void { const classCode = typeof clazz.toString !== "undefined" ? clazz.toString() : ""; - const functionNames = this.mockableFunctionsFinder.filter(classCode)[0]; + const functionNames = this.mockableFunctionsFinder.find(classCode); functionNames.forEach((functionName: string) => { this.createMethodStub(functionName); this.createInstanceActionListener(functionName, this.clazz.prototype); @@ -143,7 +143,7 @@ export class Mocker { private processFunctionsCode(object: any): void { this.objectInspector.getObjectPrototypes(object).forEach((obj: any) => { this.objectInspector.getObjectOwnPropertyNames(obj).forEach((propertyName: string) => { - const functionNames = this.mockableFunctionsFinder.filter(this.objectPropertyCodeRetriever.get(obj, propertyName))[0]; + const functionNames = this.mockableFunctionsFinder.find(this.objectPropertyCodeRetriever.get(obj, propertyName)); functionNames.forEach((functionName: string) => { this.createMethodStub(functionName); this.createInstanceActionListener(functionName, this.clazz.prototype); From fcb31ea24f3ba56e5a46f3ef55ba0ab5049a7f80 Mon Sep 17 00:00:00 2001 From: Tobias Frey Date: Wed, 28 Feb 2018 14:54:59 +0100 Subject: [PATCH 3/3] Replacced tabs with spaces according to tslint --- src/MethodStubCollection.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/MethodStubCollection.ts b/src/MethodStubCollection.ts index 6acaeb1..2c7d0d3 100644 --- a/src/MethodStubCollection.ts +++ b/src/MethodStubCollection.ts @@ -35,13 +35,13 @@ export class MethodStubCollection { } private getFirstMatchingIndexFromGroup(groupIndex: number, args: any[]): number { - for (let i = 0; i < this.items.length; i++) { - const item = this.items[i]; - if (item.getGroupIndex() === groupIndex && item.isApplicable(args)) { - return i; - } - } - return -1; + for (let i = 0; i < this.items.length; i++) { + const item = this.items[i]; + if (item.getGroupIndex() === groupIndex && item.isApplicable(args)) { + return i; + } + } + return -1; } private getItemsCountInGroup(groupIndex: number): number {