Skip to content
This repository has been archived by the owner on Jan 18, 2018. It is now read-only.

Commit

Permalink
jshint —> semistandard (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
corymsmith authored and daffl committed Oct 22, 2016
1 parent f81c0a7 commit d9bb54e
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 85 deletions.
30 changes: 0 additions & 30 deletions .jshintrc

This file was deleted.

1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.babelrc
.editorconfig
.jshintrc
.travis.yml
.idea/
.tmp/
Expand Down
8 changes: 4 additions & 4 deletions example/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if(!global._babelPolyfill) { require('babel-polyfill'); }
if (!global._babelPolyfill) { require('babel-polyfill'); }

import feathers from 'feathers';
import rest from 'feathers-rest';
Expand Down Expand Up @@ -47,7 +47,7 @@ const app = feathers()
// Turn on URL-encoded parser for REST services
.use(bodyParser.urlencoded({ extended: true }));

module.exports = new Promise(function(resolve) {
module.exports = new Promise(function (resolve) {
ORM.loadCollection(Todo);
ORM.initialize(config, (error, data) => {
if (error) {
Expand All @@ -64,13 +64,13 @@ module.exports = new Promise(function(resolve) {
}
}));

app.use(function(error, req, res, next){
app.use(function (error, req, res, next) {
res.json(error);
});

// Start the server
const server = app.listen(3030);
server.on('listening', function() {
server.on('listening', function () {
console.log('Feathers Todo waterline service running on 127.0.0.1:3030');
resolve(server);
});
Expand Down
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,17 @@
"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 test/ --compilers js:babel-core/register",
"test": "rm -rf .tmp && npm run compile && npm run jshint && npm run mocha"
"test": "rm -rf .tmp && npm run compile && npm run lint && npm run mocha"
},
"semistandard": {
"env": [
"mocha"
],
"ignore": [
"/lib"
]
},
"directories": {
"lib": "lib"
Expand All @@ -56,12 +64,13 @@
"babel-preset-es2015": "^6.1.18",
"body-parser": "^1.14.1",
"chai": "^3.4.1",
"eslint-if-supported": "^1.0.1",
"feathers": "^2.0.0",
"feathers-rest": "^1.1.1",
"feathers-service-tests": "^0.6.1",
"jshint": "^2.8.0",
"mocha": "^3.0.0",
"sails-disk": "^0.10.8",
"semistandard": "^9.1.0",
"waterline": "^0.12.1"
}
}
43 changes: 21 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import errors from 'feathers-errors';
import * as utils from './utils';

class Service {
constructor(options) {
constructor (options) {
this.paginate = options.paginate || {};
this.Model = options.Model;
this.id = options.id || 'id';
}

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

_find(params, count, getFilter = filter) {
_find (params, count, getFilter = filter) {
let { filters, query } = getFilter(params.query || {});
let where = utils.getWhere(query);
let order = utils.getOrder(filters.$sort);
Expand All @@ -31,7 +31,7 @@ class Service {
q.skip(filters.$skip);
}

if(filters.$limit) {
if (filters.$limit) {
q.limit(filters.$limit);
}

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

if(count) {
if (count) {
return counter.then(performQuery);
}

return performQuery();
}

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) {
return this.Model.findOne({ id }).then(instance => {
if (!instance) {
throw new errors.NotFound(`No record found for id '${id}'`);
Expand All @@ -77,23 +77,23 @@ class Service {
}).catch(utils.errorHandler);
}

get(... args) {
return this._get(... args);
get (...args) {
return this._get(...args);
}

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

return this._get(id);
}

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

_patch(id, data, params) {
_patch (id, data, params) {
const where = Object.assign({}, params.query);

if (id !== null) {
Expand All @@ -105,11 +105,11 @@ class Service {
.catch(utils.errorHandler);
}

patch(... args) {
return this._patch(... args);
patch (...args) {
return this._patch(...args);
}

update(id, data) {
update (id, data) {
if (Array.isArray(data)) {
return Promise.reject('Not replacing multiple records. Did you mean `patch`?');
}
Expand All @@ -121,7 +121,6 @@ class Service {

let copy = {};
Object.keys(instance.toJSON()).forEach(key => {

// NOTE (EK): Make sure that we don't waterline created fields to null
// just because a user didn't pass them in.
if ((key === 'createdAt' || key === 'updatedAt') && typeof data[key] === 'undefined') {
Expand All @@ -140,7 +139,7 @@ class Service {
.catch(utils.errorHandler);
}

remove(id, params) {
remove (id, params) {
return this._findOrGet(id, params).then(data => {
const where = Object.assign({}, params.query);

Expand All @@ -154,7 +153,7 @@ class Service {
}
}

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

Expand Down
24 changes: 12 additions & 12 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import errors from 'feathers-errors';
import { adapter as Errors } from 'waterline-errors';

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

if (error.constructor.name && (error.constructor.name === 'WLValidationError' || error.constructor.name === 'WLUsageError' )) {
if (error.constructor.name && (error.constructor.name === 'WLValidationError' || error.constructor.name === 'WLUsageError')) {
let e = error.toJSON();
let data = Object.assign({ errors: error.errors}, e);
let data = Object.assign({ errors: error.errors }, e);

feathersError = new errors.BadRequest(e.summary, data);
} else if (error.message) {
switch(error.message) {
switch (error.message) {
case Errors.PrimaryKeyUpdate.toString():
case Errors.PrimaryKeyMissing.toString():
case Errors.PrimaryKeyCollision.toString():
Expand Down Expand Up @@ -40,7 +40,7 @@ export function errorHandler(error) {
throw feathersError;
}

export function getOrder(sort={}) {
export function getOrder (sort = {}) {
let order = {};

Object.keys(sort).forEach(name => {
Expand All @@ -61,12 +61,12 @@ const queryMappings = {

const specials = ['$sort', '$limit', '$skip', '$select'];

function getValue(value, prop) {
if(typeof value === 'object' && specials.indexOf(prop) === -1) {
function getValue (value, prop) {
if (typeof value === 'object' && specials.indexOf(prop) === -1) {
let query = {};

Object.keys(value).forEach(key => {
if(queryMappings[key]) {
if (queryMappings[key]) {
query[queryMappings[key]] = value[key];
} else {
query[key] = value[key];
Expand All @@ -79,19 +79,19 @@ function getValue(value, prop) {
return value;
}

export function getWhere(query) {
export function getWhere (query) {
let where = {};

if(typeof query !== 'object') {
if (typeof query !== 'object') {
return {};
}

Object.keys(query).forEach(prop => {
const value = query[prop];

if(prop === '$or') {
if (prop === '$or') {
where.or = value;
} else if(value.$in) {
} else if (value.$in) {
where[prop] = value.$in;
} else {
where[prop] = getValue(value, prop);
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ORM.initialize(config, (error, ontology) => {
return user.create({
name: 'Doug',
age: 32
}).then(user => _ids.Doug = user.id);
}).then(user => (_ids.Doug = user.id));
});

afterEach(() => user.destroy());
Expand Down
22 changes: 10 additions & 12 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import errors from 'feathers-errors';
import * as utils from '../src/utils';
import { adapter as Errors } from 'waterline-errors';

const MockValidationError = function(msg) {
const MockValidationError = function (msg) {
return {
message: msg,
constructor: {
Expand All @@ -12,7 +12,7 @@ const MockValidationError = function(msg) {
errors: {
text: ['A record with that `text` already exists (`do dishes`).']
},
toJSON: function(){
toJSON: function () {
return {
'error': 'E_VALIDATION',
'status': 400,
Expand All @@ -31,7 +31,7 @@ const MockValidationError = function(msg) {
};
};

const MockUsageError = function(msg) {
const MockUsageError = function (msg) {
return {
message: msg,
constructor: {
Expand Down Expand Up @@ -61,8 +61,7 @@ describe('Feathers Waterline Utils', () => {
let e = MockValidationError('Invalid Text');
try {
utils.errorHandler(e);
}
catch(error) {
} catch (error) {
expect(error.message).to.equal('Invalid Email');
}
});
Expand All @@ -75,8 +74,7 @@ describe('Feathers Waterline Utils', () => {
let e = MockValidationError();
try {
utils.errorHandler(e);
}
catch(error) {
} catch (error) {
expect(error.errors).to.deep.equal(textError);
}
});
Expand Down Expand Up @@ -165,7 +163,7 @@ describe('Feathers Waterline Utils', () => {
expect(order).to.deep.equal({ name: 'asc', age: 'desc' });
});
});

describe('getWhere', () => {
it('returns empty object when nothing is passed in', () => {
let where = utils.getWhere();
Expand All @@ -174,15 +172,15 @@ describe('Feathers Waterline Utils', () => {
});

it('returns where conditions properly converted', () => {
let where = utils.getWhere({ name: 'Joe', age: { $lte: 25 }});
let where = utils.getWhere({ name: 'Joe', age: { $lte: 25 } });

expect(where).to.deep.equal({ name: 'Joe', age: { '<=': 25 }});
expect(where).to.deep.equal({ name: 'Joe', age: { '<=': 25 } });
});

it('converts $nin to $notIn', () => {
let where = utils.getWhere({ name: { $nin: ['Joe', 'Alice'] }});
let where = utils.getWhere({ name: { $nin: ['Joe', 'Alice'] } });

expect(where).to.deep.equal({ name: { '!': ['Joe', 'Alice'] }});
expect(where).to.deep.equal({ name: { '!': ['Joe', 'Alice'] } });
});
});
});

0 comments on commit d9bb54e

Please sign in to comment.