<commit_before>import {
  promiseThat, 
  fulfilled, 
  hasProperty,
  isRejectedWith,
} from 'hamjest';

const ES6_KATAS_URL_PREFIX = 'http://katas.tddbin.com/katas/es6/language/';
const GROUPED_KATAS_URL = ES6_KATAS_URL_PREFIX + '__grouped__.json';
const ALL_KATAS_URL = ES6_KATAS_URL_PREFIX + '__all__.json';

describe('load the katas from katas.tddbin.com', () => {
  it('works', () => {
    return promiseThat(loadGroupedKatasFrom(GROUPED_KATAS_URL),
      fulfilled(hasProperty('groups'))
    );
  });
  it('throws when data cant be loaded', () => {
    return promiseThat(loadGroupedKatasFrom('invalid URL'),
      isRejectedWith('Error loading katas.')
    );
  });
  it('throws when the wrong data are loaded', () => {
    return promiseThat(loadGroupedKatasFrom(ALL_KATAS_URL),
      isRejectedWith('Invalid kata data.')
    );
  });
});

import fetch from 'node-fetch';
function loadGroupedKatasFrom(url) {
  return fetch(url)
    .then(response => response.json())
    .catch(() => {
      throw 'Error loading katas.'
    })
    .then(groupedKatas => {
      if ('groups' in groupedKatas) {
        return groupedKatas;
      }
      throw 'Invalid kata data.'
    })
  ;
}<commit_msg>Separate the two things the loading function does.<commit_after>import {
  promiseThat, 
  fulfilled, 
  hasProperty,
  isRejectedWith,
} from 'hamjest';

const ES6_KATAS_URL_PREFIX = 'http://katas.tddbin.com/katas/es6/language/';
const GROUPED_KATAS_URL = ES6_KATAS_URL_PREFIX + '__grouped__.json';
const ALL_KATAS_URL = ES6_KATAS_URL_PREFIX + '__all__.json';

describe('load the katas from katas.tdd<EMAIL>', () => {
  it('works', () => {
    return promiseThat(loadGroupedKatasFrom(GROUPED_KATAS_URL),
      fulfilled(hasProperty('groups'))
    );
  });
  it('throws when data cant be loaded', () => {
    return promiseThat(loadGroupedKatasFrom('invalid URL'),
      isRejectedWith('Error loading katas.')
    );
  });
  it('throws when the wrong data are loaded', () => {
    return promiseThat(loadGroupedKatasFrom(ALL_KATAS_URL),
      isRejectedWith('Invalid kata data.')
    );
  });
});

import fetch from 'node-fetch';

function loadData(url) {
  return fetch(url)
    .then(response => response.json())
    .catch(() => { throw 'Error loading katas.'; });
}

function verifyData(groupedKatas) {
  if ('groups' in groupedKatas) {
    return groupedKatas;
  }
  throw 'Invalid kata data.';
}

function loadGroupedKatasFrom(url) {
  return loadData(url)
    .then(verifyData)
  ;
}
{'fraction_non_alphanumeric': 0.14188102893890675, 'fraction_numerical': 0.003215434083601286, 'mean_word_length': 3.91897233201581, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 17, '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': '621497', 'n_tokens_mistral': 904, 'n_tokens_neox': 862, 'n_words': 167}