Skip to content

Instantly share code, notes, and snippets.

@royhowie
royhowie / Gallery.jsx
Last active December 31, 2016 19:12
/gallery/:id
@royhowie
royhowie / pyramid.js
Last active June 14, 2016 04:19
avoiding the pyramid of doom
// http://stackoverflow.com/questions/37801654/how-to-know-when-all-promises-are-resolved-in-a-dynamic-iterable-parameter
function userLogIn () {
return $http.get('/login/user/password')
.then(data => data.loggedOk ? $http.get('/checkIfIsAdmin') : Promise.reject())
.then(data => data.isAdmin ? $http.get('/getUserName') : Promise.reject())
.then(data => data.hasUserName ? $http.get('/getDate') : Promise.reject())
}
userLogIn().then(() => {
// public key: [23, 55]
// private key: [7, 55]
class Decrypter {
constructor (priv, pub, mod) {
this.private = priv
this.public = pub
this.mod = mod
}
@royhowie
royhowie / flight.c
Last active October 16, 2015 21:41
flight.c
// Roy Howie
// flight.c
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MIN_LOWER_BOUND 0
#define MAX_LOWER_BOUND 80
@royhowie
royhowie / velocity.html
Created October 6, 2015 21:34
velocity.js usage
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test1 Velocity</title>
<script src="./velocity.js"></script>
<script>
function press () {
Velocity(document.getElementById("sq"), { opacity: 0.3}, { duration: 2000, easing: "ease-in-out", loop: 5 });
return false;
}
import Promise from 'bluebird'
let fs = Promise.promisifyAll(require('fs'))
let exec = Promise.promisify(require('child_process').exec)
function mapCommands (file) {
return file.split('\n').reduce((p, c) => {
let separation = c.indexOf(':')
p[c.slice(3, separation)] = c.slice(separation + 2)
return p
@royhowie
royhowie / a.js
Created July 26, 2015 03:07
which one?
function addSession (subject, calEvent) {
let index = getIndex(this.sessions, (session) => session.subject === subject.id)
if (index === -1) {
this.sessions.push({
subject: subject.id,
hours: 1,
events: calEvent ? [ calEvent ] : []
})
} else {
@royhowie
royhowie / a.js
Created July 13, 2015 05:22
pagination page generator
const PAGES_ON_ENDS = 3
function getPages (currentPage, perPage, total) {
let maxPage = Math.floor(total / perPage)
let lower = Math.max(currentPage - PAGES_ON_ENDS, 0)
let upper = Math.min(currentPage + PAGES_ON_ENDS, maxPage)
return Array.from(Array(upper - lower + 1), (_, i) => lower + i)
}
@royhowie
royhowie / fibonacci.js
Last active August 29, 2015 14:24
fibonacci sequence with generators
function* fib () {
var a = 0, b = 1, c;
yield 0
yield 1
while (true) {
c = b + a
a = b
b = c
yield c
}
@royhowie
royhowie / pagination-es6.js
Last active August 29, 2015 14:24
pagination in es6 vs es7
function paginate (title, baseQuery, baseUrl, errorRedirect, methods) {
if (!baseQuery.count || !baseQuery.paginateSort) {
throw new Error('baseQuery is missing a count or paginateSort method! - paginate.js')
} else if (!methods.length) {
throw new Error('methods must be an array of methods to apply to each document! - paginate.js')
}
return (req, res) => {
let documentsPerPage = Math.min(parseInt(req.query.perpage) || 15, 100)
let page = Math.max(parseInt(req.query.page), 0)