@dxos/util 0.4.7-next.f4b92be → 0.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/util",
3
- "version": "0.4.7-next.f4b92be",
3
+ "version": "0.4.7",
4
4
  "description": "Temporary bucket for misc functions, which should graduate into separate packages.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -16,13 +16,13 @@
16
16
  "src"
17
17
  ],
18
18
  "dependencies": {
19
- "@dxos/node-std": "0.4.7-next.f4b92be",
20
- "@dxos/keys": "0.4.7-next.f4b92be",
21
- "@dxos/debug": "0.4.7-next.f4b92be",
22
- "@dxos/invariant": "0.4.7-next.f4b92be"
19
+ "@dxos/invariant": "0.4.7",
20
+ "@dxos/keys": "0.4.7",
21
+ "@dxos/node-std": "0.4.7",
22
+ "@dxos/debug": "0.4.7"
23
23
  },
24
24
  "devDependencies": {
25
- "@dxos/crypto": "0.4.7-next.f4b92be"
25
+ "@dxos/crypto": "0.4.7"
26
26
  },
27
27
  "publishConfig": {
28
28
  "access": "public"
@@ -0,0 +1,33 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { describe, test, expect } from 'vitest';
6
+
7
+ import { CircularBuffer } from './circular-buffer';
8
+
9
+ describe('CircularBuffer', () => {
10
+ test('single element', () => {
11
+ const buffer = new CircularBuffer<number>(1);
12
+ for (let i = 0; i < 3; i++) {
13
+ buffer.push(i);
14
+ expect([...buffer]).toStrictEqual([i]);
15
+ expect(buffer.getLast()).toStrictEqual(i);
16
+ }
17
+ });
18
+
19
+ test('full cycle', () => {
20
+ const maxSize = 10;
21
+ const regularArray: number[] = [];
22
+ const buffer = new CircularBuffer<number>(maxSize);
23
+ for (let i = 0; i < maxSize * 2 + 2; i++) {
24
+ expect([...buffer]).toStrictEqual(regularArray);
25
+ buffer.push(i);
26
+ regularArray.push(i);
27
+ if (regularArray.length > maxSize) {
28
+ regularArray.splice(0, 1);
29
+ }
30
+ expect(buffer.getLast()).toStrictEqual(i);
31
+ }
32
+ });
33
+ });
@@ -0,0 +1,54 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { invariant } from '@dxos/invariant';
6
+
7
+ export class CircularBuffer<T> {
8
+ private readonly _buffer: T[];
9
+ private _nextIndex = 0;
10
+ private _elementCount = 0;
11
+
12
+ constructor(size: number) {
13
+ invariant(size >= 1);
14
+ this._buffer = new Array(size);
15
+ }
16
+
17
+ public push(element: T) {
18
+ this._buffer[this._nextIndex] = element;
19
+ this._nextIndex = (this._nextIndex + 1) % this._buffer.length;
20
+ this._elementCount = Math.min(this._buffer.length, this._elementCount + 1);
21
+ }
22
+
23
+ public getLast(): T | undefined {
24
+ if (this._elementCount === 0) {
25
+ return undefined;
26
+ }
27
+ if (this._nextIndex === 0) {
28
+ return this._buffer[this._buffer.length - 1];
29
+ }
30
+ return this._buffer[this._nextIndex - 1];
31
+ }
32
+
33
+ [Symbol.iterator](): IterableIterator<T> {
34
+ return this.values();
35
+ }
36
+
37
+ public *values(): IterableIterator<T> {
38
+ if (this._elementCount === 0) {
39
+ return;
40
+ }
41
+ if (this._elementCount < this._buffer.length) {
42
+ for (let i = 0; i < this._elementCount; i++) {
43
+ yield this._buffer[i];
44
+ }
45
+ return;
46
+ }
47
+ for (let i = this._nextIndex; i < this._buffer.length; i++) {
48
+ yield this._buffer[i];
49
+ }
50
+ for (let i = 0; i < this._nextIndex; i++) {
51
+ yield this._buffer[i];
52
+ }
53
+ }
54
+ }
package/src/index.ts CHANGED
@@ -31,3 +31,4 @@ export * from './for-each-async';
31
31
  export * from './weak';
32
32
  export * from './map-values';
33
33
  export * from './defer';
34
+ export * from './circular-buffer';
@@ -37,7 +37,7 @@ export const getPrototypeSpecificInstanceId = (instance: any): number => {
37
37
  };
38
38
 
39
39
  export const getDebugName = (instance: any): string => {
40
- if (instance === null) {
40
+ if (instance == null) {
41
41
  return 'null';
42
42
  }
43
43