Skip to content

Instantly share code, notes, and snippets.

View uladkasach's full-sized avatar

Uladzimir Kasacheuski uladkasach

  • Miami, FL
View GitHub Profile
@uladkasach
uladkasach / withDatabaseConnection.ts
Created March 22, 2020 12:24
wrap the function with a "higher order function" that automatically provides a managed database connection to the wrapped function
import { DatabaseConnection } from '../__nonpublished_modules__/simple-rds-dao/types';
import { dbConnection } from './database';
/**
* wrap the function with a "higher order function" that automatically provides a managed database connection to the wrapped function
*
* inspired by react higher order components: https://reactjs.org/docs/higher-order-components.html
*/
export const withDatabaseConnection = <T extends (args: { dbConnection: DatabaseConnection }) => any>(
targetFunction: T,
@uladkasach
uladkasach / hasIdDefined.ts
Created March 19, 2020 12:42
user defined type guard and type alias that define that the object has its optional id defined
// https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
export type HasIdDefined<T> = T & { id: number };
export const hasIdDefined = <T extends { id?: undefined | number }>(obj: T): obj is HasIdDefined<T> => {
return !!obj.id;
};
import { detectPeaksWithRollingWindowZScore } from './detectPeaksWithRollingWindowZScore';
describe('detectPeaksWithRollingWindowZScore', () => {
it('should be accurate for the example in the original implementation', () => {
let testData: number[] = [
1,
1,
1.1,
1,
0.9,
@uladkasach
uladkasach / ManagedDatabaseConnectionPool.ts
Created July 1, 2019 10:55
ManagedDatabaseConnectionPool
// tslint:disable max-classes-per-file
// TODO: publish as a seperate module
/*
ManagedDatabaseConnection:
- solves:
- reusing the same connection across many function invocations (e.g., lambda container reuse)
- closing the connection if no lambdas are using it automatically
- contract:
- start: state that need a connection
- end: state that no longer need connection
@uladkasach
uladkasach / pack without publish
Last active June 26, 2019 12:30
npm pack without publish
"pack": "npm pack && tar -xvzf *.tgz && rm -rf package *.tgz"
per https://medium.com/@jdxcode/for-the-love-of-god-dont-use-npmignore-f93c08909d8d
export const calculateWeightedStandardDeviation = ({ values, weights }: { values: number[], weights: number[] }) => {
const weightedCount = weights.reduce((sum, value) => sum + value, 0);
// 1. calculate the weighted mean
const weightedSum = values.reduce((sum, value, index) => {
const weight = weights[index];
return sum + value * weight;
}, 0); // tslint:disable-line align
const weightedMean = weightedSum / weightedCount; // weighted mean
@uladkasach
uladkasach / include.html
Last active May 22, 2018 21:35
Vlazoo Header Import
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<iframe id = 'header_source_iframe' style = 'position:absolute; display:none;'></iframe>
<script>
document.domain = location.host.split(".").slice(-2).join("."); // remove subdomain from domain for cross-origin iframe support
var iframe = document.querySelector("#header_source_iframe");
iframe.onload = function(){
console.log("attaching header to the document.");
iframe.contentWindow.promise_dom_rendered
.then(()=>{
iframe.contentWindow.attach_header_to_window(window)
/*
This code is based on the LwIP SNTP example
*/
// glibc dependencies:
#include <string.h>
#include <time.h>
#include <sys/time.h>
// espress IDF dependencies
#include "freertos/FreeRTOS.h"
@uladkasach
uladkasach / esp32.sh
Created February 7, 2018 07:38
Setup ESP-32 Development Environment and Run Hello World
## with ubuntu terminal
##########################################
## setup env
##########################################
#http://iot-bits.com/esp32/getting-started-with-esp32-esp-idf-part-2/
#http://esp-idf.readthedocs.io/en/latest/get-started/linux-setup.html
## 1 download package dependencies
@uladkasach
uladkasach / timeline_chartjs.html
Created July 18, 2017 19:48
Timeline ChartJS working w/ two point datasets
<div style = 'width:500px; height:500px;'>
<canvas id="canvas" width="400" height="400"></canvas>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<script>
window.chartColors = {