Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
alexhawkins / CoolJavaScriptCode.js
Last active November 6, 2017 09:30
Array iteration with async/await, Multi-dimensional array flattening, Object generation from Array with es6, remove duplicates from array of large objects
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 June 29, 2017 20:44
sweet Relay Modern Code for Refetching + Pagination
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">
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,
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,
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(() => {
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 September 13, 2016 04:53
Pretty React Relay Code
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 September 13, 2016 04:49
Pretty React Relay Code
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 June 7, 2016 14:58
The Price Equation: Creating the Optimal Society
#' @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
@alexhawkins
alexhawkins / checkboxToggling.js
Created August 27, 2015 20:24
cool lodash code
/* 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 = [];