diff --git a/src/utils/getLength.js b/src/utils/getLength.js new file mode 100644 index 00000000..acbc96b7 --- /dev/null +++ b/src/utils/getLength.js @@ -0,0 +1,11 @@ +import {pxToRem} from './units'; + +function getLength(length) { + if (typeof shortCode === 'number') { + return pxToRem(length); + } + + return length; +} + +export default getLength; diff --git a/src/utils/getSpacing.js b/src/utils/getSpacing.js new file mode 100644 index 00000000..f1acf35d --- /dev/null +++ b/src/utils/getSpacing.js @@ -0,0 +1,32 @@ +import {pxToRem} from './units'; + +const passThroughValues = ['auto']; + +function getSpacing(shortCode, theme) { + if (shortCode === undefined || shortCode === null || shortCode === false) { + return shortCode; + } + + if ( + (typeof shortCode !== 'number' && typeof shortCode !== 'string') || + shortCode === '0' + ) { + return '0'; + } + + if (typeof shortCode === 'number') { + return pxToRem(shortCode); + } + + if (passThroughValues.indexOf(shortCode) > -1) { + return shortCode; + } + + if (shortCode.charAt(0) === '-') { + return '-' + theme.globals.spacing[shortCode.substring(1)]; + } else { + return theme.globals.spacing[shortCode]; + } +} + +export default getSpacing; diff --git a/src/utils/index.js b/src/utils/index.js index 4133ed40..52fcfcce 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,5 +1,7 @@ export {default as colors} from './colors'; export {default as mergeRefs} from './mergeRefs'; +export {default as getLength} from './getLength'; +export {default as getSpacing} from './getSpacing'; export {default as spacing} from './spacing'; export {default as truncateText} from './truncateText'; export {default as units} from './units'; diff --git a/src/utils/spacing.js b/src/utils/spacing.js index ea002ae2..6ea654cb 100644 --- a/src/utils/spacing.js +++ b/src/utils/spacing.js @@ -1,32 +1,7 @@ -import {pxToRem} from './units'; - -const passThroughValues = ['auto']; - -function getSpacing(shortCode, theme) { - if (shortCode === undefined || shortCode === null || shortCode === false) { - return shortCode; - } - - if ( - (typeof shortCode !== 'number' && typeof shortCode !== 'string') || - shortCode === '0' - ) { - return '0'; - } - - if (typeof shortCode === 'number') { - return pxToRem(shortCode); - } - - if (passThroughValues.indexOf(shortCode) > -1) { - return shortCode; - } - - if (shortCode.charAt(0) === '-') { - return '-' + theme.globals.spacing[shortCode.substring(1)]; - } else { - return theme.globals.spacing[shortCode]; - } -} +import getSpacing from './getSpacing'; +/** + * Legacy export for backwards compatibility. + * Prefer `import {getSpacing} from 'utils';` + */ export {getSpacing};