Last active
July 18, 2016 18:04
-
-
Save jeffmcmahan/4cc8e57d826cac71d582928a910a4954 to your computer and use it in GitHub Desktop.
Generates default units for jss-default-unit
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
<!doctype html> | |
<html> | |
<head> | |
<title>Default Props</title> | |
</head> | |
<body> | |
<script> | |
'use strict' | |
if (window.navigator.vendor !== 'Google Inc.') { | |
alert('This was designed to be run in Chrome.') | |
} | |
/** | |
* Remove '-webkit-' and un-apply camelCase. | |
* | |
* @function | |
* @param {String} name | |
* @return {String} | |
*/ | |
function canonicalize(name) { | |
name = name.replace('webkit', '') | |
name = name.split('') | |
name[0] = name[0].toLowerCase() | |
return name.join('').replace(/([A-Z])/g, '-$1').toLowerCase() | |
} | |
/** | |
* Find all properties that can a number (unit or no unit). | |
* | |
* @function | |
* @return {Object} | |
*/ | |
function mapTypesToCompatibleProps() { | |
let div = document.createElement('div') | |
let maps = { | |
numeric: [], | |
px: [], | |
ms: [], | |
pct: [] | |
} | |
document.body.appendChild(div) | |
Object.keys(div.style).forEach((prop) => { | |
div.style[prop] = 'abc' // Exclude props accepting arbitrary strings. | |
if (div.style[prop] === 'abc') return | |
div.style[prop] = '5' | |
if (div.style[prop] === '5') maps.numeric.push(prop) | |
div.style[prop] = '5px' | |
if (div.style[prop] === '5px') maps.px.push(prop) | |
div.style[prop] = '5ms' | |
if (div.style[prop] === '5ms') maps.ms.push(prop) | |
div.style[prop] = '5%' | |
if (div.style[prop] === '5%') maps.pct.push(prop) | |
}) | |
return maps | |
} | |
/** | |
* Map each CSS property to a default unit. | |
* | |
* @function | |
* @return {String} - JSON string of prop-to-unit map. | |
*/ | |
function getDefaultUnits() { | |
let typesToProps = mapTypesToCompatibleProps() | |
let map = {} | |
typesToProps.px.forEach((prop) => { | |
if (typesToProps.numeric.indexOf(prop) === -1) { | |
map[canonicalize(prop)] = 'px' | |
} | |
}) | |
typesToProps.ms.forEach((prop) => { | |
if (typesToProps.numeric.indexOf(prop) === -1) { | |
map[canonicalize(prop)] = 'ms' | |
} | |
}) | |
typesToProps.pct.forEach((prop) => { | |
if ( | |
prop.indexOf('rigin') !== -1 || // Capture the various origin properties. | |
(typesToProps.numeric.indexOf(prop) === -1 && | |
typesToProps.px.indexOf(prop) === -1) | |
) map[canonicalize(prop)] = '%' | |
}) | |
return JSON.stringify(map, null, 2) | |
} | |
document.body.innerHTML = ( | |
`<pre> | |
/** | |
* Generated jss-default-unit CSS property units | |
* @object | |
*/ | |
export default ${ | |
getDefaultUnits() | |
.replace(/"/g, '\'') | |
.replace(/'([a-z0-9]+)'\:/g, '$1:') | |
} | |
</pre>` | |
) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment