Skip to content

Commit

Permalink
feat(utilities): Added base Factory class and some update lodash fu…
Browse files Browse the repository at this point in the history
…nction implementations
  • Loading branch information
sullivanpj committed Jan 7, 2024
1 parent 2140487 commit cb3f844
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/utilities/src/base/factory.ts
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}`
);
}
}
1 change: 1 addition & 0 deletions packages/utilities/src/base/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./factory";
12 changes: 12 additions & 0 deletions packages/utilities/src/helper-fns/arg-identity.ts
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;
}
9 changes: 9 additions & 0 deletions packages/utilities/src/helper-fns/get-unique.ts
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)];
};
2 changes: 2 additions & 0 deletions packages/utilities/src/helper-fns/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export * from "./arg-identity";
export * from "./deep-copy";
export * from "./deep-merge";
export * from "./flatten-object";
export * from "./get-unique";
export * from "./is-deep-equal";
export * from "./is-production";
export * from "./is-runtime-server";
Expand Down

0 comments on commit cb3f844

Please sign in to comment.