Instantly share code, notes, and snippets.

🏡
Working from home

# Benny Neugebauer bennycode

🏡
Working from home
Created January 6, 2017 14:30
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 function executeAllPromises(promises) { // Wrap all Promises in a Promise that will always "resolve" var resolvingPromises = promises.map(function(promise) { return new Promise(function(resolve, reject) { var payload = new Array(2); promise.then(function(result) { payload[0] = result; }) .catch(function(error) { payload[1] = error;
Last active March 19, 2017 13:46
Fibonacci numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 // Fibonacci numbers /* extended version */ function f(n) { if (n === 0) { return 0; } else if (n === 1) { return 1; } else { return f(n - 1) + f(n - 2);
Created March 20, 2017 10:17
Permutation in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 const Combinatorics = require('js-combinatorics'); const n = ['A', 'B', 'C', 'D']; const k = 2; const generator = Combinatorics.bigCombination(n, k); const combinations = generator.toArray().length; console.log(`Possible combinations: \${combinations}`); // "Possible combinations: 6"
Created March 20, 2017 14:06
Using "winston"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 const winston = require('winston'); // v2.3.1 winston.configure({ transports: [ new (winston.transports.File)({ filename: './data/test.txt', handleExceptions: true, json: false, level: 'silly', maxFiles: 5,
Created March 26, 2017 21:07
Get dynamic page content with PhantomJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 const phantom = require('phantom'); /** * Acts like a web browser and gets page content (also from dynamic HTML pages). * @param {string} url - Page URL * @returns {Promise.} Resolves with the page content. */ module.exports = async function(url) { const instance = await phantom.create(); const page = await instance.createPage();
Created October 1, 2017 18:20
Blinking LEDs example (BeagleBone Black Wireless)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 #!/usr/bin/env node 'use strict'; const b = require('bonescript'); console.log('VERSION', 2); console.log(`Working directory of the Node.js process: \${process.cwd()}`); const leds = ['USR0', 'USR1', 'USR2', 'USR3', 'P9_14'];
Created December 2, 2017 15:17
Delayed Promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 const reruns = 10; const delayedPromise = (iterations) => { return new Promise((resolve) => { const delay = 1000; setTimeout(() => { if (iterations > 0) { iterations -= 1; console.log('Iteration', iterations); delayedPromise(iterations).then(resolve);
Created December 9, 2017 21:05
Node.js Prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 const inquirer = require('inquirer'); const questions = [{ default: false, message: 'Is this correct?', name: 'isCorrect', type: 'confirm', }]; inquirer.prompt(questions)
Created December 14, 2017 13:12
Testing form input elements with enzyme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 import React from 'react'; // 16.2.0 import {mount} from 'enzyme'; // 3.2.0 it('does something', () => { class TestInput extends React.Component { constructor(props) { super(props); this.state = { inputValue: 'before', };
Created March 20, 2017 13:15
Generate all possible words with a given set of characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 function allPossibleCombinations(input, fixedLength, currentCombination) { if (currentCombination.length == fixedLength) { return [currentCombination]; } const combinations = []; for (let i = 0; i < input.length; i++) { combinations.push(...allPossibleCombinations(input, fixedLength, currentCombination + input[i])); }