Skip to content

Commit

Permalink
fix: bufferFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Aug 24, 2024
1 parent 0ad3d3c commit 03939e5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ Latest results:
┌─────────┬────────────────┬───────────┬────────────────────┬──────────┬─────────┐
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├─────────┼────────────────┼───────────┼────────────────────┼──────────┼─────────┤
│ 0 │ 'gmatch''287,789'3474.757510684889 '±0.33%'719475
│ 1 │ 'streamsearch''280,600'3563.7901000848046'±0.26%'701501
│ 0 │ 'gmatch''320,020'3124.7951309445925 '±0.89%'1600105
│ 1 │ 'streamsearch''285,266'3505.4937444482543'±0.18%'1426333
└─────────┴────────────────┴───────────┴────────────────────┴──────────┴─────────┘
gmatch matches: 12
streamsearch matches: 12
Expand Down
20 changes: 12 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
const bufferFrom = (string) => {
const buffer = new Uint8Array(string.length);

for (let i = 0; i !== string.length; ++i) {
buffer[i] = string.charCodeAt(i);
}
const textEncoder = new TextEncoder();

return buffer;
const bufferFrom = (string) => {
return textEncoder.encode(string);
};

const bufferCompare = (buffer1, offset1, buffer2, offset2, length) => {
Expand Down Expand Up @@ -35,6 +31,10 @@ export const Match = class {
* @throws {RangeError}
*/
constructor(pattern, callback, from = bufferFrom) {
if (typeof from !== "function") {
throw new TypeError("From must be a Function");
}

if (typeof callback !== "function") {
throw new TypeError("Callback must be a Function");
}
Expand Down Expand Up @@ -69,7 +69,11 @@ export const Match = class {

write(chunk) {
const buffer =
chunk instanceof Uint8Array ? chunk : this.#from(String(chunk));
chunk instanceof Uint8Array
? chunk
: chunk instanceof ArrayBuffer || ArrayBuffer.isView(chunk)
? new Uint8Array(chunk)
: this.#from(String(chunk));
let offset = 0;

while (offset !== buffer.length) {
Expand Down
32 changes: 32 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ test("Match constructor should create a Match instance with valid input", () =>
assert(match instanceof Match);
});

test("Match constructor should throw TypeError for non-function from", () => {
assert.throws(() => {
return new Match("test", () => {}, "not a function");
}, TypeError);
});

test("Match constructor should throw TypeError for non-function callback", () => {
assert.throws(() => {
return new Match("test", "not a function");
Expand Down Expand Up @@ -352,3 +358,29 @@ test("pattern test /4", (t, done) => {
assert.strictEqual(c, 1);
done();
});

test("pattern test /5", (t, done) => {
let c = 0;

const m = new Match("Hello, World!", () => {
c++;
});

m.write(new ArrayBuffer("Hello, World!".length));

assert.strictEqual(c, 1);
done();
});

test("pattern test /5", (t, done) => {
let c = 0;

const m = new Match("Hello, World!", () => {
c++;
});

m.write(new ArrayBuffer("Hello, World!".length));

assert.strictEqual(c, 1);
done();
});

0 comments on commit 03939e5

Please sign in to comment.