Skip to content

Instantly share code, notes, and snippets.

View im4aLL's full-sized avatar
🏠
Working from home

Md Habibullah Al Hadi im4aLL

🏠
Working from home
View GitHub Profile
@im4aLL
im4aLL / inject.service.ts
Created May 17, 2024 02:38
Make any class singletone
interface InjectionInstanceInterface {
[key: string]: any;
}
interface ClassRef<T> extends Function {
new (...args: any[]): T;
}
class InjectService {
private instances: InjectionInstanceInterface = {};
@im4aLL
im4aLL / nano.ts
Created April 26, 2024 04:34
Nano template engine
/**
* Parse template string
*
* @usage parseTemplate(`my name is {name} {nested.name}`, {name: 'foo', nested: { name: 'bar' }})
* @param template string
* @param data object
* @returns string
*/
export function HelperParseTemplate(template: string, data: any): string {
return template.replace(/\{([\w\\.]*)\}/g, (str: string, key: string) => {
@im4aLL
im4aLL / docker-compose-mysql.yml
Created April 5, 2024 23:28
mysql and phpmyadmin compose file for docker
version: '3.8'
services:
db:
image: mysql
container_name: db
environment:
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_DATABASE: ${DB_DATABASE}
@im4aLL
im4aLL / as-const-url.ts
Created April 5, 2024 00:24
cool items in typescript
type UserUrl = string;
// const getUserUrl = (id: number) => {
// return `/user/${id}`; // as const
// };
// type UserUrl = ReturnType<typeof getUserUrl>;
const url: UserUrl = '/user/2';
@im4aLL
im4aLL / Design pattern in typescript.ts
Last active April 5, 2024 00:21
Design pattern in typescript
// The Adapter pattern is a structural design pattern that allows objects with incompatible interfaces to work together.
interface NotificationInterface {
send(subject: string, message: string): boolean;
}
class EmailNotification implements NotificationInterface {
send(subject: string, message: string): boolean {
console.log(`Sending email notification: ${subject} ${message}`);
return true;
@im4aLL
im4aLL / guard-clause.js
Last active April 4, 2024 16:37
Guard Clause
// Guard Clause is a technique derived from the fail-fast method
// whose purpose is to validate a condition and immediately stop the code execution
// about the result of the validation.
// Using guard clauses is a common and clean technique for simplifying the code block and avoiding unnecessary complexity.
// Example 1
const submitForm = (formData, formMeta) => {
if (!formMeta.hasError(formData)) {
formData.submit();
}
@im4aLL
im4aLL / wtf.js
Created April 3, 2024 17:37
emotional abuse
// 1
console.log(typeof typeof 1);
// 2
console.log(018 - 015);
// 3
const numbers = [33, 2, 8];
numbers.sort();
console.log(numbers[1]);
@im4aLL
im4aLL / docker-compose.yml
Created March 26, 2024 16:04
docker compose file for mongo, mongo-expres, redis and redis-commander
version: '3.8'
services:
mongo:
image: mongo:latest
volumes:
- ./.data/mongodb:/data/db
environment:
MONGO_INITDB_ROOT_USERNAME: root
@im4aLL
im4aLL / babel-loader-rule.js
Last active July 12, 2021 18:59
webpack-starter
// webpack/modules/babel-loader-rule.js
module.exports = {
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env', { targets: "defaults" }]
]
@im4aLL
im4aLL / CyclicRotation.js
Created February 7, 2021 20:05
CyclicRotation ~ Rotate an array to the right by a given number of steps.
/*
An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).
The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.
Write a function:
function solution(A, K);
that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.