Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
alexhawkins / CoolJavaScriptCode.js
Last active Nov 6, 2017
Array iteration with async/await, Multi-dimensional array flattening, Object generation from Array with es6, remove duplicates from array of large objects
View CoolJavaScriptCode.js
import { map } from 'p-iteration';
import _uniqBy from 'lodash/uniqBy';
/* Array iteration with async/await */
const getMultipleCategoryProducts = async productCategories => {
return map(productCategories, async category => {
const products = await getProductsByCategory(category);
return products;
});
};
@alexhawkins
alexhawkins / refetchContainerWithRelayModern.jsx
Created Jun 29, 2017
sweet Relay Modern Code for Refetching + Pagination
View refetchContainerWithRelayModern.jsx
import React from 'react';
import PropTypes from 'prop-types';
import { graphql } from 'react-relay';
import { createRefetchContainer, QueryRenderer } from 'react-relay';
import { identifyDataKey, hasNextPage, createDataArray } from '../../../utils/RelayHelpers';
const ProductReviewsList = ({ data, loading, containerWidth, role }) => {
const renderRow = () => (
<Flex flexColumn id="ReviewComments">
<Box className="ProductReviews__divider">
View asyncAwaitReactComponent.js
import React, { PureComponent } from 'react'
const contextTypes = {
router: PropTypes.object.isRequired,
api: PropTypes.object.isRequired,
relay: PropTypes.shape({
variables: PropTypes.shape({
category: PropTypes.string.isRequired,
}).isRequired,
}).isRequired,
View asyncAwaitReactComponent.js
import React, { PureComponent } from 'react'
const contextTypes = {
router: PropTypes.object.isRequired,
api: PropTypes.object.isRequired,
relay: PropTypes.shape({
variables: PropTypes.shape({
category: PropTypes.string.isRequired,
}).isRequired,
}).isRequired,
View async.js
import React from 'react'
class AsyncAwait extends React.Component {
constructor() {
super()
this.state = {}
}
async componentDidMount() {
const res = await this.context.api.fetch('/logout', { method: 'POST' }).then(() => {
View async.js
import React from 'react'
class AsyncAwait extends React.Component {
constructor() {
super()
this.state = {}
}
async componentDidMount() {
const res = await this.context.api.fetch('/logout', { method: 'POST' }).then(() => {
@alexhawkins
alexhawkins / Cart.js
Last active Sep 13, 2016
Pretty React Relay Code
View Cart.js
import React, { PropTypes, Component } from 'react';
import Relay from 'react-relay';
import { Map } from 'immutable';
import debounce from 'lodash/debounce';
import AddToCartMutation from '../../mutations/AddToCartMutation';
import RemoveFromCartMutation from '../../mutations/RemoveFromCartMutation';
import CartEntry from '../cart/CartEntry';
import InputQuantity from '../form/InputQuantity';
@alexhawkins
alexhawkins / Cart.js
Created Sep 13, 2016
Pretty React Relay Code
View Cart.js
import React, { PropTypes } from 'react';
import Relay from 'react-relay';
import { Map } from 'immutable';
import debounce from 'lodash/debounce';
import AddToCartMutation from '../../mutations/AddToCartMutation';
import RemoveFromCartMutation from '../../mutations/RemoveFromCartMutation';
import CartEntry from '../cart/CartEntry';
import InputQuantity from '../form/InputQuantity';
@alexhawkins
alexhawkins / priceEquation.R
Created Jun 7, 2016
The Price Equation: Creating the Optimal Society
View priceEquation.R
#' @param func = a site-by-species "functioning" matrix (cells are values of ecosystem function),
#' @param base = the row name or number of the baseline community; defaults to highest productivity community
#' @param standardize = TRUE, whether results are scaled by the maximum to units (-1, 1),
#' @param avg = TRUE, whether
#' @param avglvl = The top X% of sites that should be used as the baseline and then results averaged, default is top 90%
#' @return data.frame of Price components & summed effects
price = function(func, base = "best", standardize = TRUE, avg = FALSE, avglvl = 0.90) {
# Replace NAs with zeros
View checkboxToggling.js
/* checks to see if item id arrangement in packages has changed */
let compareItemIds = (oldIds, newIds) => {
return _.difference(newIds, oldIds).concat(_.difference(oldIds, newIds));
};
/* gets the item of ids of current packages and orignal packages
and compares them for changes */
let haveItemsChanged = () => {
let curIds = [];