Skip to content

Instantly share code, notes, and snippets.

View dmiro's full-sized avatar

David Miró dmiro

View GitHub Profile
@dmiro
dmiro / runFunction.js
Last active December 12, 2015 07:48
Invoke function inside class You must Insert the snippet inside the class.
runFunction : function (name) {
this[name]();
};
@dmiro
dmiro / enumEmulate.js
Last active December 16, 2015 22:09
Javascript ENUM
function Enum() {
var that = this;
for (var i in arguments) {
that[arguments[i]] = i;
}
this.name = function(value) {
for (var key in that) {
if (that[key] == value) {
return key;
}
@dmiro
dmiro / stringFormat.js
Last active December 17, 2015 00:09
String format emulate
// Format emulate
String.prototype.format = function () {
var args = arguments;
return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function (m, n) {
if (m == "{{") { return "{"; }
if (m == "}}") { return "}"; }
return args[n];
});
};
@dmiro
dmiro / stringArrayFormat.js
Last active December 17, 2015 00:09
Apply string format to all elements of array and return result
// apply string format to all elements of array and return result
Array.prototype.format=function(prefix)
{
var result = [];
this.forEach(function(entry) {
result.push(prefix.format(entry));
});
return result;
};
@dmiro
dmiro / lastArrayElement.js
Last active December 17, 2015 00:29
Return last element of array
// return last element of array
Array.prototype.last = Array.prototype.last || function() {
var l = this.length;
return this[l-1];
}
@dmiro
dmiro / validDate.js
Created May 11, 2013 18:06
Validate date
var isValidDate = function(d) {
if (Object.prototype.toString.call(d) !== "[object Date]") return false;
return !isNaN(d.getTime());
}
@dmiro
dmiro / delrepOccurrences.js
Last active December 17, 2015 06:09
Delete or replace all pattern occurrences
//delete all pattern occurrences
String.prototype.deleteOccurrences = function (pattern) {
return this.str.replace (RegExp(pattern,'gi'),"");
};
//replace all pattern occurrences
String.prototype.replaceOccurrences = function (pattern, replace) {
return this.str.replace (RegExp(pattern,'gi'),replace);
};
@dmiro
dmiro / dateFormat.js
Last active December 17, 2015 07:49
Date.prototype.format = function(fstr, utc) {
var that = this;
utc = utc ? 'getUTC' : 'get';
return fstr.replace (/%[YmdHMS]/g, function (m) {
switch (m) {
case '%Y': return that[utc + 'FullYear'] ();
case '%m': m = 1 + that[utc + 'Month'] (); break;
case '%d': m = that[utc + 'Date'] (); break;
case '%H': m = that[utc + 'Hours'] (); break;
case '%M': m = that[utc + 'Minutes'] (); break;
@dmiro
dmiro / hasOwnProperty_correct_use.js
Last active August 8, 2017 19:53
hasOwnProperty correct use
var foo = {
hasOwnProperty: function() {
return false;
},
bar: 'Here be dragons'
};
foo.hasOwnProperty('bar'); // siempre devolverá false
// Utilice otro objeto con hasOwnProperty y llamelo con 'this' para asignarlo a foo
({}).hasOwnProperty.call(foo, 'bar'); // true
@dmiro
dmiro / RoundDecimals.js
Created May 27, 2013 13:39
Round decimals
var original=28.453
//round "original" to two decimals
var result=Math.round(original*100)/100 //returns 28.45
// round "original" to 1 decimal
var result=Math.round(original*10)/10 //returns 28.5
//round 8.111111 to 3 decimals
var result=Math.round(8.111111*1000)/1000 //returns 8.111