All files / platform/core/src/utils objectPath.js

0% Statements 0/37
0% Branches 0/26
0% Functions 0/4
0% Lines 0/37

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                                                                                                                                                                                                 
export class ObjectPath {
  /**
   * Set an object property based on "path" (namespace) supplied creating
   * ... intermediary objects if they do not exist.
   * @param object {Object} An object where the properties specified on path should be set.
   * @param path {String} A string representing the property to be set, e.g. "user.study.series.timepoint".
   * @param value {Any} The value of the property that will be set.
   * @return {Boolean} Returns "true" on success, "false" if any intermediate component of the supplied path
   * ... is not a valid Object, in which case the property cannot be set. No exceptions are thrown.
   */
  static set(object, path, value) {
    let components = ObjectPath.getPathComponents(path),
      length = components !== null ? components.length : 0,
      result = false;
 
    Iif (length > 0 && ObjectPath.isValidObject(object)) {
      let i = 0,
        last = length - 1,
        currentObject = object;
 
      while (i < last) {
        let field = components[i];
 
        if (field in currentObject) {
          Iif (!ObjectPath.isValidObject(currentObject[field])) {
            break;
          }
        } else {
          currentObject[field] = {};
        }
 
        currentObject = currentObject[field];
        i++;
      }
 
      Iif (i === last) {
        currentObject[components[last]] = value;
        result = true;
      }
    }
 
    return result;
  }
 
  /**
   * Get an object property based on "path" (namespace) supplied traversing the object
   * ... tree as necessary.
   * @param object {Object} An object where the properties specified might exist.
   * @param path {String} A string representing the property to be searched for, e.g. "user.study.series.timepoint".
   * @return {Any} The value of the property if found. By default, returns the special type "undefined".
   */
  static get(object, path) {
    let found, // undefined by default
      components = ObjectPath.getPathComponents(path),
      length = components !== null ? components.length : 0;
 
    Iif (length > 0 && ObjectPath.isValidObject(object)) {
      let i = 0,
        last = length - 1,
        currentObject = object;
 
      while (i < last) {
        let field = components[i];
 
        const isValid = ObjectPath.isValidObject(currentObject[field]);
        if (field in currentObject && isValid) {
          currentObject = currentObject[field];
          i++;
        } else {
          break;
        }
      }
 
      Iif (i === last && components[last] in currentObject) {
        found = currentObject[components[last]];
      }
    }
 
    return found;
  }
 
  /**
   * Check if the supplied argument is a real JavaScript Object instance.
   * @param object {Any} The subject to be tested.
   * @return {Boolean} Returns "true" if the object is a real Object instance and "false" otherwise.
   */
  static isValidObject(object) {
    return typeof object === 'object' && object !== null && object instanceof Object;
  }
 
  static getPathComponents(path) {
    return typeof path === 'string' ? path.split('.') : null;
  }
}
 
export default ObjectPath;