All files util.js

98.48% Statements 130/132
94.59% Branches 70/74
90% Functions 18/20
98.48% Lines 130/132

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 13316x 16x 16x 16x 16x 16x 1755x 1755x 16x 16x     16x 16x 21x 21x 16x 16x 16x 16x 6028x 6028x 16x 16x 114x 114x 16x 16x 16x 16x 16x 16x 13176x 11885x 13176x 7x 13176x 16x 16x 16x 4859x 4859x 4859x 4859x 16x 16x 16x 5488x 385x 5488x 16x 16x 135x 135x 135x 135x 135x 701x 701x 701x 135x 135x 16x 16x 10538x 10538x 10538x 10538x 16x 16x 10623x 10623x 10623x 10623x 10623x 16x 16x 364x 364x 16x 16x 16x 257x 104x 104x 257x 129x 12x 12x 12x 129x 92x 257x 16x 16x 16x 146x 146x 146x 724x 724x 724x 724x 146x 146x 16x 16x 719x 719x 16x 16x 16x 16x 97x 97x 16x 16x 3713x 3713x 973x 3713x 16x 16x 1329x 1329x 345x 1329x 16x 16x 222x 222x 222x  
// Small helpers shared by every module. Everything here must run on the mini program runtime,
// which is ES2017 at best: no TextEncoder, no AbortController, no globalThis.
 
const toStringTag = Object.prototype.toString;
 
export function isNil(v) {
  return v === null || v === undefined;
}
 
export function hasOwn(obj, key) {
  return Object.prototype.hasOwnProperty.call(obj, key);
}
 
export function isPlainObject(v) {
  return !!v && typeof v === 'object' && !Array.isArray(v) && !isBytesLike(v);
}
 
// Realm independent check. wx APIs and test sandboxes may hand over buffers from another realm,
// where `instanceof ArrayBuffer` is false.
export function isArrayBuffer(x) {
  return !!x && toStringTag.call(x) === '[object ArrayBuffer]';
}
 
export function isBytesLike(x) {
  return isArrayBuffer(x) || (!!x && typeof x === 'object' && ArrayBuffer.isView(x));
}
 
/**
 * ArrayBuffer or any ArrayBufferView to a plain Uint8Array view (no copy), null for anything else.
 * Subclasses (Node's Buffer, whose slice() does not copy) are re-wrapped as plain Uint8Array.
 */
export function toUint8(x) {
  if (x instanceof Uint8Array && x.constructor === Uint8Array) return x;
  if (isArrayBuffer(x)) return new Uint8Array(x);
  if (x && typeof x === 'object' && ArrayBuffer.isView(x)) return new Uint8Array(x.buffer, x.byteOffset, x.byteLength);
  return null;
}
 
/** A new plain Uint8Array with a copy of the bytes. */
export function copyBytes(u8) {
  const out = new Uint8Array(u8.length);
  out.set(u8);
  return out;
}
 
/** An ArrayBuffer holding exactly the bytes of the view, copying only when the view is partial. */
export function exactArrayBuffer(u8) {
  if (u8.byteOffset === 0 && u8.byteLength === u8.buffer.byteLength) return u8.buffer;
  return copyBytes(u8).buffer;
}
 
export function concatBytes(chunks) {
  let total = 0;
  for (let i = 0; i < chunks.length; i++) total += chunks[i].length;
  const out = new Uint8Array(total);
  let pos = 0;
  for (let i = 0; i < chunks.length; i++) {
    out.set(chunks[i], pos);
    pos += chunks[i].length;
  }
  return out;
}
 
export function toHex(bytes) {
  let s = '';
  for (let i = 0; i < bytes.length; i++) s += (bytes[i] < 16 ? '0' : '') + bytes[i].toString(16);
  return s;
}
 
export function randomBytes(n, random) {
  const rnd = typeof random === 'function' ? random : Math.random;
  const b = new Uint8Array(n);
  for (let i = 0; i < n; i++) b[i] = Math.floor(rnd() * 256) & 0xff;
  return b;
}
 
export function randomHex(nBytes) {
  return toHex(randomBytes(nBytes));
}
 
/** Case-insensitive header lookup. wx returns header names in server casing, sometimes lower case. */
export function headerValue(headers, name) {
  if (!headers || typeof headers !== 'object') return undefined;
  const wanted = name.toLowerCase();
  const keys = Object.keys(headers);
  for (let i = 0; i < keys.length; i++) {
    if (keys[i].toLowerCase() === wanted) {
      const v = headers[keys[i]];
      return Array.isArray(v) ? v.join(', ') : v;
    }
  }
  return undefined;
}
 
/** Query string from a flat object, dropping null, undefined and empty strings. */
export function encodeQuery(params) {
  const parts = [];
  const keys = Object.keys(params);
  for (let i = 0; i < keys.length; i++) {
    const v = params[keys[i]];
    if (isNil(v) || v === '') continue;
    parts.push(encodeURIComponent(keys[i]) + '=' + encodeURIComponent(String(v)));
  }
  return parts.join('&');
}
 
export function trimTrailingSlash(url) {
  return String(url).replace(/\/+$/, '');
}
 
export function noop() {}
 
export function microtask(fn) {
  Promise.resolve().then(fn);
}
 
export function toInt(v) {
  if (typeof v === 'number' && isFinite(v)) return Math.trunc(v);
  if (typeof v === 'string' && /^\s*-?\d+\s*$/.test(v)) return parseInt(v, 10);
  return 0;
}
 
export function toNumberOrNull(v) {
  if (typeof v === 'number') return isFinite(v) ? v : null;
  if (typeof v === 'string' && v.trim() !== '' && isFinite(Number(v))) return Number(v);
  return null;
}
 
export function truncate(s, max) {
  if (typeof s !== 'string') return s;
  return s.length > max ? s.slice(0, max) : s;
}