From 5b987f5797456b29503f90f918f46afba9822bb2 Mon Sep 17 00:00:00 2001 From: Marshall Thompson Date: Mon, 17 Oct 2016 15:38:58 -0600 Subject: [PATCH] =?UTF-8?q?jshint=20=E2=80=94>=20semistandard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .jshintrc | 32 ------------------------ .npmignore | 1 - example/app.js | 11 ++++----- package.json | 17 ++++++++++--- src/error-handler.js | 2 +- src/index.js | 59 ++++++++++++++++++++++---------------------- test/index.test.js | 16 ++++++------ test/test-app.js | 7 ++++-- 8 files changed, 62 insertions(+), 83 deletions(-) delete mode 100644 .jshintrc diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 3a88603..0000000 --- a/.jshintrc +++ /dev/null @@ -1,32 +0,0 @@ -{ - "node": true, - "esnext": true, - "bitwise": true, - "camelcase": true, - "curly": true, - "eqeqeq": true, - "immed": true, - "indent": 2, - "latedef": "nofunc", - "newcap": true, - "noarg": true, - "quotmark": "single", - "regexp": true, - "undef": true, - "unused": true, - "strict": false, - "trailing": true, - "smarttabs": true, - "white": false, - "loopfunc": true, - "expr": true, - "globals": { - "it": true, - "describe": true, - "before": true, - "beforeEach": true, - "after": true, - "afterEach": true, - "exports": true - } -} \ No newline at end of file diff --git a/.npmignore b/.npmignore index e5de67c..627f1cc 100644 --- a/.npmignore +++ b/.npmignore @@ -1,7 +1,6 @@ .babelrc .istanbul.yml .travis.yml -.jshintrc .editorconfig .idea/ src/ diff --git a/example/app.js b/example/app.js index de634d4..b01786c 100644 --- a/example/app.js +++ b/example/app.js @@ -17,10 +17,9 @@ const app = feathers() // Turn on URL-encoded parser for REST services .use(bodyParser.urlencoded({extended: true})); - -const promise = new Promise(function(resolve) { +const promise = new Promise(function (resolve) { // Connect to your MongoDB instance(s) - MongoClient.connect('mongodb://localhost:27017/feathers').then(function(db){ + MongoClient.connect('mongodb://localhost:27017/feathers').then(function (db) { // Connect to the db, create and register a Feathers service. app.use('/messages', service({ Model: db.collection('messages'), @@ -35,13 +34,13 @@ const promise = new Promise(function(resolve) { // Start the server var server = app.listen(3030); - server.on('listening', function() { + server.on('listening', function () { console.log('Feathers Message MongoDB service running on 127.0.0.1:3030'); resolve(server); }); - }).catch(function(error){ + }).catch(function (error) { console.error(error); }); }); -module.exports = promise; \ No newline at end of file +module.exports = promise; diff --git a/package.json b/package.json index 543e9a0..b585aae 100644 --- a/package.json +++ b/package.json @@ -42,12 +42,20 @@ "release:major": "npm version major && npm publish", "compile": "rm -rf lib/ && babel -d lib/ src/", "watch": "babel --watch -d lib/ src/", - "jshint": "jshint src/. test/. --config", + "lint": "eslint-if-supported semistandard --fix", "mocha": "mocha --opts mocha.opts", "coverage": "istanbul cover _mocha -- --opts mocha.opts", - "test": "rm -rf .tmp && npm run compile && npm run jshint && npm run coverage", + "test": "rm -rf .tmp && npm run compile && npm run lint && npm run coverage", "start": "node example/app" }, + "semistandard": { + "env": [ + "mocha" + ], + "ignore": [ + "/lib" + ] + }, "engines": { "node": "~0.10.0", "npm": "~1.4.0" @@ -67,14 +75,15 @@ "babel-preset-es2015": "^6.3.13", "body-parser": "^1.14.1", "chai": "^3.4.1", + "eslint-if-supported": "^1.0.1", "feathers": "^2.0.0-pre.4", "feathers-hooks": "^1.1.0", "feathers-rest": "^1.2.2", "feathers-service-tests": "^0.8.0", "feathers-socketio": "^1.3.3", "istanbul": "^1.1.0-alpha.1", - "jshint": "^2.8.0", "mocha": "^3.0.0", - "mongodb": "^2.1.3" + "mongodb": "^2.1.3", + "semistandard": "^9.1.0" } } diff --git a/src/error-handler.js b/src/error-handler.js index a352280..1ae800c 100644 --- a/src/error-handler.js +++ b/src/error-handler.js @@ -1,6 +1,6 @@ import errors from 'feathers-errors'; -export default function errorHandler(error) { +export default function errorHandler (error) { let feathersError = error; // NOTE (EK): The list of error code is way too massive to map diff --git a/src/index.js b/src/index.js index 37428c6..f83331e 100644 --- a/src/index.js +++ b/src/index.js @@ -7,7 +7,7 @@ import errorHandler from './error-handler'; // Create the service. class Service { - constructor(options) { + constructor (options) { if (!options) { throw new Error('MongoDB options have to be provided'); } @@ -22,11 +22,11 @@ class Service { this.paginate = options.paginate || {}; } - extend(obj) { + extend (obj) { return Proto.extend(obj, this); } - _objectifyId(id) { + _objectifyId (id) { if (this.id === '_id' && ObjectID.isValid(id)) { id = new ObjectID(id.toString()); } @@ -34,7 +34,7 @@ class Service { return id; } - _multiOptions(id, params) { + _multiOptions (id, params) { let query = Object.assign({}, params.query); let options = Object.assign({ multi: true }, params.mongodb || params.options); @@ -46,34 +46,36 @@ class Service { return { query, options }; } - _getSelect(select) { + _getSelect (select) { if (Array.isArray(select)) { var result = {}; - select.forEach(name => result[name] = 1); + select.forEach(name => { + result[name] = 1; + }); return result; } return select; } - _find(params, count, getFilter = filter) { + _find (params, count, getFilter = filter) { // Start with finding all, and limit when necessary. - let { filters, query } = getFilter(params.query|| {}); + let { filters, query } = getFilter(params.query || {}); let q = this.Model.find(query); if (filters.$select) { q = this.Model.find(query, this._getSelect(filters.$select)); } - if (filters.$sort){ + if (filters.$sort) { q.sort(filters.$sort); } - if (filters.$limit){ + if (filters.$limit) { q.limit(filters.$limit); } - if (filters.$skip){ + if (filters.$skip) { q.skip(filters.$skip); } @@ -95,21 +97,21 @@ class Service { return runQuery(); } - find(params) { - const paginate = (params && typeof params.paginate !== 'undefined') ? - params.paginate : this.paginate; + find (params) { + const paginate = (params && typeof params.paginate !== 'undefined') + ? params.paginate : this.paginate; const result = this._find(params, !!paginate.default, query => filter(query, paginate) ); - if(!paginate.default) { + if (!paginate.default) { return result.then(page => page.data); } return result; } - _get(id) { + _get (id) { id = this._objectifyId(id); return this.Model.findOne({ [this.id]: id }) @@ -123,38 +125,37 @@ class Service { .catch(errorHandler); } - get(id, params) { + get (id, params) { return this._get(id, params); } - _findOrGet(id, params) { - if(id === null) { + _findOrGet (id, params) { + if (id === null) { return this._find(params).then(page => page.data); } return this._get(id, params); } - create(data) { + create (data) { const setId = item => { const entry = Object.assign({}, item); // Generate a MongoId if we use a custom id - if(this.id !== '_id' && typeof entry[this.id] === 'undefined') { + if (this.id !== '_id' && typeof entry[this.id] === 'undefined') { entry[this.id] = new ObjectID().toHexString(); } return entry; }; - return this.Model .insert(Array.isArray(data) ? data.map(setId) : setId(data)) .then(result => result.ops.length > 1 ? result.ops : result.ops[0]) .catch(errorHandler); } - _normalizeId(id, data) { + _normalizeId (id, data) { if (this.id === '_id') { // Default Mongo IDs cannot be updated. The Mongo library handles // this automatically. @@ -166,7 +167,7 @@ class Service { } } - patch(id, data, params) { + patch (id, data, params) { const { query, options } = this._multiOptions(id, params); const patchParams = Object.assign({}, params, { query: Object.assign({}, query) @@ -174,7 +175,7 @@ class Service { // Account for potentially modified data Object.keys(query).forEach(key => { - if(query[key] !== undefined && data[key] !== undefined && + if (query[key] !== undefined && data[key] !== undefined && typeof data[key] !== 'object') { patchParams.query[key] = data[key]; } else { @@ -188,8 +189,8 @@ class Service { .then(() => this._findOrGet(id, patchParams)); } - update(id, data, params) { - if(Array.isArray(data) || id === null) { + update (id, data, params) { + if (Array.isArray(data) || id === null) { return Promise.reject('Not replacing multiple records. Did you mean `patch`?'); } @@ -201,7 +202,7 @@ class Service { .catch(errorHandler); } - remove(id, params) { + remove (id, params) { let { query, options } = this._multiOptions(id, params); return this._findOrGet(id, params) @@ -212,7 +213,7 @@ class Service { } } -export default function init(options) { +export default function init (options) { return new Service(options); } diff --git a/test/index.test.js b/test/index.test.js index 1b088fc..d4a57f6 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -13,17 +13,17 @@ describe('Feathers MongoDB Service', () => { before(() => MongoClient.connect('mongodb://localhost:27017/feathers-test') - .then(function(database) { + .then(function (database) { db = database; app.use('/people', service({ - Model: db.collection('people'), - events: [ 'testing' ] - })).use('/people-customid', service({ - Model: db.collection('people-customid'), - id: 'customid', - events: [ 'testing' ] - })); + Model: db.collection('people'), + events: [ 'testing' ] + })).use('/people-customid', service({ + Model: db.collection('people-customid'), + id: 'customid', + events: [ 'testing' ] + })); db.collection('people-customid').removeMany(); db.collection('people').removeMany(); diff --git a/test/test-app.js b/test/test-app.js index d4ee722..2eb6b08 100644 --- a/test/test-app.js +++ b/test/test-app.js @@ -17,9 +17,12 @@ const app = feathers() // Turn on URL-encoded parser for REST services .use(bodyParser.urlencoded({extended: true})); -export default new Promise(function(resolve) { +export default new Promise(function (resolve, reject) { // Connect to your MongoDB instance(s) MongoClient.connect('mongodb://localhost:27017/feathers-test', (error, db) => { + if (error) { + reject(error); + } // Connect to the db, create and register a Feathers service. app.use('/todos', service({ Model: db.collection('todos'), @@ -34,7 +37,7 @@ export default new Promise(function(resolve) { // Start the server var server = app.listen(3030); - server.on('listening', function() { + server.on('listening', function () { console.log('Feathers Message MongoDB service running on 127.0.0.1:3030'); resolve(server); });