Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
jshint —> semistandard (#37)
Browse files Browse the repository at this point in the history
* jshint —> semistandard

* Remove jshint directive
  • Loading branch information
marshallswain committed Oct 17, 2016
1 parent ab54b07 commit c2ee934
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 44 deletions.
14 changes: 10 additions & 4 deletions package.json
Expand Up @@ -34,12 +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 --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",
"start": "node example/app.js"
},
"semistandard": {
"env": [
"mocha"
]
},
"directories": {
"lib": "lib"
},
Expand All @@ -55,12 +60,13 @@
"babel-plugin-add-module-exports": "^0.2.0",
"babel-preset-es2015": "^6.1.2",
"body-parser": "^1.14.1",
"eslint-if-supported": "^1.0.1",
"feathers": "^2.0.0-pre.4",
"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"
"mocha": "^3.0.0",
"semistandard": "^9.1.0"
}
}
76 changes: 38 additions & 38 deletions src/index.js
Expand Up @@ -4,66 +4,66 @@ import errors from 'feathers-errors';
import { sorter, matcher } from 'feathers-commons/lib/utils';

const _ = {
values(obj) {
values (obj) {
return Object.keys(obj).map(key => obj[key]);
},
isEmpty(obj) {
isEmpty (obj) {
return Object.keys(obj).length === 0;
},
extend(... args) {
return Object.assign(... args);
extend (...args) {
return Object.assign(...args);
},
omit(obj, ...keys) {
omit (obj, ...keys) {
const result = Object.assign({}, obj);
for(let key of keys) {
for (let key of keys) {
delete result[key];
}
return result;
},
pick(source, ...keys) {
pick (source, ...keys) {
const result = {};
for(let key of keys) {
for (let key of keys) {
result[key] = source[key];
}
return result;
}
};

class Service {
constructor(options = {}) {
constructor (options = {}) {
this.paginate = options.paginate || {};
this._id = this.id = options.idField || options.id || 'id';
this._uId = options.startId || 0;
this.store = options.store || {};
this.events = options.events || [];
}

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

// Find without hooks and mixins that can be used internally and always returns
// a pagination object
_find(params, getFilter = filter) {
_find (params, getFilter = filter) {
const { query, filters } = getFilter(params.query || {});

let values = _.values(this.store).filter(matcher(query));

const total = values.length;

if(filters.$sort) {
if (filters.$sort) {
values.sort(sorter(filters.$sort));
}
}

if(filters.$skip){
if (filters.$skip) {
values = values.slice(filters.$skip);
}
}

if(filters.$limit) {
if (filters.$limit) {
values = values.slice(0, filters.$limit);
}
}

if(filters.$select) {
if (filters.$select) {
values = values.map(value => _.pick(value, filters.$select));
}

Expand All @@ -75,20 +75,20 @@ class Service {
});
}

find(params) {
const paginate = typeof params.paginate !== 'undefined' ?
params.paginate : this.paginate;
find (params) {
const paginate = typeof params.paginate !== 'undefined'
? params.paginate : this.paginate;
// Call the internal find with query parameter that include pagination
const result = this._find(params, query => filter(query, paginate));

if(!(paginate && paginate.default)) {
if (!(paginate && paginate.default)) {
return result.then(page => page.data);
}

return result;
}

get(id) {
get (id) {
if (id in this.store) {
return Promise.resolve(this.store[id]);
}
Expand All @@ -97,31 +97,31 @@ class Service {
}

// Create without hooks and mixins that can be used internally
_create(data) {
_create (data) {
let id = data[this._id] || this._uId++;
let current = _.extend({}, data, { [this._id]: id });

if (this.store[id]){
if (this.store[id]) {
return Promise.reject(new errors.Conflict(`A record with id: ${id} already exists`));
}

return Promise.resolve((this.store[id] = current));
}

create(data) {
if(Array.isArray(data)) {
create (data) {
if (Array.isArray(data)) {
return Promise.all(data.map(current => this._create(current)));
}

return this._create(data);
}

// Update without hooks and mixins that can be used internally
_update(id, data) {
_update (id, data) {
if (id in this.store) {
// We don't want our id to change type if it can be coerced
const oldId = this.store[id][this._id];
id = oldId == id ? oldId : id; // jshint ignore:line
id = oldId == id ? oldId : id; // eslint-disable-line

data = _.extend({}, data, { [this._id]: id });
this.store[id] = data;
Expand All @@ -132,8 +132,8 @@ class Service {
return Promise.reject(new errors.NotFound(`No record found for id '${id}'`));
}

update(id, data) {
if(id === null || Array.isArray(data)) {
update (id, data) {
if (id === null || Array.isArray(data)) {
return Promise.reject(new errors.BadRequest(
`You can not replace multiple instances. Did you mean 'patch'?`
));
Expand All @@ -143,7 +143,7 @@ class Service {
}

// Patch without hooks and mixins that can be used internally
_patch(id, data) {
_patch (id, data) {
if (id in this.store) {
_.extend(this.store[id], _.omit(data, this._id));

Expand All @@ -153,8 +153,8 @@ class Service {
return Promise.reject(new errors.NotFound(`No record found for id '${id}'`));
}

patch(id, data, params) {
if(id === null) {
patch (id, data, params) {
if (id === null) {
return this._find(params).then(page => {
return Promise.all(page.data.map(
current => this._patch(current[this._id], data, params))
Expand All @@ -166,7 +166,7 @@ class Service {
}

// Remove without hooks and mixins that can be used internally
_remove(id) {
_remove (id) {
if (id in this.store) {
const deleted = this.store[id];
delete this.store[id];
Expand All @@ -177,8 +177,8 @@ class Service {
return Promise.reject(new errors.NotFound(`No record found for id '${id}'`));
}

remove(id, params) {
if(id === null) {
remove (id, params) {
if (id === null) {
return this._find(params).then(page =>
Promise.all(page.data.map(current => this._remove(current[this._id])
)));
Expand All @@ -188,7 +188,7 @@ class Service {
}
}

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

Expand Down
2 changes: 0 additions & 2 deletions test/index.test.js
@@ -1,5 +1,3 @@
/*jshint expr: true*/

import { base, example } from 'feathers-service-tests';
import errors from 'feathers-errors';
import feathers from 'feathers';
Expand Down

0 comments on commit c2ee934

Please sign in to comment.