Skip to content

Commit

Permalink
First implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tusbar committed Jul 30, 2015
1 parent b9b5aad commit e2debe2
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 3 deletions.
44 changes: 44 additions & 0 deletions bin/millipede
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
#!/usr/bin/env node

var program = require('commander');
var validator = require('validator');

var pjson = require('../package.json');
var millipede = require('../lib');

// ## //

var int = function (unsigned) {
return function (value, initial) {
initial = Number.isInteger(initial) ? initial : 0;
if (validator.isInt(value)) {
value = parseInt(value, 10);
return value < 0 && unsigned ? initial : value;
}
return initial;
};
};

program
.version(pjson.version)
.option('-s, --size <value>', 'size of the millipede', int(true), 20)
.option('-r, --reverse', 'reverse the millipede')
.parse(process.argv);

try {
console.log(
millipede(program.size, {
reverse: program.reverse
})
.toString()
);
}
catch (err) {
if (err instanceof millipede.MillipedeValidationError) {
console.error('error:', err.message);
}
else {
console.error('error: something unexpected happened');
console.error(err.stack);
}
process.exit(1);
}
11 changes: 11 additions & 0 deletions lib/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require('extend-error');

// ## //

var MillipedeError = Error.extend('MillipedeError');
var MillipedeValidationError = MillipedeError.extend('MillipedeValidationError');

// ## //

exports.MillipedeError = MillipedeError;
exports.MillipedeValidationError = MillipedeValidationError;
26 changes: 26 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var Millipede = require('./millipede');
var errors = require('./errors');

// ## //

/**
* Create a millipede
*
* @return {Millipede}
* @api public
*/

function createMillipede() {
var millipede = Object.create(Millipede.prototype);
Millipede.apply(millipede, arguments);
return millipede;
}

// ## //

module.exports = exports = createMillipede;

exports.Millipede = Millipede;

exports.MillipedeError = errors.MillipedeError;
exports.MillipedeValidationError = errors.MillipedeValidationError;
100 changes: 100 additions & 0 deletions lib/millipede.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
var errors = require('./errors');

// ## //

var PADDING = [
' ',
' ',
'',
' ',
' ',
' ',
' ',
' ',
' '
];

/**
* Initialize a new `Millipede` with the given `size` and `options`.
*
* @param {Number} size
* @param {Object} options
* @return {Millipede}
* @public
*/

function Millipede(size, options) {
options = options || {};

this.size = size || 20;
this.reverse = options.reverse || false;

this.validate();
}

/**
* Return the `Millipede`’s head as a string
*
* @return {String} representing the `Millipede`’ head
* @private
*/

Millipede.prototype._getHead = function () {
if (this.reverse) {
return PADDING[PADDING.length - 1] + ' ╔⊙ ⊙╗';
}
return PADDING[0] + ' ╚⊙ ⊙╝';
};

/**
* Return the `Millipede`’s body as a string
*
* @return {String} representing the `Millipede`’ body
* @private
*/

Millipede.prototype._getBody = function () {
var self = this;

return Array.apply(null, Array(this.size)).map(function (val, index) {
if (self.reverse) {
return PADDING[PADDING.length - 1 - index % PADDING.length] + '╔═(███)═╗';
}
return PADDING[index % PADDING.length] + '╚═(███)═╝';
}).join('\n');
};

/**
* Validate `Millipede` settings
*
* @return {String} representing the `Millipede`
* @throw {MillipedeValidationError} when a setting is invalid
* @public
*/

Millipede.prototype.validate = function () {
if (this.size > 100000) {
throw new errors.MillipedeValidationError('the size is too great, try something lower than 100,000');
}
};

/**
* Return the `Millipede` as a string
*
* @param {Object} options
* @return {String} representing the `Millipede`
* @public
*/

Millipede.prototype.toString = function () {
if (this.reverse) {
return this._getBody() + '\n' + this._getHead();
}
return this._getHead() + '\n' + this._getBody();
};

Millipede.prototype.inspect = Millipede.prototype.toString;

// ## //

module.exports = exports = Millipede;
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
"time-grunt": "1.2.1"
},
"dependencies": {
"commander": "^2.8.1",
"coveralls": "^2.11.3",
"validator": "^3.41.2"
"commander": "2.8.1",
"coveralls": "2.11.3",
"extend-error": "0.0.2",
"validator": "3.41.2"
}
}
47 changes: 47 additions & 0 deletions test/millipede.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* global describe, it, expect */
/* jshint expr: true */

var millipede = require('../lib');

// ## //

describe('millipede', function () {
describe('render', function () {
it('should render a millipede of size 1', function () {
expect(millipede(1).toString()).to.equal([
' ╚⊙ ⊙╝',
' ╚═(███)═╝'
].join('\n'));
});

it('should render a millipede of size 24', function () {
expect(millipede(24).toString()).to.equal([
' ╚⊙ ⊙╝',
' ╚═(███)═╝',
' ╚═(███)═╝',
'╚═(███)═╝',
' ╚═(███)═╝',
' ╚═(███)═╝',
' ╚═(███)═╝',
' ╚═(███)═╝',
' ╚═(███)═╝',
' ╚═(███)═╝',
' ╚═(███)═╝',
' ╚═(███)═╝',
'╚═(███)═╝',
' ╚═(███)═╝',
' ╚═(███)═╝',
' ╚═(███)═╝',
' ╚═(███)═╝',
' ╚═(███)═╝',
' ╚═(███)═╝',
' ╚═(███)═╝',
' ╚═(███)═╝',
'╚═(███)═╝',
' ╚═(███)═╝',
' ╚═(███)═╝',
' ╚═(███)═╝'
].join('\n'));
});
});
});

0 comments on commit e2debe2

Please sign in to comment.