<commit_before>const { readJson, writeJson } = require('../util/readWriteJson');

const get = (path, obj) => {
  return path.split('.').reduce((acc, key) => (acc !== undefined ? acc[key] : undefined), obj);
};

const writeJsonField = (filename, field, val) => {
  const json = readJson(filename);
  const segments = field.split('.');
  const tail = segments.pop();
  const parent = get(segments.join('.'), json);
  parent[tail] = val;
  writeJson(filename, json);
};

const assertJsonFile = (report, filename, json) => {
  return function assertJsonFile(field, expectedValue) {
    const currentValue = get(field, json);
    const resolution = `--fix: change ${field} in ${filename} from "${currentValue}" to "${expectedValue}"`;
    const fixer = () => {
      writeJsonField(filename, field, expectedValue);
      console.log(`fixed: changed ${field} in ${filename} from "${currentValue}" to "${expectedValue}"`);
    };
    report(`${filename}: ${field} field`, currentValue === expectedValue, resolution, fixer);
  };
};

module.exports = { assertJsonFile };<commit_msg>fix(pluginsdk): fix 'get()' helper function such that an empty key returns the object itself<commit_after>const { readJson, writeJson } = require('../util/readWriteJson');

const get = (path, obj) => {
  return path.split('.').reduce((acc, key) => {
    return acc === undefined ? undefined : key ? acc[key] : acc;
  }, obj);
};

const writeJsonField = (filename, field, val) => {
  const json = readJson(filename);
  const segments = field.split('.');
  const tail = segments.pop();
  const parent = get(segments.join('.'), json);
  parent[tail] = val;
  writeJson(filename, json);
};

const assertJsonFile = (report, filename, json) => {
  return function assertJsonFile(field, expectedValue) {
    const currentValue = get(field, json);
    const resolution = `--fix: change ${field} in ${filename} from "${currentValue}" to "${expectedValue}"`;
    const fixer = () => {
      writeJsonField(filename, field, expectedValue);
      console.log(`fixed: changed ${field} in ${filename} from "${currentValue}" to "${expectedValue}"`);
    };
    report(`${filename}: ${field} field`, currentValue === expectedValue, resolution, fixer);
  };
};

module.exports = { assertJsonFile };
{'fraction_non_alphanumeric': 0.18093126385809313, 'fraction_numerical': 0.0, 'mean_word_length': 4.515892420537897, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '16519', 'n_tokens_mistral': 657, 'n_tokens_neox': 660, 'n_words': 186}