Skip to content

Instantly share code, notes, and snippets.

@jesusgoku
jesusgoku / change-userpic.sh
Last active October 29, 2023 19:40
MacOS change user pic from shell `./change-userpic.sh <username> <path-to-jpg>`
#!/bin/bash
set -e
declare -x USERNAME="$1"
declare -x USERPIC="$2"
declare -r DSIMPORT_CMD="/usr/bin/dsimport"
declare -r ID_CMD="/usr/bin/id"
@jesusgoku
jesusgoku / json-stringify-replacer.js
Created February 14, 2023 20:55
JSON.stringify and JSON.parse with custom replacer and reviver
const someObj = {
map: new Map([['uno', 1], ['dos', 2]]),
set: new Set([1,2,3,4,5,5,6,7]),
obj: { uno: 1, dos: 2 },
arr: [1,2,3,4,5,5,6,7],
date: new Date('1991-12-15T00:00:00-0300'),
dateISO: '1991-12-15T00:00:00-0300',
@jesusgoku
jesusgoku / promise-static-methods.js
Created January 26, 2023 01:03
Implementation of Promise static methods
function promisesAll(promisesCollection) {
return new Promise((resolve, reject) => {
const resolved = [];
promisesCollection.forEach((promise, index) => {
promise
.then((value) => {
resolved.push({ index, value });
if (resolved.length === promisesCollection.length) {
@jesusgoku
jesusgoku / smtp_credentials_generate.js
Created November 15, 2022 21:19
Obtaining Amazon SES SMTP credentials by converting existing AWS credentials (NodeJS version)
#!/usr/bin/env node
/**
* Obtaining Amazon SES SMTP credentials by converting existing AWS credentials
*
* @author Jesus Urrutia <jesus.urrutia@gmail.com>
*
* Script based on:
* https://docs.aws.amazon.com/ses/latest/dg/smtp-credentials.html
*/
@jesusgoku
jesusgoku / json.js
Last active March 18, 2021 03:24
Joi Extensions
/* eslint-disable consistent-return */
function joiExtensionJson(joi) {
return {
type: 'json',
base: joi.any(),
messages: {
'json.base': '{{#label}} is not a valid json',
'json.schema': '{{#label}} does not meet schema. ({{#error}})',
git --git-dir=../<some_other_repo>/.git \
format-patch -k -1 --stdout <commit SHA> | \
git am -3 -k
@jesusgoku
jesusgoku / usePromise.js
Created August 11, 2020 13:34
React Hook for manage state when call a Promise
/**
*
* @typedef {Object} UsePromiseOptions
* @property {boolean} [lazy=false] - when true, Promise not call into first render
*
* @typedef {Object} UsePromiseState
* @property {*} data - data resolved by Promise
* @property {Error|null} error - data rejected by Promise, if exists
* @property {boolean} loading - when true, Promise is pending
* @property {Function} callPromise - manually call Promise
@jesusgoku
jesusgoku / sudoku_solve.py
Created August 7, 2020 15:43
Sudoku solve algorithm show in Computerphile: https://www.youtube.com/watch?v=G_UYXzGuqvM
grid = [ [4, 0, 0, 0, 0, 5, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 9, 8],
[3, 0, 0, 0, 8, 2, 4, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 8, 0],
[9, 0, 3, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 3, 0, 6, 7, 0],
[0, 5, 0, 0, 0, 9, 0, 0, 0],
[0, 0, 0, 2, 0, 0, 9, 0, 7],
[6, 4, 0, 3, 0, 0, 0, 0, 0], ]
@jesusgoku
jesusgoku / axios-timeout-retry.js
Created August 1, 2020 00:00
Wrapper for axios with timeout and retry
class Request {
constructor(baseURL, { timeout, ...options } = {}) {
this.timeout = timeout;
this.client = axios.create({
baseURL,
...options,
});
return new Proxy(this, {
get(target, property) {
@jesusgoku
jesusgoku / stdin.sh
Created July 25, 2020 03:14
Read from stdin with bash
#!/usr/bin/env bash
# while read line; do
# echo "${line}"
# done < "${1:-/dev/stdin}"
# cat "${1:-/dev/stdin}"
# cat <&0