Skip to content

Instantly share code, notes, and snippets.

View cravindra's full-sized avatar
👨‍💻
Focus mode on

Chirag Ravindra cravindra

👨‍💻
Focus mode on
View GitHub Profile
@cravindra
cravindra / cookie.js
Last active April 11, 2019 09:40
Common cookie helpers in ES6
@cravindra
cravindra / vueResponsiveRoute.md
Last active February 10, 2019 15:51
A handy method to serve different views based on the viewport size using VueJS and Vue Router

Dump from my answer on SO: https://stackoverflow.com/a/54617885/2649853

I was looking for a solution for this and came here but I couldn't find what I needed:

  1. Asynchronous imports to only load into the bundle what was needed based on the viewport.
  2. Capability to serve a different layout if the layout was resized
@cravindra
cravindra / kelly_colors.js
Last active September 30, 2018 10:28 — forked from ollieglass/kelly_colors.py
Kenneth Kelly's 22 colors of maximum contrast
// theory - https://eleanormaclure.files.wordpress.com/2011/03/colour-coding.pdf (page 5)
// kelly's colors - https://i.kinja-img.com/gawker-media/image/upload/1015680494325093012.JPG
// hex values - http://hackerspace.kinja.com/iscc-nbs-number-hex-r-g-b-263-f2f3f4-242-243-244-267-22-1665795040
const kellyColors = ['#F2F3F4', '#222222', '#F3C300', '#875692', '#F38400', '#A1CAF1', '#BE0032', '#C2B280', '#848482', '#008856', '#E68FAC', '#0067A5', '#F99379', '#604E97', '#F6A600', '#B3446C', '#DCD300', '#882D17', '#8DB600', '#654522', '#E25822', '#2B3D26']
// https://sashat.me/2017/01/11/list-of-20-simple-distinct-colors/
const LABEL_COLORS = ['#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4', '#46f0f0', '#f032e6', '#bcf60c', '#fabebe', '#008080', '#e6beff', '#9a6324', '#fffac8', '#800000', '#aaffc3', '#808000', '#ffd8b1', '#000075', '#808080', '#ffffff', '#000000']
/**
* Helper to inject the YouTube iFrame API script and initialise a YouTube video
* @param container {DOMElement | string} A Dom element or a string which is the id of the element to make a youtube video
* @param [playerOptions={}] {Object} An object as defined by the [YT iFrame API](https://developers.google.com/youtube/player_parameters)
*/
export default function inject(container, playerOptions={}) {
// Extract Player vars if available
const playerVars = {...(playerOptions.playerVars || {})};
delete playerOptions.playerVars;
@cravindra
cravindra / facebook-auth.js
Created November 7, 2017 12:01
Simple wrapper which uses lasso-loader to enable facebook oauth asynchronously
const lassoLoader = require('lasso-loader');
/* globals FB, _sdk */
/**
* Performs a one time OAuth 2.0 authorization.
* Depending on the parameters used, this will open a popup to the Google sign-in flow
* or try to load the requested response silently, without user interaction.
*/
function authenticate() {
return new Promise(function (resolve, reject) {
@cravindra
cravindra / google-auth.js
Last active November 7, 2017 11:59
A wrapper which uses lasso-loader to enable google oauth flow asynchronously
const lassoLoader = require('lasso-loader');
/* globals gapi, _sdk */
/**
* Performs a one time OAuth 2.0 authorization.
* Depending on the parameters used, this will open a popup to the Google sign-in flow
* or try to load the requested response silently, without user interaction.
*/
function authenticate() {
return new Promise(function (resolve, reject) {
@cravindra
cravindra / browser-ajax.js
Last active November 7, 2017 12:03
A wrapper around fetch to do common ajax operations on the server and client
require('../../vendor/js/fetch');
/**
* The defaultOptions properties used in fetching API
* @typedef {Object} defaultOptions
* @property {string} method - Indicates the http request method
* @property {string} url - request Url
* @property {Object|null} body - holds the http request body information to be used in post or put request
*/
const fetchConfig = {
@cravindra
cravindra / nocookie-session.js
Created November 7, 2017 11:53
Middleware arrangement for using Express Session with session ID which can be overriden by a URL query parameter
const express = require('express');
const router = express.Router();
const session = require('express-session');
const MemcachedStore = require('connect-memcached')(session);
const _ = require('lodash');
const utils = require('../../services/application/utils');
const config = utils.getConfiguration();
/**
* Middleware which goes in before Express Session to enable overriding session ID assignment through a query parameter
* or a request header `uid`
@cravindra
cravindra / request.js
Created November 7, 2017 11:48
Express Middleware to augment the Request and Response Object with some helpful methods and attributes
/**
* Middleware to add the following helpers to the req object
* 1. wantsJSON
* 2. getAllParams
* @param req - The Express Request Object for the current request
* @param res - The Express Response Object for the current request
* @param next
*/
function enhanceReq(req, res, next) {
@cravindra
cravindra / youtube.js
Created August 30, 2017 11:35
Framework code to instantiate one or more youtube videos on a page
/**
* YouTube API helper for instantiating multiple videos on a page
* https://developers.google.com/youtube/iframe_api_reference
*/
/**
* Helper to queue a youtube video to be instantiated with the YouTube API
* @param {*} videoId The YouTube Video ID
* @param {*} elementId The Element ID to instantiate as a YouTube player
* @param {*} callback The callback to be called once the player object is available