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 / Event.js
Last active March 10, 2020 08:50
ES6 event listener (pub sub) - usage - component to component communication (angular, react, vue or anything)
class Event {
static events = [];
static listen(name, callback) {
if (!Event.events[name]) {
Event.events[name] = [];
}
Event.events[name].push(callback);
}
@im4aLL
im4aLL / BinaryGap.js
Created February 7, 2021 19:40
BinaryGap - Find longest sequence of zeros in binary representation of an integer.
/*
A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.
Write a function:
function solution(N);
that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.
@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.
@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 / 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 / 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 / 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 / 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 / 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 / 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}