Skip to content

Instantly share code, notes, and snippets.

@pizzarob
pizzarob / 01_DropZone.jsx
Last active November 14, 2017 08:28
HTML5 Drag and Drop File Upload React Component
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
const ANIMATION_DURATION = 1000;
class BatchDropZone extends React.Component {
static propTypes = {
@pizzarob
pizzarob / s3-client.js
Created August 16, 2016 20:21
Client Side Image Upload to AWS
const dataURItoBlob = (dataURI, type) => {
const byteString = atob(dataURI.split(',')[1]);
const ab = new ArrayBuffer(byteString.length);
const ia = new Uint8Array(ab);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ab], {
type
@pizzarob
pizzarob / MongooseSingletonModel.js
Last active December 4, 2017 17:48
Mongoose Singleton Model
schema.statics = {
getSingleton: function (cb) {
this.findOne()
.sort({ updated: -1 })
.limit(1)
.exec((err, model) => {
if (err) {
cb(err, null);
} else if (!model) {
cb(err, new this());
@pizzarob
pizzarob / xhr.js
Created August 21, 2016 18:45
Helper Functions for Making XHR Requests in JavaScript
function post(url, data) {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.addEventListener('load', () => {
let { response, status } = xhr;
let res = JSON.parse(response);
if(status >= 200 && status < 400){
function paginate(query = {}, options = {}, callback) {
const limit = options.limit || 10;
let page = options.page || 1;
const countQuery = this.find(query).count();
return new Promise(resolve => {
countQuery.exec().then(count => {
const pages = Math.ceil(count / limit) || 1;
@pizzarob
pizzarob / Product of consecutive Fibonacci numbers.js
Last active October 15, 2016 00:54
Given a number, say prod (for product), we search two Fibonacci numbers F(n) and F(n+1) verifying F(n) * F(n+1) = prod.
// Given a number, say prod (for product), we search two Fibonacci numbers F(n) and F(n+1) verifying F(n) * F(n+1) = prod
// My solution to the code wars challenge https://www.codewars.com/kata/5541f58a944b85ce6d00006a/train/javascript
// Not used for this, but here's some code to test if a number is in the Fibonacci sequence.
// const isPerfectSquare = num => {
// const sq = Math.sqrt(num);
// return sq * sq === num;
// }
// const isFib = num => {
// return isPerfectSquare(5 * num * num + 4) || isPerfectSquare(5 * num * num - 4);
const deepEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);
// Initialize cargo using the development network. This will
// tell Cargo to use it's contracts on the Rinkeby network.
const cargo = new Cargo({
network: 'development',
});
const run = async () => {
// This will fetch Cargo contract information
// so we can begin interacting with those contracts when we are ready.
await cargo.init();
if (isEnabled) {
// Get all the vendors in your crate. The creator of the crate (you) is
// added as a default vendor.
const { data: vendors } = await cargo.api.getCrateVendors(CRATE_ID);
// Map through each vendor and get the token contracts they created.
const contractResponses = await Promise.all(
vendors.map(({ vendorId }) => cargo.api.getVendorTokenContracts(vendorId))
);
// Update this with your crate ID
const CRATE_ID = '98';
// Initialize cargo using the development network. This will
// tell Cargo to use it's contracts on the Rinkeby network.
const cargo = new Cargo({
network: 'development',
});
const run = async () => {