Skip to content

Instantly share code, notes, and snippets.

View ahmadalibaloch's full-sized avatar
🎯
Focusing

Ahmad Ali ahmadalibaloch

🎯
Focusing
View GitHub Profile
@ahmadalibaloch
ahmadalibaloch / rgbHexConversion.js
Created October 11, 2019 11:55
RGB to HEX and HEX to RGB color conversion in Javascript - using shift operators
function rgbToHex([red = 0, green = 0, blue = 0] = []) {
// Left shift (<<) operator for color conversion is interesting
// the color value for each component of an RGB color is between 0 - 255 (8bits)
// The first 8 bits starting from the right will represent the blue component,
// the next 8 bits will represent the green component, and the 8 bits after that will represent the red component
return `#${(red << 16 | green << 8 | blue).toString(16)}`;
}
function hexToRgb(hex) {
// working through above shift operator procedure backwards
// we will right-shift the color bits by multiples of 8 as necessary until
@ahmadalibaloch
ahmadalibaloch / creditCardMaskify.js
Created October 11, 2019 11:58
Credit Card maskify in Javascript
function maskify(creditCard) {
if (creditCard.length < 6) return creditCard;
const last4Characters = creditCard.substr(-4);
const firstCharacter = creditCard.substr(0, 1);
const maskingCharacters = creditCard.substr(1, creditCard.length - 5).replace(/\d/g, '#');
return `${firstCharacter}${maskingCharacters}${last4Characters}`;
}
// let assert = require('chai').assert
// require('mocha').describe;
@ahmadalibaloch
ahmadalibaloch / customSetTimeoutAndsetInterval.js
Last active December 7, 2022 07:34
custom setTimeout and setInterval for sandbox environments in browsers (vm-browserify or node-vm)
const setTimeouts = [];
export function customSetTimeout(cb, interval) {
const now = window.performance.now();
const index = setTimeouts.length;
setTimeouts[index] = () => {
cb();
};
setTimeouts[index].active = true;
const handleMessage = (evt) => {
if (evt.data === index) {
@ahmadalibaloch
ahmadalibaloch / longest_symmetric_substring.js
Created November 29, 2019 08:08
Finding longest palindrome (symmetric) substring in a string using Javascript (brute-force method)
function longest_symm_substr(s) {
let longest_symmetric = '';
for(let i=0;i<s.length-1;i++){
// this loop will each time get a new substring incrementing the index from 0 to length
let curr_str = s.substring(i);
for(let j=curr_str.length-1;j>0;j--){
// this loop will find all possible symmetric substrings in the above indexed substring
let str = curr_str.substring(0,j)
if(str === str.split('').reverse().join('')){
if(longest_symmetric.length < str.length){
@ahmadalibaloch
ahmadalibaloch / fish_alias.md
Created February 18, 2023 13:31 — forked from tikolakin/fish_alias.md
Create alias in Fish shell and save this as a permanent function

Directly from CLI

alias x='exit'
funcsave x

or create a file in

~/.config/fish/functions 

with name