Skip to content

Instantly share code, notes, and snippets.

View adamrights's full-sized avatar

Adam Bedell adamrights

View GitHub Profile
@samthor
samthor / code.js
Last active July 18, 2022 11:02
Async cancellable promises
// nb. This code is available in an ES module of Promise helpers, here:
// https://github.com/samthor/promises
// symbol returned to indicate that a call was cancelled
export const takeoverSymbol = Symbol('takeover');
/**
* Accepts a generator function, which yields Promises, and converts it to an async function
* that cancels any previous calls.
*/
@tinovyatkin
tinovyatkin / loadFont.js
Last active November 17, 2022 23:09
My function to async load web fonts (real WOFF and WOFF2, not base64 behemots), cache them to localStorage and reuse on next visits
/**
* This function load font from web, stores it in localStorage and reuses on next page loads
* Different to everything I found on the internet due to following facts
* 1. Checks if the font is installed in the system and spend no time if it is
* 2. Loads actual WOFF or WOFF2 fonts (if supported) and not bit base64 encoded version - so it faster
* 3. Uses fetch API and FontFace API if available - it's cool!
* 4. Return promises and uses jQuery for cases where native promises probably not available
*
* @param fontName (Field for font-face)
* @param fontUrl (full URL but without .woff nor .woff2 extensions - format will be selected automatically)
@yoavweiss
yoavweiss / preload_feature_detection.js
Last active July 24, 2020 19:31
Preload feature detection
var DOMTokenListSupports = function(tokenList, token) {
if (!tokenList || !tokenList.supports) {
return;
}
try {
return tokenList.supports(token);
} catch (e) {
if (e instanceof TypeError) {
console.log("The DOMTokenList doesn't have a supported tokens list");
} else {
@adamrights
adamrights / Enum.es6.js
Last active August 26, 2015 02:14 — forked from xmlking/Enum.es6.js
JavaScript Enums with ES6, Type Checking and Immutability
export class EnumSymbol {
sym = Symbol.for(name);
value: number;
description: string;
constructor(name: string, {value, description}) {
if(!Object.is(value, undefined)) this.value = value;
if(description) this.description = description;
@alexggordon
alexggordon / big-query-reddit.sql
Last active August 29, 2015 14:24
Big Query Most Downvoted Reddit Comment
SELECT score,
subreddit,
link_id,
body,
author
FROM [fh-bigquery:reddit_comments.2007],
[fh-bigquery:reddit_comments.2008],
[fh-bigquery:reddit_comments.2009],
[fh-bigquery:reddit_comments.2010],
[fh-bigquery:reddit_comments.2011],
#!/bin/bash
# CentOS rbenv system wide installation script
# Forked from https://gist.github.com/1237417
# Installs rbenv system wide on CentOS 5/6, also allows single user installs.
# Install pre-requirements
yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel \
make bzip2 autoconf automake libtool bison iconv-devel git-core
@sebmarkbage
sebmarkbage / react_legacyfactory.md
Last active March 15, 2020 00:32
Use a factory or JSX

React Element Factories and JSX

You probably came here because your code is calling your component as a plain function call. This is now deprecated:

var MyComponent = require('MyComponent');

function render() {
 return MyComponent({ foo: 'bar' }); // WARNING
@xmlking
xmlking / Enum.es6.js
Last active June 25, 2019 18:09
JavaScript Enums with ES6, Type Checking and Immutability
export class EnumSymbol {
sym = Symbol.for(name);
value: number;
description: string;
constructor(name: string, {value, description}) {
if(!Object.is(value, undefined)) this.value = value;
if(description) this.description = description;
/** @jsx React.DOM */
var React = require("react");
var Lightbox = React.createClass({
render: function() {
return (
<div>
<div className="lightbox-bg"></div>
<div className="lightbox">
<div className="lightbox-wrap">
@jimmyislive
jimmyislive / api_module.js
Last active January 3, 2018 17:45
Single Page App components
var cfg = require('../config').Config,
qs = require('querystring'),
request = require('request');
request = request.defaults({jar: true})
exports.awesome = function(req, res){
headers = {};