Skip to content

Instantly share code, notes, and snippets.

View garenyondem's full-sized avatar
:shipit:

Garen Yondem garenyondem

:shipit:
View GitHub Profile
@garenyondem
garenyondem / parallel-job-queue.ts
Created October 26, 2024 09:53
Execute multiple async functions in parallel
export interface IParallelJobQueue<T> {
add(fn: AsyncFunction<T>, ...args: any[]): void;
process(): Promise<void>;
results(): {
succesfulResults: T[],
failedResults: T[]
};
progress(): number;
}
type AsyncFunction<T> = (...args: any[]) => Promise<T>;
@garenyondem
garenyondem / cancellation-token.ts
Created December 21, 2023 09:30
C# style CancellationToken implementation in Typescript
export class CancellationToken {
#isCancellationRequested: boolean = false;
get isCancellationRequested() {
return this.#isCancellationRequested;
}
cancel() {
this.#isCancellationRequested = true;
}
}
@garenyondem
garenyondem / one-liners.js
Created October 29, 2023 11:26
Useful one-liners in JS
export const shuffleArray = arr => arr.sort(() => Math.random() - 0.5);
export const isPalindrome = str => str === str.split('').reverse().join('');
export const isEmailValid = email => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
export const generateUUID = () => 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => (Math.random() * 16 | 0).toString(16));
export const calculateDistance = (lat1, lon1, lat2, lon2) => { const rad = x => x * Math.PI / 180; const R = 6371; const dLat = rad(lat2 - lat1); const dLon = rad(lon2 - lon1); return R * 2 * Math.asin(Math.sqrt(Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(rad(lat1)) * Math.cos(rad(lat2)) * Math.sin(dLon/2) * Math.sin(dLon/2))); };
@garenyondem
garenyondem / trace.ts
Created May 31, 2022 11:10
@trace decorator for classes
import { logger } from './logger';
import { isFunction } from 'lodash';
export function trace(
target: any,
propName?: string,
propDescriptor?: PropertyDescriptor
) {
if (propDescriptor) {
// Decorating a Method
@garenyondem
garenyondem / sqlite_backup.sh
Created May 15, 2022 15:09
Cron-based sqlite backup
#!/bin/bash -x
# Ensure script stops when commands fail.
set -e
# Backup & compress our database to the temp directory.
sqlite3 /path/to/db '.backup /tmp/db'
gzip /tmp/db
# Upload backup to S3 using a rolling daily naming scheme.
@garenyondem
garenyondem / restart_explorer.bat
Created January 19, 2022 12:32
fix unresponsive windows explorer
taskkill /im explorer.exe /f
start explorer.exe
exit
@garenyondem
garenyondem / gist:62a0c89c66a8fd6d80ff6cea504a1eab
Created December 28, 2021 19:49
Typescript Flatten two types
type Flatten<T> = T extends object ? {
[P in keyof T]: Flatten<T[P]>
} : T;
type A = { foo: boolean } & { bar: string };
type B = Flatten<A>; // { foo: boolean; bar: string; }
@garenyondem
garenyondem / portainer-template.json
Last active September 23, 2021 14:39
Portainer Templates
{
"version": "2",
"templates": [
{
"type": 1,
"title": "Registry",
"description": "Docker image registry",
"categories": [
"docker"
],
@garenyondem
garenyondem / logger.js
Created September 29, 2020 08:37
Express req res logging middleware
import { Request, Response, NextFunction } from 'express';
import { environment } from '../environment';
// This functions prints Request and Response data on graphql endpoint
export function loggerMiddleware(
req: Request,
res: Response,
next: NextFunction
) {
if (!environment.loggerMiddleware) {
@garenyondem
garenyondem / async-handlers.js
Created June 15, 2020 18:52
Async handler, wrapers, error handlers
exports.errorHandler = (response, err) => {
const statusCode = err.status || 500;
response.status(statusCode).json({
success: false,
error: err.message
});
};
exports.successHandler = (response, data) => {
response.json({