Skip to content

Instantly share code, notes, and snippets.

@preco21
Last active August 21, 2017 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save preco21/dcbc793ce134dcd17a1c312dae63f059 to your computer and use it in GitHub Desktop.
Save preco21/dcbc793ce134dcd17a1c312dae63f059 to your computer and use it in GitHub Desktop.
A simple `calcMargin()`
export default function calcMargin(obj = {}) {
if (Number.isInteger(obj)) {
return {
top: obj,
right: obj,
bottom: obj,
left: obj,
};
}
if (typeof obj === 'string') {
const [top, right = top, bottom = top, left = right] = obj.split(' ');
return {
top: Number(top),
right: Number(right),
bottom: Number(bottom),
left: Number(left),
};
}
const {top = 0, right = 0, bottom = 0, left = 0} = obj;
return {
top,
right,
bottom,
left,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment