Skip to content

Instantly share code, notes, and snippets.

View ali-master's full-sized avatar
working on multiple AI Native Products

Ali Torki ali-master

working on multiple AI Native Products
View GitHub Profile
@ali-master
ali-master / MinHeap.md
Created December 29, 2024 23:35
Top-K Data Structure in Javascript

1. Min-Heap Implementation in TypeScript

We first define a generic MinHeap class with the following operations:

  • size(): Returns the number of elements in the heap.
  • peek(): Returns the minimum (root) without removing it.
  • push(value): Inserts a value, keeping the heap structure.
  • pop(): Removes and returns the minimum (root).
  • heapifyUp() and heapifyDown(): Internal methods to maintain heap order.
@ali-master
ali-master / server-time-handler.ts
Created December 24, 2024 16:51
Javascript Server Time handler
import ko from 'knockout';
import {getLogger} from 'Util/Logger';
export const serverTimeHandler = {
computeTimeOffset(serverTimeString: string): void {
const timeOffset = Date.now() - new Date(serverTimeString).valueOf();
this.timeOffset(timeOffset);
this.logger.log(`Current backend time is '${serverTimeString}'. Time offset updated to '${this.timeOffset()}' ms`);
},
@ali-master
ali-master / farm.config.ts
Created December 22, 2024 00:47
Run Nestjs with Farm bundler
import { defineConfig } from "@farmfe/core";
export default defineConfig({
plugins: [
{
name: "NestJS",
priority: 0,
config(config) {
const inputFileEntry = Object.values(config?.compilation?.input || {})[0] ?? "src/main.ts";
const mode = config.compilation.mode ?? process.env.NODE_ENV ?? "development";
@ali-master
ali-master / DragAndDrop.jsx
Last active November 26, 2024 06:30
React Drag and Drop with indicator(Swap)
import React, {useState} from "react";
export const DragAndDrop = () => {
const cardData = [
{
id: 1,
image: "https://i.ibb.co.com/XxvZ2Kq/Logo.png"
},
{
id: 2,
@ali-master
ali-master / cluster.service.ts
Created November 4, 2024 20:35
NestJS clustering via NodeJS workers
import cluster from "node:cluster";
import { Injectable } from "@nestjs/common";
import { cpuCountSync } from "node-cpu-count";
const numCPUs = cpuCountSync();
@Injectable()
export class AppClusterService {
static register(callback: () => void): void {
if (cluster.isPrimary) {
@ali-master
ali-master / README.md
Created October 21, 2024 21:00
Macos Operation not permitted issue solution

I find a solution for this, in finder, if the hosts file has a lock on it, use this command to unlock it:

sudo chflags nouchg,noschg /etc/hosts

then it will be editable by root, so you can use sudo nano /etc/hosts.

BTY, you can find hosts file in finder by this command: open /etc.

@ali-master
ali-master / date-timezone-converter.ts
Created October 19, 2024 08:40
Javascript Date Timezone awareness utility
export const convertDate = (date: Date | string) => {
if (typeof date === "string") {
const d = new Date(date);
return d.toISOString().slice(0, 19).replace("T", " ");
}
return date.toISOString().slice(0, 19).replace("T", " ");
};
export const getNow = () => {
return new Date(convertDate(new Date()));
@ali-master
ali-master / retryDynamicImport.ts
Created October 13, 2024 20:29 — forked from mberneti/retryDynamicImport.ts
This utility function retryDynamicImport enhances React’s lazy loading mechanism by adding retry logic with a versioned query parameter. It retries importing a component multiple times in case of failure, which can be useful for bypassing browser cache or dealing with intermittent network issues. It can be used as a drop-in replacement for React…
// Usage:
// Replace React.lazy(() => import('x'));
// with retryDynamicImport(() => import('x'));
import { ComponentType, lazy } from 'react';
const MAX_RETRY_COUNT = 15;
const RETRY_DELAY_MS = 500;
// Regex to extract the module URL from the import statement
@ali-master
ali-master / index.ts
Created September 29, 2024 16:16
NodeJS Hook0
import axios from "axios";
import { randomUUID } from 'crypto';
const checkProcessEnv = () => {
if (
!process.env.HOOK0_APPLICATION_ID ||
!process.env.HOOK0_APPLICATION_SECRET
) {
throw new Error('Cannot subscribe to webhooks without Hook0 settings');
}
@ali-master
ali-master / timespan.ts
Created September 19, 2024 14:30
Javascript human time converter
export type TimeSpanUnit = "ms" | "s" | "m" | "h" | "d" | "w";
export class TimeSpan {
constructor(value: number, unit: TimeSpanUnit) {
this.value = value;
this.unit = unit;
}
public value: number;
public unit: TimeSpanUnit;