All files / metadata/src/utilities/metadataProvider makeArrayLike.ts

66.66% Statements 6/9
75% Branches 6/8
50% Functions 1/2
66.66% Lines 6/9

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                              39452x     39452x     39452x         39452x             39452x             39452x    
/**
 * Converts an object into an array-like object.
 * This is used to convert a single object into an array-like object with length 1
 * so that it can be iterated over or otherwise extended.  The array like
 * properties are hidden so it doesn't show up in JSON.stringify or other serializations.
 *
 * This is used to allow VM=1 attributes to be stored in the NATURAL cache as a single object but
 * still accessed as an array-like object for backwards compatibility.
 *
 * Note this does not work well on arrays or String objects.
 *
 * @param obj - The object to convert.
 * @returns The array-like object.
 */
export function makeArrayLike(obj) {
  Iif (obj === null || obj === undefined) {
    return obj;
  }
  Iif (typeof obj !== 'object' || Array.isArray(obj)) {
    return obj;
  }
  Object.defineProperty(obj, 'length', {
    value: 1,
    configurable: true,
  });
 
  Object.defineProperty(obj, 0, {
    value: obj,
    writable: true,
    configurable: true,
    enumerable: false, // do not iterate index either
  });
 
  Object.defineProperty(obj, Symbol.iterator, {
    value: function* () {
      yield this; // iterator yields only the object
    },
    configurable: true,
  });
 
  return obj;
}