Skip to content

Instantly share code, notes, and snippets.

View jerolan's full-sized avatar

Jerome Olvera jerolan

View GitHub Profile
/**
* From https://v1.dinerojs.com/dinero.js
* Allocates the amount of a Dinero object according to a list of ratios.
*
* Sometimes you need to split monetary values but percentages can't cut it without adding or losing pennies.
* A good example is invoicing: let's say you need to bill $1,000.03 and you want a 50% downpayment.
* If you use {@link module:Dinero~percentage percentage}, you'll get an accurate Dinero object but the amount won't be billable: you can't split a penny.
* If you round it, you'll bill a penny extra.
* With {@link module:Dinero~allocate allocate}, you can split a monetary amount then distribute the remainder as evenly as possible.
*
function flip(arr, right) {
let temp;
let left = 0;
while (left < right) {
temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
// scroll
interface Success<a> {
value: a;
}
interface Failure<e> {
error: e;
}
type Result<a, e> = Success<a> | Failure<e>;
@jerolan
jerolan / sharpImageOptim.js
Last active April 27, 2021 15:23
sharpImageOptim.js
const sharp = require('sharp');
const fs = require('fs');
const directory = './public';
const extensions = ['jpeg', 'jpg', 'png', 'webp', 'avif', 'gif', 'svg', 'tiff'];
const breakpoints = [640, 768, 1024, 1280, 1536];
fs.readdirSync(directory)
.filter(filterImagePath)
.forEach((dir) => {
// stringChallenge va a divide el problema en dos pequeñas soluciones
// conocer el valor en 24 horas, para hacer las fácil una comparación de tiempo entre 0 y 24
// convertir las horas en minutos para hacer una diferencia simple
function stringChallenge(strArr) {
// pair nos va a servir como auxiliar para comparar los elementos en el arreglo
// podríamos hacer una doble iteración y seria una solución fácil, pero no optima
let pair = [];
// min es el valor actual, de la diferencia entre las horas
// la estaremos actualizando a medida que hagamos las comparaciones
let min = 0;
function largestPair(num) {
let pair = null;
num
.toString()
.split("")
.forEach((val, index, arr) => {
if (arr.length - 1 !== index) {
let currentPair = parseInt(val + arr[index + 1]);
if (pair < currentPair) pair = currentPair;
function simpleAdding(num) {
let sum = 0;
for (let i = 0; i <= num; i++) sum += i;
return sum;
}
function capitalize(word) {
return word.replace(word[0], word[0].toUpperCase())
}
function letterCapitalize(str) {
return str.split(" ").map(capitalize).join(" ");
}
const regex = /\W|_/g;
function longest(acc, elem) {
if (acc.length < elem.length && !regex.test(elem)) return elem;
return acc
}
function longestWord(sen) {
return sen.split(" ").reduce(longest, "");
}
/**
* First Reverse
* Have the function FirstReverse(str) take the str parameter
* being passed and return the string in reversed order.
* For example: if the input string is "Hello World and Coders"
* then your program should return the string sredoC dna dlroW olleH.
*
* Test Cases
* > firstReverse "Hello World and Coders"
* "sredoC dna dlroW olleH"