Skip to content

Commit

Permalink
JSHint: Make sure code style is enforced.
Browse files Browse the repository at this point in the history
  • Loading branch information
noseglid committed Apr 8, 2014
1 parent fe37188 commit b1b24d7
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 38 deletions.
1 change: 1 addition & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
18 changes: 18 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"camelcase" : true,
"curly" : true,
"eqeqeq" : true,
"freeze" : true,
"immed" : true,
"indent" : 2,
"newcap" : true,
"nonew" : true,
"quotmark" : "single",
"undef" : true,
"unused" : true,
"strict" : true,
"trailing" : true,
"maxlen" : 120,

"node" : true
}
4 changes: 3 additions & 1 deletion lib/alphabets.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var _ = require('underscore');

var alphabet = {};
Expand Down Expand Up @@ -186,7 +188,7 @@ alphabet.z85.enc = {
82: '%',
83: '$',
84: '#'
}
};
alphabet.z85.dec = _.object(
_.map(_.values(alphabet.z85.enc), function(v) { /* The keys */
return v.charCodeAt(0);
Expand Down
43 changes: 25 additions & 18 deletions lib/base85.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var alphabets = require('./alphabets');

var NUM_MAXVALUE = Math.pow(2, 32) - 1;
Expand All @@ -21,7 +23,7 @@ var IGNORE_CHARS = [
var ASCII85_ENC_START = '<~';
var ASCII85_ENC_END = '~>';

function encode_buffer(buffer, encoding)
function encodeBuffer(buffer, encoding)
{
var enctable = alphabets[encoding].enc;
var padding = (buffer.length % 4 === 0) ? 0 : 4 - buffer.length % 4;
Expand Down Expand Up @@ -51,13 +53,13 @@ function encode_buffer(buffer, encoding)
(('ascii85' === encoding) ? ASCII85_ENC_END : '');
}

function encode_string(string, encoding)
function encodeString(string, encoding)
{
var buffer = new Buffer(string, 'utf8'); // utf8 at all times?
return encode_buffer(buffer, encoding);
return encodeBuffer(buffer, encoding);
}

function decode_buffer(buffer, encoding)
function decodeBuffer(buffer, encoding)
{
var dectable = alphabets[encoding].dec;

Expand All @@ -71,14 +73,15 @@ function decode_buffer(buffer, encoding)
var bufferStart = ('ascii85' === encoding) ? ASCII85_ENC_START.length : 0;
var bufferEnd = bufferStart + dataLength;

var result = new Buffer(4 * Math.ceil((bufferEnd - bufferStart) / 5))
var result = new Buffer(4 * Math.ceil((bufferEnd - bufferStart) / 5));

var nextValidByte = function(index) {
if (index < bufferEnd)
if (index < bufferEnd) {
while (-1 !== IGNORE_CHARS.indexOf(buffer[index])) {
padding = (padding + 1) % 5;
index++; // skip newline character
}
}
return index;
};

Expand Down Expand Up @@ -115,39 +118,43 @@ function decode_buffer(buffer, encoding)
return result.slice(0, writeIndex - padding);
}

function decode_string(string, encoding)
function decodeString(string, encoding)
{
var buffer = new Buffer(string, 'utf8'); // utf8 at all times?
return decode_buffer(buffer, encoding);
return decodeBuffer(buffer, encoding);
}

module.exports = {
encode : function(data, encoding) {
var encoding = encoding || DEFAULT_ENCODING;
encoding = encoding || DEFAULT_ENCODING;
if (-1 === [ 'ascii85', 'z85' ].indexOf(encoding)) {
return false;
}

if (data instanceof Buffer)
return encode_buffer(data, encoding);
if (data instanceof Buffer) {
return encodeBuffer(data, encoding);
}

if (typeof data === 'string')
return encode_string(data, encoding);
if (typeof data === 'string') {
return encodeString(data, encoding);
}

return false;
},

decode : function(data, encoding) {
var encoding = encoding || DEFAULT_ENCODING;
encoding = encoding || DEFAULT_ENCODING;
if (-1 === [ 'ascii85', 'z85' ].indexOf(encoding)) {
return false;
}

if (data instanceof Buffer)
return decode_buffer(data, encoding);
if (data instanceof Buffer) {
return decodeBuffer(data, encoding);
}

if (typeof data === 'string')
return decode_string(data, encoding);
if (typeof data === 'string') {
return decodeString(data, encoding);
}

return false;
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"description": "Base85 (Ascii85) encode and decode functionality",
"main": "lib/base85.js",
"scripts": {
"test": "./node_modules/.bin/nodeunit tests"
"test": "./node_modules/.bin/jshint . && ./node_modules/.bin/nodeunit tests"
},
"devDependencies": {
"nodeunit": "0.8.x",
"underscore": "1.6.x"
"underscore": "1.6.x",
"jshint": "2.5.x"
},
"repository": {
"type": "git",
Expand Down
20 changes: 17 additions & 3 deletions tests/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,24 @@ exports.data = [
}
},
{
'raw' : new Buffer('Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure', 'ascii'),
'raw' : new Buffer(
'Man is distinguished, not only by his reason, but by this singular passion ' +
'from other animals, which is a lust of the mind, that by a perseverance of ' +
'delight in the continued and indefatigable generation of knowledge, exceeds ' +
'the short vehemence of any carnal pleasure',
'ascii'),
'enc' : {
'ascii85' : '<~9jqo^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>[email protected]$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!,O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKYi(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIal(DId<j@<?3r@:F%a+D58\'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G>uD.RTpAKYo\'+CT/5+Cei#DII?(E,9)oF*2M7~>',
'z85' : 'o<}]Zx(+zcx(!xgzFa9aB7/b}efF?GBrCHty<vdjC{3^mB0bHmvrlv8efFzABrC4raARphB0bKrzFa9dvr9GfvrlH7z/cXfA=k!qz//V7AV!!dx(do{B1wCTxLy%&azC)tvixxeB95Kyw/#hewGU&7zE+pvBzb98ayYQsvixJ2A=U/nwPzi%v}u^3w/$R}y?WJ}BrCpnaARpday/tcBzkSnwN(](zE:(7zE^r<vrui@vpB4:azkn6wPzj3x(v(iz!pbczF%-nwN]B+efFIGv}xjZB0bNrwGV5cz/P}xC4Ct#zdNP{wGU]6ayPekay!&2zEEu7Abo8]B9hIm'
'ascii85' : '<~9jqo^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>[email protected]$d7F!,L7@<6@)/0JDEF' +
'<G%<+EV:2F!,O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF"AGXBPC' +
'si+DGm>@3BB/F*&OCAfu2/AKYi(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$A' +
'ftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIal(DId<j@<?3r@:F%a+D58\'ATD' +
'4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G>uD.RTpAKYo\'' +
'+CT/5+Cei#DII?(E,9)oF*2M7~>',
'z85' : 'o<}]Zx(+zcx(!xgzFa9aB7/b}efF?GBrCHty<vdjC{3^mB0bHmvrlv8efFzABrC4raA' +
'RphB0bKrzFa9dvr9GfvrlH7z/cXfA=k!qz//V7AV!!dx(do{B1wCTxLy%&azC)tvixx' +
'eB95Kyw/#hewGU&7zE+pvBzb98ayYQsvixJ2A=U/nwPzi%v}u^3w/$R}y?WJ}BrCpna' +
'ARpday/tcBzkSnwN(](zE:(7zE^r<vrui@vpB4:azkn6wPzj3x(v(iz!pbczF%-nwN]' +
'B+efFIGv}xjZB0bNrwGV5cz/P}xC4Ct#zdNP{wGU]6ayPekay!&2zEEu7Abo8]B9hIm'
}
},
{
Expand Down
20 changes: 11 additions & 9 deletions tests/decode.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
'use strict';

var _ = require('underscore');
var base85 = require('../lib/base85');
var data = require('./data').data;

exports.testErrors = function(test)
{
test.equal(base85.decode(1234, 'ascii85'), false)
test.equal(base85.decode(1234, 'ascii85'), false);
test.equal(base85.decode('<~u~>', 'ascii85'), false);
test.equal(base85.decode('<~uuuuu~>', 'ascii85'), false);
test.done();
}
};

exports.testBasic = function(test)
{
Expand All @@ -23,16 +25,16 @@ exports.testBasic = function(test)
});

test.done();
}
};

exports.testWhiteSpace = function(test)
{
test.deepEqual(base85.decode("<~\n@p\ns7\ntD.3~>", 'ascii85'), new Buffer('canumb'));
test.deepEqual(base85.decode("<~\n @ p \ns7 \n t D .3~>", 'ascii85'), new Buffer('canumb'));
test.deepEqual(base85.decode("<~\n @ p \ns7 \n t D .3 ~>", 'ascii85'), new Buffer('canumb'));
test.deepEqual(base85.decode("<[email protected] \n ~>", 'ascii85'), new Buffer('canumb'));
test.deepEqual(base85.decode("<~ @ps7tD.3 \n ~>", 'ascii85'), new Buffer('canumb'));
test.deepEqual(base85.decode('<~\n@p\ns7\ntD.3~>', 'ascii85'), new Buffer('canumb'));
test.deepEqual(base85.decode('<~\n @ p \ns7 \n t D .3~>', 'ascii85'), new Buffer('canumb'));
test.deepEqual(base85.decode('<~\n @ p \ns7 \n t D .3 ~>', 'ascii85'), new Buffer('canumb'));
test.deepEqual(base85.decode('<[email protected] \n ~>', 'ascii85'), new Buffer('canumb'));
test.deepEqual(base85.decode('<~ @ps7tD.3 \n ~>', 'ascii85'), new Buffer('canumb'));

test.deepEqual(base85.decode('v{ \n % \t m$z di'), new Buffer('canumb'));
test.done();
}
};
6 changes: 4 additions & 2 deletions tests/encode.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var _ = require('underscore');
var base85 = require('../lib/base85');
var data = require('./data').data;
Expand All @@ -6,7 +8,7 @@ exports.testErrors = function(test)
{
test.equal(base85.encode(1234, 'ascii85'), false);
test.done();
}
};

exports.testBasic = function(test)
{
Expand All @@ -20,4 +22,4 @@ exports.testBasic = function(test)
});

test.done();
}
};
7 changes: 4 additions & 3 deletions tests/loremipsum.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var base85 = require('../lib/base85');
var fs = require('fs');

Expand All @@ -12,7 +14,7 @@ module.exports.ascii85 = function(test)
test.deepEqual(decoded, raw);
test.deepEqual(encoded, enc.toString('ascii'));
test.done();
}
};

module.exports.z85 = function(test)
{
Expand All @@ -25,5 +27,4 @@ module.exports.z85 = function(test)
test.deepEqual(decoded, raw);
test.deepEqual(encoded, enc.toString('ascii'));
test.done();
}

};

0 comments on commit b1b24d7

Please sign in to comment.