Skip to content

Instantly share code, notes, and snippets.

View KhalilZaidoun's full-sized avatar

Khalil Zaidoun KhalilZaidoun

View GitHub Profile
@KhalilZaidoun
KhalilZaidoun / isDomElement.js
Created April 20, 2018 08:02
Checks if the passed element is dom object or not
/**
* Checks if the passed element is dom object or not
* @param element
* @returns {boolean}
*/
function isDomElement(element) {
return element && typeof element === 'object' && 'nodeType' in element;
};
@KhalilZaidoun
KhalilZaidoun / responsive.js
Created March 29, 2018 10:35
Media templates utility for styled-components library
import { css } from 'styled-components';
const sizes = {
big: 1600,
desktop: 1024,
laptop: 768,
tablet: 430,
};
// taken from: https://github.com/styled-components/styled-components/blob/master/docs/tips-and-tricks.md#media-templates
@KhalilZaidoun
KhalilZaidoun / isSVGElement.js
Created March 22, 2018 11:43
Verify element is a SVG element
const elements = [
'animate',
'circle',
'defs',
'ellipse',
'g',
'line',
'linearGradient',
'mask',
'path',
@KhalilZaidoun
KhalilZaidoun / SingletonClass.js
Last active March 19, 2018 14:45
Singleton ES6 pattern using Symbol to ensure singularity
// https://stackoverflow.com/a/26227662
const singleton = Symbol();
const singletonEnforcer = Symbol()
class SingletonClass {
constructor(enforcer) {
if(enforcer != singletonEnforcer) throw "Cannot construct singleton";
}
@KhalilZaidoun
KhalilZaidoun / indexed-db-helpers.js
Created March 18, 2018 13:08
indexed-db helpers methods
import indexedDB from './indexedDB'
if (!indexedDB) {
// console.log("Your browser does not support a stable version of IndexedDB. Some features will not be available.")
}
const DATABASE_NAME = 'db_name';
const READ_WRITE_PERMISSION = 'readwrite';
@KhalilZaidoun
KhalilZaidoun / Toggle.js
Last active March 16, 2018 15:52
React Toggle switch with only inline style !
import styled from 'styled-components';
const Toggle = styled.input`
-moz-appearance: none;
-webkit-appearance: none;
-o-appearance: none;
position: relative;
height: 20px;
width: 40px;
border-radius: 10px;
@KhalilZaidoun
KhalilZaidoun / forEachPromise.js
Created February 25, 2018 12:49
Apply asynchronous function to each item in array
/**
*
* @param items An array of items.
* @param fn A function that accepts an item from the array and returns a promise.
* @returns {Promise}
*/
export default function forEachPromise(items = [], fn = Promise.resolve) {
return items.reduce(
(promise, item) => promise.then(() => fn(item)),
Promise.resolve()
@KhalilZaidoun
KhalilZaidoun / timeoutPromise.js
Created December 27, 2017 17:24
Timeout Promise wrapper
// Timeout Promise wrapper
export default function timeoutPromise(timeout, err, promise) {
return new Promise(function(resolve,reject) {
promise.then(resolve,reject);
setTimeout(reject.bind(null,err), timeout);
});
}
/*
@KhalilZaidoun
KhalilZaidoun / promiseWhen.js
Last active November 29, 2017 13:05
Function to wait until condition is met before resolving promise
export default function promiseWhen(condition, timeout = 2000) {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject();
}, timeout);
const loop = () => {
if (condition()) {
return resolve();
}
@KhalilZaidoun
KhalilZaidoun / chrome_unsecure_mode.md
Created November 15, 2017 09:31
Run chrome in insecure mode

Ubuntu

google-chrome --args --disable-web-security --allow-running-insecure-content

OSX

open -a Google\ Chrome --args --disable-web-security --allow-running-insecure-content