-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style: implement eslint and clean up code
- Loading branch information
Tom Kirkpatrick
committed
Apr 27, 2017
1 parent
5677d92
commit 27704ab
Showing
10 changed files
with
226 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "fullcube", | ||
"root": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "fullcube/mocha" | ||
} |
Oops, something went wrong.