Skip to content

Instantly share code, notes, and snippets.

@moodysalem
moodysalem / rasterize-letter-pdf.js
Created March 2, 2016 16:24
Render a page to PDF using phantomjs 2.1
"use strict";
var page = require('webpage').create(),
system = require('system'),
address, output, size;
if (system.args.length !== 3) {
console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]');
console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
console.log(' image (png/jpg output) examples: "1920px" entire page, window width 1920px');
console.log(' "800px*600px" window, clipped to 800x600');
@moodysalem
moodysalem / React Autoresize Textarea
Last active March 17, 2016 16:57
A React TextArea component that automatically resizes to its content
React.createClass({
displayName: 'Autoresizing TextArea',
propTypes: {
height: React.PropTypes.number.isRequired,
onChangeHeight: React.PropTypes.func.isRequired
},
getDefaultProps: function () {
return {
@moodysalem
moodysalem / queuePromises.jsx
Created November 7, 2016 21:11
JS function that calls a set of functions that generate promises (called generators) in batches and resolves when they have all completed
import _ from 'underscore';
/**
* Takes an array of generators, i.e. functions that return promises, and calls them such that there are only ever
* 5 requests that are waiting on responses. Returns a promise that resolves to all the resulting promises only after
* they have all been executed
*
* @param generators array of functions that
* @param batchSize max number of requests to execute at a time
* @param throttle how often the queue is checked for more requests to process
@moodysalem
moodysalem / react-portal.jsx
Last active November 10, 2016 21:02
Render children to a div tag at the end of the body
import React, { Children, PureComponent, PropTypes } from 'react';
import { unmountComponentAtNode, unstable_renderSubtreeIntoContainer } from 'react-dom';
// renders children at the end of the body
export default class Portal extends PureComponent {
static propTypes = {
children: PropTypes.node.isRequired
};
componentDidMount() {
@moodysalem
moodysalem / app.jsx
Created January 22, 2017 19:48
Help with rendering reddit comment
import React, { Component } from "react";
const Entry = ({ entry: { name, game } }) => (
<div>
<h3>{name}</h3>
<p>{game}</p>
</div>
);
const CHANNELS = [
@moodysalem
moodysalem / render-component-to-print-html.jsx
Created November 22, 2016 16:09
Render a react component to an html document that shares all the links and styles for printing, and convert it to a data URI for window.open
import React, { PureComponent, PropTypes } from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import { Base64 } from 'js-base64';
import Promise from 'bluebird';
const htmlDocTemplate = ({ links, pageStyles, body, title = 'Print' }) => (`
<html>
<head>
<title>${title}</title>
${links}
@moodysalem
moodysalem / SearchBuilder.java
Last active March 15, 2017 20:41
Used for building ElasticSearch query objects
package com.fastmodelsports.social.lambda.util.es;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
public abstract class SearchBuilder {
public static JsonArray sortOn(final String attribute, final boolean desc) {
final JsonObject sort = new JsonObject(),
attr = new JsonObject();
@moodysalem
moodysalem / swapfile.config
Last active March 21, 2017 13:32
ebextensions swapfile creation script ec2 swap
commands:
000_dd:
test: test ! -e /swapfile
command: dd if=/dev/zero of=/swapfile bs=1M count=2048 && chmod 600 /swapfile
001_mkswap:
command: mkswap /swapfile
ignoreErrors: true
002_swapon:
command: swapon /swapfile
ignoreErrors: true
@moodysalem
moodysalem / swagger-definitions.ts
Created February 8, 2018 19:51
TypeScript type interfaces for Swagger Models
interface IDataType {
type: 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'object';
readOnly?: true;
writeOnly?: true;
}
interface IStringType extends IDataType {
type: 'string';
format?: 'date' | 'date-time' | 'password' | 'byte' | 'binary' | 'uuid' | 'email' | 'uri' | 'hostname' | 'ipv4' | 'ipv6';
@moodysalem
moodysalem / prevent-duplicate-async.test.ts
Created December 22, 2018 04:57
TypeScript: Higher order function to prevent duplicate calls to an async function, i.e. memoize
import { expect } from 'chai';
import preventDuplicateAsync from '../src/util/prevent-duplicate-async';
function testPromise<T>(): { promise: Promise<T>; resolve: (T) => void; reject: (Error) => void; } {
let rslv, rjct;
const promise = new Promise<T>((resolve, reject) => {
rslv = resolve;
rjct = reject;