Skip to content

Commit

Permalink
style: implement eslint and clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Kirkpatrick committed Apr 27, 2017
1 parent 5677d92 commit 27704ab
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 260 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/coverage/
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "fullcube",
"root": true
}
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
npm-debug.log
node_modules/
.idea
node_modules
coverage
.nyc_output
23 changes: 0 additions & 23 deletions .jscsrc

This file was deleted.

26 changes: 0 additions & 26 deletions .jshintrc

This file was deleted.

115 changes: 59 additions & 56 deletions lib/calculated.js
Original file line number Diff line number Diff line change
@@ -1,115 +1,118 @@
'use strict';
'use strict'

var debug = require('debug')('loopback-ds-calculated-mixin');
var _ = require('lodash');
var Promise = require('bluebird');
const debug = require('debug')('loopback-ds-calculated-mixin')
const _ = require('lodash')
const Promise = require('bluebird')

function getPropertyConfig(property) {
if (typeof property === 'string') {
return {
callback: property,
};
}
}
return property;
return property
}

module.exports = function(Model, options) {
module.exports = (Model, options) => {

// Sanity Checks - verify that the defined properties and callbacks exist on the model and log a message if not.
_.forEach(options.properties, function(options, property) {
var config = getPropertyConfig(options);
_.forEach(options.properties, (config, property) => {
config = getPropertyConfig(config)

if (_.isUndefined(Model.definition.properties[property])) {
debug('Property %s on %s is undefined', property, Model.modelName);
debug('Property %s on %s is undefined', property, Model.modelName)
}

if (_.isUndefined(config.callback)) {
debug('Callback on %s is undefined', property);
debug('Callback on %s is undefined', property)
}

if (typeof Model[config.callback] !== 'function') {
debug('Callback %s for %s is not a model function', config.callback, property);
debug('Callback %s for %s is not a model function', config.callback, property)
}
});
})

debug('Calculated mixin for Model %s with options %o', Model.modelName, options);
debug('Calculated mixin for Model %s with options %o', Model.modelName, options)

// The loaded observer is triggered when an item is loaded
Model.observe('before save', function(ctx, next) {
Model.observe('before save', (ctx, next) => {
// Allow user to bypass calculation by setting `skipCalculated` option.
if (_.get(ctx, 'options.skipCalculated')) {
debug('Not calculating properties %s (skipCalculated was set)');
return next();
debug('Not calculating properties %s (skipCalculated was set)')
return next()
}

var data = ctx.data || ctx.instance;
var instance = ctx.instance || ctx.currentInstance;
const data = ctx.data || ctx.instance
let instance = ctx.instance || ctx.currentInstance

// upsert or updateAll detected - We only act on single model updates.
if (!instance) {
var instanceId = ctx.where[Model.getIdName()];
const instanceId = ctx.where[Model.getIdName()]

if (instanceId) {
instance = ctx.data;
instance = new Model(instance);
} else {
debug('Not calculating properties (updateAll not supported)');
return next();
instance = ctx.data
instance = new Model(instance)
}
else {
debug('Not calculating properties (updateAll not supported)')
return next()
}
}

return Promise.map(Object.keys(options.properties), function(property) {
var config = getPropertyConfig(options.properties[property]);
var callback = config.callback;
var action = ctx.isNewInstance ? 'Calculating' : 'Recalculating';
return Promise.map(Object.keys(options.properties), property => {
const config = getPropertyConfig(options.properties[property])
const callback = config.callback
const action = ctx.isNewInstance ? 'Calculating' : 'Recalculating'

if (!callback) {
debug('Callback not defined for property %s', property);
return false;
debug('Callback not defined for property %s', property)
return null
}

if (typeof Model[callback] !== 'function') {
debug('Function %s not found on Model', callback);
return false;
debug('Function %s not found on Model', callback)
return null
}

if (!ctx.isNewInstance && !config.recalculateOnUpdate) {
debug('Not recalculating property %s (not a new instance and recalculateOnUpdate not set)', property);
return false;
debug('Not recalculating property %s (not a new instance and recalculateOnUpdate not set)', property)
return null
}

// Convert the data to be plain object to avoid pollutions.
var instanceCopy = instance.toObject(true);
let instanceCopy = instance.toObject(true)

// Apply changes from partial update to make available for use in calculation.
if (ctx.data) {
instanceCopy = Object.assign(instanceCopy, ctx.data);
instanceCopy = Object.assign(instanceCopy, ctx.data)
}

// Make the clone an model instance to pass through to callback.
instanceCopy = new Model(instanceCopy);
instanceCopy = new Model(instanceCopy)

debug(`${action} ${property} property for ${Model.modelName} ${instanceCopy.getId()} with callback ${callback}`);
debug(`${action} ${property} property for ${Model.modelName} ${instanceCopy.getId()} with callback ${callback}`)

var value = Model[callback](instanceCopy);
const value = Model[callback](instanceCopy)

if (typeof value === 'undefined') {
debug('Callback returned undefined. Not setting property');
return false;
} else if (!_.get(value, 'then')) {
debug('Setting property %s to %s', property, value);
data[property] = value;
} else {
debug('Callback returned undefined. Not setting property')
return null
}
if (_.get(value, 'then')) {
return value
.then(function(res) {
.then(res => {
if (typeof res === 'undefined') {
debug('Callback returned undefined. Not setting property');
return false;
debug('Callback returned undefined. Not setting property')
return null
}
debug('Setting property %s to %s', property, res);
data[property] = res;
return data;
});
debug('Setting property %s to %s', property, res)
data[property] = res
return null
})
}
});
});
};
debug('Setting property %s to %s', property, value)
data[property] = value
return null
})
})
}
12 changes: 6 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var deprecate = require('depd')('loopback-ds-calculated-mixin');
var calculated = require('./calculated');
const deprecate = require('depd')('loopback-ds-calculated-mixin')
const calculated = require('./calculated')

module.exports = function mixin(app) {
'use strict';
'use strict'
app.loopback.modelBuilder.mixins.define = deprecate.function(app.loopback.modelBuilder.mixins.define,
'app.modelBuilder.mixins.define: Use mixinSources instead');
app.loopback.modelBuilder.mixins.define('Calculated', calculated);
};
'app.modelBuilder.mixins.define: Use mixinSources instead')
app.loopback.modelBuilder.mixins.define('Calculated', calculated)
}
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"url": "https://github.com/fullcube/loopback-ds-calculated-mixin.git"
},
"scripts": {
"lint": "jscs lib && jshint lib",
"test": "mocha -R spec --timeout 10000 test.js",
"lint": "eslint .",
"test": "NODE_ENV=test nyc --reporter=lcov --reporter=text --reporter=text-summary mocha test/*test.js",
"test:watch": "npm run test -- -w",
"pretest": "npm run lint",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
Expand All @@ -30,12 +30,14 @@
"chai": "latest",
"chai-datetime": "^1.4.1",
"condition-circle": "^1.5.0",
"jscs": "latest",
"jshint": "latest",
"dirty-chai": "^1.2.2",
"eslint": "^2.11.1",
"eslint-config-fullcube": "^1.0.46",
"loopback": "^3.6.0",
"loopback-datasource-juggler": "^3.5.0",
"mocha": "latest",
"mocha-sinon": "latest",
"nyc": "^10.2.0",
"semantic-release": "^6.3.2",
"sinon": "latest",
"sinon-chai": "latest"
Expand Down
3 changes: 3 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "fullcube/mocha"
}
Loading

0 comments on commit 27704ab

Please sign in to comment.