Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make the conditional order factory case insensitive #225

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/composable/ConditionalOrderFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,31 @@ import { ConditionalOrderParams } from './types'
export type FromParams<D, S> = (params: ConditionalOrderParams) => ConditionalOrder<D, S>
export type ConditionalOrderRegistry = Record<string, FromParams<unknown, unknown>>

/**
* Factory for conditional orders.
*
* It uses a registry to instantiate the correct conditional order based on the handler.
*
* Knowing the handler, the factory will instantiate the correct conditional order using the staticInput data.
*/
export class ConditionalOrderFactory {
public knownOrderTypes

constructor(registry: ConditionalOrderRegistry) {
this.knownOrderTypes = registry
// Normalize the keys to lowercase
this.knownOrderTypes = Object.entries(registry).reduce<ConditionalOrderRegistry>((acc, [key, value]) => {
acc[key.toLowerCase()] = value
return acc
}, {})
}

/**
* Get the conditional order factory from the conditional order parameters
*/
public fromParams(params: ConditionalOrderParams): ConditionalOrder<unknown, unknown> | undefined {
const { handler } = params

const factory = this.knownOrderTypes[handler]
const factory = this.knownOrderTypes[handler.toLocaleLowerCase()]
if (!factory) {
return undefined
}
Expand Down
Loading