{"size": 1007, "ext": "js", "max_stars_repo_path": "exercises/circular-buffer/example.js", "max_stars_repo_name": "joelwallis/exercism-xecmascript", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "exercises/circular-buffer/example.js", "max_issues_repo_name": "joelwallis/exercism-xecmascript", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "exercises/circular-buffer/example.js", "max_forks_repo_name": "joelwallis/exercism-xecmascript", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 20.14, "max_line_length": 56, "alphanum_fraction": 0.6266137041}
let buffer, bufferMax;

const BufferEmptyException = () => ({
    name: "buffer empty exception!",
    message: "can't read from an empty buffer!"
  });

const BufferFullException = () => ({
    name: "buffer full exception!",
    message: "can't write to a full buffer!"
  });

const read = () => {
  if (buffer.length === 0){
    throw BufferEmptyException();
  }
  return buffer.splice(0,1)[0];
};

const write = (value) => {
  if (buffer.length === bufferMax){
    throw BufferFullException();
  }
  value ? buffer.push(value) : null;
};

const forceWrite = (value) => {
  if (buffer.length === bufferMax){
    read();
  }
  write(value);
};

const clear = () => buffer = [];

const CircularBuffer = (capacity) => {
  buffer = [];
  bufferMax = capacity;
  return {
    read: read,
    write: write,
    forceWrite: forceWrite,
    clear: clear
  }
};

export { CircularBuffer as default };
export { BufferFullException as bufferFullException };
export { BufferEmptyException as bufferEmptyException };
