Skip to content

Commit 6b281bb

Browse files
committed
feat(Buffer): add static from method
1 parent 86a8b51 commit 6b281bb

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

src/Buffer.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,9 @@ describe('Buffer', () => {
7373
expect(it[Symbol.iterator]()).toBe(it[Symbol.iterator]());
7474
expect([...q]).toEqual([1, 2]);
7575
});
76+
77+
it('constructs from an iterable', () => {
78+
const q = Buffer.from([1, 2, 3], 2);
79+
expect([...q]).toEqual([2, 3]);
80+
});
7681
});

src/Buffer.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,12 @@ export default class Buffer<A> {
6565
},
6666
};
6767
}
68+
69+
static from<A>(iterable: Iterable<A>, limit: number) {
70+
const buffer = new Buffer<A>(limit);
71+
for (const value of iterable) {
72+
buffer.enqueue(value);
73+
}
74+
return buffer;
75+
}
6876
}

0 commit comments

Comments
 (0)