I'm trying to write a JavaScript class constructor that can accept multiple options (just like some command-line tools, e.g. OpenSSL). For example:
class myClass {
constructor(pathOrSize, isPublic) {
// Receive an file path
if (typeof pathOrSize === 'string') {
if (isPublic) {
// Read public key file
this.public = ...
} else {
// Read private key file, and then generate public key instance
this.private = ...
this.public = ...
}
} else if (typeof pathOrSize === 'number') {
// Create a new key pair based on the given size
this.private = ...
this.public = ...
} else {
// Throw an error
}
}
// Use this.public (or this.private, if provided) to encrypt/decrypt message
}
When the class is instantiated, this.public
is required, but this.private
is optional. The conditional logic guarantees that if this.public
is not provided, an error will be thrown.
For the first parameter pathOrSize
, there are two possible input types so I use an if-else here to check the type. Then if it's string
, based on the value of isPublic
, we will get two more different scenarios that need to be handled. In my case, there are more options, which make the code snippet above looks even worse. It works, but it's verbose and hard to read :(
Given the fact that JavaScript doesn't support overloading (like Java) and pattern matching (like Rust), what's the most elegant or efficient way to handle situations like this?
Thanks for any solutions or thoughts!