Skip to content

Instantly share code, notes, and snippets.

View avtarnanrey's full-sized avatar
:electron:

Avtar Nanrey avtarnanrey

:electron:
View GitHub Profile
@avtarnanrey
avtarnanrey / apache_config.conf
Last active May 6, 2020 16:08
Common apache config to redirect http to https and setting SSL certificate and allow .httaccess settings
<VirtualHost *:80>
Redirect "/" "https://your_website.com"
</VirtualHost>
<VirtualHost *:443>
ServerAdmin avtar@avtarnanrey.com
ServerName your_website.com
ServerAlias www.your_website.com
DocumentRoot /var/www/your_website.com
ErrorLog ${APACHE_LOG_DIR}/website_error.log
@avtarnanrey
avtarnanrey / observer.js
Last active March 10, 2020 03:17
JavaScript Patterns: Observer Pattern
// ES5 example
function Subject () {
this.observers = []; // Array of observer functions
}
Subject.prototype = {
subscribe: function (fn) {
this.observers.push(fn)
},
@avtarnanrey
avtarnanrey / git-bash
Created March 2, 2020 17:28
Clean local branches merged or obsolete
// To delete all merged branches except master and dev
git branch --merged | grep -v '\*\|master\|dev' | xargs -n 1 git branch -d
// To delete all branches except master and dev:
git branch | grep -v '\*\|master\|dev' | xargs -n 1 git branch -D
@avtarnanrey
avtarnanrey / regex.js
Created February 24, 2020 15:05
Regular expressions & common form helper methods
// Phone Number format 000-000-0000
export const formattedPhoneRegex = /^[0-9]\d{2}-\d{3}-\d{4}$/i;
// Email
export const emailRegex = /^[a-z0-9`!#\$%&\*\+\/=\?\^\'\-_]+((\.)+[a-z0-9`!#\$%&\*\+\/=\?\^\'\-_]+)*@([a-z0-9]+([\-][a-z0-9])*)+([\.]([a-z0-9]+([\-][a-z0-9])*)+)+$/i;
// No Special Chars
export const noSpecialCharRegex = /^[a-zA-Z0-9]+$/i;
// Auto format the phone number
@avtarnanrey
avtarnanrey / cloudSettings
Created February 24, 2020 12:47
Visual Studio Code Settings Sync Gist
{"lastUpload":"2020-02-24T12:47:51.099Z","extensionVersion":"v3.4.3"}
@avtarnanrey
avtarnanrey / dom_observer.js
Created October 10, 2019 20:12
Wait for element to be added into DOM then execute JavaScript
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (!mutation.addedNodes) return
for (var i = 0; i < mutation.addedNodes.length; i++) {
var item = Array.prototype.slice.call(mutation.addedNodes).find(function (i) { return i.id == "selector" });
if (item) {
// Do your stuff here
observer.disconnect();
}
@avtarnanrey
avtarnanrey / ajax.js
Created November 28, 2017 20:47
AJAX calls using vanilla JavaScript and jQuery
(function(){
var httpRequest, root;
root = 'https://jsonplaceholder.typicode.com'; // URL for JSON data
document.querySelector('.ajaxButton').addEventListener('click', makeRequest);
function makeRequest() {
httpRequest = new XMLHttpRequest();
@avtarnanrey
avtarnanrey / smoothScroll.js
Created February 7, 2017 19:12
Simple jQuery script for smooth page scroll on anchor tags.
// Smooth Scroll
jQuery(function() {
jQuery('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = jQuery(this.hash);
target = target.length ? target : jQuery('[name=' + this.hash.slice(1) +']');
if (target.length) {
jQuery('html, body').animate({
scrollTop: target.offset().top
}, 1000);
@avtarnanrey
avtarnanrey / hide_show_on_scroll.js
Created November 26, 2016 15:19
Hide / Show button on Scroll Function
var position, direction, previous;
$(window).scroll(function(){
if( $(this).scrollTop() >= position ){
direction = 'down';
if(direction != previous){
$('.menu-toggle').addClass('hide');
previous = direction;
}
} else {
@avtarnanrey
avtarnanrey / checkbox.html
Created November 22, 2016 19:31
Working with Checkbox - Hide Show the content with data-type attribute from the checkbox
<!-- Checboxes -->
<form id="productCategories">
<input type="checkbox" name="product[]" value="vitamin" checked>Vitamin<br>
<input type="checkbox" name="product[]" value="mineralwater" checked>Mineral Water<br>
<input type="checkbox" name="product[]" value="proteinbar" checked>Proteinbar
</form>
<!-- List Items -->
<ul class="product-list">
<li class="product-item" data-prod_id="V-BC2178">