From 834e1adc99d46e32a9b10dec95bea1d82c267583 Mon Sep 17 00:00:00 2001 From: =?utf8?q?To=CC=83nis=20Tiigi?= Date: Wed, 27 Feb 2013 21:10:07 +0200 Subject: [PATCH] Add support for fill() --- index.js | 13 +++++++++++++ test/buffer.js | 10 ++++++++++ 2 files changed, 23 insertions(+) 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(); +}) -- 2.34.1