Skip to content

Commit

Permalink
Merge pull request #465 from dadi/patch/remove-underscore-2
Browse files Browse the repository at this point in the history
Remove underscorejs dependency
  • Loading branch information
jimlambie committed Jan 31, 2019
2 parents c56f1a2 + 602f56c commit e9be2c0
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 332 deletions.
253 changes: 0 additions & 253 deletions dadi/lib/handlers/colour.js

This file was deleted.

41 changes: 24 additions & 17 deletions dadi/lib/handlers/image.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const _ = require('underscore')
const fs = require('fs-extra')
const exifReader = require('exif-reader-paras20xx')
const fit = require('aspect-fit')
Expand Down Expand Up @@ -1056,35 +1055,40 @@ ImageHandler.prototype.sanitiseOptions = function (options) {
// check the options for aliases
// e.g. "dpr" === "devicePixelRatio"

var imageOptions = {}
let imageOptions = {}

// handle querystring options that came from a remote image url
// as if the original remote url had it's own querystring then we'll
// get an option here that starts with a ?, from where the CDN params were added
_.each(Object.keys(options), key => {
Object.keys(options).forEach(key => {
if (key[0] === '?') {
options[key.substring(1)] = options[key]
delete options[key]
}
})

_.each(Object.keys(options), key => {
var settings = _.filter(IMAGE_PARAMETERS, setting => {
return setting.name === key || _.contains(setting.aliases, key)
Object.keys(options).forEach(key => {
let settings = IMAGE_PARAMETERS.filter(setting => {
return setting.name === key || setting.aliases.includes(key)
})

if (settings && settings[0]) {
var value = options[key]
let value = options[key]

if (options[key] !== '0' || settings[0].allowZero || settings[0].default) {
if (options[key] !== '0' || settings[0].allowZero) {
if (settings[0].lowercase) value = value.toLowerCase()
value = _.isFinite(value) ? parseFloat(value) : value
if (settings[0].lowercase) {
value = value.toLowerCase()
}

value = isNaN(value) ? value : Number.parseFloat(value)

if (settings[0].minimumValue && value < settings[0].minimumValue) {
value = settings[0].minimumValue
} else if (settings[0].maximumValue && value > settings[0].maximumValue) {
value = settings[0].maximumValue
}

imageOptions[settings[0].name] = value
} else {
imageOptions[settings[0].name] = settings[0].default
Expand All @@ -1096,18 +1100,18 @@ ImageHandler.prototype.sanitiseOptions = function (options) {
})

// ensure we have defaults for options not specified
var defaults = _.filter(IMAGE_PARAMETERS, setting => {
let defaults = IMAGE_PARAMETERS.filter(setting => {
return setting.default
})

_.each(defaults, setting => {
defaults.forEach(setting => {
if (typeof imageOptions[setting.name] === 'undefined') {
imageOptions[setting.name] = setting.default
}
})

// add any URL parameters that aren't part of the core set
_.extend(imageOptions, options)
imageOptions = Object.assign({}, imageOptions, options)

return imageOptions
}
Expand All @@ -1118,26 +1122,29 @@ ImageHandler.prototype.setBaseUrl = function (baseUrl) {

function getColours (buffer, options) {
return new Promise((resolve, reject) => {
var v = new Vibrant(buffer, options)
let v = new Vibrant(buffer, options)

v.getSwatches((err, swatches) => {
if (err) {
return reject(err)
}

// remove empty swatches and sort by population descending
swatches = _.compact(_.sortBy(swatches, 'population')).reverse()
swatches = Object.values(swatches).sort((a, b) => {
if (a.population === b.population) return 0
return a.population > b.population ? -1 : 1
})

var colourData = {
let colourData = {
primaryColour: swatches[0].getHex(),
palette: {
rgb: [],
hex: []
}
}

_.each(swatches, (swatch, key) => {
if (key !== 0) {
swatches.forEach((swatch, index) => {
if (index !== 0) {
colourData.palette.rgb.push(swatch.getRgb())
colourData.palette.hex.push(swatch.getHex())
}
Expand Down
2 changes: 1 addition & 1 deletion dadi/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ Server.prototype.stop = function (done) {

this.server.close(err => {
// If statusServer is running in standalone, close that too.
if (this.statusServer) {
if (this.statusServer && this.statusServer._handle) {
this.statusServer.close(err => {
this.readyState = 0

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@dadi/boot": "^1.1.4",
"@dadi/cache": "^3.0.0",
"@dadi/logger": "latest",
"@dadi/status": "latest",
"@dadi/status": "^1.0.4",
"accept-language-parser": "^1.2.0",
"aspect-fit": "^1.0.2",
"aws-sdk": "2.252.x",
Expand Down Expand Up @@ -69,7 +69,6 @@
"streamifier": "^0.1.1",
"ua-parser-js": "^0.7.10",
"uglify-js": "^3.0.25",
"underscore": "~1.8.3",
"url-join": "^3.0.0",
"useragent": "2.3.0",
"uuid": "latest",
Expand Down

0 comments on commit e9be2c0

Please sign in to comment.