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 / toUrlParams.js
Last active June 20, 2023 10:22
Builds the url query parameters from an object
const isObject = (it) => it != null && typeof it === 'object';
/**
* Encodes an object to be used as query-string.
* It uses 'encodeURIComponent' to set each value.
*
* @param {object} data an object with key-value pairs to create the query-string
* @returns {string} the query-string
*/
function toUrlParams(data) {
@jherax
jherax / memoize.js
Last active July 5, 2022 21:03
High-order function that memoizes a function
/**
* @summary
* High-order function that memoizes a function, by creating a scope
* to store the result of each function call, returning the cached
* result when the same inputs is given.
*
* @description
* Memoization is an optimization technique used primarily to speed up
* functions by storing the results of expensive function calls, and returning
* the cached result when the same inputs occur again.
@jherax
jherax / docker-clean.sh
Last active May 30, 2020 15:36
Load docker and put it in the shell
# in your .zshrc
function docker_clean() {
containers=$(docker ps -a -q -f status=exited)
# echo $containers
if [ "" != "$containers" ] ; then
docker rm -v $containers
fi
}
pact install python-setuptools python-ming
pact install libxml2-devel libxslt-devel libyaml-devel
curl -skS https://bootstrap.pypa.io/get-pip.py | python
Optional/Not sure what these are for:
pip install virtualenv
curl -skS https://raw.githubusercontent.com/mitsuhiko/pipsi/master/get-pipsi.py | python
@jherax
jherax / is-private-mode.js
Last active March 19, 2024 18:29
Detect if the browser is running in Private mode - Promise based (last update: Feb 2020)
/**
* Lightweight script to detect whether the browser is running in Private mode.
* @returns {Promise<boolean>}
*
* Live demo:
* @see https://output.jsbin.com/tazuwif
*
* This snippet uses Promises. If you want to run it in old browsers, polyfill it:
* @see https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.min.js
*
@jherax
jherax / alterDate.js
Last active May 30, 2020 15:37
Adds or subtracts date portions to the given date.
/**
* Adds or subtracts date portions to the given date and returns the new date.
*
* @param {Object} options: It contains the date parts to add or remove, and can have the following properties:
* - {Date} date: if provided, this date will be affected, otherwise the current date will be used.
* - {number} minutes: minutes to add/subtract
* - {Number} hours: hours to add/subtract
* - {Number} days: days to add/subtract
* - {Number} months: months to add/subtract
* - {Number} years: years to add/subtract
@jherax
jherax / multi-entries.js
Last active November 14, 2021 17:47
Load multiple entries in webpack
var glob = require('glob');
module.exports = {
entry: toObject(glob.sync('assets/**/*.js*')),
output: {
filename: '[name].js'
},
//...
};
@jherax
jherax / isPalindrome.js
Last active January 5, 2020 13:29
Determines whether a string is a palindrome
/**
* Determines whether a text is a palindrome.
* Text is lowercased and non-alphabetic characters are removed.
*
* @param {string} text - the words to check
* @return {boolean}
*/
function isPalindrome(text) {
if (!text || text.length < 2) return false;
text = text.replace(/[\s\W]/g, '');
@jherax
jherax / stringify.js
Created October 14, 2016 21:52
Converts a JavaScript value to a JSON string
// This is a reference to JSON.stringify and provides a polyfill for old browsers.
// stringify serializes an object, array or primitive value and return it as JSON.
var stringify = (function() {
var _PRIMITIVE, _OPEN, _CLOSE;
if (window.JSON && typeof JSON.stringify === "function")
return JSON.stringify;
_PRIMITIVE = /string|number|boolean|null/;
_OPEN = {
@jherax
jherax / filterArray.js
Last active February 23, 2024 12:59
Filters an array of objects with multiple match-criteria.
/**
* Filters an array of objects using custom predicates.
*
* @param {Array} array: the array to filter
* @param {Object} filters: an object with the filter criteria
* @return {Array}
*/
function filterArray(array, filters) {
const filterKeys = Object.keys(filters);
return array.filter(item => {