Skip to content

Instantly share code, notes, and snippets.

View aMarCruz's full-sized avatar

Alberto Martínez aMarCruz

  • Quits
  • México
View GitHub Profile
@aMarCruz
aMarCruz / sanitizeForRegex.js
Created August 28, 2018 14:30
Escape an string for using with a Regex constructor
function sanitizeForRegex (str) {
return String(str).replace(/(?=[.?*+^$[\]\\(){}|-])/g, '\\')
}
@aMarCruz
aMarCruz / hasclass.js
Last active April 20, 2017 19:51
Fast hasClass function.
/**
* 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 / xmerge.js
Created January 22, 2016 08:10
Returns a new object with all the properties of the objects passed as parameters.
/**
* 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 / better-nodejs-require-paths.md
Created October 2, 2015 14:07 — forked from branneman/better-nodejs-require-paths.md
Better local require() paths for Node.js

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 / precomp_tmpl.js
Created August 31, 2015 19:30
Alternative riot tmpl() function for precompiled expressions.
/*
-----------------------------------------------------------------------------
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 / starts_ends_with.js
Last active December 26, 2018 02:01
String startsWith & endsWith polyfills
;(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
@aMarCruz
aMarCruz / indexOf_polyfill.js
Last active August 29, 2015 14:20
Array.prototype.indexOf polyfill by aMarCruz
//
// 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 / requestAnimationFrame_polyfill.js
Last active August 29, 2015 14:20
requestAnimationFrame polyfill by aMarCruz
// 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 / classList_polyfill_ie9.js
Last active March 12, 2021 09:27
classList polyfill for IE9+ and modern browsers
/*
* classList.js MOD by aMarCruz
* 2015-05-07
* Supports IE9+ and modern browsers.
*
* classList.js: Cross-browser full element.classList implementation.
* 1.1.20150312
*
* By Eli Grey, http://eligrey.com
* License: Dedicated to the public domain.
@aMarCruz
aMarCruz / isFunction
Created May 4, 2015 08:12
Returns true if argument is a function (CommonJS)
// 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;