Skip to content

Instantly share code, notes, and snippets.

Avatar
🏡
Working from home

Benny Neugebauer bennycode

🏡
Working from home
View GitHub Profile
View executeAllPromises
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;
@bennycode
bennycode / fibonacci.js
Last active Mar 19, 2017
Fibonacci numbers
View fibonacci.js
// 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);
@bennycode
bennycode / permutation.js
Created Mar 20, 2017
Permutation in JavaScript
View permutation.js
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"
@bennycode
bennycode / winston.js
Created Mar 20, 2017
Using "winston"
View winston.js
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,
@bennycode
bennycode / getPageContent.js
Created Mar 26, 2017
Get dynamic page content with PhantomJS
View getPageContent.js
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.<string, Error>} Resolves with the page content.
*/
module.exports = async function(url) {
const instance = await phantom.create();
const page = await instance.createPage();
@bennycode
bennycode / bonescript-blinking-leds.js
Created Oct 1, 2017
Blinking LEDs example (BeagleBone Black Wireless)
View bonescript-blinking-leds.js
#!/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'];
View gist:71c4b859c5b277d4660ddc5265a589a1
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);
View node-prompt.js
const inquirer = require('inquirer');
const questions = [{
default: false,
message: 'Is this correct?',
name: 'isCorrect',
type: 'confirm',
}];
inquirer.prompt(questions)
@bennycode
bennycode / TestInput.test.jsx
Created Dec 14, 2017
Testing form input elements with enzyme
View TestInput.test.jsx
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',
};
@bennycode
bennycode / allPossibleCombinations.js
Created Mar 20, 2017
Generate all possible words with a given set of characters
View allPossibleCombinations.js
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]));
}