Skip to content

Instantly share code, notes, and snippets.

View uladkasach's full-sized avatar

Uladzimir Kasacheuski uladkasach

  • Miami, FL
View GitHub Profile
/*
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 / 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)
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 / 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
@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
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 / 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;
};
@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 / vims.md
Last active March 24, 2020 12:51
favorite commands

searching

  • /${__regex__}, enter, n/shift-n => forward search
  • ?${__regex__}, enter, n/shift-n => backwards search

search and replace

  • :${__from__},${__to__}s/${__search_regex__}/${__replace_regex__}/gc
    • drop c for no confirmation
    • replace ${__from__},${__to__} w/ % for all lines

move screen w/o cursor

@uladkasach
uladkasach / withTimeout.ts
Last active June 24, 2020 16:15
withTimeout.ts
/**
* returns a new function which calls the input function and "races" the result against a promise that throws an error on timeout.
*
* the result is:
* - if your async fn takes longer than timeout ms, then an error will be thrown
* - if your async fn executes faster than timeout ms, you'll get the normal response of the fn
*
* ### usage
* ```ts
* const result = await withTimeout(() => doSomethingAsync(...args), 3000);