-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
StreamFunctions = function(features) { | ||
var terminator = features.eof || {} | ||
var functionsForExport = {} | ||
|
||
function eof(thing) { | ||
return thing === terminator | ||
} | ||
functionsForExport.eof = eof | ||
|
||
|
||
function detect(stream, matcher) { | ||
var candidate = null | ||
while (!eof(candidate=stream.next())) { | ||
if (matcher(candidate)) return candidate | ||
} | ||
return terminator | ||
} | ||
functionsForExport.detect = detect | ||
// | ||
// function select(iterator, matcher) { | ||
// return { | ||
// var empty = {} | ||
// var nextMatch == features.nothing() | ||
// | ||
// function advanceIfNecessary() { | ||
// if (nextMatch === empty) nextMatch = detect(iterator, matcher) | ||
// return nextMatch | ||
// } | ||
// | ||
// hasNext: function() { | ||
// return !(advanceIfNecessary() === terminator) | ||
// }, | ||
// next: function() { | ||
// if (!hasNext()) throw "after end" | ||
// var result = advanceIfNecessary() | ||
// nextMatch = empty | ||
// return result | ||
// } | ||
// } | ||
// } | ||
// functionsForExport.select = select | ||
|
||
return {functions:functionsForExport} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require("./spec_helper.js"); | ||
|
||
describe("detect", function() { | ||
|
||
var f = StreamFunctions({eof:EOF}).functions | ||
|
||
it("finds the next occurrence in a stream and then stops", function(){ | ||
var stream = arrayStream([1,2,3,4,5,6]) | ||
expect(f.detect(stream, function(item){return item %2==0})).toEqual(2) | ||
expect(f.detect(stream, function(item){return item %2==0})).toEqual(4) | ||
expect(f.detect(stream, function(item){return item %2==0})).toEqual(6) | ||
expect(f.eof(f.detect(stream, function(item){return item %2==0}))).toEqual(true) | ||
}) | ||
|
||
it("returns eof if there's no match", function(){ | ||
var stream = arrayStream([1,2,3,4,5,6]) | ||
var result = f.detect(stream, function(item){return item == 99}) | ||
expect(f.eof(result)).toEqual(true) | ||
}) | ||
|
||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require.paths.push("spec") | ||
require.paths.push("lib") | ||
|
||
require("../../../jasmine-node/lib/jasmine") | ||
|
||
for(var key in jasmine) { | ||
global[key] = jasmine[key] | ||
} | ||
|
||
require("stream_functions") | ||
|
||
EOF = "THIS IS THE END" | ||
|
||
arrayStream = function(arr) { | ||
var position = 0 | ||
return { | ||
next: function() { | ||
if (position == arr.length) { | ||
return EOF | ||
} else { | ||
var result = arr[position] | ||
position += 1 | ||
return result | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require("./spec_helper") | ||
|
||
jasmine.requireAllSpecFiles(__dirname) |