Skip to content

Commit

Permalink
Merge pull request #48 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 6522877 + 3fb16d8 commit 48df5f8
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 99 deletions.
32 changes: 0 additions & 32 deletions .jshintrc

This file was deleted.

3 changes: 1 addition & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
.babelrc
.codeclimate.yml
.jshintrc
.travis.yml
.idea/
src/
test/
!lib/
!lib/
6 changes: 3 additions & 3 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ app.use('/messages', service({
// Create a dummy Message
app.service('messages').create({
text: 'Oh hai!'
}).then(function(message) {
}).then(function (message) {
console.log('Created message', message);
});

Expand All @@ -43,6 +43,6 @@ app.use(errors.handler());
// Start the server.
const port = 3030;

app.listen(port, function() {
app.listen(port, function () {
console.log(`Feathers server listening on port ${port}`);
});
});
17 changes: 13 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,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 db-data && npm run compile && npm run jshint && npm run coverage",
"test": "rm -rf db-data && 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"
Expand All @@ -59,13 +67,14 @@
"babel-preset-es2015": "^6.3.13",
"body-parser": "^1.13.2",
"chai": "^3.0.0",
"eslint-if-supported": "^1.0.1",
"feathers": "^2.0.0-pre.4",
"feathers-rest": "^1.3.0",
"feathers-service-tests": "^0.8.0",
"feathers-socketio": "^1.3.3",
"istanbul": "^1.1.0-alpha.1",
"jshint": "^2.9.4",
"mocha": "^3.0.1",
"nedb": "^1.5.1"
"nedb": "^1.5.1",
"semistandard": "^9.1.0"
}
}
71 changes: 35 additions & 36 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { nfcall, getSelect, multiOptions } from './utils';

// Create the service.
class Service {
constructor(options) {
constructor (options) {
if (!options) {
throw new Error('NeDB options have to be provided');
}
Expand All @@ -22,11 +22,11 @@ class Service {
this.paginate = options.paginate || {};
}

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

_find(params, count, getFilter = filter) {
_find (params, count, getFilter = filter) {
// Start with finding all, and limit when necessary.
let { filters, query } = getFilter(params.query || {});

Expand All @@ -38,17 +38,17 @@ class Service {
}

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

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

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

Expand All @@ -62,52 +62,51 @@ class Service {
};
});
};
if(count) {

if (count) {
return nfcall(this.Model, 'count', query).then(runQuery);
}

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) {
return nfcall(this.Model, 'findOne', { [this.id]: id }).then(doc => {
if(!doc) {
if (!doc) {
throw new errors.NotFound(`No record found for id '${id}'`);
}

return doc;
});
}
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(raw) {
const addId = item => {
if(this.id !== '_id' && item[this.id] === undefined) {
create (raw) {
const addId = item => {
if (this.id !== '_id' && item[this.id] === undefined) {
return Object.assign({
[this.id]: crypto.randomBytes(8).toString('hex')
}, item);
Expand All @@ -116,17 +115,17 @@ class Service {
return item;
};
const data = Array.isArray(raw) ? raw.map(addId) : addId(raw);

return nfcall(this.Model, 'insert', data);
}

patch(id, data, params) {
patch (id, data, params) {
const { query, options } = multiOptions(id, this.id, params);
const patchQuery = {};

// 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') {
patchQuery[key] = data[key];
} else {
Expand All @@ -144,23 +143,23 @@ class Service {
}, options).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`?');
}

const { query, options } = multiOptions(id, this.id, params);
const entry = omit(data, '_id');

if(this.id !== '_id') {
if (this.id !== '_id') {
entry[this.id] = id;
}

return nfcall(this.Model, 'update', query, entry, options)
.then(() => this._findOrGet(id));
}

remove(id, params) {
remove (id, params) {
let { query, options } = multiOptions(id, this.id, params);

return this._findOrGet(id, params).then(items =>
Expand All @@ -170,7 +169,7 @@ class Service {
}
}

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

Expand Down
16 changes: 8 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
export function multiOptions(id, idField, params) {
export function multiOptions (id, idField, params) {
let query = Object.assign({}, params.query);
let options = Object.assign({ multi: true }, params.nedb || params.options);

if(id !== null) {
if (id !== null) {
options.multi = false;
query[idField] = id;
}

return { query, options };
}

export function getSelect(select) {
if(Array.isArray(select)) {
export function getSelect (select) {
if (Array.isArray(select)) {
var result = {};
select.forEach(name => result[name] = 1);
select.forEach(name => (result[name] = 1));
return result;
}

return select;
}

export function nfcall(ctx, method) {
export function nfcall (ctx, method) {
let args = Array.prototype.slice.call(arguments, 2);

return new Promise((resolve, reject) => {
args.push(function(error, data) {
if(error) {
args.push(function (error, data) {
if (error) {
return reject(error);
}

Expand Down
16 changes: 7 additions & 9 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/*jshint expr: true*/

import { expect } from 'chai';
import path from 'path';
import assert from 'assert';
Expand All @@ -11,26 +9,26 @@ import errors from 'feathers-errors';
import server from './test-app';
import service from '../src';

function createService(name, options) {
function createService (name, options) {
// NeDB ids do not seem to be generated sequentially but sorted lexigraphically
// if no other sort order is given. This means that items can not be returned in the
// same order they have been created so this counter is used for sorting instead.
let counter = 0;

const filename = path.join('db-data', name);
const db = new NeDB({ filename, autoload: true });

return service(Object.assign({ Model: db }, options)).extend({
_find(params) {
_find (params) {
params.query = params.query || {};
if(!params.query.$sort) {
if (!params.query.$sort) {
params.query.$sort = { counter: 1 };
}

return this._super.apply(this, arguments);
},

create(raw, params) {
create (raw, params) {
const convert = item => Object.assign({}, item, { counter: ++counter });
const items = Array.isArray(raw) ? raw.map(convert) : convert(raw);

Expand All @@ -39,7 +37,7 @@ function createService(name, options) {
});
}

describe('NeDB Service', function() {
describe('NeDB Service', function () {
const app = feathers()
.use('/people', createService('people', {
events: [ 'testing' ]
Expand All @@ -61,7 +59,7 @@ describe('NeDB Service', function() {
});

describe('Common functionality', () => {
it('is CommonJS compatible', () =>
it('is CommonJS compatible', () =>
assert.ok(typeof require('../lib') === 'function')
);

Expand Down
Loading

0 comments on commit 48df5f8

Please sign in to comment.