Skip to content

Instantly share code, notes, and snippets.

View gunar's full-sized avatar

Gunar Gessner gunar

View GitHub Profile
@gunar
gunar / functionalObjSum.js
Last active April 29, 2018 17:13
Function to sum an object values using recursion
// This is super slow though: http://jsperf.com/summing-objects/2
var sumObj = function (object) {
if (Object.keys(object).length) {
var firstKey = Object.keys(object)[0];
var clone = Object.assign({}, object);
delete clone[firstKey];
return parseInt(object[firstKey]) + sumObj(clone);
}
@gunar
gunar / recursiveDir.js
Last active March 6, 2016 00:21
Comparison of recursive directory listing methods in JavaScript
/*
* Comparison of methods to list all files in a directory
* and subdirectories ("recursive directory walk").
*
* gunargessner.com 2015-12-25
*
*/
var fs = require('fs');
var fsPath = require('path');
var _ = require('highland');
@gunar
gunar / Promise.race.js
Last active February 1, 2016 11:26
Promise cancelation
const myPromise = new Promise((resolve, reject) => {
setTimeout(resolve, 1000, 42);
});
var cancel;
const myCancelator = new Promise((resolve, reject) => {
cancel = reject;
});
Promise.race([
@gunar
gunar / rejectIfNil.js
Last active October 25, 2016 01:47
Handling empty promises
// What is your opinion on the use of this function?
function rejectIfNil(msg) {
return payload => {
if (!payload) return Promise.reject(new Error(msg))
return payload
}
}
function update(_id) {
return User
@gunar
gunar / curryNamed.js
Last active November 11, 2019 22:20
Currying Functions with Named Parameters in JavaScript
/**
* Currying Functions with Named Parameters.
* @gunar, @drboolean, @dtipson 2016
*
* Why does it return a thunk, though?
* Because in JS named arguments are opaque. There is no way of getting a function's named arguments list.
* e.g.
* const foo = function ({ a, b }) { }
* console.log(foo.arguments) // Throws. Ideally would return ['a', 'b'].
*
@gunar
gunar / code.js
Last active April 6, 2024 17:06
Remove Duplicates From GDrive Root
/*
Gunar C. Gessner
@gunar 2016
INSTALL: https://script.google.com/macros/s/AKfycbym7IaTORzJ7LQPcxMx1LV1SQEC6RVGv5tzLOgYS8iQe8XAJxM/exec
After installation, the script will, every 10 minutes, search for all files that have duplicates (shift+z) and remove them from the root directory ("My Drive").
This Google Apps Script webapp aims to solve the problem that when transferring ownership of folders &
files to another account, duplicate files shown in "My Drive".
@gunar
gunar / osho.com.tampermonkey.js
Created October 11, 2016 20:50
Make osho.com selectable again
// ==UserScript==
// @name OSHO.com selectable
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match http://www.osho.com/*
// @grant none
// ==/UserScript==
function addGlobalStyle(css) {
@gunar
gunar / adt.js
Last active October 27, 2016 14:27
Algebraic Data Types
// Inspired by [Brian Lonsdorf - Oh Composable World! - YouTube](https://www.youtube.com/watch?v=SfWR3dKnFIo)
// Inspired by [Mostly adequate guide to FP](https://github.com/MostlyAdequate/mostly-adequate-guide)
const Box = function(x) {
this.__value = x;
}
Box.of = x => new Box(x)
Box.prototype.map = function (f) {
return Box.of(f(this.__value))
}
@gunar
gunar / exceptionsAndPromises.js
Created November 1, 2016 12:20
How to differentiate exceptions from rejections
// 1. Using rejections for both exceptions and rejections
foo(5)
.then(bar)
.catch(err => {
if (!(err instanceof MyError)) {{
// exception!
throw err
}
// else, process business logic error properly
})
@gunar
gunar / cycleSort.js
Created May 4, 2017 02:37
Cycle sort implemented in JavaScript
'use strict'
// ref https://en.wikipedia.org/wiki/Cycle_sort
const cycleSort = array => {
// last item will already be in place
for (let cycleStart = 0; cycleStart < array.length-1; cycleStart++) {
let item = array[cycleStart]
// find where to put the item
let pos = cycleStart