Skip to content

Instantly share code, notes, and snippets.

@jenyayel
jenyayel / working_days.sql
Last active April 7, 2022 08:01
Get next working day in MySQL
/*
* Returns calendar dates along with their next working date for the last 2 years.
* Non working days are either weekends or "hardcoded" holidays.
* Author: Jenya Y.
*/
SELECT
CAST(calendar_date AS DATE) AS calendar_date,
MIN(working_days.working_date) AS next_working_day
FROM
(
/**
* Returns a modified array of transactions where each categorized based on similarity.
*/
const categorizeSimilarTransactions = (transactions) => {
if (transactions.length < 2) {
return transactions;
}
// Since each transaction can be matched
// only to transaction that has category,
/**
* Calculates the balance in a specific category within the specified time period.
*/
const getBalanceByCategoryInPeriod = (
transactions,
categories,
start,
end
) => {
// Implementation notes:
@jenyayel
jenyayel / unwrapper.ts
Last active May 8, 2021 21:06
Unwraps path keys into object
/**
* Converts object with flat structure, where key contains a full path to each leaf,
* into regular POJO.
*
* @example
* // input:
* {
'recipients[0].name': 'is required',
'recipients[0].email': ['is required', 'invalid format'],
'subject': 'is required',
@jenyayel
jenyayel / symmetric-crypto.ts
Last active January 15, 2021 21:32
Symmetric encryption for nodejs
import * as crypto from 'crypto';
import { CipherGCMTypes } from 'crypto';
// defaults source: https://stackoverflow.com/a/53573115/2307459
const BLOCK_CIPHER: CipherGCMTypes = 'aes-256-gcm';
const AUTH_TAG_BYTE_LEN = 16;
const IV_BYTE_LEN = 12;
const KEY_BYTE_LEN = 32;
export class SymmetricCrypto {
@jenyayel
jenyayel / deferred.ts
Created July 28, 2020 07:49
Wrapper for promise to resolve/reject externally and get indication whether the promise was fulfilled
export class Deferred<T = void> {
public resolve!: (value?: T | PromiseLike<T>) => void;
public reject!: (reason?: any) => void;
public readonly promise: Promise<T>;
private isFulfilled = false;
constructor() {
this.promise = new Promise(
(resolve, reject) => {
this.resolve = resolve;
@jenyayel
jenyayel / rsync_retry.sh
Created April 20, 2020 22:04
Runs rsync with retry
#!/bin/bash
while [ 1 ]
do
rsync \
--progress \
--partial \
--append \
--timeout=30 \
-vz \
@jenyayel
jenyayel / retry.ts
Last active May 20, 2020 19:56
Simple retry with constant backoff in nodejs with TypeScript
/**
* Performs operation and retries it in case either thrown
* exception or an optional `success` checker returns `false`.
* @param work The operation that need to perform
* @param checker Optional delegate to test whether the outcome of `work()` was successful operation
* @param retries An amount of retries to perform
* @param delayBetween An amount of milliseconds to wait between retries
*/
export default async <TResult>(
work: () => TResult | Promise<TResult>,
@jenyayel
jenyayel / deploy.ps1
Created October 28, 2019 19:01
Powershell to deploy Azure WebJob (can/should be used in Azure DevOps)
# configurations
$workingDirectory = "$(System.DefaultWorkingDirectory)"
$sourceArtifact = "ZIP_FILE_THAT_HAS_YOUR_AAPLICATION_(PRODUCED_BY_BUILD)"
$entryPointDll = "FILE_THAT_HAS_STATIC_VOID_MAIN.dll"
$webJobName = "THE_NAME_OF_THE_WEBJOB"
$jobType = "continuous"
$appName = "THE_NAME_OF_AZURE_WEB_APP_THAT_WILL_HOST_JOB"
$resourceGroupName = "GROUP_NAME"
Write-Host "Validating artifact file"
@jenyayel
jenyayel / ec2-ssh.sh
Created December 17, 2018 09:51
Retrieves EC2 DNS by instance name and opens SSH
#!/bin/bash
set -e
command -v aws >/dev/null 2>&1 || { echo >&2 "AWS CLI required. Please install from https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html"; exit 1; }
# get the name of EC2 instance from 1st argument
if [ -z "$1" ]
then
echo "Instance name is required"
exit 1