Skip to content

Instantly share code, notes, and snippets.

View ali-master's full-sized avatar
🎯
Focusing on Something new!

Ali Torki ali-master

🎯
Focusing on Something new!
View GitHub Profile
@ali-master
ali-master / fixed-point.ts
Created April 9, 2024 21:37
Javscript Number fixed point
export function formatFixedPoint(val: bigint, decimals = 18): string {
const l = val / 10n ** BigInt(decimals);
const r = val % 10n ** BigInt(decimals);
if (r === 0n) {
return l.toString();
}
return `${l}.${r.toString().padStart(decimals, "0").replace(/0*$/, "")}`;
}
@ali-master
ali-master / pick.interceptor.ts
Created April 9, 2024 21:36
Nestjs Pick Interceptor
import {
CallHandler,
ExecutionContext,
Injectable,
NestInterceptor,
} from "@nestjs/common";
import { Observable, map } from "rxjs";
export class PickOptions {
type?: any;
@ali-master
ali-master / lock.module.ts
Last active April 9, 2024 21:35
Nestjs Redis Lock
import { Global, Module } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
@Global()
@Module({
providers: [
{
provide: LockService,
inject: [ConfigService],
@ali-master
ali-master / docker-compose
Created April 5, 2024 20:44 — forked from hexfusion/docker-compose
etcd cluster v3 docker-compose example
version: '2'
services:
etcd-1:
image: gcr.io/etcd-development/etcd:v3.3.9
restart: always
ports:
- 2379
- 2380
volumes:
@ali-master
ali-master / useBroadcastLeader.ts
Created April 4, 2024 04:05 — forked from tannerlinsley/useBroadcastLeader.ts
A React Hook to determine if a tab of your application is the "leader" using BroadcastChannel and leader election
import { BroadcastChannel, createLeaderElection } from 'broadcast-channel'
import React from 'react'
const channels = {}
export function useBroadcastLeader(id = 'default') {
const [isBroadcastLeader, setIsBroadcastLeader] = React.useState(false)
React.useEffect(() => {
if (!channels[id]) {
@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