LCOV - code coverage report
Current view: top level - modules/script - byteArray.js (source / functions) Coverage Total Hit
Test: gjs- Code Coverage Lines: 88.1 % 42 37
Test Date: 2024-04-20 17:42:51 Functions: 100.0 % 9 9
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 83.3 % 18 15

             Branch data     Line data    Source code
       1                 :           1 : /* exported ByteArray, fromArray, fromGBytes, fromString, toGBytes, toString */
       2                 :             : // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
       3                 :             : // SPDX-FileCopyrightText: 2017 Philip Chimento <philip.chimento@gmail.com>
       4                 :             : 
       5                 :             : // Allow toString to be declared.
       6                 :             : /* eslint no-redeclare: ["error", { "builtinGlobals": false }] */
       7                 :             : 
       8                 :           1 : var {fromGBytes, fromString, toString} = imports._byteArrayNative;
       9                 :             : 
      10                 :           1 : const {GLib} = imports.gi;
      11                 :             : 
      12                 :             : // For backwards compatibility
      13                 :             : 
      14                 :             : /**
      15                 :             :  * @param {Iterable<number>} array an iterable to convert into a ByteArray
      16                 :             :  *   wrapper
      17                 :             :  * @returns {ByteArray}
      18                 :             :  */
      19                 :           2 : function fromArray(array) {
      20                 :           2 :     return new ByteArray(Uint8Array.from(array));
      21                 :             : }
      22                 :             : 
      23                 :             : /**
      24                 :             :  * @param {Uint8Array} array the Uint8Array to convert to GLib.Bytes
      25                 :             :  * @returns {GLib.Bytes}
      26                 :             :  */
      27                 :           3 : function toGBytes(array) {
      28         [ +  + ]:           3 :     if (!(array instanceof Uint8Array))
      29                 :           1 :         throw new Error('Argument to ByteArray.toGBytes() must be a Uint8Array');
      30                 :             : 
      31                 :           2 :     return new GLib.Bytes(array);
      32                 :             : }
      33                 :             : 
      34                 :           1 : var ByteArray = class ByteArray {
      35         [ +  + ]:          14 :     constructor(arg = 0) {
      36         [ +  + ]:          14 :         if (arg instanceof Uint8Array)
      37                 :           2 :             this._array = arg;
      38                 :             :         else
      39                 :          12 :             this._array = new Uint8Array(arg);
      40                 :          14 :         return new Proxy(this, ByteArray);
      41                 :             :     }
      42                 :             : 
      43                 :           1 :     static get(target, prop, receiver) {
      44         [ +  + ]:        1350 :         if (!Number.isNaN(Number.parseInt(prop)))
      45                 :         281 :             return Reflect.get(target._array, prop);
      46                 :        1069 :         return Reflect.get(target, prop, receiver);
      47                 :             :     }
      48                 :             : 
      49                 :         273 :     static set(target, prop, val, receiver) {
      50                 :         272 :         let ix = Number.parseInt(prop);
      51         [ +  + ]:         272 :         if (!Number.isNaN(ix)) {
      52         [ +  + ]:         270 :             if (ix >= target._array.length) {
      53                 :           6 :                 let newArray = new Uint8Array(ix + 1);
      54                 :           6 :                 newArray.set(target._array);
      55                 :           6 :                 target._array = newArray;
      56                 :             :             }
      57                 :         270 :             return Reflect.set(target._array, prop, val);
      58                 :             :         }
      59                 :           2 :         return Reflect.set(target, prop, val, receiver);
      60                 :         272 :     }
      61                 :             : 
      62                 :           1 :     get length() {
      63                 :         532 :         return this._array.length;
      64                 :             :     }
      65                 :             : 
      66                 :           2 :     set length(newLength) {
      67         [ +  - ]:           1 :         if (newLength === this._array.length)
      68                 :           0 :             return;
      69         [ -  + ]:           1 :         if (newLength < this._array.length) {
      70                 :           1 :             this._array = new Uint8Array(this._array.buffer, 0, newLength);
      71                 :           1 :             return;
      72                 :             :         }
      73                 :           0 :         let newArray = new Uint8Array(newLength);
      74                 :           0 :         newArray.set(this._array);
      75                 :           0 :         this._array = newArray;
      76                 :           1 :     }
      77                 :             : 
      78         [ -  + ]:           2 :     toString(encoding = 'UTF-8') {
      79                 :           1 :         return toString(this._array, encoding);
      80                 :             :     }
      81                 :             : 
      82                 :           1 :     toGBytes() {
      83                 :           0 :         return toGBytes(this._array);
      84                 :             :     }
      85                 :             : };
        

Generated by: LCOV version 2.0-1