export function singleIterationOptions(overrides = {}) {
  return {
    iterations: 1,
    thresholds: {
      checks: ["rate==1.0"],
      ...(overrides.thresholds || {}),
    },
    ...overrides,
  };
}

export const defaultOptions = singleIterationOptions();

export function json(res) {
  return JSON.parse(res.body);
}

export function contains(rows, field, value) {
  return rows.some((row) => row[field] === value);
}

export function allMatch(rows, predicate) {
  return rows.length > 0 && rows.every(predicate);
}

export function isSorted(rows, field, direction = "asc") {
  if (rows.length <= 1) return true;

  for (let index = 1; index < rows.length; index += 1) {
    if (direction === "asc" && rows[index][field] < rows[index - 1][field]) {
      return false;
    }
    if (direction === "desc" && rows[index][field] > rows[index - 1][field]) {
      return false;
    }
  }

  return true;
}
