Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable hex4 and hex8 #461

Merged
merged 1 commit into from
Feb 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/helpers/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export default {
},

isValidHex(hex) {
return tinycolor(hex).isValid()
// disable hex4 and hex8
const lh = (String(hex).charAt(0) === '#') ? 1 : 0
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor point, but I'm curious: why isn't this hex[0] == '#'? Since length is checked, hex is expected to be a string.

Another alternative:

const len = hex.startsWith('#') ? hex.length - 1 : hex.length
return (len == 3 || len == 6) && tinycolor(hex).isValid()

By checking that length is 3 or 6, instead of NOT 4 or >6 (per the spec of this function), it won't matter if tinycolor later allows other lengths.

return hex.length !== (4 + lh) && hex.length < (7 + lh) && tinycolor(hex).isValid()
},

getContrastingColor(data) {
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ describe('helpers/color', () => {
})

describe('isValidHex', () => {
test('allows strings of length 3, 6, or 8', () => {
test('allows strings of length 3 or 6', () => {
expect(color.isValidHex('f')).toBeFalsy()
expect(color.isValidHex('ff')).toBeFalsy()
expect(color.isValidHex('fff')).toBeTruthy()
// expect(color.isValidHex('ffff')).toBeFalsy()
expect(color.isValidHex('ffff')).toBeFalsy()
expect(color.isValidHex('fffff')).toBeFalsy()
expect(color.isValidHex('ffffff')).toBeTruthy()
expect(color.isValidHex('fffffff')).toBeFalsy()
expect(color.isValidHex('ffffffff')).toBeTruthy()
expect(color.isValidHex('ffffffff')).toBeFalsy()
expect(color.isValidHex('fffffffff')).toBeFalsy()
expect(color.isValidHex('ffffffffff')).toBeFalsy()
expect(color.isValidHex('fffffffffff')).toBeFalsy()
Expand Down