{"size": 776, "ext": "js", "max_stars_repo_path": "src/lib/cache.js", "max_stars_repo_name": "yj1438/tiledmap-loader", "max_stars_repo_stars_event_min_datetime": "2021-10-03T16:07:43.000Z", "max_stars_repo_stars_event_max_datetime": "2021-10-03T16:07:43.000Z", "max_issues_repo_path": "src/lib/cache.js", "max_issues_repo_name": "yj1438/tiledmap-loader", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/lib/cache.js", "max_forks_repo_name": "yj1438/tiledmap-loader", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 19.8974358974, "max_line_length": 61, "alphanum_fraction": 0.587628866}
const sortKeys = require('sort-keys');

const cacheMap = {};

function toString(value) {
  let _value = '';
  switch (typeof value) {
    case 'function':
      _value = value.toString();
      break;
    case 'object':
      _value = JSON.stringify(sortKeys(value, {deep: true}));
      break;
    default:
      _value = String(value);
      break;
  }
  return _value;
}

module.exports = {
  isChange(key, value) {
    const _value = toString(value);
    const oldValue = cacheMap[key];
    return _value !== oldValue;
  },
  set(key, value) {
    if (this.isChange(key, value)) {
      cacheMap[key] = toString(value);
      return true;
    }
    return false;
  },
  get(key) {
    const value = cacheMap[key];
    return value ? JSON.parse(value) : undefined;
  },
};
