Skip to content

Instantly share code, notes, and snippets.

View chrisgfortes's full-sized avatar

Christian Fortes chrisgfortes

View GitHub Profile
@chrisgfortes
chrisgfortes / pagination.js
Last active October 25, 2022 16:33
Pagination JS
const pagination = {
list: [],
generateListBySize: (size, predicate) => Array.from({ length: size }).map(predicate),
generatePageList: (size, range, total) => pagination.generateListBySize(size, (_, i) => range.start + i).filter(page => page <= total),
generateMapRange: (totalRange, size) => (
pagination
.generateListBySize(totalRange, (_) => ({}))
.reduce((acc, _, i) => {
const lastItem = acc?.[i - 1];
const index = i + 1;
@chrisgfortes
chrisgfortes / base64ArrayBuffer.js
Created November 18, 2021 15:38 — forked from jonleighton/base64ArrayBuffer.js
Encode an ArrayBuffer as a base64 string
// Converts an ArrayBuffer directly to base64, without any intermediate 'convert to string then
// use window.btoa' step. According to my tests, this appears to be a faster approach:
// http://jsperf.com/encoding-xhr-image-data/5
/*
MIT LICENSE
Copyright 2011 Jon Leighton
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
@chrisgfortes
chrisgfortes / linux-cedilla-USA-intl-keyboard-layout.md
Last active May 10, 2021 15:04
[Linux] How to type “ç” / cedilla when using USA intl as the keyboard layout?
@chrisgfortes
chrisgfortes / check_git_local_remote_branch_exists.sh
Created March 25, 2021 16:51
How to check if a git branch exists in the local/remote repository?
# 1. To check the branch in the local repository
# Local:
# https://stackoverflow.com/questions/21151178/shell-script-to-check-if-specified-git-branch-exists
# test if the branch is in the local repository.
# return 1 if the branch exists in the local, or 0 if not.
function is_in_local() {
local branch=${1}
local existed_in_local=$(git branch --list ${branch})
if [[ -z ${existed_in_local} ]]; then
@chrisgfortes
chrisgfortes / pull_request.sh
Last active March 24, 2021 20:22 — forked from devongovett/pull_request.sh
Bash script to make a pull request from the current git repository. Tries the upstream remote if possible, otherwise uses origin.
# put this in your .bash_profile
pull_request() {
to_branch=$1
if [ -z $to_branch ]; then
to_branch="master"
fi
# try the upstream branch if possible, otherwise origin will do
upstream=$(git config --get remote.upstream.url)
origin=$(git config --get remote.origin.url)
@chrisgfortes
chrisgfortes / index.js
Created July 16, 2020 00:44
Creating lib as command line
// command line args
const args = process.argv
@chrisgfortes
chrisgfortes / promisify.js
Last active June 5, 2020 20:35
Promisify - Function promisification
// Reference: https://stackoverflow.com/a/49250525
const promisify = func => (...args) => new Promise((resolve, reject)=> func(...args, (err, res) => err ? reject(err) : resolve(res)));
@chrisgfortes
chrisgfortes / deep-object-get.js
Created May 29, 2020 16:36
Deep diving in object to get a value (based on _.get from lodash)
const stringToPath = (path) => {
const pattern = /\[([0-9]+)\]/g;
if (pattern.test(path)) return path.replace(pattern, '.$1').split('.');
return path.split('.');
};
// First Approach:
const deepGet = (source, path, defaultValue) => {
const props = stringToPath(path);
@chrisgfortes
chrisgfortes / replace.js
Created May 27, 2020 22:06
Replace text with an identifier #{key}
/**
* @param {string} text - Text with replacement keys identified by #{key}
* @param {object} replacements - An object used to replace keys from text
* @return {string} string replaced
*/
const replace = (text = "", replacements = {}) => {
const pattern = /#\{(.*?)\}/g;
const keys = text.match(pattern) || [];
return keys.reduce((acc, key) => {
@chrisgfortes
chrisgfortes / branch-parent.js
Last active December 17, 2019 02:03
Get git branch parent name in node.js
const { promisify } = require('util');
const { exec } = require('child_process');
const run = promisify(exec);
const normalizeString = string => (string || '').trim();
function getBranchParent () {
return run(`git show-branch | grep '*' | grep -v "$(git rev-parse --abbrev-ref HEAD)" | head -n1`)
.then(resp => {
const commit = normalizeString(resp.stdout);