[gjs/ewlsh/whatwg-console: 1/8] modules: Cleanup JSDoc, use ArrayBuffer.isView(), and use Object.defineProperty




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

    modules: Cleanup JSDoc, use ArrayBuffer.isView(), and use Object.defineProperty
    
    We don't need to roll our own check for ArrayBuffers. TypeScript linting can only
    check Object.defineProperty, assigning multiple properties in a single call is
    unsupported.

 modules/esm/_encoding/encoding.js | 45 +++++++++++++++++----------------------
 1 file changed, 20 insertions(+), 25 deletions(-)
---
diff --git a/modules/esm/_encoding/encoding.js b/modules/esm/_encoding/encoding.js
index e84b752b..41261b96 100644
--- a/modules/esm/_encoding/encoding.js
+++ b/modules/esm/_encoding/encoding.js
@@ -21,6 +21,12 @@ class TextDecoder {
      */
     fatal;
 
+    /**
+     * @private
+     * @type {string}
+     */
+    _internalEncoding;
+
     get [Symbol.toStringTag]() {
         return 'TextDecoder';
     }
@@ -39,7 +45,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 +102,9 @@ 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;
-            input = new Uint8Array(buffer, byteOffset, byteLength);
-        } else if (
-            typeof bytes === 'object' &&
-            bytes !== null &&
-            'buffer' in bytes &&
-            bytes.buffer instanceof ArrayBuffer
-        ) {
+        } else if (ArrayBuffer.isView(bytes)) {
             let {buffer, byteLength, byteOffset} = bytes;
+
             input = new Uint8Array(buffer, byteOffset, byteLength);
         } else if (bytes === undefined) {
             input = new Uint8Array(0);
@@ -127,7 +124,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 +152,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]