Skip to content

Instantly share code, notes, and snippets.

View muhammadfaizan's full-sized avatar

Muhammad Faizan muhammadfaizan

View GitHub Profile
@muhammadfaizan
muhammadfaizan / loopwithpromise.js
Last active July 13, 2017 18:12
Looping through promises to fetch data.
let o = 0, l = 10;
let fetchAddress = (offset = 0, limit= 10) => {
let addresses = [...Array(100).keys()];
return Promise.resolve(addresses.slice(offset, offset + limit))
}
let task = (data) => {
// Process with data here;
let log = console.log.bind(console, "task: ")
log(data);
// increment iteration
@muhammadfaizan
muhammadfaizan / gist:654385e8002cc3e797f4b2cf8129bd78
Created October 6, 2017 05:26 — forked from dbrugne/gist:2a62d4dd88f11fa36b75
MongoDB bulk insert from mongoose models
var mongoose = require('mongoose');
var hitSchema = mongoose.Schema({
text: String,
music: String
});
hitSchema.statics.bulkInsert = function(models, fn) {
if (!models || !models.length)
return fn(null);
let tasks = [ /* ... */ ]
let promise = tasks.reduce((prev, task) => {
return prev.then(() => {
return task();
});
}, Promise.resolve());
promise.then(() => {
//All tasks completed
});
@muhammadfaizan
muhammadfaizan / README.md
Last active October 24, 2017 10:16
To seed value with migration

Title

Function of migration to populate data in database with reference data.

Summary

  • up and down functions uploads and removes data from database respectively

Reading data before application startup in Angular 2

In this demonstration I will show you how to read data in Angular2 final release before application startup. You can use it to read configuration files like you do in other languages like Java, Python, Ruby, Php.

This is how the demonstration will load data:

a) It will read an env file named 'env.json'. This file indicates what is the current working environment. Options are: 'production' and 'development';

b) It will read a config JSON file based on what is found in env file. If env is "production", the file is 'config.production.json'. If env is "development", the file is 'config.development.json'.

@muhammadfaizan
muhammadfaizan / node-on-ec2-port-80.md
Created February 2, 2018 09:49 — forked from kentbrew/node-on-ec2-port-80.md
How I Got Node.js Talking on EC2's Port 80

The Problem

Standard practices say no non-root process gets to talk to the Internet on a port less than 1024. How, then, could I get Node talking on port 80 on EC2? (I wanted it to go as fast as possible and use the smallest possible share of my teeny tiny little micro-instance's resources, so proxying through nginx or Apache seemed suboptimal.)

The temptingly easy but ultimately wrong solution:

Alter the port the script talks to from 8000 to 80:

}).listen(80);
@muhammadfaizan
muhammadfaizan / script.js
Created April 27, 2018 11:40
This gist is to redirect mobile apps to mobile theme.
function redirect(location) {
var iframe = document.createElement('iframe');
iframe.setAttribute('src', location);
iframe.setAttribute('width', '1px');
iframe.setAttribute('height', '1px');
iframe.setAttribute('position', 'absolute');
iframe.setAttribute('top', '0');
iframe.setAttribute('left', '0');
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
@muhammadfaizan
muhammadfaizan / mobile-detection.js
Created April 27, 2018 11:41
Device detection on mobile theme with Javascript
/**
* Determine the mobile operating system.
* This function returns one of 'iOS', 'Android', 'Windows Phone', or 'unknown'.
*
* @returns {String}
*/
function getMobileOperatingSystem() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
// Windows Phone must come first because its UA also contains "Android"
@muhammadfaizan
muhammadfaizan / expander.js
Created November 23, 2018 06:24
Script to expand through table and nested table.
function resetSeller(box, oldbox) {
var iconClassName = '.expander.class.name';
if (box === null) {
box = oldbox.parentElement;
console.log(box);
}
var expander = box.querySelector(iconClassName);
if (expander) {
expander.click();
setTimeout(() => {
@muhammadfaizan
muhammadfaizan / gist:0ab8409e34a542a0040b2b79e2bb40f1
Created December 27, 2018 13:20
Gist to redirect app based on link
<script type="text/javascript">
(function() {
var appPath = '{{{appPath}}}';
var pageType = {
CART: '/cart/',
PRODUCT: '/d/',
CATALOG: '/c/',
WISHLIST: '/w/',
LOGIN: '/l/',
REGISTER: '/r/',