Skip to content

Instantly share code, notes, and snippets.

View jherax's full-sized avatar
👾
Performance focused

David Rivera jherax

👾
Performance focused
View GitHub Profile
@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 / 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 / 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 / getValueByKey.js
Last active May 30, 2020 15:41
Gets the first value associated to an object's property
/**
* Gets the first value associated to an object's property.
* Can go recursively through objects and arrays.
*
* @param {Object | Array} obj: the object or array to look for the property/index value
* @param {String | Number} key: the object's property name or array index
* @return {Any}
*/
function getValueByKey(obj, key) {
let value;
@jherax
jherax / storage-available.js
Last active May 30, 2020 15:40
Detect availability of Web Storage
/**
* Checks whether a storage mechanism is available.
* @param {String} storageType: it can be "localStorage", "sessionStorage" or any global object that implements Web Storage interface.
* @return {Boolean}
*/
function storageAvailable(storageType) {
var storage = window[storageType];
var data = '__proxy-storage__';
try {
storage.setItem(data, data);
@jherax
jherax / sortBy.js
Last active May 31, 2021 07:50
Sorts an array with multiple ordering criteria (Schwartzian transform)
/**
* Sorts an array and allows multiple sorting criteria.
*
* It applies the Schwartzian transform:
* https://en.wikipedia.org/wiki/Schwartzian_transform
*
* Author: David Rivera
* Github: https://github.com/jherax
*
* You can fork this project on github:
@jherax
jherax / sumValues.js
Last active May 30, 2020 15:40
Sums all the values in an array by reducing it
/**
* Sums all the values in an array by reducing it.
*
* @param {Array} array: the collection to iterate
* @param {String | Number} prop: (optional) property name or array index
* @return {Number}
*/
function sumValues(array, prop) {
if (!array || !array.length) return 0;
if ((/string|number/).test(typeof prop)) {
@jherax
jherax / flatten-es6.js
Last active May 30, 2020 15:39
Merges (flattens) all inner arrays into one-level depth array
/**
* Merges all inner arrays into one-level depth array.
*
* @param {Array} array: the array to be flattened
* @return {Array}
*/
const flatten = array => array.reduce(
(flattened, cv) => flattened.concat(Array.isArray(cv) ? flatten(cv) : cv),
[] // initial value of flattened array
);
@jherax
jherax / randomNumbers.js
Last active May 30, 2020 15:39
Generates an array of unique values given a range
/**
* Returns a random integer between min and max
*
* @export
* @param {Number} min: minimum value (inclusive)
* @param {Number} max: maximum value (inclusive)
* @return {Number}
*/
function randomInt(min, max) {
return Math.floor(Math.random() * (1 + max - min)) + min;
@jherax
jherax / repeatedValues.js
Last active May 30, 2020 15:39
Gets an array of repeated elements between two arrays
/**
* Gets all repeated values between two arrays
*
* @param {Array} array1: array to compare
* @param {Array} array2: array to compare
* @return {Array}
*/
function repeatedValues(array1, array2) {
return array1.reduce((repeated, value) => {
if (~array2.indexOf(value) && !~repeated.indexOf(value)) {