Skip to content

Instantly share code, notes, and snippets.

Avatar

Atul Chaudhary dhunmoon

View GitHub Profile
@dhunmoon
dhunmoon / arrayDepth.js
Created Apr 30, 2018
Get Depth of an array.
View arrayDepth.js
function depthOf(arr) {
if(!Array.isArray(arr))
throw "Not an Array"
var depth = 1;
var i;
for(var i = 0; i< arr.length; i++){
if (!Array.isArray(arr[i])) continue;
if(Array.isArray(arr[i])){
var depth = depthOf(arr[i]) + 1;
View jsJsonGrepFilter.js
$.grep( [{"name":"Lenovo Thinkpad 41A4298","website":"google"},{"name":"Lenovo Thinkpad 41A2222","website":"google"}], function( n, i ) {
return n.website==='google';
});
View jsJsonDataFilter.js
$([
{"name":"Lenovo Thinkpad 41A4298","website":"google222"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"}
])
.filter(function (i,n){
return n.website==='google';
});
@dhunmoon
dhunmoon / jsDatatableFilter.js
Created Mar 23, 2017
JS:Datatable Multiple Filters
View jsDatatableFilter.js
/* Custom filtering function which will search data in column four between two values */
$.fn.dataTable.ext.search.push(
function( settings, data, dataIndex ) {
var min = parseInt( $('#min').val(), 10 );
var max = parseInt( $('#max').val(), 10 );
var age = parseFloat( data[3] ) || 0; // use data for the age column
if ( ( isNaN( min ) && isNaN( max ) ) ||
( isNaN( min ) && age <= max ) ||
( min <= age && isNaN( max ) ) ||
View jsCSVFileCreation.js
var data = [["name1", "city1", "some other info"], ["name2", "city2", "more info"]];
var csvContent = "data:text/csv;charset=utf-8,";
data.forEach(function(infoArray, index){
dataString = infoArray.join(",");
csvContent += index < data.length ? dataString+ "\n" : dataString;
});
var encodedUri = encodeURI(csvContent);
window.open(encodedUri);
@dhunmoon
dhunmoon / jsCSVFileCreationBlob.js
Created Mar 22, 2017
JS: CSV File Creation Using BLOB
View jsCSVFileCreationBlob.js
function exportToCsv(filename, rows) {
var processRow = function (row) {
var finalVal = '';
for (var j = 0; j < row.length; j++) {
var innerValue = row[j] === null ? '' : row[j].toString();
if (row[j] instanceof Date) {
innerValue = row[j].toLocaleString();
};
var result = innerValue.replace(/"/g, '""');
if (result.search(/("|,|\n)/g) >= 0)
View idDetection.js
/*Default string of IE 10:
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)
Default string of IE 11:
Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko
Default string of IE 12 (aka Edge):
Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0
Default string of Edge 13 (thx @DrCord):
@dhunmoon
dhunmoon / jsGetOuterHTML.js
Last active Jan 18, 2018
JS:Get Outer HTML
View jsGetOuterHTML.js
$('<div>').append($('#ID').clone()).html();
@dhunmoon
dhunmoon / jsEscapeEvent.js
Created Feb 2, 2017
JS: Escape Key Event
View jsEscapeEvent.js
$(document).keyup(function(e) {
if (e.keyCode == 27) { // escape key maps to keycode `27`
// <DO YOUR WORK HERE>
}
});
@dhunmoon
dhunmoon / jsCheckLeapYear.js
Last active Feb 2, 2017
JS: Check Leap Year
View jsCheckLeapYear.js
if(month == 2){//Means if it is month of feborary
if(nyear %4 == 0 && ( nyear %400 != 0 || nyear %100 !== 0 ){//Leap year statement
return true
}
}else{
return false;
}