[gjs/ewlsh/whatwg-console: 1/7] esm: Cleanup JSDoc type assertions, use appropriate type guards




commit cdcce163cbd8f7666432a4c7d61dace05ca8a55d
Author: Evan Welsh <contact evanwelsh com>
Date:   Sat Aug 14 15:43:41 2021 -0700

    esm: Cleanup JSDoc type assertions, use appropriate type guards

 modules/esm/_encoding/encoding.js | 86 ++++++++++++++++++++++++++++-----------
 1 file changed, 62 insertions(+), 24 deletions(-)
---
diff --git a/modules/esm/_encoding/encoding.js b/modules/esm/_encoding/encoding.js
index e84b752b..1773d987 100644
--- a/modules/esm/_encoding/encoding.js
+++ b/modules/esm/_encoding/encoding.js
@@ -5,6 +5,46 @@ const Encoding = import.meta.importSync('_encodingNative');
 
 import {getEncodingFromLabel} from './encodingMap.js';
 
+/**
+ * @param {unknown} value
+ * @returns {value is Record<string, unknown>}
+ */
+function isObject(value) {
+    if (typeof value === 'object' && value !== null) {
+        /** @type {Record<string, any>} */
+        let _ = value;
+
+        return true;
+    }
+
+    return false;
+}
+
+/**
+ * @param {unknown} value
+ * @returns {value is ArrayBufferView}
+ */
+function isArrayBufferView(value) {
+    if (!isObject(value)) return false;
+
+    return (
+        'buffer' in value &&
+        'byteLength' in value &&
+        'byteOffset' in value &&
+        typeof value.buffer === 'object' &&
+        value.buffer instanceof ArrayBuffer &&
+        typeof value.byteLength === 'number' &&
+        typeof value.byteOffset === 'number'
+    );
+}
+
+// "TypedArray" doesn't actually exist in the type system,
+// but we can mimic it by saying this constructor could
+// produce multiple typed array types.
+
+/** @type {Uint8ArrayConstructor | Uint32ArrayConstructor} */
+const TypedArrayConstructor = Object.getPrototypeOf(Uint8Array);
+
 class TextDecoder {
     /**
      * @type {string}
@@ -21,6 +61,12 @@ class TextDecoder {
      */
     fatal;
 
+    /**
+     * @private
+     * @type {string}
+     */
+    _internalEncoding;
+
     get [Symbol.toStringTag]() {
         return 'TextDecoder';
     }
@@ -39,7 +85,6 @@ class TextDecoder {
         if (!encodingDefinition)
             throw new RangeError(`Invalid encoding label: '${encoding}'`);
 
-
         if (encodingDefinition.label === 'replacement') {
             throw new RangeError(
                 `Unsupported replacement encoding: '${encoding}'`
@@ -97,17 +142,12 @@ class TextDecoder {
             input = new Uint8Array(bytes);
         } else if (bytes instanceof Uint8Array) {
             input = bytes;
-        } else if (bytes instanceof Object.getPrototypeOf(Uint8Array)) {
-            let {buffer, byteLength, byteOffset} =
-                /** @type {Uint32Array} */ bytes;
+        } else if (bytes instanceof TypedArrayConstructor) {
+            let {buffer, byteLength, byteOffset} = bytes;
             input = new Uint8Array(buffer, byteOffset, byteLength);
-        } else if (
-            typeof bytes === 'object' &&
-            bytes !== null &&
-            'buffer' in bytes &&
-            bytes.buffer instanceof ArrayBuffer
-        ) {
+        } else if (isArrayBufferView(bytes)) {
             let {buffer, byteLength, byteOffset} = bytes;
+
             input = new Uint8Array(buffer, byteOffset, byteLength);
         } else if (bytes === undefined) {
             input = new Uint8Array(0);
@@ -127,7 +167,6 @@ class TextDecoder {
             if (this.encoding !== 'utf-8')
                 throw new Error('Cannot ignore BOM for non-UTF8 encoding.');
 
-
             let {buffer, byteLength, byteOffset} = input;
             input = new Uint8Array(buffer, byteOffset + 3, byteLength - 3);
         }
@@ -156,17 +195,16 @@ class TextEncoder {
     }
 }
 
-Object.defineProperties(globalThis, {
-    TextEncoder: {
-        configurable: false,
-        enumerable: true,
-        writable: false,
-        value: TextEncoder,
-    },
-    TextDecoder: {
-        configurable: false,
-        enumerable: true,
-        writable: false,
-        value: TextDecoder,
-    },
+Object.defineProperty(globalThis, 'TextEncoder', {
+    configurable: false,
+    enumerable: true,
+    writable: false,
+    value: TextEncoder,
+});
+
+Object.defineProperty(globalThis, 'TextDecoder', {
+    configurable: false,
+    enumerable: true,
+    writable: false,
+    value: TextDecoder,
 });


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]