This repository has been archived by the owner on Sep 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from WJXHenry/master
Colour parsing using Lodash and Colornames (Light The Bridge)
- Loading branch information
Showing
5 changed files
with
78 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,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 |
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,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' }]) | ||
}) |