Skip to content

Commit

Permalink
Merge pull request #131 from feathersjs/semistandard
Browse files Browse the repository at this point in the history
jshint —> semistandard
  • Loading branch information
corymsmith authored Oct 22, 2016
2 parents f3955c5 + d7e6efb commit 131c52a
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 99 deletions.
32 changes: 0 additions & 32 deletions .jshintrc

This file was deleted.

1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.babelrc
.istanbul.yml
.travis.yml
.jshintrc
.editorconfig
.idea/
src/
Expand Down
3 changes: 1 addition & 2 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ mongoose.Promise = global.Promise;
// Connect to your MongoDB instance(s)
mongoose.connect('mongodb://localhost:27017/feathers');


// Create a feathers instance.
const app = feathers()
// Enable Socket.io
Expand Down Expand Up @@ -47,4 +46,4 @@ if (!module.parent) {
console.log('Feathers Message mongoose service running on 127.0.0.1:3030');
}

module.exports = app;
module.exports = app;
2 changes: 1 addition & 1 deletion example/models/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ MessageSchema.index({'updatedAt': -1, background: true});

var MessageModel = mongoose.model('Message', MessageSchema);

module.exports = MessageModel;
module.exports = MessageModel;
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,18 @@
"compile": "rimraf lib/ && babel -d lib/ src/",
"start": "node example/app",
"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": "npm run compile && npm run jshint && npm run coverage"
"test": "npm run compile && npm run lint && npm run coverage"
},
"semistandard": {
"env": [
"mocha"
],
"ignore": [
"/lib"
]
},
"directories": {
"lib": "lib"
Expand All @@ -67,16 +75,17 @@
"babel-preset-es2015": "^6.1.2",
"body-parser": "^1.14.1",
"chai": "^3.4.1",
"eslint-if-supported": "^1.0.1",
"feathers": "^2.0.0",
"feathers-hooks": "^1.1.0",
"feathers-rest": "^1.2.2",
"feathers-service-tests": "^0.6.2",
"feathers-socketio": "^1.3.3",
"istanbul": "^1.1.0-alpha.1",
"jshint": "^2.8.0",
"mocha": "^3.0.0",
"mongoose": "^4.5.2",
"rimraf": "^2.5.2",
"semistandard": "^9.1.0",
"sinon": "^1.17.3",
"sinon-chai": "^2.8.0"
}
Expand Down
7 changes: 3 additions & 4 deletions src/error-handler.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import errors from 'feathers-errors';

