Skip to content

Instantly share code, notes, and snippets.

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 / 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){
@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 / 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 / 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 = {