Skip to content

Commit

Permalink
refactor: remove underscorejs dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
jimlambie committed Jun 23, 2018
1 parent d920cc9 commit 69feb98
Show file tree
Hide file tree
Showing 6 changed files with 1,816 additions and 1,924 deletions.
110 changes: 65 additions & 45 deletions dadi/lib/handlers/colour.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
var _ = require('underscore')
var colourNamer = require('color-namer')
var Vibrant = require('node-vibrant')
const colourNamer = require('color-namer')
const Vibrant = require('node-vibrant')

/**
* Handles colour-related tasks for CDN images
*/
var ColourHandler = function () {
const ColourHandler = function () {

}

Expand All @@ -14,18 +13,22 @@ var ColourHandler = function () {
* @returns {object}
*/
ColourHandler.prototype.getColours = function (buffer, callback) {
var v = new Vibrant(buffer, { colorCount: 12, quality: 1 })
let v = new Vibrant(buffer, { colorCount: 12, quality: 1 })

v.getSwatches((err, swatches) => {
if (err) {
console.log(err)
}

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

var dominantColour = swatches[0]
var palette = swatches.slice(1)
let dominantColour = swatches[0]
let palette = swatches.slice(1)

return callback(null, {
flattened: this.getFlattenedColours(dominantColour, palette),
Expand All @@ -40,16 +43,16 @@ ColourHandler.prototype.getColours = function (buffer, callback) {
* @param {Array} palette - an array of colour swatches
*/
ColourHandler.prototype.getFullColours = function (dominantColour, palette) {
var primaryColourHex = dominantColour.getHex()
var primaryColourHSL = this.getHsl(dominantColour.getHsl())
var humanColour = new HumanColours(primaryColourHSL)
let primaryColourHex = dominantColour.getHex()
let primaryColourHSL = this.getHsl(dominantColour.getHsl())
let humanColour = new HumanColours(primaryColourHSL)

var paletteColours = {}
let paletteColours = {}

_.each(palette, (colour, index) => {
var hex = colour.getHex()
var hsl = this.getHsl(colour.getHsl())
var humanColourPalette = new HumanColours(hsl)
palette.forEach((colour, index) => {
let hex = colour.getHex()
let hsl = this.getHsl(colour.getHsl())
let humanColourPalette = new HumanColours(hsl)

paletteColours[index] = {
rgb: colour.getRgb(),
Expand Down Expand Up @@ -85,26 +88,31 @@ ColourHandler.prototype.getFullColours = function (dominantColour, palette) {
* @param {Array} palette - an array of colour swatches
*/
ColourHandler.prototype.getFlattenedColours = function (dominantColour, palette) {
var primaryColourHex = dominantColour.getHex()
var primaryColourHSL = this.getHsl(dominantColour.getHsl())
var humanColour = new HumanColours(primaryColourHSL)

var colourNames = colourNamer(dominantColour.getRgb())
var colours = _.sortBy([colourNames.basic[0], colourNames.roygbiv[0], colourNames.html[0], colourNames.pantone[0]], 'distance')
var names = _.pluck(colours, 'name')
var primaryColourArrays = {
names: _.map(names, (name) => { return name.toLowerCase() }),
hex: _.pluck(colours, 'hex')
let primaryColourHex = dominantColour.getHex()
let primaryColourHSL = this.getHsl(dominantColour.getHsl())
let humanColour = new HumanColours(primaryColourHSL)

let colourNames = colourNamer(dominantColour.getRgb())
let colours = [colourNames.basic[0], colourNames.roygbiv[0], colourNames.html[0], colourNames.pantone[0]].sort((a, b) => {
if (a.distance === b.distance) return 0
return a.distance < b.distance ? -1 : 1
})

let names = colours.map(({ name }) => name)
let primaryColourArrays = {
names: names.map(name => { return name.toLowerCase() }),
hex: colours.map(({ hex }) => hex)
}

primaryColourArrays.names.push(humanColour.hueName())
primaryColourArrays.names = _.uniq(primaryColourArrays.names)
// dedupe
primaryColourArrays.names = [...(new Set(primaryColourArrays.names))]

var colourPalette = _.map(palette, (colour) => {
var pc = colour.getHex()
var hsl = this.getHsl(colour.getHsl())
var humanColour = new HumanColours(hsl)
var names = colourNamer(pc)
let colourPalette = palette.map(colour => {
let pc = colour.getHex()
let hsl = this.getHsl(colour.getHsl())
let humanColour = new HumanColours(hsl)
let names = colourNamer(pc)

return {
primary: pc,
Expand All @@ -116,16 +124,28 @@ ColourHandler.prototype.getFlattenedColours = function (dominantColour, palette)
}
})

var colourArrays = _.map(colourPalette, (colour) => {
var colours = _.sortBy([colour.basic, colour.roygbiv, colour.html, colour.pantone], 'distance')
let colourArrays = colourPalette.map(colour => {
let colours = [colour.basic, colour.roygbiv, colour.html, colour.pantone].sort((a, b) => {
if (a.distance === b.distance) return 0
return a.distance < b.distance ? -1 : 1
})
return {
names: [colours[0].name.toLowerCase(), colour.human.toLowerCase()],
hex: [colours[0].hex]
}
})

var nameArray = _.uniq(_.flatten(_.pluck(colourArrays, 'names')))
var hexArray = _.uniq(_.flatten(_.pluck(colourArrays, 'hex')))
let nameArray = colourArrays.map(({ names }) => names)
// flatten
nameArray.reduce((acc, val) => acc.concat(val), [])
// dedupe
nameArray = [...(new Set(nameArray))]

let hexArray = colourArrays.map(({ hex }) => hex)
// flatten
hexArray.reduce((acc, val) => acc.concat(val), [])
// dedupe
hexArray = [...(new Set(hexArray))]

return {
primary: {
Expand All @@ -145,10 +165,10 @@ ColourHandler.prototype.getFlattenedColours = function (dominantColour, palette)
* Gets the named colours for a specified colour. Draws from lists of colours such as Basic, HTML and Pantone
*/
ColourHandler.prototype.getColourNames = function (colour) {
var obj = colourNamer(colour)
var data = {}
let obj = colourNamer(colour)
let data = {}

_.each(Object.keys(obj), (group) => {
Object.keys(obj).forEach(group => {
data[group] = obj[group][0]
})

Expand All @@ -166,12 +186,12 @@ ColourHandler.prototype.getHsl = function (hslArray) {
return [ Math.ceil(hslArray[0] * 360), Math.ceil(hslArray[1] * 100).toString() + '%', Math.ceil(hslArray[2] * 100).toString() + '%' ]
}

var h // Hue
var s // Saturation
var l // Lightness
var hue
var sat
var light
let h // Hue
let s // Saturation
let l // Lightness
let hue
let sat
let light

function HumanColours (hsl) {
this.HSL = hsl
Expand Down
35 changes: 20 additions & 15 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 concat = require('concat-stream')
const ExifImage = require('exif').ExifImage
Expand Down Expand Up @@ -1109,25 +1108,25 @@ ImageHandler.prototype.sanitiseOptions = function (options) {
// 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
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) {
Expand All @@ -1144,18 +1143,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 @@ -1166,26 +1165,32 @@ 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)

var colourData = {
// swatches = swatches.filter(Boolean)
swatches.sort((a, b) => {
if (a.population === b.population) return 0
return a.population > b.population ? -1 : 1
})

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

0 comments on commit 69feb98

Please sign in to comment.