Skip to content

Instantly share code, notes, and snippets.

View mediaupstream's full-sized avatar
🍉

Derek Anderson mediaupstream

🍉
View GitHub Profile
const isDeliverable = async address => {
if (disableEcommerce) {
return false
}
const api = Api.init()
const { deliverable } = await api.post(
'/location?delivery_type=all',
{
...address,
tentative: undefined
function parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\n\[\033[38;5;247m\]\w\[\033[34m\]\$(parse_git_branch)\n🐻 \[\033[0m\]"
@mediaupstream
mediaupstream / check-page-visibility.js
Last active March 22, 2018 16:54
Check if the current page is hidden or active
function checkPageVisibility() {
let eventName = 'visibilitychange'
let hiddenProp = 'hidden'
// set proper event name for other browsers
if (typeof document.msHidden !== 'undefined') {
hiddenProp = 'msHidden'
eventName = 'msvisibilitychange'
} else if (typeof document.webkitHidden !== "undefined") {
hiddenProp = 'webkitHidden'
// compose fns together
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)))
// If `fn` evaluates to true call fn2 passing args through
const middleware = fn => fn2 => (...args) => fn(...args) && fn2(...args)
//
// Example usage and tests
//
// It's composable
const compose = (a,b) => v => a(b(v))
const isValid = valid => fn => (...p) => valid(...p) && fn(...p)
const hasTwoParams = isValid((...params) => params.length === 2)
const firstParamIsArray = isValid(a => Array.isArray(a))
const hasTwoFirstArray = compose(hasTwoParams, firstParamIsArray)
const myValidatedFn = hasTwoFirstArray((a, b) => console.log('It worked', a, b))
@mediaupstream
mediaupstream / index.js
Last active March 2, 2018 17:25
Download csv in Node.js
app.get('/csv', (req, res) => {
let data = "name,age,status\nderek,30,online\nsarah,28,offline\nDan,36,online"
let filename = 'cool.csv'
res.setHeader('Content-disposition', `attachment; filename=${filename}`)
res.set('Content-Type', 'text/csv')
res.send(data)
})
const express = require('express')
const request = require('request')
// APP 1 (the proxy server)
const app1 = express()
app.use('/proxy', (req, res) => {
let target = req.url.replace('/', 'http://')
req.pipe(request(target)).pipe(res)
@mediaupstream
mediaupstream / repeat.js
Created February 25, 2018 03:46
repeat a function n number of times.
function repeat(n = 1) {
let i = 0;
return fn => {
while(n--) {
i++;
fn(i)
}
}
}
@mediaupstream
mediaupstream / bitmask-crud.js
Last active November 8, 2021 12:34
Store CRUD settings in 2 bytes with bitmasks
const CREATE = 0
const READ = 1
const UPDATE = 2
const DELETE = 3
const add = (a, b) => a |= (1 << b)
const remove = (a, b) => a &= ~(1 << b)
const has = (a, b) => ((a & (1 << b)) > 0)
const bin = (a) => (s).toString(2)
https://twitone.bandcamp.com/album/hi-hat-club-vol-1-testiculo-y-uno