Skip to content

Instantly share code, notes, and snippets.

View jherax's full-sized avatar
:atom:
Performance focused

David Rivera jherax

:atom:
Performance focused
View GitHub Profile
@jherax
jherax / fromOneLevelDepth.js
Last active May 30, 2020 15:41
Build a one-level-depth object, by moving all nested objects to the first level
// @private
var isObject = (value) =>
value != null && typeof value === 'object';
/**
* @private
* Restores the one-level-depth object to the original nested object.
*
* @param {Array} names: list of keys in the object
* @param {any} value: the value of the object to transform
@jherax
jherax / removeEmpty.js
Last active May 30, 2020 15:41
Deletes empty values from an Object or Array
var removeEmpty = (() => {
/**
* Determines if the entry parameter is not an object or array.
*
* @private
* @param {Any} v: the value to test
* @return {Boolean}
*/
const isNotObject = v => v === null || typeof v !== 'object';
@jherax
jherax / install.sh
Last active May 30, 2020 15:41 — forked from wdullaer/install.sh
Install Latest Docker and Docker-compose on Ubuntu
# Ask for the user password
# Script only works if sudo caches the password for a few minutes
sudo true
# Install kernel extra's to enable docker aufs support
# sudo apt-get -y install linux-image-extra-$(uname -r)
# Add Docker PPA and install latest version
# sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
# sudo sh -c "echo deb https://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
@jherax
jherax / $.center.js
Last active October 17, 2016 05:07
jQuery: center an element relative to another
(function ($) {
// Reverses the array of matched elements
$.fn.reverse = Array.prototype.reverse;
/**
* Centers an element relative to another.
* @signature: $(selector).center(options)
* @param {Object} options:
* Defines the options with the following properties:
@jherax
jherax / getEventHandlers.js
Last active October 25, 2023 02:42
jQuery: Gets all event handlers bound to a DOM Element
/**
* Gets all event-handlers from a DOM element.
* Events with namespace are allowed.
*
* @param {Element} node: DOM element
* @param {String} eventns: (optional) name of the event/namespace
* @return {Object}
*/
function getEventHandlers(element, eventns) {
const $ = window.jQuery;
@jherax
jherax / clone.js
Last active May 30, 2020 15:42
Clones or extends an object (deep copy) supporting objects with circular references
/**
* @author
* David Rivera (jherax)
* https://github.com/jherax
*/
/* eslint-disable no-bitwise */
/** @private */
const toString = Object.prototype.toString;
@jherax
jherax / _parseValue.js
Last active May 30, 2020 15:42
Gets values from the url search
/* eslint-disable no-underscore-dangle */
/**
* Supported types
*/
const TYPES = {
_true: /^true$/i,
_false: /^false$/i,
_null: /^null$/i,
_number: /^[0-9]+$/,
@jherax
jherax / customError.js
Last active May 30, 2020 15:43
JavaScript: Custom Error Builder
// Creates user-defined exceptions
var CustomError = (function() {
'use strict';
//constructor
function CustomError() {
//enforces 'new' instance
if (!(this instanceof CustomError)) {
return new CustomError(arguments);
}
@jherax
jherax / createNS.js
Created May 26, 2015 04:00
JavaScript: Crear namespaces
//utility to create safe namespaces
function createNS (namespace) {
var nsparts = namespace.toString().split("."),
reName = (/^[A-Za-z_]\w+/),
cparent = window,
i, subns, nspartsLength;
// we want to be able to include or exclude the root namespace so we strip it if it's in the namespace
if (nsparts[0] === "window") nsparts = nsparts.slice(1);
// loop through the parts and create a nested namespace if necessary
for (i = 0, nspartsLength = nsparts.length; i < nspartsLength; i += 1) {
@jherax
jherax / js-delete-properties.js
Created May 26, 2015 03:14
JavaScript: Eliminar propiedades
(function() {
//verificamos cual es el ámbito global
//(generalmente el objeto window)
console.log("Global scope", this);
var x = 42; //variable local
var point = { x: 10, y: 15 }; //objeto local
//si no se especifica el keyword "var",