Skip to content

Instantly share code, notes, and snippets.

@mcraz
mcraz / array_mode_fastest.js
Created April 9, 2018 20:40
Shortest ways to find Mode (highest occurring element & it's frequency)
// This is most concise I could get without compromising on speed.
const arr = [1, 5, 2, 2, 5, 5, 6, 6, 5, 6, 6, 2, 3, 3, 5, 3];
let f = new Map();
for (let i = 0; i < arr.length; i++)
f.set(arr[i], f.has(arr[i]) ? f.get(arr[i]) + 1 : 1);
let l = arr[0];
for (let i = 1; i < arr.length; i++)
@mcraz
mcraz / Time Since JS
Created April 27, 2014 16:13
Get time difference in human readable format
function timeSince(date) {
var seconds = Math.floor((new Date() - date) / 1000);
var interval = Math.floor(seconds / 31536000);
if (interval > 1) {
return interval + " years";
}
interval = Math.floor(seconds / 2592000);
@mcraz
mcraz / HTML5 : Image preview on upload
Created January 12, 2014 07:21
Preview image on upload
<!-- Source : http://stackoverflow.com/questions/14069421/in-html5-how-to-show-preview-of-image-before-upload -->
<img id="uploadPreview" style="width: 100px; height: 100px;" />
<input id="uploadImage" type="file" name="myPhoto" onchange="PreviewImage();" />
<script type="text/javascript">
function PreviewImage() {
var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("uploadImage").files[0]);
@mcraz
mcraz / JavaScript - Center Popup
Created January 3, 2014 05:56
Create & Center Popup Windows (Also on dual monitor)
function PopupCenter(url, title, w, h) {
// Fixes dual-screen position Most browsers Firefox
var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
var left = ((width / 2) - (w / 2)) + dualScreenLeft;
var top = ((height / 2) - (h / 2)) + dualScreenTop;
@mcraz
mcraz / PHP - MySQL DATETIME Format
Created December 25, 2013 08:28
PHP Date function MySQL DATETIME Format
date("Y-m-d H:i:s")
@mcraz
mcraz / .htaccess - Codeigniter
Created December 24, 2013 07:46
.htaccess Template for Codeingiter Projects
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
@mcraz
mcraz / PHP - XML by URL & CDATA to Text
Created December 23, 2013 06:02
Get XML from URL (CDATA as Text)
public function getXML($uri = NULL)
{
$xml = simplexml_load_file($uri, "SimpleXMLElement", LIBXML_NOCDATA);
return $xml;
}
@mcraz
mcraz / JavaScript - Numeric Input Only
Last active December 31, 2015 10:58
Accept numeric input only using JavaScript
onkeypress="if ( isNaN( String.fromCharCode(event.keyCode) )) return false;"
Example Use : <input type="text" name="length" value="" class="form-control " placeholder="Video Duration (in seconds)" autocomplete="off" onkeypress="if ( isNaN( String.fromCharCode(event.keyCode) )) return false;">
@mcraz
mcraz / JavaScript - Open popup & write something
Created December 9, 2013 16:11
Open a popup & write something
var newWindow = window.open("","Test","width=300,height=300,scrollbars=1,resizable=1")
newWindow.document.open()
newWindow.document.write(responseText)
newWindow.document.close()
@mcraz
mcraz / JavaScript - AJAX Loading Icon Processor
Created December 9, 2013 14:44
Show spinning refresh icon (FontAwesome) when AJAX calls are in progress.
// AJAX Loading Proccesor
$("#data-terminal-processor").bind("ajaxStart", function(){
$(this).removeClass("icon-desktop").addClass("icon-refresh icon-spin");;
$("#display-teminal-header").hide();
}).bind("ajaxStop", function(){
$(this).removeClass("icon-refresh icon-spin").addClass("icon-desktop");;
$("#display-teminal-header").show();
});