All files index.js

95.34% Statements 123/129
91.89% Branches 34/37
85.71% Functions 6/7
95.34% Lines 123/129

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 1301x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 16x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 28x 28x 25x 25x 28x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 4x 4x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 12x 12x 10x 10x 12x     10x 12x 1x 1x 1x 1x 1x 1x 1x 1x 27x 27x 1x 1x 1x 1x 1x 1x 1x 1x 10x 4x 4x 10x 13x 10x 10x 11x 11x     11x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x  
// @ts-check
// SPDX-License-Identifier: Zlib
// SPDX-FileCopyrightText: 2024 CERN (home.cern)
// SPDX-Created: 2024-02-06T14:20:41
// SPDX-FileContributor: Author: Sylvain Fargier <sylvain.fargier@cern.ch>
 
import * as prom from "./prom.js";
export * from "./prom.js";
export { prom };
export * from "./VarBuffer.js";
export * from "./IOBuffer.js";
 
/**
 * @brief check if maybe object/array is empty
 * @param {any} value
 * @returns {boolean} true if object is empty or not an enumerable
 */
export function isEmpty(value) {
  return (value?.length ?? value?.size ??
    ((value instanceof Object && Object.entries(value).length) || 0)) === 0;
}
 
/**
 * @brief test if object is null or undefined
 * @param {any} value
 * @returns {boolean} true if value is nullish
 */
export function isNil(value) {
  return value === undefined || value === null;
}
 
/**
 * @brief get last element of maybe list
 * @template T
 * @param {T[]|any} value
 * @returns {T|undefined}
 */
export function last(value) {
  const len = value?.length;
  if (isNaN(len)) { return undefined; }
 
  return value[len - 1];
}
 
/**
 * @brief get first element of maybe list
 * @template T
 * @param {T[]|any} value
 * @returns {T|undefined}
 */
export function first(value) {
  const len = value?.length; // identify arrays
  if (isNaN(len)) { return undefined; }
 
  return value[0];
}
 
/**
 * @brief try to recursively set a value on object
 * @param {any} obj
 * @param {(string|number)[]|any} path
 * @param {any} value
 * @returns {any|null} input object or null on error
 */
export function set(obj, path, value) {
  try {
    let leaf = obj;
 
    for (let i = 0; i < (path?.length ?? 0) - 1; ++i) {
      if (!(leaf instanceof Object)) { return null; }
 
      const pathPart = path[i];
 
      let child = leaf[pathPart];
      if (!(child instanceof Object)) {
        child = {};
        leaf[pathPart] = child;
      }
      leaf = child;
    }
    const key = last(path);
    if (!(leaf instanceof Object) || key === undefined) { return null; }
    leaf[last(path)] = value;
  }
  catch {
    return null;
  }
  return obj;
}
 
/**
 * @brief deeply clone object (native implementation)
 * @template T
 * @param {T} value
 * @return {T}
 */
export function cloneDeep(value) {
  return structuredClone(value);
}
 
/**
 * @brief merge sources in target
 * @param {any} target
 * @param  {any[]} sources
 * @returns {any}
 */
export function merge(target, ...sources) {
  if (!(target instanceof Object)) {
    target = {};
  }
  sources.forEach((source) => {
    if (!(source instanceof Object)) { return; }
 
    Object.entries(source).forEach(([ key, value ]) => {
      const targetValue = target[key];
      if (value === undefined) {
        /* skip as in lodash */
      }
      else if (value instanceof Object && targetValue instanceof Object) {
        merge(targetValue, value);
      }
      else {
        target[key] = cloneDeep(value);
      }
    });
  });
 
  return target;
}