Sha256: 9121b5fe656b62720933fc445c8d7989d72f5d1600a26a2b5c49d55e398e7b6a

Contents?: true

Size: 922 Bytes

Versions: 26

Compression:

Stored size: 922 Bytes

Contents

let buffer, bufferMax;

export class BufferEmptyError extends Error {
  constructor(message) {
    super();
    this.message = message || 'Buffer is empty.';
  }
};
export class BufferFullError extends Error {
  constructor(message) {
    super();
    this.message = message || 'Buffer is full.';
  }
};

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

const write = (value) => {
  if (buffer.length === bufferMax){
    throw new BufferFullError();
  }
  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 };

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
trackler-2.2.0.0 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.55 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.54 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.53 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.52 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.51 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.50 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.49 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.48 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.47 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.46 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.45 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.44 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.43 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.42 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.41 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.40 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.39 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.38 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.1.0.37 tracks/ecmascript/exercises/circular-buffer/example.js