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 / int
Last active August 29, 2015 14:05
Angular: Simple functions
function int(str) {
return parseInt(str, 10);
}
@kanakiyajay
kanakiyajay / removeSpecialChars
Created August 21, 2014 11:23
Simple function to remove special characters from a string.
function removeSpecialChars (str) {
return str.replace(/[^a-zA-Z ]/g, "");
}
@kanakiyajay
kanakiyajay / isArray
Created August 21, 2014 11:34
Angular: Function to calculate the length of string
var isArray = (function() {
if (!isFunction(Array.isArray)) {
return function(value) {
return toString.call(value) === '[object Array]';
};
}
return Array.isArray;
})();
/*
* @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);
}
@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)
@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 / 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 / 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 / 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 / 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);
}
};
}