Permalink
Browse files

making asynchronous

  • Loading branch information...
1 parent e6cdcb3 commit d3b7dc4bf8a7fc010056fd5fedbc31ff49200ee8 @yacc committed Oct 3, 2013
Showing with 43 additions and 15 deletions.
  1. +5 −0 bin/sendsms
  2. +16 −8 lib/main.js
  3. +2 −1 package.json
  4. +20 −6 test/main.js
View
5 bin/sendsms
@@ -0,0 +1,5 @@
+#!/usr/bin/env node
+var sendsms = require('../lib/main');
+
+var args = process.argv.slice(2);
+sendsms(args,console.log);
View
24 lib/main.js
@@ -1,11 +1,19 @@
-function sendsms(cellphone,carrier) {
- if (arguments.length < 2) {
- return 'Please provide a phone number, a phone carrier';
- }
- // if (typeof arguments[0] != "string") {
- // alert("Has to be string");
- // }
- return true;
+// args : [country_code,phone_number,carrier]
+// callback : callback to execute
+//
+function sendsms(args,callback) {
+ var valid_number = /^\d{1,12}/;
+
+ if (args.length < 3)
+ return process.nextTick(function() {callback('Please provide a country code, a phone number (6503944444) and a phone carrier');});
+
+ if (typeof args[1].search(valid_number)==-1 )
+ return process.nextTick(function() {callback('Please provide the phone number using this format +[country code][area code][number]');});
+
+ if (typeof args[2] != "string")
+ return process.nextTick(function() {callback('Phone carrier has to be a string');});
+
+ callback('message sent to +' + args[0] + ' ' + args[1] + ' through ' + args[2]);
}
View
3 package.json
@@ -28,5 +28,6 @@
"devDependencies": {
"mocha": "",
"should": ""
- }
+ },
+ "bin": { "sleep-sort": "./bin/sendsms" }
}
View
26 test/main.js
@@ -2,11 +2,25 @@ var should = require('should');
var sendsms = require('../lib/main');
describe('sendsms', function() {
- describe('with no arguments', function() {
- it('returns an error', function() {
- var result = sendsms();
- result.should.eql('Please provide a phone number, a phone carrier');
- });
- });
+
+ describe('with an empty array argument', function() {
+ it('calls the callback with an empty array', function(done) {
+ var result = sendsms([], function(result) {
+ result.should.eql('Please provide a country code, a phone number (6503944444) and a phone carrier');
+ done();
+ });
+ });
+ });
+
+ describe('with valid arguments', function() {
+ it('calls the callback with a success message', function(done) {
+ var result = sendsms(['1','6503444444','TMobile'], function(result) {
+ result.should.eql('message sent to +1 6503444444 through TMobile');
+ done();
+ });
+ });
+ });
+
+
});

0 comments on commit d3b7dc4

Please sign in to comment.