-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utilities): Added base
Factory
class and some update lodash fu…
…nction implementations
- Loading branch information
1 parent
2140487
commit cb3f844
Showing
5 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Factory class that returns an object instance based on some context. | ||
*/ | ||
export class Factory { | ||
/** | ||
* Instantiate the object. | ||
* Any additional arguments are passed to the object constructor. | ||
* | ||
* @param context - The information we base on to decide which object to return. | ||
* @returns The instantiated object. | ||
*/ | ||
public static create<TClass = any>(context: any) { | ||
const constructorArgs = Array.prototype.slice.call(arguments); | ||
constructorArgs.shift(); | ||
|
||
const Implementor = this.getClass<TClass>(context); | ||
|
||
// This is the ES5 friendly version of new Implementor(...constructorArgs) | ||
return new (Function.prototype.bind.apply( | ||
Implementor, | ||
[null].concat(constructorArgs) as [thisArg: any, ...argArray: any[]] | ||
))(); | ||
} | ||
|
||
/** | ||
* Decide which class to instantiate based on the context. | ||
* | ||
* @param context - The information we base on to decide which object to return. | ||
* @returns The class to instantiate. | ||
*/ | ||
protected static getClass<TClass = any>( | ||
context: any | ||
): new (...argArray: any[]) => TClass { | ||
throw new Error( | ||
`The \`Factory.getClass\` method should be implemented in the factory subclasses. Context: ${context}` | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./factory"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* This method returns the first argument provided to it. | ||
* | ||
* @remarks | ||
* For more info, please see {@link https://lodash.com/docs/4.17.15#identity | the original Lodash documentation}. | ||
* | ||
* @param value - The value to return. | ||
* @returns The value provided. | ||
*/ | ||
export function argIdentity(value: any) { | ||
return value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Returns an array of unique values from the given array. | ||
* | ||
* @param arr - The array to get unique values from. | ||
* @returns An array of unique values. | ||
*/ | ||
export const getUnique = <T = any>(arr: T[]): T[] => { | ||
return [...new Set<T>(arr)]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters