Skip to content

Instantly share code, notes, and snippets.

Avatar
🐛

DominicTobias

🐛
View GitHub Profile
View gist:94e39847361d7e5b360d
var Q = require('q');
var util = require('util');
var request = require('request');
var _ = require('lodash');
var sso = {};
/**
* Redirect to the SSO login page.
*/
@DominicTobias
DominicTobias / conduit.apiquery.js
Created Dec 20, 2013
Send a query to phabricator using node.js
View conduit.apiquery.js
var ApiQuery = function() {
this.http = require('http');
this.q = require('q');
this.Connect = require('./conduit.connect');
this.initialize();
};
ApiQuery.prototype = {
initialize: function() {
View react-authenticate-route.js
/*
Authenticate component using a wrapper
*/
import React, {Component} from 'react';
import { connect } from 'react-redux';
export default function(ComposedComponent) {
class Auth extends Component {
static contextTypes = {
View add.js
function add(...args) {
const sum = args.reduce((prev, curr) => prev + curr, 0);
const ret = add.bind(void 0, sum);
ret.value = ret.valueOf = () => sum;
ret.add = ret;
return ret;
}
console.log(add(1, 2).value() === 3);
console.log(add(1, 2)(3).value() === 6);
View copyToClipboard.js
export const copyToClipboard = (function initClipboardText() {
const id = 'copy-to-clipboard-helper';
const element = document.getElementById(id);
const textarea = element || document.createElement('textarea');
if (!element) {
textarea.id = id;
// Place in top-left corner of screen regardless of scroll position.
textarea.style.position = 'fixed';
textarea.style.top = 0;
@DominicTobias
DominicTobias / express-oauth-example.js
Created Jan 6, 2019
NodeJS OAuth from scratch (using GitHub as an example)
View express-oauth-example.js
// Packages like PassportJS and useless diagrams online make OAuth seem complicated to implement, but in reality it's simple...
const got = require('got');
const jwt = require('jsonwebtoken');
const querystring = require('querystring');
const mins = min => 1000 * 60 * min;
// 1. Direct users browser to this url.
app.get('auth/github', (req, res) => {
const csrfState = Math.random().toString(36).substring(7);
@DominicTobias
DominicTobias / adaptiveComponent.js
Last active Feb 2, 2019
Loading different components at runtime depending on media queries in React
View adaptiveComponent.js
/* globals matchMedia */
import React, { PureComponent } from 'react';
function adaptiveComponent(mediaQueries) {
const firstMatchingQuery = Object.keys(mediaQueries).find(mediaQuery =>
matchMedia(mediaQuery).matches);
if (!firstMatchingQuery) {
throw new Error(`No media query matches found in ${mediaQueries}`);
}
@DominicTobias
DominicTobias / colorScale.md
Created Feb 27, 2019
Javascript percentage colour scale
View colorScale.md
// green to red
const getColor = v => `hsl(${((1 - v) * 120)},100%,50%)`;

// red to green
const getColor = v => `hsl(${((1 - Math.abs(v - 100)) * 120)},100%,50%)`;

Pass in (perccentage / 100).

@DominicTobias
DominicTobias / storage-wrapper.js
Last active Mar 21, 2019
Simple local and session storage wrapper - falls back to memory in the case of Safari Private browser
View storage-wrapper.js
function isAvailable() {
try {
this.storage.setItem('__testFeature', true);
this.storage.removeItem('__testFeature');
return true;
} catch (ex) {
return false;
}
}
@DominicTobias
DominicTobias / bem-class.js
Last active Mar 21, 2019
BEM Class helper
View bem-class.js
/*
BEM Class helper:
const c = makeClass('myparentclass');
className={c`myclass myclass--active`}
becomes: className="myparentclass__myclass myparentclass__myclass--active"
*/
export const makeClass = (cls) =>
(subCls) => subCls[0].split(' ').reduce((acc, s) =>
`${acc}${cls}__${s} `, '').trimRight();