Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 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 / 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');