Skip to content

Instantly share code, notes, and snippets.

View rijkvanzanten's full-sized avatar
💜
Building Directus

Rijk van Zanten rijkvanzanten

💜
Building Directus
View GitHub Profile
@rijkvanzanten
rijkvanzanten / hex-to-rgb.js
Created March 26, 2017 10:07
Converts hex value to RGB(a) values
/**
* Converts hex value (without #) to rgb(a) object
* @param {String} hex 3, 6 or 8 digit hex (without #)
* @return {Object} rgba values
*/
function convertHexToRGB(hex) {
var fullHex = hex;
var rgba = {};
if(hex.length === 3) {
/**
* Validates RGB(a) value
* @param {Number} r
* @param {Number} g
* @param {Number} b
* @param {Number} [a=0]
* @return {Boolean}
*/
function isValidRGB(r, g, b, a) {
a = a || 0;
/**
* Validates hex value
* @param {String} color hex color value
* @return {Boolean}
*/
function isValidHex(color) {
if(!color || typeof color !== 'string') return false;
// Validate hex values
if(color.substring(0, 1) === '#') color = color.substring(1);
@rijkvanzanten
rijkvanzanten / css-name-validator.js
Created March 26, 2017 09:29
Validate css color value based on css name
function validateTextColor(color) {
var cssColorsArray = ['AliceBlue', 'AntiqueWhite', 'Aqua', 'Aquamarine', 'Azure', 'Beige', 'Bisque', 'Black', 'BlanchedAlmond', 'Blue', 'BlueViolet', 'Brown', 'BurlyWood', 'CadetBlue', 'Chartreuse', 'Chocolate', 'Coral', 'CornflowerBlue', 'Cornsilk', 'Crimson', 'Cyan', 'DarkBlue', 'DarkCyan', 'DarkGoldenRod', 'DarkGray', 'DarkGrey', 'DarkGreen', 'DarkKhaki', 'DarkMagenta', 'DarkOliveGreen', 'Darkorange', 'DarkOrchid', 'DarkRed', 'DarkSalmon', 'DarkSeaGreen', 'DarkSlateBlue', 'DarkSlateGray', 'DarkSlateGrey', 'DarkTurquoise', 'DarkViolet', 'DeepPink', 'DeepSkyBlue', 'DimGray', 'DimGrey', 'DodgerBlue', 'FireBrick', 'FloralWhite', 'ForestGreen', 'Fuchsia', 'Gainsboro', 'GhostWhite', 'Gold', 'GoldenRod', 'Gray', 'Grey', 'Green', 'GreenYellow', 'HoneyDew', 'HotPink', 'IndianRed', 'Indigo', 'Ivory', 'Khaki', 'Lavender', 'LavenderBlush', 'LawnGreen', 'LemonChiffon', 'LightBlue', 'LightCoral', 'LightCyan', 'LightGoldenRodYellow', 'LightGray', 'LightGrey', 'LightGreen', 'LightPink
@rijkvanzanten
rijkvanzanten / convert-range.js
Created March 25, 2017 16:20
Convert number from one range to another
/**
* Convert value from one range to another
* @param {Number} value value to convert
* @param {Object} oldRange min, max of values range
* @param {Object} newRange min, max of desired range
* @return {Number} value converted to other range
*/
function convertRange(value, oldRange, newRange) {
return ((value - oldRange.min) * (newRange.max - newRange.min)) / (oldRange.max - oldRange.min) + newRange.min;
}
@rijkvanzanten
rijkvanzanten / keycode-map.js
Created February 22, 2017 16:37
Commonly used keycodes in an object.
const keyCodeMap = {
48: '0', 49: '1', 50: '2', 51: '3', 52: '4', 53: '5', 54: '6', 55: '7', 56: '8', 57: '9', 59: ';',
65: 'a', 66: 'b', 67: 'c', 68: 'd', 69: 'e', 70: 'f', 71: 'g', 72: 'h', 73: 'i', 74: 'j', 75: 'k', 76: 'l',
77: 'm', 78: 'n', 79: 'o', 80: 'p', 81: 'q', 82: 'r', 83: 's', 84: 't', 85: 'u', 86: 'v', 87: 'w', 88: 'x', 89: 'y', 90: 'z',
96: '0', 97: '1', 98: '2', 99: '3', 100: '4', 101: '5', 102: '6', 103: '7', 104: '8', 105: '9'
};
// Usage: keyCodeMap[65] === 'a';
@rijkvanzanten
rijkvanzanten / mcrypt-mamp.sh
Created December 25, 2016 20:15
Mcrypt missing fix ~ MAMP
# .bash_profile / .zshrc
# make sure the php version is the version selected in MAMP
export MAMP_PHP=/Applications/MAMP/bin/php/php5.6.10/bin
export PATH="$MAMP_PHP:$PATH"
@rijkvanzanten
rijkvanzanten / formatPrice.js
Last active December 1, 2016 09:34
Nice little function to format prices
function formatPrice(price, currency = 'USD', locale = 'en-US') {
const options = { style: 'currency', currency };
const singles = price / 100;
if(singles % 1 === 0) options.maximumSignificantDigits = 0;
return singles.toLocaleString(locale, options);
}
@rijkvanzanten
rijkvanzanten / months.js
Created November 6, 2016 14:39
arrays of month names
['jan', 'feb', 'maa', 'apr', 'mei', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec'];
@rijkvanzanten
rijkvanzanten / requiredFunctionParameters.js
Created November 2, 2016 10:28
Required function params
const required = function(paramName) {
throw new Error(`${paramName} is required`);
};
const foo = function((param1 = required('param1')), param2) {
// ...
};
foo(); // Error: param1 is required