Skip to content

Instantly share code, notes, and snippets.

View avishwakarma's full-sized avatar

Ashok Vishwakarma ✪ avishwakarma

View GitHub Profile
@avishwakarma
avishwakarma / requestAnimationFrame.js
Last active June 28, 2016 05:45
JavaScript requestanimation polyfill for non supporting browsers.
/**
* window.requestAnimationFrame polyfill
*/
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
|| window[vendors[x]+'CancelRequestAnimationFrame'];
@avishwakarma
avishwakarma / onScreen.js
Created June 28, 2016 05:49
Check element is in viewport - jQuery plugin
/**
* $.onScreen jQuery plugin
* @uses $("#eample").onScreen() // return true if element is in current view port
*/
$.fn.onScreen = function(x, y){
if(x == null || typeof x == 'undefined') x = 1;
if(y == null || typeof y == 'undefined') y = 1;
var win = $(window);
var viewport = {
top : win.scrollTop(),
@avishwakarma
avishwakarma / swap.js
Created June 28, 2016 05:52
jQuery plugin to swap two elements to each other using $.replaceWith function
/**
* $.swap - jQuery plugin to swap two elements
* Swap the current element with other element
* @uses $("#eample").swap($("#element"))
*/
$.fn.swap = function(to) {
return this.each(function(){
var ct = $(to).clone(true);
var cf = $(this).clone(true);
@avishwakarma
avishwakarma / replaceAll.js
Created June 28, 2016 05:57
JavaScript replace multiple items in a string at once worked as str_replace PHP function
/**
* replaceAll - JavaScript string function
* @uses replace all matching substring to its corresponding replace string
* Worked similarly as str_replace in PHP
*/
String.prototype.replaceAll = function(find, replace) {
var replaceString = this;
var regex;
for (var i = 0; i < find.length; i++) {
@avishwakarma
avishwakarma / serializeObject.js
Created June 28, 2016 05:59
jQuery plugin to serialize form elements as an object
/**
* serializeObject - jQuery plugin to serialize form elements as an object
* @uses $("#form").serializeObject(); // return {input:"value", input2:"value" .. }
*/
$.fn.serializeObject = function(){
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
@avishwakarma
avishwakarma / palindrome.js
Created June 28, 2016 06:05
Check if a number or string is palindrome
/**
* isPalindrome - function to check if a number or string is palindrome
* @uses isPalindrome(141) // true
*/
function isPalindrome(str){
return (str + "").split("").reverse().join("") == (str + "");
}
@avishwakarma
avishwakarma / removeSpaces.js
Created June 28, 2016 06:08
remove all spaces (" ") from string
/**
* remove all spaces (" ") from string
* @uses removeSpaces("sample string with s p a c e s") // samplestringwithspaces
*/
function removeSpaces(str){
return (str + "").replace(/ /g,'');
}
@avishwakarma
avishwakarma / unique.js
Last active June 28, 2016 06:36
JavaScript Array function for unique elements
/**
* unique - Array function for unique elements
* @use [2, 2, 3, 4, 3, 2].unqiue() // [2, 3, 4]
*/
Array.prototype.unique = function() {
var unique = [];
for (var i = 0; i < this.length; i++) {
if (unique.indexOf(this[i]) == -1) {
unique.push(this[i]);
@avishwakarma
avishwakarma / size.js
Created June 28, 2016 06:39
JavaScript object size
/**
* size - Object size in javascript
* @uses {a:1, b:2}.size() // 2
*/
Object.prototype.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
@avishwakarma
avishwakarma / array_flat.js
Last active May 2, 2017 06:44
JavaScript Array Flat / Merge
/**
* flat / merge all sub array into one array
* @uses [['1', '2'], ['3', '4], '5', '6'].flat(); // ['1', '2', '3', '4', '5', '6']
*/
Array.prototype.flat = function(){
return [].concat.apply([], this);
}