Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #80 from WJXHenry/master
Browse files Browse the repository at this point in the history
Colour parsing using Lodash and Colornames (Light The Bridge)
  • Loading branch information
WJXHenry committed Mar 25, 2019
2 parents e218520 + a3a47ce commit 0f3fd5a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 6 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"watch": "nodemon",
"test": "jest",
"coverage": "jest --coverage && cat ./coverage/lcov.info | coveralls",
"prettify": "prettier '**/*.{js,md}' --write",
"prettify": "prettier \"**/*.{js,md}\" --write",
"lint": "eslint src/**"
},
"repository": {
Expand All @@ -30,6 +30,7 @@
"homepage": "https://github.com/j-rewerts/IFTTT-Edmonton#readme",
"dependencies": {
"body-parser": "^1.18.3",
"colornames": "^1.1.1",
"express": "^4.16.4",
"redis": "^2.8.0",
"request": "^2.88.0",
Expand Down
17 changes: 12 additions & 5 deletions src/controllers/light-the-bridge.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
const request = require('request-promise-native')
const parseXML = require('../utils/parse-ltb-xml')
const parseColors = require('../utils/color-parse')

/**
* Returns the most recent light the bridge results to IFTTT.
*/
module.exports = async function(req, res) {
let tweet
var colors = []
try {
let xmlString = await request(process.env.LTB_URL)
tweet = await parseXML(xmlString)
colors = parseColors(tweet.title)
} catch (e) {
console.error(e)
return res.status(500).send({
Expand All @@ -25,11 +28,15 @@ module.exports = async function(req, res) {
...tweet,
...{
created_at: new Date().toISOString(),
color_description: '',
color1: '',
color2: '',
color3: '',
color4: '',
color_description: colors
.map(function(element) {
return element.color.charAt(0).toUpperCase() + element.color.slice(1)
})
.join(', '),
color1: colors[0] ? colors[0].hex : '',
color2: colors[1] ? colors[1].hex : '',
color3: colors[2] ? colors[2].hex : '',
color4: colors[3] ? colors[3].hex : '',
meta: {
id: tweet.id,
timestamp: Math.round(new Date() / 1000)
Expand Down
16 changes: 16 additions & 0 deletions src/utils/color-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var toHex = require('colornames')

function parseColors(string_to_parse = '') {
const words = string_to_parse
.replace(new RegExp(/[^a-zA-Z\s]/g), ' ')
.toLowerCase()
.split(' ')
var colors = []
for (let i = 0; i < words.length; i++) {
var hexColor = toHex(words[i])
if (hexColor) colors.push({ color: words[i], hex: hexColor })
}
return colors
}

module.exports = parseColors
43 changes: 43 additions & 0 deletions src/utils/color-parse.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const parseColors = require('./color-parse')

test('empty-arg', () => {
expect(parseColors()).toEqual([])
})

test('no-color', () => {
expect(parseColors('Hello! There are no colors here')).toEqual([])
})

test('single-color', () => {
expect(parseColors('green')).toEqual([{ color: 'green', hex: '#008000' }])
})

test('random-casing', () => {
expect(parseColors('tUrQuOIse')).toEqual([
{ color: 'turquoise', hex: '#40E0D0' }
])
})

test('multiple-colors', () => {
const message = 'This message has the colors maroon, gold, and cobalt'
expect(parseColors(message)).toEqual([
{ color: 'maroon', hex: '#800000' },
{ color: 'gold', hex: '#FFD700' },
{ color: 'cobalt', hex: '#3D59AB' }
])
})

test('punctuations', () => {
const message =
'This message has red! green. ?blue with starting/ending punctuations'
expect(parseColors(message)).toEqual([
{ color: 'red', hex: '#FF0000' },
{ color: 'green', hex: '#008000' },
{ color: 'blue', hex: '#0000FF' }
])
})

test('additional-cases', () => {
const message = 'This should only register blue... r.e.d'
expect(parseColors(message)).toEqual([{ color: 'blue', hex: '#0000FF' }])
})

0 comments on commit 0f3fd5a

Please sign in to comment.