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

Should Network.create() take a specific type as its parameter? #1126

Open
haxwell opened this issue Jan 14, 2023 · 1 comment · May be fixed by #1136
Open

Should Network.create() take a specific type as its parameter? #1126

haxwell opened this issue Jan 14, 2023 · 1 comment · May be fixed by #1136

Comments

@haxwell
Copy link

haxwell commented Jan 14, 2023

In network.js..

static create(options) {
    if (typeof options === 'string')
      options = networks[options];

    assert(options, 'Unknown network.');

    if (Network[options.type])               //  <<< Line 136
      return Network[options.type];

    const network = new Network(options);

    Network[network.type] = network;

    if (!Network.primary)
      Network.primary = network;

    return network;
  }

This method will take a string, or a set of key-value pairs it uses as options. Still, it is possible to send an empty object, or other not-specifically-handled type, and it would cause an exception on line 136. Should we create a type for these options, and use instanceof like, for example the get() method a little further down in network.js...

  static get(type) {
    if (!type) {
      assert(Network.primary, 'No default network.');
      return Network.primary;
    }

    if (type instanceof Network)    //    Like this here..
      return type;

    if (typeof type === 'string')
      return Network.create(type);

    throw new Error('Unknown network.');
  }
@yatharthagoenka
Copy link

Can't we just check if options is an object instance, and if so whether it has an attribute 'type' of type string using the assert function? In case this doesn't check out, the function would simply return a message invalidating the object, and avoid the issue altogether.
If it isn't an object, we could carry out the function as done already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants