From b1b24d707df123df73a832c81ab03d4bb5e039ef Mon Sep 17 00:00:00 2001 From: Alexander Olsson Date: Tue, 8 Apr 2014 20:29:24 +0200 Subject: [PATCH] JSHint: Make sure code style is enforced. --- .jshintignore | 1 + .jshintrc | 18 ++++++++++++++++++ lib/alphabets.js | 4 +++- lib/base85.js | 43 +++++++++++++++++++++++++------------------ package.json | 5 +++-- tests/data.js | 20 +++++++++++++++++--- tests/decode.js | 20 +++++++++++--------- tests/encode.js | 6 ++++-- tests/loremipsum.js | 7 ++++--- 9 files changed, 86 insertions(+), 38 deletions(-) create mode 100644 .jshintignore create mode 100644 .jshintrc diff --git a/.jshintignore b/.jshintignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.jshintignore @@ -0,0 +1 @@ +node_modules diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..caa7f77 --- /dev/null +++ b/.jshintrc @@ -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 +} diff --git a/lib/alphabets.js b/lib/alphabets.js index 3f3dd32..759b3f7 100644 --- a/lib/alphabets.js +++ b/lib/alphabets.js @@ -1,3 +1,5 @@ +'use strict'; + var _ = require('underscore'); var alphabet = {}; @@ -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); diff --git a/lib/base85.js b/lib/base85.js index 2966892..758ba83 100644 --- a/lib/base85.js +++ b/lib/base85.js @@ -1,3 +1,5 @@ +'use strict'; + var alphabets = require('./alphabets'); var NUM_MAXVALUE = Math.pow(2, 32) - 1; @@ -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; @@ -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; @@ -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; }; @@ -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; } diff --git a/package.json b/package.json index 5fe0317..f088ce1 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tests/data.js b/tests/data.js index 8eb53fb..1e0501c 100644 --- a/tests/data.js +++ b/tests/data.js @@ -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/0JhKFCj@.4Gp$d7F!,L7@<6@)/0JDEF@3BB/F*&OCAfu2/AKYi(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF-FD5W8ARlolDIal(DIduD.RTpAKYo\'+CT/5+Cei#DII?(E,9)oF*2M7~>', - 'z85' : 'o<}]Zx(+zcx(!xgzFa9aB7/b}efF?GBrCHtyCj@.4Gp$d7F!,L7@<6@)/0JDEF' + + '@3BB/F*&OCAfu2/AKYi(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$A' + + 'ftVqCh[NqF-FD5W8ARlolDIal(DIduD.RTpAKYo\'' + + '+CT/5+Cei#DII?(E,9)oF*2M7~>', + 'z85' : 'o<}]Zx(+zcx(!xgzFa9aB7/b}efF?GBrCHty', 'ascii85'), false); test.equal(base85.decode('<~uuuuu~>', 'ascii85'), false); test.done(); -} +}; exports.testBasic = function(test) { @@ -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("<~@ps7tD.3 \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('<~@ps7tD.3 \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(); -} +}; diff --git a/tests/encode.js b/tests/encode.js index 4f9f018..0a47300 100644 --- a/tests/encode.js +++ b/tests/encode.js @@ -1,3 +1,5 @@ +'use strict'; + var _ = require('underscore'); var base85 = require('../lib/base85'); var data = require('./data').data; @@ -6,7 +8,7 @@ exports.testErrors = function(test) { test.equal(base85.encode(1234, 'ascii85'), false); test.done(); -} +}; exports.testBasic = function(test) { @@ -20,4 +22,4 @@ exports.testBasic = function(test) }); test.done(); -} +}; diff --git a/tests/loremipsum.js b/tests/loremipsum.js index 731a4fd..2213d75 100644 --- a/tests/loremipsum.js +++ b/tests/loremipsum.js @@ -1,3 +1,5 @@ +'use strict'; + var base85 = require('../lib/base85'); var fs = require('fs'); @@ -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) { @@ -25,5 +27,4 @@ module.exports.z85 = function(test) test.deepEqual(decoded, raw); test.deepEqual(encoded, enc.toString('ascii')); test.done(); -} - +};