All files / src/objectf objectf.ts

0% Statements 0/15
0% Branches 0/4
0% Functions 0/6
0% Lines 0/14

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                                                                                                                                                         
import * as arrayf from '../arrayf/arrayf';
 
/**
 * Deep copies an object by looping through it.
 * @alias arrayf.deepcopy.
 */
export function deepCopy(
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  input: Array<any> | [key: string]
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
): Array<any> | Object {
  return arrayf.deepCopy(input);
}
 
/**
 * A quick JSON parse, stringify based copy.
 * This is fast but will destroy functions, Date etc so
 * has limited usage.
 * @param object
 */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function jsonCopy(obj: Object | Array<any>): Object | Array<any> {
  return JSON.parse(JSON.stringify(obj));
}
 
/**
 * A shallow 1 level equality check of objects.
 * @param a
 * @param b
 */
export function areEqual(
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  a: {[key: string]: any},
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  b: {[key: string]: any}
): boolean {
  const aProps = Object.getOwnPropertyNames(a);
  const bProps = Object.getOwnPropertyNames(b);
 
  // If the length of object isn't equal, we know they are equal.
  if (aProps.length !== bProps.length) {
    return false;
  }
 
  for (let i = 0; i < aProps.length; i++) {
    const propName = aProps[i];
    if (a[propName] !== b[propName]) {
      return false;
    }
  }
 
  return true;
}
 
/**
 * Copies an object (shallow).
 * @param object
 */
export function copy(obj: Object): Object {
  return Object.assign({}, obj);
}
 
/**
 *
 * ```
 * objectf.forEach(myObject, (key:string, value:any)=> {
 *
 * })
 * ```
 * @param callback
 */
export function forEach(obj: Object, callback: Function) {
  Object.entries(obj).forEach(([key, value]) => {
    callback(key, value);
  });
}