Skip to content

Instantly share code, notes, and snippets.

🎯
Focusing

Ahmad Ali ahmadalibaloch

🎯
Focusing
Block or report user

Report or block ahmadalibaloch

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@ahmadalibaloch
ahmadalibaloch / creditCardMaskify.js
Created Oct 11, 2019
Credit Card maskify in Javascript
View creditCardMaskify.js
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 / rgbHexConversion.js
Created Oct 11, 2019
RGB to HEX and HEX to RGB color conversion in Javascript - using shift operators
View rgbHexConversion.js
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 / customSetTimeoutAndsetInterval.js
Last active Apr 23, 2019
custom setTimeout and setInterval for sandbox environments in browsers (vm-browserify or node-vm)
View customSetTimeoutAndsetInterval.js
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) {
You can’t perform that action at this time.