Sha256: b135e0a4c02b20d4045806c733a37449cb0c7cf84030a700fd3a774afc3b2507

Contents?: true

Size: 895 Bytes

Versions: 185

Compression:

Stored size: 895 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,
    write,
    forceWrite,
    clear,
  };
};

export { CircularBuffer as default };

Version data entries

185 entries across 185 versions & 1 rubygems

Version Path
trackler-2.2.1.38 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.37 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.36 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.35 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.34 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.33 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.32 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.31 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.30 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.29 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.28 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.27 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.26 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.25 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.24 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.23 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.22 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.21 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.20 tracks/ecmascript/exercises/circular-buffer/example.js
trackler-2.2.1.19 tracks/ecmascript/exercises/circular-buffer/example.js