Skip to content

Instantly share code, notes, and snippets.

View stamat's full-sized avatar
💥

Nikola Stamatović stamat

💥
View GitHub Profile
@stamat
stamat / types.js
Last active December 16, 2015 00:49
JavaScript data types
//Thanks to perfectionkills.com <http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/>
__getClass = function(val) {
return Object.prototype.toString.call(val)
.match(/^\[object\s(.*)\]$/)[1];
};
var whatis = function(val) {
if(val === undefined)
return 'undefined';
@stamat
stamat / function-overloading.js
Last active December 16, 2015 00:49
JavaScript function overloading
var def = function(functions, parent) {
return function() {
var types = [];
var args = [];
eachArg(arguments, function(i, elem) {
args.push(elem);
types.push(whatis(elem));
});
if(functions.hasOwnProperty(types.join())) {
return functions[types.join()].apply(parent, args);
@stamat
stamat / namespaces.js
Last active December 16, 2015 03:39
JavaScript namespace declaration
var namespace = function(str, root) {
var chunks = str.split('.');
if(!root)
root = window;
var current = root;
for(var i = 0; i < chunks.length; i++) {
if (!current.hasOwnProperty(chunks[i]))
current[chunks[i]] = {};
current = current[chunks[i]];
}
@stamat
stamat / setinterval.py
Created April 12, 2013 10:50
Python setInterval() equivalent
import threading
def set_interval(func, sec):
def func_wrapper():
set_interval(func, sec)
func()
t = threading.Timer(sec, func_wrapper)
t.start()
return t
@stamat
stamat / require.js
Last active October 31, 2017 20:04
JavaScript require / import / include modules through namespaces
var _rmod = _rmod || {}; //require module namespace
_rmod.LOADED = false;
_rmod.on_ready_fn_stack = [];
_rmod.libpath = '';
_rmod.imported = {};
_rmod.loading = {
scripts: {},
length: 0
};
@stamat
stamat / get_goodreads.php
Last active December 16, 2015 08:59
Get Goodreads.com books from the shelf and return JSON with no cover fallback
<?php
function request($url) {
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($c, CURLOPT_TIMEOUT, 5);
curl_setopt($c, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$response = curl_exec($c);
@stamat
stamat / request.php
Created April 18, 2013 02:31
CURL request method
<?php
function request($url) {
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($c, CURLOPT_TIMEOUT, 5);
curl_setopt($c, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$response = curl_exec($c);
@stamat
stamat / fancytime.js
Created May 3, 2013 20:52
Twitter-like fancy time
function fancyDate(then, now, suffix) {
if(now === undefined)
now = new Date();
if(suffix === undefined)
suffix = 'ago';
var thenMs = null;
typeof then === 'number' ? thenMs = then : thenMs = then.getTime();
var nowMs = null;
@stamat
stamat / equal.js
Last active August 19, 2016 15:29
Compare objects and arrays...
//Returns the object's class, Array, Date, RegExp, Object are of interest to us
var getClass = function(val) {
return Object.prototype.toString.call(val)
.match(/^\[object\s(.*)\]$/)[1];
};
//Defines the type of the value, extended typeof
var whatis = function(val) {
if (val === undefined)
@stamat
stamat / Map.js
Last active October 10, 2016 22:08
Java like Map class for JavaScript, a wrapper arround JavaScript's native key value hash tree storage inside objects.
/**
* @file IVARTECH Map data class
* @author Nikola Stamatovic Stamat <stamat@ivartech.com>
* @copyright IVARTECH http://ivartech.com
* @version 20130313
*
* @namespace ivar.data
*/
/**