Imagine a scenario where there is a directory structure like the one below:
Shapes/
Square.ts
Circle.ts
Rectangle.ts
The objective here is to create a data structure, either a Map
or an object
, where the filename (without the extension) serves as the key and the corresponding class within the file acts as the value. This will allow for easy initialization using the new
keyword.
For instance:
import Application from './Application' // This would be the entry point of the application
let instances: Map<string, Shape> = Application.getShapes() // The 'getShapes' should return a Map with the specified format.
let square = new instanes.get("Square") // Expectation is to instantiate a Square object
The goal here is to move away from manually creating instances and instead dynamically generate them.
import AlnumRule from './Rules/AlnumRule'
import Rule from './Rules/Contracts/Rule'
import RequiredRule from './Rules/RequiredRule'
export default class RulesMapper {
static map: { [index: string]: Rule } = {
required: new RequiredRule(),
alnum: new AlnumRule(),
}
static resolve(rule: string): Rule {
return RulesMapper.map[rule]
}
}
To put it simply, the aim is to scan all files within a specific directory, extract the constructible classes, and then store them in either an object or a Map for future use.