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 / signal.js
Created December 6, 2024 04:34
minimal implementation of signal
const signal = (value) => {
const fn = () => value;
let subscribers = [];
const notifySubscribers = () => {
subscribers.forEach((subscriber) => subscriber(value));
};
fn.set = (newValue) => {
value = newValue;
@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" }]
]