{"size": 3663, "ext": "js", "max_stars_repo_path": "community/tools/firebase2graphql/src/firebase/utils.js", "max_stars_repo_name": "kevboh/graphql-engine", "max_stars_repo_stars_event_min_datetime": "2019-10-29T00:05:58.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-20T21:42:21.000Z", "max_issues_repo_path": "community/tools/firebase2graphql/src/firebase/utils.js", "max_issues_repo_name": "kevboh/graphql-engine", "max_issues_repo_issues_event_min_datetime": "2021-09-02T21:21:10.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-08T23:35:41.000Z", "max_forks_repo_path": "community/tools/firebase2graphql/src/firebase/utils.js", "max_forks_repo_name": "kevboh/graphql-engine", "max_forks_repo_forks_event_min_datetime": "2019-10-27T08:43:28.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-17T20:08:41.000Z"}
{"max_stars_count": 76, "max_issues_count": 22, "max_forks_count": 9, "avg_line_length": 24.5838926174, "max_line_length": 82, "alphanum_fraction": 0.5782145782}
const getParentPrimaryKeyMap = obj => {
  const pkeyMap = {};
  for (var pkey in obj) {
    if (pkey.indexOf('_id') === 0) {
      pkeyMap[pkey] = obj[pkey];
    }
  }
  return pkeyMap;
};

const getLastPrimaryKey = (obj, index = 0, selfGenerated = '') => {
  const id = index === 0 ? `_id${selfGenerated}` : `_id${selfGenerated}_${index}`;
  const nextIndex = index === 0 ? 2 : index + 1;
  if (!obj[`_id_${nextIndex}`]) {
    return id;
  }
  getLastPrimaryKey(obj, nextIndex, selfGenerated);
};

const getPrimaryKeyName = (obj, index = 0, selfGenerated = '') => {
  const id = index === 0 ? `_id${selfGenerated}` : `_id${selfGenerated}_${index}`;
  const nextIndex = index === 0 ? 2 : index + 1;
  if (obj[id] === undefined) {
    return id;
  }
  return getPrimaryKeyName(obj, nextIndex, selfGenerated);
};

const isRandomList = obj => {
  if (!obj) {
    return false;
  }
  for (var objKey in obj) {
    if (obj[objKey] !== null && typeof obj[objKey] === 'object') {
      return false;
    }
  }
  return true;
};

const isList = obj => {
  if (Object.keys(obj).length === 0) {
    return false;
  }
  if (obj.constructor.name === 'Array') {
    let arrayElementDataType = null;
    for (let _i = obj.length - 1; _i >= 0; _i--) {
      if (arrayElementDataType === null) {
        arrayElementDataType = typeof obj[_i];
      } else if (arrayElementDataType !== typeof obj[_i]) {
        return false;
      }
    }
    return true;
  }
  for (var objkey in obj) {
    if (obj[objkey] === null) {
      return false;
    }
    if (obj[objkey].constructor.name !== 'Boolean' || !obj[objkey]) {
      return false;
    }
  }
  return true;
};

const makeFirebaseListFromObj = obj => {
  if (obj.constructor.name === 'Array') {
    const firebaseList = {};
    for (var i = obj.length - 1; i >= 0; i--) {
      const element = obj[i];
      firebaseList[i.toString()] = element;
    }
    return firebaseList;
  }
  return obj;
};

const makeFirebaseListFromArr = obj => {
  if (obj.constructor.name === 'Array') {
    const firebaseList = {};
    for (var i = obj.length - 1; i >= 0; i--) {
      const element = obj[i];
      firebaseList[element] = true;
    }
    return firebaseList;
  }
  return obj;
};

const isObjectList = obj => {
  if (obj === null || obj === undefined) {
    return false;
  }
  const listChildStructure = {};
  const checkElementConsistency = element => {
    if (element === null) {
      return false;
    }
    if (typeof element !== 'object') {
      return false;
    }
    if (Object.keys(obj).length === 0) {
      return false;
    }
    for (var childKey in element) {
      if (!listChildStructure[childKey]) {
        if (element[childKey] !== null && element[childKey] !== undefined) {
          listChildStructure[childKey] = typeof element[childKey];
        }
      } else if (element[childKey] !== null && element[childKey] !== undefined) {
        if (typeof element[childKey] !== listChildStructure[childKey]) {
          return false;
        }
      }
    }
    return true;
  };
  if (obj.constructor.name === 'Array') {
    for (let _i = obj.length - 1; _i >= 0; _i--) {
      let element = obj[_i];
      let consistent = checkElementConsistency(element);
      if (!consistent) {
        return false;
      }
    }
    return true;
  }
  for (var key in obj) {
    const element = obj[key];
    let consistent = checkElementConsistency(element);
    if (!consistent) {
      return false;
    }
  }
  return true;
};

module.exports = {
  getParentPrimaryKeyMap,
  getLastPrimaryKey,
  getPrimaryKeyName,
  isRandomList,
  isList,
  isObjectList,
  makeFirebaseListFromObj,
  makeFirebaseListFromArr,
};
