From b66cc4369e40ce826838e523dd916d2204c9d733 Mon Sep 17 00:00:00 2001 From: Cashew Date: Wed, 27 Dec 2023 07:54:20 +0700 Subject: [PATCH 1/4] switch updates --- dev/util.js | 53 +++++++++++++------- ext/js/data/json-schema.js | 20 +++++--- ext/js/display/display-generator.js | 23 ++++++--- ext/js/general/object-property-accessor.js | 58 +++++++++++++--------- 4 files changed, 100 insertions(+), 54 deletions(-) diff --git a/dev/util.js b/dev/util.js index 542ad6a29e..731b545622 100644 --- a/dev/util.js +++ b/dev/util.js @@ -45,25 +45,44 @@ export function getArgs(args, argMap) { } const target = argMap.get(key); - if (typeof target === 'boolean') { - argMap.set(key, true); - key = null; - } else if (typeof target === 'number') { - argMap.set(key, target + 1); - key = null; - } else if (target === null || typeof target === 'string') { - if (!onKey) { - argMap.set(key, arg); + + switch (typeof target) { + case 'boolean': + argMap.set(key, true); key = null; - } - } else if (Array.isArray(target)) { - if (!onKey) { - target.push(arg); + break; + case 'number': + argMap.set(key, target + 1); key = null; - } - } else { - console.error(`Unknown argument: ${arg}`); - key = null; + break; + case 'string': + if (!onKey) { + argMap.set(key, arg); + key = null; + } + break; + case 'object': + if (target === null) { + if (!onKey) { + argMap.set(key, arg); + key = null; + } + return argMap; + } else if (Array.isArray(target)) { + if (!onKey) { + target.push(arg); + key = null; + } + return argMap; + } else { + console.error(`Unknown argument: ${arg}`); + key = null; + } + break; + default: + console.error(`Unknown argument: ${arg}`); + key = null; + break; } } diff --git a/ext/js/data/json-schema.js b/ext/js/data/json-schema.js index 08414164f7..664cc187af 100644 --- a/ext/js/data/json-schema.js +++ b/ext/js/data/json-schema.js @@ -1343,12 +1343,20 @@ class JsonSchemaProxyHandler { * @returns {?number} */ _getArrayIndex(property) { - if (typeof property === 'string' && this._numberPattern.test(property)) { - return Number.parseInt(property, 10); - } else if (typeof property === 'number' && Math.floor(property) === property && property >= 0) { - return property; - } else { - return null; + switch (typeof property) { + case 'string': + if (this._numberPattern.test(property)) { + return Number.parseInt(property, 10); + } + break; + case 'number': + if (Math.floor(property) === property && property >= 0) { + return property; + } + break; + default: + break; } + return null; } } diff --git a/ext/js/display/display-generator.js b/ext/js/display/display-generator.js index 920d2c94c1..a86a225cc5 100644 --- a/ext/js/display/display-generator.js +++ b/ext/js/display/display-generator.js @@ -401,15 +401,22 @@ export class DisplayGenerator { * @returns {?HTMLElement} */ _createTermDefinitionEntry(entry, dictionary) { - if (typeof entry === 'string') { - return this._createTermDefinitionEntryText(entry); - } else if (typeof entry === 'object' && entry !== null) { - switch (entry.type) { - case 'image': - return this._createTermDefinitionEntryImage(entry, dictionary); - case 'structured-content': - return this._createTermDefinitionEntryStructuredContent(entry.content, dictionary); + switch (typeof entry) { + case 'string': + return this._createTermDefinitionEntryText(entry); + case 'object': { + switch (entry.type) { + case 'image': + return this._createTermDefinitionEntryImage(entry, dictionary); + case 'structured-content': + return this._createTermDefinitionEntryStructuredContent(entry.content, dictionary); + case 'text': + break; + } + break; } + default: + break; } return null; diff --git a/ext/js/general/object-property-accessor.js b/ext/js/general/object-property-accessor.js index d818c9d1de..0d82a9117b 100644 --- a/ext/js/general/object-property-accessor.js +++ b/ext/js/general/object-property-accessor.js @@ -207,16 +207,21 @@ export class ObjectPropertyAccessor { v === 0x5f // '_' ) { value += c; - } else if (v === 0x5b) { // '[' - pathArray.push(value); - value = ''; - state = 'open-bracket'; - } else if (v === 0x2e) { // '.' - pathArray.push(value); - value = ''; - state = 'id-start'; } else { - throw new Error(`Unexpected character: ${c}`); + switch (v) { + case 0x5b: // '[' + pathArray.push(value); + value = ''; + state = 'open-bracket'; + break; + case 0x2e: // '.' + pathArray.push(value); + value = ''; + state = 'id-start'; + break; + default: + throw new Error(`Unexpected character: ${c}`); + } } break; case 'open-bracket': // Open bracket @@ -253,24 +258,31 @@ export class ObjectPropertyAccessor { throw new Error(`Unexpected character: ${c}`); } break; - case 'close-bracket': // Expecting closing bracket after quoted string - if (v === 0x5d) { // ']' - pathArray.push(value); - value = ''; - state = 'next'; - } else { - throw new Error(`Unexpected character: ${c}`); + case 'close-bracket': { // Expecting closing bracket after quoted string + switch (v) { + case 0x5d: // ']' + pathArray.push(value); + value = ''; + state = 'next'; + break; + default: + throw new Error(`Unexpected character: ${c}`); } break; - case 'next': // Expecting . or [ - if (v === 0x5b) { // '[' - state = 'open-bracket'; - } else if (v === 0x2e) { // '.' - state = 'id-start'; - } else { - throw new Error(`Unexpected character: ${c}`); + } + case 'next': { // Expecting . or [ + switch (v) { + case 0x5b: // '[' + state = 'open-bracket'; + break; + case 0x2e: // '.' + state = 'id-start'; + break; + default: + throw new Error(`Unexpected character: ${c}`); } break; + } } } switch (state) { From 5d3e20360b31f3f9e765de6e0cb7a41090ee0a8b Mon Sep 17 00:00:00 2001 From: Cashew Date: Wed, 27 Dec 2023 08:48:21 +0700 Subject: [PATCH 2/4] revert to if-else --- ext/js/general/object-property-accessor.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/ext/js/general/object-property-accessor.js b/ext/js/general/object-property-accessor.js index 0d82a9117b..5851b00873 100644 --- a/ext/js/general/object-property-accessor.js +++ b/ext/js/general/object-property-accessor.js @@ -259,14 +259,12 @@ export class ObjectPropertyAccessor { } break; case 'close-bracket': { // Expecting closing bracket after quoted string - switch (v) { - case 0x5d: // ']' - pathArray.push(value); - value = ''; - state = 'next'; - break; - default: - throw new Error(`Unexpected character: ${c}`); + if (v === 0x5d) { // ']' + pathArray.push(value); + value = ''; + state = 'next'; + } else { + throw new Error(`Unexpected character: ${c}`); } break; } From 4f1299f209ac1cf0528d0d973c9eade8e07466b5 Mon Sep 17 00:00:00 2001 From: Cashew Date: Wed, 27 Dec 2023 08:49:35 +0700 Subject: [PATCH 3/4] revert to if-else --- ext/js/general/object-property-accessor.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ext/js/general/object-property-accessor.js b/ext/js/general/object-property-accessor.js index 5851b00873..b8d6ddc546 100644 --- a/ext/js/general/object-property-accessor.js +++ b/ext/js/general/object-property-accessor.js @@ -258,7 +258,7 @@ export class ObjectPropertyAccessor { throw new Error(`Unexpected character: ${c}`); } break; - case 'close-bracket': { // Expecting closing bracket after quoted string + case 'close-bracket': // Expecting closing bracket after quoted string if (v === 0x5d) { // ']' pathArray.push(value); value = ''; @@ -267,7 +267,6 @@ export class ObjectPropertyAccessor { throw new Error(`Unexpected character: ${c}`); } break; - } case 'next': { // Expecting . or [ switch (v) { case 0x5b: // '[' From ca0ed1212feb19fdce9ba22140421d30a026e8e0 Mon Sep 17 00:00:00 2001 From: Cashew Date: Wed, 27 Dec 2023 08:53:31 +0700 Subject: [PATCH 4/4] remove empty default --- ext/js/data/json-schema.js | 2 -- ext/js/display/display-generator.js | 2 -- 2 files changed, 4 deletions(-) diff --git a/ext/js/data/json-schema.js b/ext/js/data/json-schema.js index 664cc187af..ee58976ebf 100644 --- a/ext/js/data/json-schema.js +++ b/ext/js/data/json-schema.js @@ -1354,8 +1354,6 @@ class JsonSchemaProxyHandler { return property; } break; - default: - break; } return null; } diff --git a/ext/js/display/display-generator.js b/ext/js/display/display-generator.js index a86a225cc5..b91d0ce917 100644 --- a/ext/js/display/display-generator.js +++ b/ext/js/display/display-generator.js @@ -415,8 +415,6 @@ export class DisplayGenerator { } break; } - default: - break; } return null;