Skip to content

Instantly share code, notes, and snippets.

View kanakiyajay's full-sized avatar
💭
Reach out to my email address

Jay Kanakiya kanakiyajay

💭
Reach out to my email address
View GitHub Profile
@kanakiyajay
kanakiyajay / addUtmParams
Created October 18, 2014 14:46
Add Utm params to each and every link of your website.
(function addUtmParams(source, medium, campaign) {
$("a").each(function () {
var $this = $(this);
var linkHref = $this.attr("href");
if (linkHref.indexOf(window.location.hostname) < 0) {
var updateLink = updateQueryString({
utm_source: source,
utm_medium: medium,
utm_campaign: campaign
}, linkHref);
@kanakiyajay
kanakiyajay / UpdateQueryString
Created October 18, 2014 14:07
Update Query String of any url
function UpdateQueryString(key, value, url) {
if (!url) url = window.location.href;
var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi");
if (re.test(url)) {
if (typeof value !== 'undefined' && value !== null)
return url.replace(re, '$1' + key + "=" + value + '$2$3');
else {
var hash = url.split('#');
url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
@kanakiyajay
kanakiyajay / parseURL
Created October 18, 2014 13:42
Function to parseUrl and extract useful information in javascript
function parseURL(url) {
var a = document.createElement('a');
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':',''),
host: a.hostname,
port: a.port,
query: a.search,
params: (function(){
@kanakiyajay
kanakiyajay / throttle.js
Created September 16, 2014 13:14
Throttle: Call a function only once every few milliseconds. Important for scroll events, or when a function is called multiple times.
function throttle(delay, callback) {
var previousTime = new Date().getTime();
return function() {
var newTime = new Date().getTime();
if ((newTime - previousTime) > delay) {
previousTime = newTime;
callback.apply(null, arguments);
}
};
}
@kanakiyajay
kanakiyajay / debounce
Created September 16, 2014 12:30
Postpone/Debounce a function important for showing autocomplete search results and saving ajax requests.
function debounce (delay, callback) {
var timeout = null;
return function (){
if (timeout) {
clearTimeout(timeout);
}
var context = this;
var args = arguments;
timeout = setTimeout(function(){
callback.apply(context, args);
@kanakiyajay
kanakiyajay / injector
Created August 27, 2014 07:43
Angular : The actual code behind the magical angular injector dependencies.
var FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m;
var FN_ARG_SPLIT = /,/;
var FN_ARG = /^\s*(_?)(\S+?)\1\s*$/;
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
var $injectorMinErr = minErr('$injector');
function annotate(fn) {
var $inject,
fnText,
argDecl,
last;
@kanakiyajay
kanakiyajay / regex-tags
Created August 27, 2014 07:36
Angular: Regex to search for html tags
var SINGLE_TAG_REGEXP = /^<(\w+)\s*\/?>(?:<\/\1>|)$/;
var HTML_REGEXP = /<|&#?\w+;/;
var TAG_NAME_REGEXP = /<([\w:]+)/;
var XHTML_TAG_REGEXP = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/gi;
@kanakiyajay
kanakiyajay / toJson
Created August 27, 2014 06:37
Angular | output prettified JSON output with whitespaces and newlines
function toJson(obj) {
return JSON.stringify(obj, function(k,v) { return v; }, ' ')
}
@kanakiyajay
kanakiyajay / getTimeStamp
Created August 22, 2014 09:23
Get pretty timestamp in javascript
function getTimeStamp() {
var now = new Date();
return ((now.getMonth() + 1) + '/' +
(now.getDate()) + '/' +
now.getFullYear() + " " +
now.getHours() + ':' +
((now.getMinutes() < 10)
? ("0" + now.getMinutes())
: (now.getMinutes())) + ':' +
((now.getSeconds() < 10)
/*
* @param {Object|Array|Date|string|number} obj Input to be serialized into JSON.
* @param {boolean=} pretty If set to true, the JSON output will contain newlines and whitespace.
* @returns {string|undefined} JSON-ified string representing `obj`.
*/
function toJson(obj, pretty) {
if (typeof obj === 'undefined') return undefined;
return JSON.stringify(obj, toJsonReplacer, pretty ? ' ' : null);
}