Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,33 @@ Usage: morse [options] string

Options:
-d, --decode Decode a string of Morse code [boolean]
-h, --help Show this text
-h, --help Show this text
-p, --pipe Use stdin and pipe to stdout

$ morse hello > hello.txt
$ morse -d "`cat hello.txt`"
HELLO
````

### using pipes
You can use pipes to pipe text into morse with the `-p` flag or `--pipe`. Encode:
```bash
$ echo "Hello morse" | morse -p
.... . .-.. .-.. --- ....... -- --- .-. ... . ?
```

Decode:
```bash
$ echo ".... . .-.. .-.. --- ....... -- --- .-. ... . ?" | morse -p -d
HELLO MORSE
```

Pipe twice for fun
```bash
$ echo "Hello morse" | morse -p | morse -p -d
HELLO MORSE
```

## example usage as a library

````javascript
Expand All @@ -49,6 +69,23 @@ morse.decode(encoded);
// [ 'HELLO', 'WORLD' ]
````

### example usage with streams

```javascript
var morse = require('morse');
var fs = require('fs');

// Encode
fs.createReadStream(__dirname + '/file.txt')
.pipe(morse.createEncodeStream())
.pipe(process.stdout);

// Decode
fs.createReadStream(__dirname + '/file-encoded.txt')
.pipe(morse.createDecodeStream())
.pipe(process.stdout);
```

## methods

### morse.encode(obj)
Expand Down
13 changes: 12 additions & 1 deletion bin/morse.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@ var argv = optimist
.describe('d', 'Decode a string of Morse code')
.alias('h', 'help')
.describe('h', 'Show this text')
.alias('p', 'pipe')
.describe('p', 'Use stdin and pipe to stdout')
.argv;

