-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
232 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); | ||
}); | ||
}); |