Skip to content

Instantly share code, notes, and snippets.

View annibal's full-sized avatar

Arthur Annibal annibal

View GitHub Profile
@annibal
annibal / IIARFE.js
Last active May 21, 2024 03:48
Immediately Invoking, Anonymous, Recursive Function Expression
(function() {
const c = confirm("again?")
if (!c) return;
if (c === true) {
eval(`(${arguments.callee.toString()})()`)
}
})()
@annibal
annibal / randomUtils.js
Created April 1, 2024 20:36
Some Random utility functions
function randomRange(min=0, max=100, dec=0) {
const mag = 10 ** dec;
return Math.floor( (Math.random() * (max - min) + min) * mag ) / mag;
}
function randomLetter(uppercase = false) {
return String.fromCharCode(randomRange(uppercase ? 65 : 97, uppercase ? 90 : 122))
}
function randomString(minLength=8, maxLength=16) {
@annibal
annibal / color-tags.md
Created March 28, 2024 21:40
Colored Tags in GitHub Markdown

Using place-hold.it

  • image Task is Yet To Start
  • image Work In Progress
  • image Pull Request in Review
  • image Merged
  • image Blocked by some kind of issue

Syntax:

@annibal
annibal / generateCsv.js
Last active March 28, 2024 21:40
Function to generate controlled random CSV
function generateCsv(params) {
const { colsDef, rowsDef, withHeaders, asString } = params || {};
const formatNumber = (val, intDigits = 1, decDigits = 0) => {
if (isNaN(val)) return "";
return (+val).toLocaleString(undefined, {
minimumIntegerDigits: intDigits,
minimumFractionDigits: decDigits,
});
@annibal
annibal / docValidationHelper.js
Last active November 10, 2023 15:54
Functions to generate Brazil's Document validator number - CPF / CNPJ
/**
* Receives a part of the document and returns a validation digit
* Used mostly for CPF or CNPJ
*
* Ex.:
* getVerificationDigit("000000001") // 9
* getVerificationDigit("0000000019") // 1
* getVerificationDigit("100200300") // 8
* getVerificationDigit(1, 9) // 9
* getVerificationDigit(19, 9) // 1
@annibal
annibal / onClickMakeDots.js
Created November 8, 2023 16:55
Fn to create dots when clicking an element, Fn helper to convert these to scaled, compact array
/* const */ dotsMadeStoreArr = [];
function attachOnClickMakeDotsListener(elm) {
function onClickMakeDotsListener(evt) {
onClickMakeDots(evt);
}
elm.addEventListener("click", onClickMakeDotsListener);
return function detachOnClickMakeDotsListener() {
elm.removeEventListener("click", onClickMakeDotsListener);
/**
* Create a function that transforms an index into an pseudo-random value, where the values are tied to a string
* @param salt string, must have at least one [a-z0-9] character
* @param parseVal [optional] function(val: number, i: number, saltRange: [number, number]) to transform the salt result
* @returns function(idx: number, skipParse: boolean = false), will always return the same value for the specific SALT and IDX
*/
const getSaltedRandomFn = (salt, parseVal) => {
const safeSalt = (salt || "").replace(/[^a-z0-9]/gi, "").toLowerCase();
@annibal
annibal / jsAirflow.js
Created August 11, 2023 13:10
JS Airflow
// Main "Lib" function
/**
* Executes a flow of callbacks passing inputs and storing outputs
* Example: jsAirflow(
* [
* { fn: 'sum', in: ['v1', 'v2'], out: ['result'] },
* { fn: 'power', in: ['result', 'power'], out: ['result'] },
#!/bin/ksh
#
# @(69)$Id$
#
# Helpful functions for your terminal
function git_branches()
{
if [[ -z "$1" ]]; then
git_branches .
@annibal
annibal / input.js
Created August 14, 2019 23:04
Javascript input mapper
const getKey = inputName => (inputs[namedIndexesMap[inputName]] || {value:null}).value
//TODO gamepad
class InputMap {
constructor(inputName, type='', assignedKeyboardKeys=[], assignedGamepadButtons=[], assignedGamepadAxis=[]) {
const strArrayToKeyObj = arr => arr.reduce( (all,curr) => ({...all, [curr]: true}),{})
if (inputName == null) {
throw TypeError("Input Name cannot be null or undefined ")
}