{"size": 1115, "ext": "js", "max_stars_repo_path": "packages/veritone-client-js/apis/helper/test-util.js", "max_stars_repo_name": "jerry-li-dev/veritone-sdk", "max_stars_repo_stars_event_min_datetime": "2017-09-30T00:17:34.000Z", "max_stars_repo_stars_event_max_datetime": "2020-11-12T22:56:11.000Z", "max_issues_repo_path": "packages/veritone-client-js/apis/helper/test-util.js", "max_issues_repo_name": "jerry-li-dev/veritone-sdk", "max_issues_repo_issues_event_min_datetime": "2017-10-03T00:44:05.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-15T19:52:02.000Z", "max_forks_repo_path": "packages/veritone-client-js/apis/helper/test-util.js", "max_forks_repo_name": "jerry-li-dev/veritone-sdk", "max_forks_repo_forks_event_min_datetime": "2017-11-08T06:32:53.000Z", "max_forks_repo_forks_event_max_datetime": "2021-05-11T21:24:46.000Z"}
{"max_stars_count": 16, "max_issues_count": 172, "max_forks_count": 7, "avg_line_length": 23.2291666667, "max_line_length": 72, "alphanum_fraction": 0.5560538117}
import { get, isRegExp, forOwn, isPlainObject } from 'lodash';

// matches({
// 	a: 'some/path/with/id',
// 	b: 'exact-value'
// }, {
// 	a: /id/,
// 	b: 'exact-value'
// }) === true

function walk(obj, cb, path = '') {
  forOwn(obj, (v, k) => {
    const newPath = path === '' ? k : `${path}.${k}`;

    if (isPlainObject(v)) {
      return walk(v, cb, newPath);
    }

    cb(v, k, newPath);
  });
}

// do nothing if obj matches source, otherwise throw a relevant error.
// obj/source are deep-compared; if sourceVal is a string, the matching
// objectVal is compared with ===. if sourceVal is a regex, objectVal is
// tested against it.
export function assertMatches(obj, source) {
  return walk(source, (srcVal, key, path) => {
    const objVal = get(obj, path);

    if (isRegExp(srcVal)) {
      if (!srcVal.test(objVal)) {
        throw new Error(
          `Expected ${objVal} to match ${srcVal} at path "${path}"`
        );
      }

      return true;
    }

    if (srcVal !== objVal) {
      throw new Error(
        `Expected "${objVal}" to equal "${srcVal}" at path "${path}"`
      );
    }
  });
}
