Skip to content

Instantly share code, notes, and snippets.

Avatar

Alberto Martínez aMarCruz

View GitHub Profile
@aMarCruz
aMarCruz / datedif.prg
Created Feb 10, 2014
Devuelve la diferencia en años y meses de una a otra fecha.
View datedif.prg
*------------------------------------------------------------------------------
* nResult = DateDiff(tFromDate, tToDate)
*------------------------------------------------------------------------------
* Devuelve un número con la diferencia en años y meses de tFromDate a tToDate.
* Los meses están representados con decimales.
* P.Ej. ? DateDiff({^1961/03/31}, {^2013/08/18})
* Imprime 52.04 (52 años y 4 meses)
*==============================================================================
LPARAMETERS T1 AS Date, T2 AS Date
@aMarCruz
aMarCruz / isFunction
Created May 4, 2015
Returns true if argument is a function (CommonJS)
View isFunction
// Safe isFunction()
// Avoid a Chakra JIT bug in compatibility modes of IE 11.
var isFunction = function (expr) {
return typeof expr === 'function' || false;
};
if (isFunction(/./) || (Uint8Array && !isFunction(Uint8Array))) {
isFunction = (function (expr) {
var _toString = Object.prototype.toString;
@aMarCruz
aMarCruz / requestAnimationFrame_polyfill.js
Last active Aug 29, 2015
requestAnimationFrame polyfill by aMarCruz
View requestAnimationFrame_polyfill.js
// requestAnimationFrame polyfill by aMarCruz.
// 2015/01/19
// 2015/05/09 - last rev
// Adapted from https://gist.github.com/paulirish/1579671 which derived from
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// Fixes from Paul Irish, Tino Zijdel, Andrew Mao, Klemen Slavič, Darius Bacon, Erik Möller.
// MIT license
@aMarCruz
aMarCruz / indexOf_polyfill.js
Last active Aug 29, 2015
Array.prototype.indexOf polyfill by aMarCruz
View indexOf_polyfill.js
//
// https://msdn.microsoft.com/library/ff679977(v=vs.94).aspx
// https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Array/indexOf
//
Array.prototype.indexOf || (Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
'use strict';
if (this == null) {
throw new TypeError();
}
@aMarCruz
aMarCruz / precomp_tmpl.js
Created Aug 31, 2015
Alternative riot tmpl() function for precompiled expressions.
View precomp_tmpl.js
/*
-----------------------------------------------------------------------------
riot-tmpl/lib/brackets.js
brackets function returns a string or regexp based on fix '#@ #' brackets.
With a numeric parameter...
0,1 - the current left (0) or right (1) current brackets characters.
2,3 - the current left (2) or right (3) escaped brackets characters.
4 - regexp based on `/{|}/g`, matches any bracket.
@aMarCruz
aMarCruz / better-nodejs-require-paths.md
Created Oct 2, 2015 — forked from branneman/better-nodejs-require-paths.md
Better local require() paths for Node.js
View better-nodejs-require-paths.md

Better local require() paths for Node.js

Problem

When the directory structure of your Node.js application (not library!) has some depth, you end up with a lot of annoying relative paths in your require calls like:

var Article = require('../../../models/article');

Those suck for maintenance and they're ugly.

Possible solutions

@aMarCruz
aMarCruz / xmerge.js
Created Jan 22, 2016
Returns a new object with all the properties of the objects passed as parameters.
View xmerge.js
/**
* Returns a new object with all the properties of the objects passed as parameters.
* The parameters can be zero or more instances of Object or derivates.
*
* @returns {object} - Any number of objects
*/
module.exports = function xmerge () {
var target = {}
for (var i = 0; i < arguments.length; i++) {
@aMarCruz
aMarCruz / hasclass.js
Last active Apr 20, 2017
Fast hasClass function.
View hasclass.js
/**
* Detect is an element contains a class name.
*
* @param {Element} el - HTML element
* @param {string} name - Class name
* @returns {boolean} `true` if the element contains the class name.
*/
function hasClass(el, name) {
let classes = name && el && el.className
if (classes) {
@aMarCruz
aMarCruz / sanitizeForRegex.js
Created Aug 28, 2018
Escape an string for using with a Regex constructor
View sanitizeForRegex.js
function sanitizeForRegex (str) {
return String(str).replace(/(?=[.?*+^$[\]\\(){}|-])/g, '\\')
}
@aMarCruz
aMarCruz / starts_ends_with.js
Last active Dec 26, 2018
String startsWith & endsWith polyfills
View starts_ends_with.js
;(function (sp) {
if (!sp.startsWith)
sp.startsWith = function (str) {
return !!(str && this) && !this.lastIndexOf(str, 0)
}
if (!sp.endsWith)
sp.endsWith = function (str) {
var offset = str && this ? this.length - str.length : -1