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 / isObjectEqual.js
Created October 11, 2016 14:44
Function which checks if two objects are equal
function isEqual(obj1, obj2) {
const props1 = Object.getOwnPropertyNames(obj1);
const props2 = Object.getOwnPropertyNames(obj2);
if(props1.length != props2.length) {
return false;
}
for(let i = 0; i < props1.length; i++) {
const propName = props1[i];
if(obj1[propName] !== obj2[propName]) {
@rijkvanzanten
rijkvanzanten / socketMiddleware.js
Created October 19, 2016 15:11
Redux Action Middleware which forwards actions to the server via socket.io when `server: true` is set in action object
const createSocketMiddleware = function(socket, {eventName = 'action'} = {}) {
return ({dispatch}) => {
socket.on(eventName, dispatch);
return (next) => (action) => {
if(action.hasOwnProperty('server')) socket.emit(eventName, action);
return next(action);
};
};
};
@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
@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 / 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 / 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 / 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 / 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 / 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) {
@rijkvanzanten
rijkvanzanten / group-objects-by-date.js
Created June 23, 2017 13:01
Convert array of objects w/ timestamp to object by date
/**
* Convert array of objects w/ timestamp to object by date
* @param {Array} messages Messages to convert
* @return {Object} Messages grouped by date YYYY-MM-DD
*/
function groupMessagesByDate(messages = []) {
const groupedMessages = {};
messages.forEach(message => {
const {timestamp: date} = message;