From: Tõnis Tiigi Date: Wed, 27 Feb 2013 19:10:07 +0000 (+0200) Subject: Add support for fill() X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=834e1adc99d46e32a9b10dec95bea1d82c267583;p=buffer.git Add support for fill() --- diff --git a/index.js b/index.js index 07931fe..1d81be0 100644 --- a/index.js +++ b/index.js @@ -305,6 +305,19 @@ SlowBuffer.prototype.copy = function(target, targetstart, sourcestart, sourceend } }; +SlowBuffer.prototype.fill = function(value, start, end) { + if (end > this.length) { + throw new Error('oob'); + } + if (start > end) { + throw new Error('oob'); + } + + for (var i = start; i < end; i++) { + this[i] = value; + } +} + function coerce(length) { // Coerce length to a number (possibly NaN), round up // in case it's fractional (e.g. 123.456) then do a diff --git a/test/buffer.js b/test/buffer.js index 99443ec..0255f94 100644 --- a/test/buffer.js +++ b/test/buffer.js @@ -206,3 +206,13 @@ test("buffer from buffer", function (t) { t.equal(b1.toString('hex'), b2.toString('hex')); t.end(); }); + +test("fill", function(t) { + t.plan(1); + var b1 = new Buffer(10); + var b2 = new buffer.Buffer(10); + b1.fill(2); + b2.fill(2); + t.equal(b1.toString('hex'), b2.toString('hex')); + t.end(); +})