Skip to content

Commit

Permalink
feat(Buffer): add static from method
Browse files Browse the repository at this point in the history
  • Loading branch information
slikts committed Jan 24, 2023
1 parent 86a8b51 commit 6b281bb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Buffer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,9 @@ describe('Buffer', () => {
expect(it[Symbol.iterator]()).toBe(it[Symbol.iterator]());
expect([...q]).toEqual([1, 2]);
});

it('constructs from an iterable', () => {
const q = Buffer.from([1, 2, 3], 2);
expect([...q]).toEqual([2, 3]);
});
});
8 changes: 8 additions & 0 deletions src/Buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,12 @@ export default class Buffer<A> {
},
};
}

static from<A>(iterable: Iterable<A>, limit: number) {
const buffer = new Buffer<A>(limit);
for (const value of iterable) {
buffer.enqueue(value);
}
return buffer;
}
}

0 comments on commit 6b281bb

Please sign in to comment.