From e34aec407bf70cd301f42517c5e3137a6c8177fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Sat, 12 Dec 2015 00:38:15 +0100 Subject: [PATCH] :lipstick: Rewrite utils in ES2015 --- scripts/utils.js | 85 +++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 41 deletions(-) diff --git a/scripts/utils.js b/scripts/utils.js index d26a90c..2bcc9ea 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -1,43 +1,46 @@ -/*jslint browser:true, plusplus:true */ -/*globals self */ +'use strict'; -(function (self) { - 'use strict'; - - var utils = {}; - - // random utils - utils.getRandomArbitary = function (min, max) { - return Math.random() * (max - min) + min; - }; - - utils.getRandomInt = function (min, max) { - return Math.floor(Math.random() * (max - min + 1)) + min; - }; - - utils.getRandomColor = function () { - var color = [], i; - - for (i = 0; i < 3; i++) { - color.push(parseFloat( - utils.getRandomArbitary(0, 1).toFixed(2) - ) * 255); - } - - return color; - }; - - // colors utils - function componentToHex(c) { - var hex = parseInt(c, 10).toString(16); - return hex.length === 1 ? '0' + hex : hex; +/** + * Get a random whole number + * + * @param {number} min Minimal value for the number + * @param {number} max Maximal value for the number (excluded) + * @return {number} Random number + */ +export const randomNumber = (min, max) => + Math.floor(Math.random() * (max - min)) + min; + +/** + * Generate a random color + * + * @return {Array} RGB components + */ +export const randomColor = () => { + const color = []; + + for (let i = 0; i < 3; i++) { + color.push(parseFloat(Math.random().toFixed(2)) * 255); } - - utils.rgbToHex = function (c) { - console.log(c); - return '#' + componentToHex(c[0]) + componentToHex(c[1]) + componentToHex(c[2]); - }; - - // exports - self.utils = utils; -}(self)); \ No newline at end of file + + return color; +}; + +/** + * Convert a decimal number to its hexadecimal representation + * + * @param {number} input Number to be converted + * @return {string} Number representation + */ +const hex = input => { + let hex = parseInt(input, 10).toString(16); + return hex.length === 1 ? '0' + hex : hex; +}; + +/** + * Convert a RGB color to its hexadecimal representation + * + * @param {Array} color RGB color + * @return {string} Hex representation + */ +export const rgbToHex = + color => '#' + hex(color[0]) + hex(color[1]) + hex(color[2]);