export default function errorHandler(error) {
export default function errorHandler (error) {
let feathersError = error;

if (error.name) {
switch(error.name) {
switch (error.name) {
case 'ValidationError':
case 'ValidatorError':
case 'CastError':
Expand All @@ -21,8 +21,7 @@ export default function errorHandler(error) {
case 'MongoError':
if (error.code === 11000) {
feathersError = new errors.Conflict(error);
}
else {
} else {
feathersError = new errors.GeneralError(error);
}
break;
Expand Down
8 changes: 3 additions & 5 deletions src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
* @return {Object} The hook object with the result as a plain js object.
*/

export let toObject = function(options = {}, dataField = 'data') {
return function(hook) {
export let toObject = function (options = {}, dataField = 'data') {
return function (hook) {
// Only perform this if it's used as an after hook.
if (hook.result) {
let data = hook.result[dataField] || hook.result;
Expand All @@ -41,9 +41,7 @@ export let toObject = function(options = {}, dataField = 'data') {

return obj;
});
}
// Handle single mongoose models
else if (typeof data.toObject === 'function') {
} else if (typeof data.toObject === 'function') { // Handle single mongoose models
res = data.toObject(options);
}
// If our data is transformed set it to appropriate location on the hook
Expand Down
44 changes: 21 additions & 23 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import errorHandler from './error-handler';

// Create the service.
class Service {
constructor(options) {
constructor (options) {
if (!options) {
throw new Error('Mongoose options have to be provided');
}
Expand All @@ -20,14 +20,14 @@ class Service {
this.id = options.id || '_id';
this.paginate = options.paginate || {};
this.lean = options.lean || false;
this.overwrite = (options.overwrite === false) ? false : true;
this.overwrite = options.overwrite !== false;
}

extend(obj) {
extend (obj) {
return Proto.extend(obj, this);
}

_find(params, count, getFilter = filter) {
_find (params, count, getFilter = filter) {
const { filters, query } = getFilter(params.query || {});
const q = this.Model.find(query).lean(this.lean);

Expand All @@ -41,7 +41,7 @@ class Service {

q.select(fields);
} else {
if(filters.$select && typeof filters.$select === 'object') {
if (filters.$select && typeof filters.$select === 'object') {
q.select(filters.$select);
}
}
Expand All @@ -62,7 +62,7 @@ class Service {
}

// Handle $populate
if (filters.$populate){
if (filters.$populate) {
q.populate(filters.$populate);
}

Expand All @@ -77,28 +77,27 @@ class Service {
});
};

if(count) {
if (count) {
return this.Model.where(query).count().exec().then(executeQuery);
}

return executeQuery();
}

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, params = {}) {
_get (id, params = {}) {
params.query = params.query || {};

let modelQuery = this
Expand All @@ -113,7 +112,7 @@ class Service {
.lean(this.lean)
.exec()
.then(data => {
if(!data) {
if (!data) {
throw new errors.NotFound(`No record found for id '${id}'`);
}

Expand All @@ -122,23 +121,23 @@ class Service {
.catch(errorHandler);
}

get(id, params) {
get (id, params) {
return this._get(id, params);
}

_getOrFind(id, params) {
if(id === null) {
_getOrFind (id, params) {
if (id === null) {
return this._find(params).then(page => page.data);
}

return this._get(id, params);
}

create(data) {
create (data) {
return this.Model.create(data).catch(errorHandler);
}

update(id, data, params) {
update (id, data, params) {
if (id === null) {
return Promise.reject('Not replacing multiple records. Did you mean `patch`?');
}
Expand Down Expand Up @@ -177,7 +176,7 @@ class Service {
.catch(errorHandler);
}

patch(id, data, params) {
patch (id, data, params) {
params.query = params.query || {};

// Handle case where data might be a mongoose model
Expand Down Expand Up @@ -221,13 +220,12 @@ class Service {
.exec()
.then(() => this._getOrFind(id, params))
.catch(errorHandler);
}
catch(e) {
} catch (e) {
return errorHandler(e);
}
}

remove(id, params) {
remove (id, params) {
const query = Object.assign({}, params.query);

if (id !== null) {
Expand All @@ -248,7 +246,7 @@ class Service {
}
}

export default function init(options) {
export default function init (options) {
return new Service(options);
}

Expand Down
2 changes: 1 addition & 1 deletion test/error-handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('Feathers Mongoose Error Handler', () => {
});

it('wraps a DivergentArrayError as a GeneralError', done => {
let fn = function(){};
let fn = function () {};
let e = new mongoose.Error.DivergentArrayError({join: fn});
errorHandler(e).catch(error => {
expect(error).to.be.an.instanceof(errors.GeneralError);
Expand Down
24 changes: 12 additions & 12 deletions test/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'feathers';
import chai from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import { hooks } from '../src';
import {hooks} from '../src';

const expect = chai.expect;
chai.use(sinonChai);
Expand All @@ -15,7 +15,7 @@ describe('Feathers Mongoose Hooks', () => {
beforeEach(() => {
toObject = sinon.spy();
hook = {
result: { toObject }
result: {toObject}
};
});

Expand All @@ -25,7 +25,7 @@ describe('Feathers Mongoose Hooks', () => {
});

it('supports custom options', () => {
let options = { feathers: 'awesome' };
let options = {feathers: 'awesome'};
hooks.toObject(options)(hook);
expect(toObject).to.be.calledWith(options);
});
Expand All @@ -38,28 +38,28 @@ describe('Feathers Mongoose Hooks', () => {
user1 = {
name: 'Jerry',
age: 23,
toObject: sinon.stub().returns({ name: 'Jerry', age: 23})
toObject: sinon.stub().returns({name: 'Jerry', age: 23})
};

user2 = {
name: 'Mary',
age: 32,
toObject: sinon.stub().returns({ name: 'Mary', age: 32})
toObject: sinon.stub().returns({name: 'Mary', age: 32})
};

users = [user1, user2];
});

it('converts paginated arrays of mongoose models', () => {
let hook = {
result: { data: users }
result: {data: users}
};

hooks.toObject()(hook);
expect(users[0].toObject).to.be.calledOnce;
expect(users[1].toObject).to.be.calledOnce;
expect(hook.result.data[0]).to.deep.equal({ name: 'Jerry', age: 23});
expect(hook.result.data[1]).to.deep.equal({ name: 'Mary', age: 32});
expect(hook.result.data[0]).to.deep.equal({name: 'Jerry', age: 23});
expect(hook.result.data[1]).to.deep.equal({name: 'Mary', age: 32});
});

it('converts a single mongoose model', () => {
Expand All @@ -69,7 +69,7 @@ describe('Feathers Mongoose Hooks', () => {

hooks.toObject()(hook);
expect(users[0].toObject).to.be.calledOnce;
expect(hook.result).to.deep.equal({ name: 'Jerry', age: 23});
expect(hook.result).to.deep.equal({name: 'Jerry', age: 23});
});

it('converts non-paginated arrays of mongoose models', () => {
Expand All @@ -80,8 +80,8 @@ describe('Feathers Mongoose Hooks', () => {
hooks.toObject()(hook);
expect(users[0].toObject).to.be.calledOnce;
expect(users[1].toObject).to.be.calledOnce;
expect(hook.result[0]).to.deep.equal({ name: 'Jerry', age: 23});
expect(hook.result[1]).to.deep.equal({ name: 'Mary', age: 32});
expect(hook.result[0]).to.deep.equal({name: 'Jerry', age: 23});
expect(hook.result[1]).to.deep.equal({name: 'Mary', age: 32});
});
});

Expand All @@ -104,7 +104,7 @@ describe('Feathers Mongoose Hooks', () => {

it('does not convert paginated arrays of objects', () => {
let hook = {
result: { data: users }
result: {data: users}
};

hooks.toObject()(hook);
Expand Down
Loading

0 comments on commit 131c52a

Please sign in to comment.