diff --git a/.jscs.json b/.jscs.json new file mode 100644 index 00000000..9a551051 --- /dev/null +++ b/.jscs.json @@ -0,0 +1,80 @@ +{ + "requireCurlyBraces": ["if", "else", "for", "while", "do", "try", "catch"], + "requireSpaceAfterKeywords": ["if", "else", "for", "while", "do", "switch", "return", "try", "catch"], + + "requireSpaceBeforeBinaryOperators": ["?", "+", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="], + "requireSpaceAfterBinaryOperators": ["?", "+", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="], + "disallowSpaceAfterBinaryOperators": ["!"], + "disallowSpaceBeforeBinaryOperators": [","], + + "disallowMultipleVarDecl": true, + "disallowEmptyBlocks": true, + "disallowKeywords": ["with"], + "disallowKeywordsOnNewLine": ["else"], + "disallowSpacesInsideObjectBrackets": true, + "disallowSpacesInsideArrayBrackets": true, + "disallowSpacesInsideParentheses": true, + "disallowSpaceBeforePostfixUnaryOperators": ["++", "--"], + "disallowMultipleLineStrings": true, + "disallowTrailingWhitespace": true, + "disallowPaddingNewlinesInBlocks": true, + + "requireCommaBeforeLineBreak": true, + + "requireParenthesesAroundIIFE": true, + + "requireSpaceBeforeBlockStatements": true, + + "requireSpacesInConditionalExpression": true, + + "requireSpacesInFunctionExpression": { + "beforeOpeningCurlyBrace": true + }, + + "requireSpaceBeforeBinaryOperators": [ + "+", + "-", + "/", + "*", + "=", + "==", + "===", + "!=", + "!==" + ], + + "requireOperatorBeforeLineBreak": [ + "?", + "+", + "-", + "/", + "*", + "=", + "==", + "===", + "!=", + "!==", + ">", + ">=", + "<", + "<=" + ], + + "safeContextKeyword": "self", + + "validateIndentation": 4, + "validateParameterSeparator": ", ", + + "excludeFiles": [ + "test/data/**", + "test/support/fakes/**" + ], + "validateJSDoc": { + "checkParamNames": true, + "requireParamTypes": true + }, + + "disallowMultipleLineBreaks": true, + "validateLineBreaks": "LF", + "disallowYodaConditions": true +} diff --git a/History.md b/History.md index 7bd24a64..4a9c3231 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,7 @@ +- 0.14.0 2014-10-15 + Set ttl in wrap #14 - nguyenchr + Added JSCS for style checking + - 0.13.0 2014-10-14 Applied work function locking for multi_caching (#13). -aletorrado diff --git a/Makefile b/Makefile index 0e46e24d..4d45a4d6 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,9 @@ test-travis: lint ./node_modules/.bin/istanbul cover test/run.js --report lcovonly \ -- -T unit,functional -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage lint: + ./node_modules/.bin/jscs ./lib && \ + ./node_modules/.bin/jscs ./test && \ + ./node_modules/.bin/jscs ./examples && \ ./node_modules/.bin/jshint ./lib --config $(BASE)/.jshintrc && \ ./node_modules/.bin/jshint ./test --config $(BASE)/.jshintrc ./node_modules/.bin/jshint ./examples --config $(BASE)/.jshintrc diff --git a/examples/example.js b/examples/example.js index 69624fa3..1282656e 100644 --- a/examples/example.js +++ b/examples/example.js @@ -1,7 +1,8 @@ /*jshint unused:false*/ +// Note: ttls are in seconds var cache_manager = require('../'); -var memory_cache = cache_manager.caching({store: 'memory', max: 100, ttl: 10/*seconds*/}); -var memory_cache2 = cache_manager.caching({store: 'memory', max: 100, ttl: 100/*seconds*/}); +var memory_cache = cache_manager.caching({store: 'memory', max: 100, ttl: 10}); +var memory_cache2 = cache_manager.caching({store: 'memory', max: 100, ttl: 100}); var ttl; //Can't use a different ttl per set() call with memory cache // @@ -74,7 +75,6 @@ get_cached_user(user_id, function (err, user) { // { id: 123, name: 'Bob' } // { id: 123, name: 'Bob' } - // Same as above, but written differently: memory_cache.wrap(key, function (cb) { get_user(user_id, cb); diff --git a/examples/redis_example/example.js b/examples/redis_example/example.js index 90be1276..4ef67d78 100644 --- a/examples/redis_example/example.js +++ b/examples/redis_example/example.js @@ -5,7 +5,8 @@ var util = require('util'); var cache_manager = require('../../'); var redis_store = require('./redis_store'); -var redis_cache = cache_manager.caching({store: redis_store, db: 0, ttl: 100/*seconds*/}); +// Note: ttl is in seconds +var redis_cache = cache_manager.caching({store: redis_store, db: 0, ttl: 100}); var ttl = 60; console.log("set/get/del example:"); diff --git a/lib/caching.js b/lib/caching.js index 517246b3..a956e300 100644 --- a/lib/caching.js +++ b/lib/caching.js @@ -35,10 +35,9 @@ var caching = function (args) { * }); */ self.wrap = function (key, work, ttl, cb) { - - if(typeof(ttl) == 'function') { - cb = ttl; - ttl = undefined; + if (typeof ttl === 'function') { + cb = ttl; + ttl = undefined; } self.store.get(key, function (err, result) { @@ -66,8 +65,7 @@ var caching = function (args) { self.queues[key].forEach(function (done) { done.call(null, err); }); - } - else { + } else { self.queues[key].forEach(function (done) { done.apply(null, work_args); }); diff --git a/lib/multi_caching.js b/lib/multi_caching.js index 8d1f1308..2dc52e73 100644 --- a/lib/multi_caching.js +++ b/lib/multi_caching.js @@ -42,10 +42,9 @@ var multi_caching = function (caches) { * cache, it gets set in all higher-priority caches. */ self.wrap = function (key, work, ttl, cb) { - - if(typeof(ttl) == 'function') { - cb = ttl; - ttl = undefined; + if (typeof ttl === 'function') { + cb = ttl; + ttl = undefined; } get_from_highest_priority_cache(key, function (err, result, index) { @@ -54,9 +53,9 @@ var multi_caching = function (caches) { } else if (result) { var caches_to_update = caches.slice(0, index); var opts = { - key: key, - value: result, - ttl: ttl + key: key, + value: result, + ttl: ttl }; set_in_multiple_caches(caches_to_update, opts, function (err) { cb(err, result); @@ -75,9 +74,9 @@ var multi_caching = function (caches) { return; } var opts = { - key: key, - value: work_args[1], - ttl: ttl + key: key, + value: work_args[1], + ttl: ttl }; set_in_multiple_caches(caches, opts, function (err) { if (err) { @@ -99,9 +98,9 @@ var multi_caching = function (caches) { self.set = function (key, value, ttl, cb) { var opts = { - key: key, - value: value, - ttl: ttl + key: key, + value: value, + ttl: ttl }; set_in_multiple_caches(caches, opts, cb); }; diff --git a/package.json b/package.json index b737ae84..87454e73 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cache-manager", - "version": "0.13.0", + "version": "0.14.0", "description": "Cache module for Node.js", "main": "index.js", "scripts": { @@ -26,7 +26,8 @@ "devDependencies": { "coveralls": "^2.3.0", "istanbul": "^0.2.11", - "jshint": "^2.5.1", + "jscs": "^1.7.1", + "jshint": "^2.5.4", "mocha": "^1.20.1", "optimist": "^0.6.1", "sinon": "^1.10.2" diff --git a/test/caching.unit.js b/test/caching.unit.js index 0e7dafa6..33837fdf 100644 --- a/test/caching.unit.js +++ b/test/caching.unit.js @@ -251,7 +251,6 @@ describe("caching", function () { }); context("calls back with the result of the wrapped function", function () { - beforeEach(function () { sinon.spy(memory_store_stub, 'set'); }); @@ -260,7 +259,7 @@ describe("caching", function () { memory_store_stub.set.restore(); }); - it("when a ttl is passed in", function(done) { + it("when a ttl is passed in", function (done) { cache.wrap(key, function (cb) { methods.get_widget(name, cb); }, ttl, function (err, widget) { @@ -269,10 +268,9 @@ describe("caching", function () { sinon.assert.calledWith(memory_store_stub.set, key, {name: name}, ttl); done(); }); - }); - it("when a ttl is not passed in", function(done) { + it("when a ttl is not passed in", function (done) { cache.wrap(key, function (cb) { methods.get_widget(name, cb); }, function (err, widget) { @@ -282,7 +280,6 @@ describe("caching", function () { done(); }); }); - }); context("when result is already cached", function () { diff --git a/test/multi_caching.unit.js b/test/multi_caching.unit.js index 3311ab73..746c9511 100644 --- a/test/multi_caching.unit.js +++ b/test/multi_caching.unit.js @@ -185,7 +185,6 @@ describe("multi_caching", function () { }); context("calls back with the result of a function", function () { - beforeEach(function () { sinon.spy(memory_cache3.store, 'set'); }); @@ -215,7 +214,6 @@ describe("multi_caching", function () { done(); }); }); - }); context("when wrapped function calls back with an error", function () {