From c4fa5304db9f16b94f15c2b44793a5a11d595885 Mon Sep 17 00:00:00 2001 From: PieterGit Date: Mon, 23 Jul 2018 00:02:54 +0200 Subject: [PATCH] more lodash modularization --- lib/admin_plugins/index.js | 7 ++++--- lib/api/activity/index.js | 11 +++++++---- lib/api/alexa/index.js | 7 +++---- lib/api/entries/index.js | 18 +++++++++++------- lib/api/index.js | 4 ++-- lib/api/properties.js | 13 ++++++++----- lib/api/treatments/index.js | 11 +++++++---- 7 files changed, 42 insertions(+), 29 deletions(-) diff --git a/lib/admin_plugins/index.js b/lib/admin_plugins/index.js index c5e86cb5405..c6463ecca8e 100644 --- a/lib/admin_plugins/index.js +++ b/lib/admin_plugins/index.js @@ -1,6 +1,7 @@ 'use strict'; -var _ = require('lodash'); +var _find = require('lodash/find'); +var _each = require('lodash/each'); function init() { var allPlugins = [ @@ -12,14 +13,14 @@ function init() { function plugins(name) { if (name) { - return _.find(allPlugins, {name: name}); + return _find(allPlugins, {name: name}); } else { return plugins; } } plugins.eachPlugin = function eachPlugin(f) { - _.each(allPlugins, f); + _each(allPlugins, f); }; plugins.createHTML = function createHTML(client) { diff --git a/lib/api/activity/index.js b/lib/api/activity/index.js index 772a437c60a..d88019ab936 100644 --- a/lib/api/activity/index.js +++ b/lib/api/activity/index.js @@ -1,6 +1,9 @@ 'use strict'; -var _ = require('lodash'); +var _forEach = require('lodash/forEach'); +var _isNil = require('lodash/isNil'); +var _isArray = require('lodash/isArray'); + var consts = require('../../constants'); var moment = require('moment'); @@ -36,7 +39,7 @@ function configure(app, wares, ctx) { ctx.activity.list(req.query, function(err, results) { var d1 = null; - _.forEach(results, function clean(t) { + _forEach(results, function clean(t) { var d2 = null; @@ -55,7 +58,7 @@ function configure(app, wares, ctx) { } }); - if (!_.isNil(d1)) res.setHeader('Last-Modified', d1.toUTCString()); + if (!_isNil(d1)) res.setHeader('Last-Modified', d1.toUTCString()); if (ifModifiedSince && d1.getTime() <= moment(ifModifiedSince).valueOf()) { res.status(304).send({ @@ -75,7 +78,7 @@ function configure(app, wares, ctx) { function post_response(req, res) { var activity = req.body; - if (!_.isArray(activity)) { + if (!_isArray(activity)) { activity = [activity]; }; diff --git a/lib/api/alexa/index.js b/lib/api/alexa/index.js index ad1413b0980..65f477ad85d 100644 --- a/lib/api/alexa/index.js +++ b/lib/api/alexa/index.js @@ -1,8 +1,7 @@ 'use strict'; var moment = require('moment'); -var _ = require('lodash'); - +var _each = require('lodash/each'); function configure (app, wares, ctx, env) { var entries = ctx.entries; @@ -21,7 +20,7 @@ function configure (app, wares, ctx, env) { if (plugin.alexa) { if (plugin.alexa.intentHandlers) { console.log(plugin.name + ' is Alexa enabled'); - _.each(plugin.alexa.intentHandlers, function (route) { + _each(plugin.alexa.intentHandlers, function (route) { if (route) { ctx.alexa.configureIntentHandler(route.intent, route.intentHandler, route.routableSlot, route.slots); } @@ -29,7 +28,7 @@ function configure (app, wares, ctx, env) { } if (plugin.alexa.rollupHandlers) { console.log(plugin.name + ' is Alexa rollup enabled'); - _.each(plugin.alexa.rollupHandlers, function (route) { + _each(plugin.alexa.rollupHandlers, function (route) { console.log('Route'); console.log(route); if (route) { diff --git a/lib/api/entries/index.js b/lib/api/entries/index.js index c9290236513..691cba7ecc0 100644 --- a/lib/api/entries/index.js +++ b/lib/api/entries/index.js @@ -1,6 +1,10 @@ 'use strict'; -var _ = require('lodash'); +var _last = require('lodash/last'); +var _isNil = require('lodash/isNil'); +var _first = require('lodash/first'); +var _includes = require('lodash/includes'); + var consts = require('../../constants'); var es = require('event-stream'); var sgvdata = require('sgvdata'); @@ -77,11 +81,11 @@ function configure(app, wares, ctx) { function ifModifiedSinceCTX(req, res, next) { - var lastEntry = _.last(ctx.ddata.sgvs); + var lastEntry = _last(ctx.ddata.sgvs); var lastEntryDate = null; - if (!_.isNil(lastEntry)) { - lastEntryDate = new Date(_.last(ctx.ddata.sgvs).mills); + if (!_isNil(lastEntry)) { + lastEntryDate = new Date(_last(ctx.ddata.sgvs).mills); res.setHeader('Last-Modified', lastEntryDate.toUTCString()); } @@ -140,10 +144,10 @@ function configure(app, wares, ctx) { res.entries.sort(compare); - var lastEntry = _.first(res.entries); + var lastEntry = _first(res.entries); var lastEntryDate = null; - if (!_.isNil(lastEntry)) { + if (!_isNil(lastEntry)) { if (lastEntry.mills) lastEntryDate = new Date(lastEntry.mills); if (!lastEntry.mills && lastEntry.date) lastEntryDate = new Date(lastEntry.date); res.setHeader('Last-Modified', lastEntryDate.toUTCString()); @@ -626,7 +630,7 @@ curl -s -g 'http://localhost:1337/api/v1/times/20{14..15}/T{13..18}:{00..15}'.js * by default. */ function prep_storage(req, res, next) { - if (req.params.storage && _.includes(['entries', 'treatments', 'devicestatus'], req.params.storage)) { + if (req.params.storage && _includes(['entries', 'treatments', 'devicestatus'], req.params.storage)) { req.storage = ctx[req.params.storage]; } else { req.storage = ctx.entries; diff --git a/lib/api/index.js b/lib/api/index.js index 57c24975d81..3c9748c93e6 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -1,7 +1,7 @@ 'use strict'; function create (env, ctx) { - var _ = require('lodash') + var _each = require('lodash/each') , express = require('express') , app = express( ) ; @@ -22,7 +22,7 @@ function create (env, ctx) { if (env.settings.enable) { app.extendedClientSettings = ctx.plugins && ctx.plugins.extendedClientSettings ? ctx.plugins.extendedClientSettings(env.extendedSettings) : {}; - _.each(env.settings.enable, function (enable) { + _each(env.settings.enable, function (enable) { console.info('enabling feature:', enable); app.enable(enable); }); diff --git a/lib/api/properties.js b/lib/api/properties.js index 11e03e07395..981f3c31328 100644 --- a/lib/api/properties.js +++ b/lib/api/properties.js @@ -1,6 +1,9 @@ 'use strict'; -var _ = require('lodash'); +var _isEmpty = require('lodash/isEmpty'); +var _filter = require('lodash/filter'); +var _pick = require('lodash/pick'); + var express = require('express'); var sandbox = require('../sandbox')(); @@ -22,21 +25,21 @@ function create (env, ctx) { ctx.plugins.setProperties(sbx); function notEmpty (part) { - return ! _.isEmpty(part); + return ! _isEmpty(part); } - var segments = _.filter(req.path.split('/'), notEmpty); + var segments = _filter(req.path.split('/'), notEmpty); var selected = [ ]; if (segments.length > 0) { - selected = _.filter(segments[0].split(','), notEmpty); + selected = _filter(segments[0].split(','), notEmpty); } var result = sbx.properties; if (selected.length > 0) { - result = _.pick(sbx.properties, selected); + result = _pick(sbx.properties, selected); } if (req.query && req.query.pretty) { diff --git a/lib/api/treatments/index.js b/lib/api/treatments/index.js index 842c26df800..3fff4ceb331 100644 --- a/lib/api/treatments/index.js +++ b/lib/api/treatments/index.js @@ -1,6 +1,9 @@ 'use strict'; -var _ = require('lodash'); +var _forEach = require('lodash/forEach'); +var _isNil = require('lodash/isNil'); +var _isArray = require('lodash/isArray'); + var consts = require('../../constants'); var moment = require('moment'); @@ -36,7 +39,7 @@ function configure(app, wares, ctx) { ctx.treatments.list(req.query, function(err, results) { var d1 = null; - _.forEach(results, function clean(t) { + _forEach(results, function clean(t) { t.carbs = Number(t.carbs); t.insulin = Number(t.insulin); @@ -57,7 +60,7 @@ function configure(app, wares, ctx) { } }); - if (!_.isNil(d1)) res.setHeader('Last-Modified', d1.toUTCString()); + if (!_isNil(d1)) res.setHeader('Last-Modified', d1.toUTCString()); if (ifModifiedSince && d1.getTime() <= moment(ifModifiedSince).valueOf()) { res.status(304).send({ @@ -77,7 +80,7 @@ function configure(app, wares, ctx) { function post_response(req, res) { var treatments = req.body; - if (!_.isArray(treatments)) { + if (!_isArray(treatments)) { treatments = [treatments]; };