Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit c4a86c37bf839366e372c32e2a5103666d7ca178 @pofallon committed Aug 25, 2012
Showing with 214 additions and 0 deletions.
  1. +22 −0 .gitattributes
  2. +3 −0 .gitignore
  3. +4 −0 .travis.yml
  4. +64 −0 index.js
  5. +29 −0 package.json
  6. +92 −0 test/test.js
@@ -0,0 +1,22 @@
+# Auto detect text files and perform LF normalization
+* text=auto
+
+# Custom for Visual Studio
+*.cs diff=csharp
+*.sln merge=union
+*.csproj merge=union
+*.vbproj merge=union
+*.fsproj merge=union
+*.dbproj merge=union
+
+# Standard to msysgit
+*.doc diff=astextplain
+*.DOC diff=astextplain
+*.docx diff=astextplain
+*.DOCX diff=astextplain
+*.dot diff=astextplain
+*.DOT diff=astextplain
+*.pdf diff=astextplain
+*.PDF diff=astextplain
+*.rtf diff=astextplain
+*.RTF diff=astextplain
@@ -0,0 +1,3 @@
+.DS_Store
+node_modules
+npm-debug.log
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - 0.4
+ - 0.6
@@ -0,0 +1,64 @@
+/*!
+ * node-callbackmaybe
+ * Copyright(c) 2012 Paul O'Fallon <paul@ofallonfamily.com>
+ * MIT Licensed
+ */
+
+var EventEmitter = require('events').EventEmitter;
+var util = require('util');
+
+util.inherits(CallbackMaybe, EventEmitter);
+
+function CallbackMaybe(func, options) {
+
+ var that = this;
+
+ EventEmitter.call(this);
+
+ if (!options) {
+ options = {};
+ }
+
+ this.writable = true;
+ this.count = 0;
+
+ if (typeof func === 'function') {
+ var list = [];
+
+ this.on('data', function(chunk) {
+ list.push(chunk);
+ });
+
+ this.on('end', function() {
+ func(null,list);
+ });
+
+ this.on('error', function(err) {
+ func(err);
+ });
+
+ }
+
+};
+
+CallbackMaybe.prototype.write = function(chunk) {
+ if (this.writable) {
+ this.count++;
+ this.emit('data', chunk);
+ return true;
+ } else {
+ return false;
+ }
+};
+
+CallbackMaybe.prototype.end = function() {
+ this.writable = false;
+ this.emit('end', this.count);
+};
+
+CallbackMaybe.prototype.error = function(err) {
+ this.writable = false;
+ this.emit('error', err);
+};
+
+module.exports = CallbackMaybe;
@@ -0,0 +1,29 @@
+{
+ "name": "callbackmaybe",
+ "description": "I just met you / and this is crazy / but here's my module / callbackmaybe",
+ "keywords": ["utility", "events", "callbacks"],
+ "author": "Paul O'Fallon <paul@ofallonfamily.com>",
+ "homepage": "https://github.com/pofallon/node-callbackmaybe",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/pofallon/node-callbackmaybe.git"
+ },
+ "scripts": {
+ "test": "mocha test/test.js"
+ },
+ "main": "index",
+ "dependencies": {},
+ "devDependencies": {
+ "mocha": "1.x.x",
+ "should": "1.x.x",
+ },
+ "engine": "node >= 0.4.1",
+ "version": "0.0.1",
+ "files": [
+ "index.js",
+ "package.json",
+ "README.md",
+ "test"
+ ],
+ "license": "MIT"
+}
@@ -0,0 +1,92 @@
+/*!
+ * node-callbackmaybe
+ * Copyright(c) 2012 Paul O'Fallon <paul@ofallonfamily.com>
+ * MIT Licensed
+ */
+
+var CallbackMaybe = require('../index.js');
+var should = require('should');
+
+describe('CallbackMaybe', function() {
+
+ beforeEach(function() {
+ });
+
+ afterEach(function() {
+ });
+
+ describe('with a callback', function() {
+
+ it('should return an array of results', function(done) {
+ var values = [1,2,3,4,5];
+ var count = 0;
+
+ var aCallback = function(err, list) {
+ should.not.exist(err);
+ should.exist(list);
+ list.should.eql(values);
+ done();
+ }
+
+ var cbm = new CallbackMaybe(aCallback);
+ values.forEach(function(value) {
+ cbm.write(value);
+ if (++count === values.length) {
+ cbm.end();
+ }
+ });
+ });
+
+ it('should handle errors', function(done) {
+ var aCallback = function(err, list) {
+ should.exist(err);
+ should.not.exist(list);
+ done();
+ }
+
+ var cbm = new CallbackMaybe(aCallback);
+ cbm.error(new Error());
+ });
+
+ });
+
+ describe('without a callback', function() {
+
+ it('should return an EventEmitter', function(done) {
+ var values = ["foo","foo","foo"];
+ var count = 0;
+
+ var cbm = new CallbackMaybe();
+ cbm.on('data', function(chunk) {
+ chunk.should.equal("foo");
+ });
+ cbm.on('end', function(count) {
+ count.should.equal(values.length);
+ done();
+ });
+ cbm.on('error', function(err) {
+ should.not.exist(err);
+ done();
+ });
+
+ values.forEach(function(value) {
+ cbm.write(value);
+ if (++count === values.length) {
+ cbm.end();
+ }
+ });
+
+ });
+
+ it('should handle errors', function(done) {
+ var cbm = new CallbackMaybe();
+ cbm.on('error', function(err) {
+ should.exist(err);
+ done();
+ });
+ cbm.error(new Error());
+ });
+
+ });
+
+});

0 comments on commit c4a86c3

Please sign in to comment.