Replace deprecated substr with substring (#355)

* Make the QueryRunner more readable

* Remove use of deprecated substr and replace with substring, and some formatting
This commit is contained in:
CosminPerRam 2023-09-13 17:31:58 +03:00 committed by GitHub
parent 65dd876252
commit 5b01e1be17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 66 additions and 61 deletions

View file

@ -9,16 +9,20 @@ class GameResolver {
}
lookup(type) {
if(!type) throw Error('No game specified');
if(!type)
throw Error('No game specified');
if(type.substr(0,9) === 'protocol-') {
if(type.substring(0,9) === 'protocol-') {
return {
protocol: type.substr(9)
protocol: type.substring(9)
};
}
const game = this.gamesByKey.get(type);
if(!game) throw Error('Invalid game: '+type);
if(!game)
throw Error('Invalid game: '+type);
return game.options;
}
@ -61,7 +65,7 @@ class GameResolver {
for (let line of lines) {
// strip comments
const comment = line.indexOf('#');
if(comment !== -1) line = line.substr(0,comment);
if(comment !== -1) line = line.substring(0,comment);
line = line.trim();
if(!line) continue;
@ -82,27 +86,34 @@ class GameResolver {
for (const key of keys) {
gamesByKey.set(key, game);
}
games.push(game);
}
return { gamesByKey, games };
}
_parseList(str) {
if(!str) return {};
const out = {};
if(!str)
return {};
let out = {};
for (const one of str.split(',')) {
const equals = one.indexOf('=');
const key = equals === -1 ? one : one.substr(0,equals);
const key = equals === -1 ? one : one.substring(0, equals);
/** @type {string|number|boolean} */
let value = equals === -1 ? '' : one.substr(equals+1);
let value = equals === -1 ? '' : one.substring(equals + 1);
if(value === 'true' || value === '') value = true;
else if(value === 'false') value = false;
else if(!isNaN(parseInt(value))) value = parseInt(value);
if(value === 'true' || value === '')
value = true;
else if(value === 'false')
value = false;
else if(!isNaN(parseInt(value)))
value = parseInt(value);
out[key] = value;
}
return out;
}
}