Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add slice function #250

Merged
merged 2 commits into from
Apr 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 42 additions & 42 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2381,45 +2381,63 @@ Stream.prototype.split = function () {
exposeMethod('split');

/**
* Creates a new Stream with the first `n` values from the source.
* Creates a new Stream with the values from the source in the range of `start` to `end`.
*
* @id take
* @id slice
* @section Transforms
* @name Stream.take(n)
* @param {Number} n - integer representing number of values to read from source
* @name Stream.slice(start, end)
* @param {Number} start - integer representing index to start reading from source
* @param {Number} stop - integer representing index to stop reading from source
* @api public
*
* _([1, 2, 3, 4]).take(2) // => 1, 2
* _([1, 2, 3, 4]).slice(1, 3) // => 2, 3
*/

Stream.prototype.take = function (n) {
if (n === 0) {
Stream.prototype.slice = function(start, end) {
var index = 0;
start = typeof start != 'number' || start < 0 ? 0 : start;
end = typeof end != 'number' ? Infinity : end;

if (start === 0 && end === Infinity) {
return this;
} else if (start >= end) {
return _([]);
}
var s = this.consume(function (err, x, push, next) {
var done = x === nil;
if (err) {
push(err);
if (n > 0) {
next();
}
else {
push(null, nil);
}
}
else if (x === nil) {
push(null, nil);
else if (!done && index++ >= start) {
push(null, x);
}

if (!done && index < end) {
next();
}
else {
n--;
push(null, x);
if (n > 0) {
next();
}
else {
push(null, nil);
}
push(null, nil);
}
});
s.id = 'slice:' + s.id;
return s;
};
exposeMethod('slice');

/**
* Creates a new Stream with the first `n` values from the source.
*
* @id take
* @section Transforms
* @name Stream.take(n)
* @param {Number} n - integer representing number of values to read from source
* @api public
*
* _([1, 2, 3, 4]).take(2) // => 1, 2
*/

Stream.prototype.take = function (n) {
var s = this.slice(0, n);
s.id = 'take:' + s.id;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this stuff important?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The s.id? No. It's mostly for debugging. No harm in leaving it though.

return s;
};
Expand All @@ -2440,25 +2458,7 @@ exposeMethod('take');
*/

Stream.prototype.drop = function (n) {
if (n <= 0) {
return this;
}
return this.consume(function (err, x, push, next) {
if (err) {
push(err);
next();
}
else if (x === nil) {
push(null, nil);
}
else {
n--;
if (n < 0) {
push(null, x);
}
next();
}
});
return this.slice(n, Infinity);
};
exposeMethod('drop');

Expand Down
54 changes: 54 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,55 @@ exports['take'] = function (test) {
test.done();
};

exports['slice'] = {
setUp: function (cb) {
this.input = [1, 2, 3, 4, 5];
this.expected = [3, 4, 5];
this.tester = function (expected, test) {
return function (xs) {
test.same(xs, expected);
};
};
cb();
},
'arrayStream': function (test) {
test.expect(5);
_(this.input).slice(2, 6).toArray(this.tester(this.expected, test));
_(this.input).slice(2).toArray(this.tester(this.expected, test));
_(this.input).slice().toArray(this.tester(this.input, test));
_(this.input).slice(-1, 6).toArray(this.tester(this.input, test));
_(this.input).slice(0).toArray(this.tester(this.input, test));
test.done();
},
'partial application': function (test) {
test.expect(1);
var s = _(this.input);
_.slice(1, 4)(s).toArray(this.tester([2, 3, 4], test));
test.done();
},
'negative indicies': function(test) {
test.expect(1);
_.slice(-5, Infinity)(this.input).toArray(this.tester(this.input, test));
test.done();
},
'error': function (test) {
test.expect(2);
var s = _(function (push, next) {
push(null, 1),
push(new Error('Slice error')),
push(null, 2),
push(null, 3),
push(null, 4),
push(null, 5),
push(null, _.nil)
});
s.slice(2, 4).errors(errorEquals(test, 'Slice error'))
.toArray(this.tester([3, 4], test));
test.done();
},
'noValueOnError': noValueOnErrorTest(_.slice(2, 3))
};

exports['take - noValueOnError'] = noValueOnErrorTest(_.take(1));

exports['take - errors'] = function (test) {
Expand Down Expand Up @@ -1010,6 +1059,11 @@ exports['drop'] = {
_.drop(2)(s).toArray(this.tester(this.expected, test));
test.done();
},
'negative indicies': function(test) {
test.expect(1);
_.drop(-1)(this.input).toArray(this.tester(this.input, test));
test.done();
},
'error': function (test) {
test.expect(2);
var s = _(function (push, next) {
Expand Down