Skip to content

Commit

Permalink
Implement solution for removing casting
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikola Mihaylov committed Mar 29, 2021
1 parent 6790988 commit 9624cc7
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Obfuscator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ class Obfuscator {
}

public obfuscateObject<T extends PlainObject | Error>(object: T, obfuscateSettings: Array<[string, Tag]>): T {
if (this.isPlainObject(object)) {
if (this.isError(object)) {
return this.obfuscateError(object, obfuscateSettings);
} else if (this.isPlainObject(object)) {
return this.obfuscatePlainObject(object, obfuscateSettings);
}

return this.obfuscateError(object as Error, obfuscateSettings) as T;
throw new Error('Input must be plain object or a class inheriting from Error');

}

private obfuscatePlainObject<T extends PlainObject>(plainObject: T, obfuscateSettings: Array<[string, Tag]>): T {
Expand Down Expand Up @@ -89,6 +92,10 @@ class Obfuscator {
private isPlainObject(value: unknown): value is PlainObject {
return lodash.isPlainObject(value);
}

private isError(value: unknown): value is Error {
return value instanceof Error;
}
}

export {
Expand Down

0 comments on commit 9624cc7

Please sign in to comment.