Skip to content

Instantly share code, notes, and snippets.

View umair-khanzada's full-sized avatar
💡
Building something that women loves 😉👗

Umair Ahmed umair-khanzada

💡
Building something that women loves 😉👗
View GitHub Profile
@shahzadns
shahzadns / nodejs-enhanced-cjs-pattern
Last active January 23, 2017 19:19
This gist proposes an enhanced CommonJS module declaration pattern for NodeJS.
/*
Date: Jan 19 2017
title: NodeJS enhanced CJS module pattern
author: Shahzad Nawaz
email: shahzadscs@gmail.com
Description:
This gist proposes an enhanced CommonJS module declaration pattern for nodeJS/express modules.
*/
//Reverse string in javascript.
var str = "your string";
str.split('').reverse().join('');
//using ES6 is more easy.
[...str].reverse().join('');
let lsb = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'saven', 'eight', 'nine'],
mid = ['', 'ten', 'tweenty', 'therty', 'fourty', 'fifty', 'sixty', 'saventy', 'eighty', 'ninty'],
msb = ['', 'one hundred', 'two hundred', 'three hundred', 'four hundred', 'five hundred', 'six hundred', 'saven hundred', 'eight hundred', 'nine hundred'],
num = 445;
input = String(num);
let msbBit = Number(input[0]),
midBit = input.length > 1 ? Number(input[1]) : 0,
lsbBit = input.length > 2 ? Number(input[2]) : 0;
let array = [{ id: 1, name: 'umair', age: 14 }, { id: 2, name: 'zubair', age: 16 }, { id: 3, name: 'owais', age: 20 }];
//get any field/key of obj using map.
let idsArray = array.map( (obj) => obj.id );
//result ids = [1, 2, 3]
//get special object from array using find.
//Array syntax
let promises = [promiseOne(), promiseTwo(), promiseThree()];
$q.all(promises).then((values) => {
console.log(values[0]); // value One
console.log(values[1]); // value Two
console.log(values[2]); // value Three
// do something
@erans
erans / encrypt_decrypt_example.js
Last active October 14, 2022 02:31
Example of encryption and decryption in node.js
var crypto = require("crypto")
function encrypt(key, data) {
var cipher = crypto.createCipher('aes-256-cbc', key);
var crypted = cipher.update(data, 'utf-8', 'hex');
crypted += cipher.final('hex');
return crypted;
}
@crtr0
crtr0 / client.js
Created June 8, 2012 17:02
A simple example of setting-up dynamic "rooms" for socket.io clients to join
// set-up a connection between the client and the server
var socket = io.connect();
// let's assume that the client page, once rendered, knows what room it wants to join
var room = "abc123";
socket.on('connect', function() {
// Connected, let's sign-up for to receive messages for this room
socket.emit('room', room);
});