Skip to content

Instantly share code, notes, and snippets.

View cakesmith's full-sized avatar

Nick LaRosa cakesmith

  • Diameter Health
  • New Jersey
View GitHub Profile
//hijack.js
const http = require("http")
function hijack() {
override(http)
}
function requestLogger(req) {
const log = {
method: req.method || "GET",
@cakesmith
cakesmith / coroutines-and-generators.js
Created January 24, 2021 02:44 — forked from adambene/coroutines-and-generators.js
Coroutines and generators in JavaScript
function* delays() {
let a = yield delay(800, "Hello, I'm an");
console.log(a);
let b = yield delay(400, "async coroutine!");
console.log(b);
}
const coroutine = nextValue => iterator => {
const { done, value } = iterator.next(nextValue);
@cakesmith
cakesmith / sync.js
Created August 17, 2020 16:28
js synchronization primitives
const locked = 1;
const unlocked = 0;
class Mutex {
/**
* Instantiate Mutex.
* If opt_sab is provided, the mutex will use it as a backing array.
* @param {SharedArrayBuffer} opt_sab Optional SharedArrayBuffer.
*/
constructor(opt_sab) {
@cakesmith
cakesmith / timing.js
Created July 30, 2020 17:57
use process.hrtime to time something
const start = process.hrtime();
const duration = process.hrtime(start);
const ms = (parseFloat(`${duration[0]}.${duration[1]}`) * 1000).toFixed(3);
@cakesmith
cakesmith / index.js
Created May 4, 2020 00:06
Cypress test http server
//cypress/plugins/index.js
let server;
module.exports = (on, config) => {
on('task', {
async startTestServer(data) {
if(server) {
await new Promise(resolve => server.close(resolve));
}
const port = 3000;
const ip = '127.0.0.1';
loc, _ := time.LoadLocation("America/New_York")
longForm := "January 02 2006 03:04 pm"
year, month, day := time.Now().Date()
todayAt, err := time.ParseInLocation(longForm, fmt.Sprintf("%v %v %v %v", month, day, year, timeInput), loc)
if err != nil {
fmt.Printf("Error parsing time: %s\n", err.Error())
s.User.SendString(fmt.Sprintf(timeTroubleF, timeInput))
return
@cakesmith
cakesmith / diffSet.js
Created September 30, 2016 17:50
ES6 Diff Set
function diffSet(a, b) {
let x = new Set([...a].filter(n => !b.has(n)));
let y = new Set([...b].filter(n => !a.has(n)));
return new Set([...x, ...y]);
}
@cakesmith
cakesmith / renderbuddy.js
Created September 12, 2016 23:01
Render and return html
/* global phantom */
"use strict";
var port, server, service,
system = require('system');
if (system.args.length !== 2) {
console.log('Usage: phantomjs renderbuddy.js <portnumber>');
phantom.exit(1);
} else {
port = system.args[1];
@cakesmith
cakesmith / options-objects.js
Created July 26, 2016 20:24 — forked from rwaldron/options-objects.js
ES6 Object.assign( target, source ) as a native options object merging pattern.
let defaults = { file: null };
function foo(options) {
// Merging defaults and options objects in ES6
options = [ defaults, options ].reduce(Object.assign, {});
}
foo({ file: "happy.md" });
@cakesmith
cakesmith / jest-babel.js
Created July 18, 2016 16:18
jest preprocessor script for babel 5.x.x
// jest-babel.js
//
// usage (package.json):
// "jest": {
// "scriptPreprocessor": "jest-babel.js"
// }
//
var path = require('path');
var babel = require('babel-core');