if (argv.h || !argv._.length) {
if (argv.h || (!argv._.length && !argv.p)) {
optimist.showHelp();
process.exit();
}

// Check for pipe
if(argv.p) {
if(argv.d) {
process.stdin.pipe(morse.createDecodeStream()).pipe(process.stdout);
} else {
process.stdin.pipe(morse.createEncodeStream()).pipe(process.stdout);
}
}

var str = argv._.join(' ');
if (argv.d) {
console.log(morse.decode(str));
Expand Down
20 changes: 19 additions & 1 deletion morse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,29 @@ module.exports = {
encode: encode,
decode: decode,
map: map,
tree: tree
tree: tree,
createEncodeStream: createEncodeStream,
createDecodeStream: createDecodeStream
};

var map = require('./map');
var tree = require('./tree');
var util = require('util');
var Transform = require('stream').Transform;
var through2 = require('through2');

// Stream support
function createEncodeStream() {
return through2({}, function(chunk, enc, callback) {
callback(null, encode(chunk.toString()));
});
}

function createDecodeStream() {
return through2({}, function(chunk, enc, callback) {
callback(null, decode(chunk.toString()));
});
}

function encode (obj) {
return maybeRecurse(obj, encodeMorseString);
Expand Down
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"name": "morse",
"description": "Morse code for mad scientists",
"version": "0.1.0",
"scripts": {
"test": "mocha test/*Spec.js"
},
"repository": {
"type": "git",
"url": "git://github.com/ecto/morse.git"
Expand All @@ -12,9 +15,12 @@
},
"main": "morse.js",
"dependencies": {
"optimist": "*"
"optimist": "*",
"through2": "^2.0.1"
},
"devDependencies": {
"mocha": "^2.4.5"
},
"devDependencies": {},
"optionalDependencies": {},
"engines": {
"node": "*"
Expand Down
1 change: 1 addition & 0 deletions test/lorem-ipsum-encoded.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.-.. --- .-. . -- ....... .. .--. ... ..- -- ....... -.. --- .-.. --- .-. ....... ... .. - ....... .- -- . - --..-- ....... -.-. --- -. ... . -.-. - . - ..- .-. ....... .- -.. .. .--. .. ... -.-. .. -. --. ....... . .-.. .. - .-.-.- ....... ..- - ....... ..- - ....... --- .-. -.-. .. ....... . .-.. .. - .-.-.- ....... ...- . ... - .. -... ..- .-.. ..- -- ....... - .-. .. ... - .. --.- ..- . ....... .- .-. -.-. ..- ....... . ..- ....... --- -.. .. --- ....... -... .. -... . -. -.. ..- -- ....... - .. -. -.-. .. -.. ..- -. - .-.-.- ....... .- . -. . .- -. ....... .-.. .- --- .-. . . - ....... .- - ....... --- -.. .. --- ....... -.-. --- -. -.. .. -- . -. - ..- -- ....... ..-. .- ..- -.-. .. -... ..- ... .-.-.- ....... .. -. - . --. . .-. ....... -.-. --- -. ... . --.- ..- .- - ....... -. .. ... .. ....... -.. .. .- -- .-.-.- ....... -. ..- .-.. .-.. .- -- ....... .. -. ....... --- .-. -.-. .. ....... ..- - ....... ..-. . .-.. .. ... ....... ... --- -.. .- .-.. . ... ....... .- .-.. .. --.- ..- . - .-.-.- ....... .--. .... .- ... . .-.. .-.. ..- ... ....... . - ....... -. ..- .-.. .-.. .- ....... . --. . - ....... .-.. .. --. ..- .-.. .- ....... ...- .. ...- . .-. .-. .- ....... ...- .- .-. .. ..- ... ....... . - ....... .- - ....... -. ..- -. -.-. .-.-.- ....... -. ..- .-.. .-.. .- ....... ..-. .- -.-. .. .-.. .. ... .. .-.-.- ....... -. ..- .-.. .-.. .- -- ....... .- ..- -.-. - --- .-. ....... -. . -.-. ....... --- .-. -.-. .. ....... -. --- -. ....... ..-. .-. .. -. --. .. .-.. .-.. .- .-.-.- ....... -.-. .-. .- ... ....... -. --- -. ....... .. -. - . .-. -.. ..- -- ....... ... .- .--. .. . -. .-.-.- ....... -.. ..- .. ... ....... ...- .. ...- . .-. .-. .- ....... ... .. - ....... .- -- . - ....... ...- . .-.. .. - ....... ... .. - ....... .- -- . - ....... . .-.. . .. ..-. . -. -.. .-.-.- ....... -. ..- .-.. .-.. .- -- ....... .- ..- -.-. - --- .-. ....... .- ....... ...- . .-.. .. - ....... .- -.-. ....... ..-. .- -.-. .. .-.. .. ... .. ... .-.-.-
1 change: 1 addition & 0 deletions test/lorem-ipsum.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut ut orci elit. Vestibulum tristique arcu eu odio bibendum tincidunt. Aenean laoreet at odio condimentum faucibus. Integer consequat nisi diam. Nullam in orci ut felis sodales aliquet. Phasellus et nulla eget ligula viverra varius et at nunc. Nulla facilisi. Nullam auctor nec orci non fringilla. Cras non interdum sapien. Duis viverra sit amet velit sit amet eleifend. Nullam auctor a velit ac facilisis.
67 changes: 67 additions & 0 deletions test/streamSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var morse = require('../');
var assert = require('assert');
var fs = require('fs');
var through2 = require('through2');

// Saves everything in .result
function StringStream() {
this.result = '';
}

StringStream.prototype.createStream = function() {
var _this = this;

return through2({}, function(chunk, enc, callback) {
_this.result += chunk.toString();
callback(null, chunk);
}, function(callback) {
callback();
});
}

describe('stream', function() {

it('should encode a stream', function(done) {

fs.readFile(__dirname + '/lorem-ipsum.txt', 'utf8', function(err, text) {
if(err) return console.error(err);

var encoded = morse.encode(text);

var stringStream = new StringStream();

// Test it with streams
var readStream = fs.createReadStream(__dirname + '/lorem-ipsum.txt', {encoding: 'utf8'});
readStream
.pipe(morse.createEncodeStream())
.pipe(stringStream.createStream())
.on('finish', function() {

assert.equal(stringStream.result, encoded);
done();
});
});
});


it('should decode a stream', function(done) {
fs.readFile(__dirname + '/lorem-ipsum-encoded.txt', 'utf8', function(err, text) {
if(err) return console.error(err);

var decoded = morse.decode(text);

var stringStream = new StringStream();

// Test it with streams
var readStream = fs.createReadStream(__dirname + '/lorem-ipsum-encoded.txt', {encoding: 'utf8'});
readStream
.pipe(morse.createDecodeStream())
.pipe(stringStream.createStream())
.on('finish', function() {

assert.equal(stringStream.result, decoded);
done();
});
});
});
});