Skip to content

Commit 77fc61d

Browse files
committed
Default value for second arg of Buffer#slice
1 parent 4fe3007 commit 77fc61d

3 files changed

Lines changed: 10 additions & 7 deletions

File tree

doc/api.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ into `buf2`, starting at the 8th byte in `buf2`.
175175
// !!!!!!!!qrst!!!!!!!!!!!!!
176176

177177

178-
### buffer.slice(start, end)
178+
### buffer.slice(start, end=buffer.length)
179179

180180
Returns a new buffer which references the
181181
same memory as the old, but offset and cropped by the `start` and `end`

lib/buffer.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,9 @@ Buffer.prototype.copy = function copy (target, target_start, start, end) {
322322

323323
// slice(start, end)
324324
Buffer.prototype.slice = function (start, end) {
325-
if (end > this.length) {
326-
throw new Error("oob");
327-
}
328-
if (start > end) {
329-
throw new Error("oob");
330-
}
325+
if (!end) end = this.length;
326+
if (end > this.length) throw new Error("oob");
327+
if (start > end) throw new Error("oob");
331328

332329
return new Buffer(this.parent, end - start, +start + this.offset);
333330
};

test/simple/test-buffer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,9 @@ for (i = 0; i < l; i++) {
312312
sb = b.toString();
313313
assert.equal(sb.length, s.length);
314314
assert.equal(sb, s);
315+
316+
317+
// Single argument slice
318+
b = new Buffer("abcde");
319+
assert.equal("bcde", b.slice(1).toString());
320+

0 commit comments

Comments
 (0)