Skip to content

Instantly share code, notes, and snippets.

View bennycode's full-sized avatar
🏡
Working from home

Benny Neugebauer bennycode

🏡
Working from home
View GitHub Profile
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 / dexie-auto-increment.html
Last active May 25, 2020 20:41
Auto incremental primary keys with Dexie
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="bower_components/dexie/dist/dexie.js"></script>
</head>
<body>
<script>
@bennycode
bennycode / fibonacci.js
Last active March 19, 2017 13:46
Fibonacci numbers
// 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 March 20, 2017 10:17
Permutation in JavaScript
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 / allPossibleCombinations.js
Created March 20, 2017 13:15
Generate all possible words with a given set of 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]));
}
@bennycode
bennycode / winston.js
Created March 20, 2017 14:06
Using "winston"
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 March 26, 2017 21:07
Get dynamic page content with PhantomJS
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 October 1, 2017 18:20
Blinking LEDs example (BeagleBone Black Wireless)
#!/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'];
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);
@bennycode
bennycode / node-prompt.js
Created December 9, 2017 21:05
Node.js Prompt
const inquirer = require('inquirer');
const questions = [{
default: false,
message: 'Is this correct?',
name: 'isCorrect',
type: 'confirm',
}];
inquirer.prompt(questions)