Skip to content

Instantly share code, notes, and snippets.

@ali-master
ali-master / main.ts
Created April 3, 2024 21:29
Simple Multi Tenant Manager
import { Controller, Get, Inject, Injectable, Module, Scope } from "@nestjs/common";
import {
ContextId, ContextIdFactory, ContextIdResolver, ContextIdResolverFn, ContextIdStrategy,
HostComponentInfo, ModuleRef, NestFactory, REQUEST
} from "@nestjs/core";
import { NestExpressApplication } from "@nestjs/platform-express";
// Context strategy
const tenants = new Map<string, ContextId>();
export class TenantContextIdStrategy implements ContextIdStrategy {
@ali-master
ali-master / main.bash
Created November 23, 2023 16:30
SSH port forwarding to. remote host/server
ssh -L 3000:localhost:3000 your-non-root-user@yourserver-ip
@ali-master
ali-master / main.js
Created May 6, 2023 14:32
Convert HTML document numbers to Persian Digits
String.prototype.toPersianDigit = function (a) {
return this.replace(/\d+/g, function (digit) {
var enDigitArr = [], peDigitArr = [];
for (var i = 0; i < digit.length; i++) {
enDigitArr.push(digit.charCodeAt(i));
}
for (var j = 0; j < enDigitArr.length; j++) {
peDigitArr.push(String.fromCharCode(enDigitArr[j] + ((!!a && a == true) ? 1584 : 1728)));
}
return peDigitArr.join('');
@ali-master
ali-master / parseq.js
Created December 3, 2022 13:05 — forked from aSapien/parseq.js
Parallel / Sequential execution control flow with Promises in JS #javascript
export const parallel = (...fs) => sequential(
() => fs.map(f => f()),
Promise.all.bind(Promise));
export const sequential = (f, ...fs) => val => Promise.resolve()
.then(() => f(val))
.then(fs.length ? sequential(...fs) : id => id);
--log_gc (Log heap samples on garbage collection for the hp2ps tool.)
type: bool default: false
--expose_gc (expose gc extension)
type: bool default: false
--max_new_space_size (max size of the new generation (in kBytes))
type: int default: 0
--max_old_space_size (max size of the old generation (in Mbytes))
type: int default: 0
--max_executable_size (max size of executable memory (in Mbytes))
type: int default: 0
@ali-master
ali-master / typescript-session1.ts
Created December 10, 2021 19:13
Typescript Workshop(First session)
////////////////////
// Exclude
////////////////////
type MyExclude<T, L> = T extends L ? never : T;
type TestExclude = MyExclude<"a" | "b" | "c" | "d", "a" | "b">
////////////////////
// Extract
////////////////////
type MyExtract<T, L> = T extends L ? T : never;
@ali-master
ali-master / react-pwa-install-button.js
Created December 5, 2021 13:01
Install a PWA application manually in React.js
import React, { useEffect, useRef } from 'react'
export const PWAInstallButton = () => {
const deferredPrompt = useRef(null)
useEffect(() => {
const handlePrompt = (e) => {
// Prevent the mini-infobar from appearing on mobile
e.preventDefault()
// Stash the event so it can be triggered later.
@ali-master
ali-master / docker-compose.yml
Created December 2, 2021 20:51
Unleash self-hosted docker-compose version
version: "3.7"
services:
unleashDB:
expose:
- "5432"
image: postgres:13
container_name: unleashDB
networks:
- internal
@ali-master
ali-master / partial-require-only-one.ts
Created November 6, 2021 07:44
RequireOnlyOne & PartialOnlyOne in typescript
type RequireOnlyOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
[K in Keys]-?: Required<Pick<T, K>>;
}[Keys];
type PartialOnlyOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
[K in Keys]?: Partial<Pick<T, K>>;
}[Keys];
@ali-master
ali-master / converter.js
Created September 11, 2021 00:39
Two functions to convert from Celsius to Fahrenheit and back in Javascript
function toCelsius(fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
function toFahrenheit(celsius) {
return (celsius * 9 / 5) + 32;
}