Comparing version 0.0.3 to 0.0.4
@@ -10,2 +10,3 @@ "use strict"; | ||
exports.exit = exit; | ||
exports.exitAfter = exitAfter; | ||
exports.find = find; | ||
@@ -49,2 +50,6 @@ exports.filter = filter; | ||
exitAfter(fn, result) { | ||
return new Seq(exitAfter(this.seq, fn, result)); | ||
} | ||
filter(fn) { | ||
@@ -58,4 +63,4 @@ return new Seq(filter(this.seq, fn)); | ||
first() { | ||
return first(this.seq); | ||
first(predicate) { | ||
return first(this.seq, predicate); | ||
} | ||
@@ -67,4 +72,4 @@ | ||
last() { | ||
return last(this.seq); | ||
last(predicate) { | ||
return last(this.seq, predicate); | ||
} | ||
@@ -137,2 +142,14 @@ | ||
function exitAfter(seq, fn, result) { | ||
return function* () { | ||
for (const item of seq()) { | ||
if (fn(item)) { | ||
yield item; | ||
return; | ||
} | ||
yield item; | ||
} | ||
}; | ||
} | ||
function find(seq, fn) { | ||
@@ -156,3 +173,5 @@ for (const item of seq()) { | ||
function first(seq) { | ||
function first(_seq, predicate) { | ||
const seq = predicate ? filter(_seq, predicate) : _seq; | ||
for (const item of seq()) { | ||
@@ -169,3 +188,5 @@ return item; | ||
function last(seq) { | ||
function last(_seq, predicate) { | ||
const seq = predicate ? filter(_seq, predicate) : _seq; | ||
let prev; | ||
@@ -172,0 +193,0 @@ for (const item of seq()) { |
{ | ||
"name": "lazily", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"author": "Jeswin<jeswinpk@agilehead.com>", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -15,2 +15,4 @@ # lazily | ||
```javascript | ||
import { Seq } from "lazily"; | ||
const seq = Seq.of([1, 2, 3]) | ||
@@ -23,3 +25,4 @@ for (const i of seq) { | ||
## It's lazy | ||
Sequences are lazy. For example, the following example only one map() action is performed irrespective of the length of the sequence. | ||
Sequences are lazy. For example, in the following example only one map() action is performed irrespective of the length of the sequence. | ||
```javascript | ||
@@ -61,2 +64,11 @@ const seq = Seq.of([1, 2, 3]) | ||
## exitAfter(predicate) | ||
Similar to exit(), but includes the last item as well. | ||
```javascript | ||
Seq.of([1, 2, 3, 4, 5]) | ||
.exitAfter(x => x > 3) | ||
.toArray() | ||
// [1, 2, 3, 4] | ||
``` | ||
## exit(predicate) in the middle | ||
@@ -91,2 +103,8 @@ ```javascript | ||
## first(predicate) | ||
```javascript | ||
Seq.of([1, 2, 3, 4, 5]).first(x => x > 3); | ||
// | ||
``` | ||
## last() | ||
@@ -98,2 +116,8 @@ ```javascript | ||
## last(predicate) | ||
```javascript | ||
Seq.of([1, 2, 3, 4, 5]).last(x => x < 3); | ||
// 2 | ||
``` | ||
## every(predicate) | ||
@@ -129,3 +153,3 @@ ```javascript | ||
## slice(begin, end)() | ||
## slice(begin, end) | ||
```javascript | ||
@@ -132,0 +156,0 @@ Seq.of([1, 2, 3, 4, 5, 6]).slice(2, 4).toArray(); |
Sorry, the diff of this file is not supported yet
44694
211
153