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

Export Helpers #613

Merged
merged 1 commit into from
Apr 29, 2019
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
108 changes: 54 additions & 54 deletions src/helpers/color.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,65 @@
import each from 'lodash/each'
import tinycolor from 'tinycolor2'

export default {
simpleCheckForValidColor(data) {
const keysToCheck = ['r', 'g', 'b', 'a', 'h', 's', 'l', 'v']
let checked = 0
let passed = 0
each(keysToCheck, (letter) => {
if (data[letter]) {
checked += 1
if (!isNaN(data[letter])) {
export const simpleCheckForValidColor = (data) => {
const keysToCheck = ['r', 'g', 'b', 'a', 'h', 's', 'l', 'v']
let checked = 0
let passed = 0
each(keysToCheck, (letter) => {
if (data[letter]) {
checked += 1
if (!isNaN(data[letter])) {
passed += 1
}
if (letter === 's' || letter === 'l') {
const percentPatt = /^\d+%$/
if (percentPatt.test(data[letter])) {
passed += 1
}
if (letter === 's' || letter === 'l') {
const percentPatt = /^\d+%$/
if (percentPatt.test(data[letter])) {
passed += 1
}
}
}
})
return (checked === passed) ? data : false
},

toState(data, oldHue) {
const color = data.hex ? tinycolor(data.hex) : tinycolor(data)
const hsl = color.toHsl()
const hsv = color.toHsv()
const rgb = color.toRgb()
const hex = color.toHex()
if (hsl.s === 0) {
hsl.h = oldHue || 0
hsv.h = oldHue || 0
}
const transparent = hex === '000000' && rgb.a === 0
})
return (checked === passed) ? data : false
}

return {
hsl,
hex: transparent ? 'transparent' : `#${ hex }`,
rgb,
hsv,
oldHue: data.h || oldHue || hsl.h,
source: data.source,
}
},
export const toState = (data, oldHue) => {
const color = data.hex ? tinycolor(data.hex) : tinycolor(data)
const hsl = color.toHsl()
const hsv = color.toHsv()
const rgb = color.toRgb()
const hex = color.toHex()
if (hsl.s === 0) {
hsl.h = oldHue || 0
hsv.h = oldHue || 0
}
const transparent = hex === '000000' && rgb.a === 0

isValidHex(hex) {
// disable hex4 and hex8
const lh = (String(hex).charAt(0) === '#') ? 1 : 0
return hex.length !== (4 + lh) && hex.length < (7 + lh) && tinycolor(hex).isValid()
},
return {
hsl,
hex: transparent ? 'transparent' : `#${ hex }`,
rgb,
hsv,
oldHue: data.h || oldHue || hsl.h,
source: data.source,
}
}

getContrastingColor(data) {
if (!data) {
return '#fff'
}
const col = this.toState(data)
if (col.hex === 'transparent') {
return 'rgba(0,0,0,0.4)'
}
const yiq = ((col.rgb.r * 299) + (col.rgb.g * 587) + (col.rgb.b * 114)) / 1000
return (yiq >= 128) ? '#000' : '#fff'
},
export const isValidHex = (hex) => {
// disable hex4 and hex8
const lh = (String(hex).charAt(0) === '#') ? 1 : 0
return hex.length !== (4 + lh) && hex.length < (7 + lh) && tinycolor(hex).isValid()
}

export const getContrastingColor = (data) => {
if (!data) {
return '#fff'
}
const col = toState(data)
if (col.hex === 'transparent') {
return 'rgba(0,0,0,0.4)'
}
const yiq = ((col.rgb.r * 299) + (col.rgb.g * 587) + (col.rgb.b * 114)) / 1000
return (yiq >= 128) ? '#000' : '#fff'
}

export const red = {
Expand All @@ -70,3 +68,5 @@ export const red = {
rgb: { r: 255, g: 0, b: 0, a: 1 },
hsv: { h: 0, s: 1, v: 1, a: 1 },
}

export default exports

Choose a reason for hiding this comment

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

I was just browsing the latest release notes and inspecting the changed code and got curious about this as I couldn't find any spec about export default exports. It seems that this is some kind of Babel magic or a coincidence that Babel-generated code just exports a common-js implicit module.exports variable as the default export.

Please see the example and the generated code on the Babel REPL:
https://babeljs.io/repl#?browsers=&build=&builtIns=false&spec=false&loose=false&code_lz=KYDwDg9gTgLgBAYwgOwM7xHAvHArAbgChDRJZEV04BPbOANiMIHpm4BVZJAW2-GXgwAFgEtUcAG4BDADYiAJhXnA4MCHFTAVwlQHVoMxUhkypYTahZskaeKWgxxOAEQRuAc2fF75ZQDMpAFcZO3AHVCA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=es2015%2Creact%2Cstage-2&prettier=false&targets=&version=7.7.7&externalPlugins=

As the exports is not a reserved keyword, another export can be named exports to make this scheme to fail horribly exporting that binding, not "all the named exports" as intended by the "accidental syntax".

Please see the generated code: https://babeljs.io/repl#?browsers=&build=&builtIns=false&spec=false&loose=false&code_lz=KYDwDg9gTgLgBAYwgOwM7xHAvHArAbgChDRJZEV04BPbOANiJPGniTXlNdToCIIAtgHNexLuQAmwAGYBDAK4AbTi1iogA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=es2015%2Creact%2Cstage-2&prettier=false&targets=&version=7.7.7&externalPlugins=

@casesandberg - I think an explicit default export of all the named exports is way better as it does not rely on Babel quirks and it is a valid piece of ES code. Are PRs welcome?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Good catch! We are working on the next major release right now which is fully ES6, so it will not have these problems going forward. I appreciate your thoroughness, however!

2 changes: 2 additions & 0 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './checkboard'
export * from './color'