Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael Jaser meaku

View GitHub Profile
@meaku
meaku / params.js
Created Apr 21, 2020
something qs.stringify with indice style for UrlSearchParams
View params.js
function appendObject(params, obj, prefix) {
for (let key of Object.keys(obj)) {
if (typeof obj[key] === "object") {
appendObject(params, obj[key], prefix ? `${prefix}[${key}]` : key);
} else {
params.append(prefix ? `${prefix}[${key}]` : key, obj[key]);
}
}
return params;
View leds.js
//PIN 12 (BCM18)
const ws281x = require("rpi-ws281x-native");
const numLeds = 10;
ws281x.init(numLeds);
let pixelData = new Uint32Array(numLeds);
pixelData = [
[255,0,0],
View .spamfilter
# set default Maildir
MAILDIR="$HOME/Maildir"
# check if we're called from a .qmail-EXT instead of .qmail
import EXT
if ( $EXT )
{
# does a vmailmgr user named $EXT exist? if yes, deliver mail to his Maildir instead
CHECKMAILDIR = `dumpvuser $EXT | grep '^Directory' | awk '{ print $2 }'`
if ( $CHECKMAILDIR )
View pi-temp.js
"use strict";
//hardware modules
const Raspi = require("raspi-io");
const five = require("johnny-five");
const board = new five.Board({
io: new Raspi()
});
//networking modules
View sim
"use strict";
const performance = window.performance;
function streamedPolling(baseUrl, onItem) {
return fetch("https://" + baseUrl + "/streamed-polling")
.then(response => {
const decoder = new TextDecoder();
const reader = response.body.getReader();
let buffer = "";
View blueman.js
"use strict";
/**
* convert blueprint to a postman collection
* pretty much a dirty port of https://github.com/pixelfusion/blueman
*/
var fs = require("fs"),
path = require("path"),
inspect = require("util").inspect,
View koaflow.js
function failing() {
return new Promise((resolve, reject) => {
setTimeout(function() {
reject(new Error("failed"));
},10);
});
}
View Promise Best Practices.md

Passing values

Problem: Promises resolve only with a single value. If you want to pass on the result of PromiseCall1 to PromiseCall2, you have different options:

Nesting

You can nest a .then() for Promise Call 2 and return both value from the scope.

  • Nesting makes it harder to read and should be avoided
@meaku
meaku / BunyanDynamo.js
Created Aug 4, 2015
A simple dynamodb stream for bunyan logging each line
View BunyanDynamo.js
"use strict";
var util = require("util");
var Writable = require("stream").Writable;
var model = require("../db/dynamodb");
/**
* Log Stream
*
@meaku
meaku / fetchSSHKeys.js
Last active Aug 29, 2015
fetch ssh keys
View fetchSSHKeys.js
"use strict";
var request = require("request");
function fetchKeys(userName) {
return new Promise(function (resolve, reject) {
request(`https://github.com/${userName}.keys`, function (err, res, body) {
if (err) {
reject(err);