From b436caf431c680ee2dbb00d1f9c49452b5ed137c Mon Sep 17 00:00:00 2001 From: Steve Conover Date: Wed, 16 Feb 2011 11:32:18 -0800 Subject: [PATCH] functions that generically filter and map. not quite sure where I'm going with this. --- README.markdown | 2 +- lib/async_functions.js | 19 +++++++++++++++++++ spec/async/filter_spec.js | 21 +++++++++++++++++++++ spec/async/map_spec.js | 25 +++++++++++++++++++++++++ spec/async/spec_helper.js | 10 ++++++++++ spec/async/suite.js | 3 +++ 6 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 lib/async_functions.js create mode 100644 spec/async/filter_spec.js create mode 100644 spec/async/map_spec.js create mode 100644 spec/async/spec_helper.js create mode 100644 spec/async/suite.js diff --git a/README.markdown b/README.markdown index 1e7c553..662d14a 100644 --- a/README.markdown +++ b/README.markdown @@ -2,7 +2,7 @@ CollectionFunctions is a javascript library that provides classic collection man I'm a fan of underscore.js [LINK] but I found I needed collection functions that didn't make strong assumptions about the underlying storage format (underscore assumes Arrays). -CollectionFunctions.Array is an Array-backed instance of the functions, which can be used very much like underscore: +CollectionFunctions.Array is an Array-backed instance of the functions, which can be used very much like underscore //aside: http://aresemicolonsnecessaryinjavascript.com diff --git a/lib/async_functions.js b/lib/async_functions.js new file mode 100644 index 0000000..6758b46 --- /dev/null +++ b/lib/async_functions.js @@ -0,0 +1,19 @@ +AsyncFunctions = function(features) { + var functionsForExport = {} + + function filter(selector, callback) { + return function(item) { + if (selector(item)) callback(item) + } + } + functionsForExport.filter = filter + + function map(transformer, callback) { + return function(item) { + callback(transformer(item)) + } + } + functionsForExport.map = map + + return {functions:functionsForExport} +} \ No newline at end of file diff --git a/spec/async/filter_spec.js b/spec/async/filter_spec.js new file mode 100644 index 0000000..75fb864 --- /dev/null +++ b/spec/async/filter_spec.js @@ -0,0 +1,21 @@ +require("./spec_helper.js"); + +describe("filter", function() { + + var f = AsyncFunctions().functions + + it("filter out items", function(){ + var results = [] + var filter = f.filter(function(item){return item % 3}, function(item){results.push(item)}) + + filter(1) + filter(2) + filter(3) + filter(4) + filter(5) + filter(6) + filter(7) + + expect(results).toEqual([1,2, 4,5, 7]) + }) +}) diff --git a/spec/async/map_spec.js b/spec/async/map_spec.js new file mode 100644 index 0000000..382a388 --- /dev/null +++ b/spec/async/map_spec.js @@ -0,0 +1,25 @@ +require("./spec_helper.js"); + +describe("map", function() { + + var f = AsyncFunctions().functions + + //in a way "stream" is now inaccurate. + //stream implies order. async means it may or may not be ordered, + //there's no way for the library to tell + //in fact there's no way for the library to even get in the way...it just getting called back and doing some tranform on the result. + + it("simply transforms a value from a callback", function(){ + var results = [] + var mapper = f.map(function(item){return "x" + item}, function(transformedItem){results.push(transformedItem)}) + + mapper(1) + mapper(2) + + expect(results).toEqual(["x1", "x2"]) + + mapper(3) + + expect(results).toEqual(["x1", "x2", "x3"]) + }) +}) diff --git a/spec/async/spec_helper.js b/spec/async/spec_helper.js new file mode 100644 index 0000000..93c4274 --- /dev/null +++ b/spec/async/spec_helper.js @@ -0,0 +1,10 @@ +require.paths.push("spec") +require.paths.push("lib") + +require("../../../jasmine-node/lib/jasmine") + +for(var key in jasmine) { + global[key] = jasmine[key] +} + +require("async_functions") \ No newline at end of file diff --git a/spec/async/suite.js b/spec/async/suite.js new file mode 100644 index 0000000..02d1167 --- /dev/null +++ b/spec/async/suite.js @@ -0,0 +1,3 @@ +require("./spec_helper") + +jasmine.requireAllSpecFiles(__dirname)