Skip to content

Instantly share code, notes, and snippets.

View eriandev's full-sized avatar
❇️
erian.dev

Erick Vargas eriandev

❇️
erian.dev
View GitHub Profile
@eriandev
eriandev / github_gpg_key.md
Created March 2, 2024 17:06 — forked from ankurk91/github_gpg_key.md
Signing git commits using GPG (Ubuntu/Mac)

Github : Signing commits using GPG (Ubuntu/Mac) 🔐

  • Do you have an Github account ? If not create one.
  • Install required tools
  • Latest Git Client
  • gpg tools
# Ubuntu
sudo apt-get install gpa seahorse
# MacOS with https://brew.sh/
@eriandev
eriandev / pipe.js
Created July 21, 2022 16:59 — forked from henrik1/pipe.js
Left-to-right function composition. First function can have any arity while latter functions must be unary (taking only one argument)
const pipe = (functions, ...args) => (
functions.reduce(
(prev, next) => Array.isArray(prev) ? next(...prev) : next(prev),
args
)
);
pipe([Math.abs, Math.floor, val => -val], 4.20); // = -4
pipe([(a, b) => a - b, Math.abs], 5, 10); // = 5
@eriandev
eriandev / bifurcateBy.js
Created July 21, 2022 16:59 — forked from henrik1/bifurcateBy.js
Split the values of a given list into two lists, one containing values the predicate function evaluates to truthy, the other one containing the falsy
const bifurcateBy = (predicate, list) =>
list.reduce((acc, val, i) => (
acc[predicate(val, i) ? 0 : 1].push(val), acc),
[[], []]
);
bifurcateBy(val => val > 0, [-1, 2, -3, 4]);
// = [[2, 4], [-1, -3]]
@eriandev
eriandev / findKey.js
Created July 21, 2022 16:59 — forked from henrik1/findKey.js
Find the first key within an index that satisfies a given predicate.
const findKey = (predicate, index) => Object
.keys(index)
.find(key => predicate(index[key], key, index));
findKey(
car => !car.available,
{
tesla: { available: true },
ford: { available: false },
gm: { available: true }
@eriandev
eriandev / descending.js
Created July 21, 2022 16:59 — forked from henrik1/descending.js
Creates a descending comparator-function given a valuating function.
const descending = (fn) => (a, b) => {
const valA = fn(b);
const valB = fn(a);
return valA < valB ? -1 : valA > valB ? 1 : 0;
}
const byPrice = descending(val => val.price);
[{ price: 300 }, { price: 100 }, { price: 200 }].sort(byPrice);
// = [{ price: 300 }, { price: 200 }, { price: 100 }]
@eriandev
eriandev / ascending.js
Created July 21, 2022 16:59 — forked from henrik1/ascending.js
Creates an ascending comparator function given a valuating function
const ascending = (fn) => (a, b) => {
const valA = fn(a);
const valB = fn(b);
return valA < valB ? -1 : valA > valB ? 1 : 0;
}
const byPrice = ascending(val => val.price);
[{ price: 300 }, { price: 100 }, { price: 200 }].sort(byPrice);
// = [{ price: 100 }, { price: 200 }, { price: 300 }]
@eriandev
eriandev / sumBy.js
Created July 21, 2022 16:59 — forked from henrik1/sumBy.js
Calculate the sum of all elements given by a valuating function
const sumBy = (fn, list) =>
list.reduce((prev, next) => prev + fn(next), 0);
sumBy(product => product.price, [
{ name: 'pizza', price: 10 },
{ name: 'pepsi', price: 5 },
{ name: 'salad', price: 5 },
]); // = 20
@eriandev
eriandev / dropWhile.js
Created July 21, 2022 16:59 — forked from henrik1/dropWhile.js
Drops elements from a list until some condition is met
const dropWhile = (pred, list) => {
let index = 0;
list.every(elem => {
index++;
return pred(elem);
});
return list.slice(index-1);
}
dropWhile(val => (val < 5), [1,2,3,4,5,6,7]); // = [5,6,7]
@eriandev
eriandev / distance.js
Created July 21, 2022 16:59 — forked from henrik1/distance.js
Calculates the Euclidean distance between two points.
const distance = ([x0, y0], [x1, y1]) => (
Math.hypot(x1 - x0, y1 - y0)
);
distance([0, 1], [5, 4]); // = 5.8309518948453
@eriandev
eriandev / recoverWith.js
Created July 21, 2022 16:59 — forked from henrik1/recoverWith.js
Recover from eventual errors with the given value
const recoverWith = async (defaultValue, fn, ...args) => {
try {
const result = await fn(...args);
return result;
} catch (_e) {
return defaultValue;
}
}
recoverWith('A', val => val, 'B'); // = B