Skip to content

Instantly share code, notes, and snippets.

View yuriuliam's full-sized avatar
💻

Yuri Uliam yuriuliam

💻
  • São Paulo, State of São Paulo, Brazil
View GitHub Profile
/**
* Creates a timer
* @param {number} secondsAmount Amount of time in seconds
* @param {number} minutesAmount Amount of time in minutes
* @param {number} hoursAmount Amount of time in hours
* @param {number} daysAmount Amount of time in days
* @returns an object representing the timer.
*/
function createTimer(
secondsAmount = 0,
/**
* Wait for given amount of milliseconds.
* @param {number} ms time in milliseconds.
* @returns {Promise<void>}
*/
const wait = async (ms: number): Promise<void> =>
new Promise(resolve => void setTimeout(resolve, ms))
@yuriuliam
yuriuliam / locales.ts
Last active April 2, 2022 19:33
[TS/JS] some locale functions and utilities
type LocaleTag = "af-ZA" | "am-ET" | "ar-AE" | "ar-BH" | "ar-DZ" | "ar-EG" | "ar-IQ" | "ar-JO" | "ar-KW" | "ar-LB" | "ar-LY" | "ar-MA" | "ar-OM" | "ar-QA" | "ar-SA" | "ar-SD" | "ar-SY" | "ar-TN" | "ar-YE" | "arn-CL" | "as-IN" | "az-Cyrl-AZ" | "az-Latn-AZ" | "az-az" | "ba-RU" | "be-BY" | "bg-BG" | "bn-BD" | "bn-IN" | "bo-CN" | "br-FR" | "bs-Cyrl-BA" | "bs-Latn-BA" | "ca-ES" | "co-FR" | "cs-CZ" | "cy-GB" | "da-DK" | "de-AT" | "de-CH" | "de-DE" | "de-LI" | "de-LU" | "dsb-DE" | "dv-MV" | "el-CY" | "el-GR" | "en-029" | "en-AU" | "en-BZ" | "en-CA" | "en-GB" | "en-IE" | "en-IN" | "en-JM" | "en-MT" | "en-MY" | "en-NZ" | "en-PH" | "en-SG" | "en-TT" | "en-US" | "en-ZA" | "en-ZW" | "en-cb" | "es-AR" | "es-BO" | "es-CL" | "es-CO" | "es-CR" | "es-DO" | "es-EC" | "es-ES" | "es-GT" | "es-HN" | "es-MX" | "es-NI" | "es-PA" | "es-PE" | "es-PR" | "es-PY" | "es-SV" | "es-US" | "es-UY" | "es-VE" | "et-EE" | "eu-ES" | "fa-IR" | "fi-FI" | "fil-PH" | "fo-FO" | "fr-BE" | "fr-CA" | "fr-CH" | "fr-FR" | "fr-LU" | "fr-MC" | "fy-NL" | "ga
[
"af-ZA",
"am-ET",
"ar-AE",
"ar-BH",
"ar-DZ",
"ar-EG",
"ar-IQ",
"ar-JO",
"ar-KW",
@yuriuliam
yuriuliam / array-split.ts
Last active April 2, 2022 19:32
[TS/JS] Array split functions. One to split based on it's size, the other based on a given predicate.
/**
*
* @param {Array<T>} array The array to be sorted.
* @param {number} size The size of the array.
* @returns {Array<Array<T>>} The sorted array.
* @template {T} T The type of the array.
*/
function splitArrayBySize<T>(array: Array<T>, size: number): Array<Array<T>> {
if (!Array.isArray(array)) {
throw new TypeError(`Expected an array, received ${typeof array}`)
@yuriuliam
yuriuliam / colors.ts
Last active March 4, 2024 06:15
A way to convert colors from one format to another by using Math Algorithms
import { isNumber, isBetween } from './numbers'
type ARGB32 = number
/**
* Represents a red-green-blue key-value object.
* all values are limited from 0 to 255 when used internally.
*/
type RGB = { red: number; green: number; blue: number }
type RGBA = RGB & { alpha: number }
type RGBA32 = number
const createPageOptions = ((currentPage, pageCount) => {
// from component properties
const currentPage = currentPage
// from Constants
const NUM_OF_PAGE_OPTIONS = 7
// useMemo with [currentPage] as deps
const pageOptions = (() => {
if (pageCount <= 0) return []
@yuriuliam
yuriuliam / hash.ts
Created October 24, 2023 17:09
A simple hash function to verify integrity between values
const hash = (str: string) => {
let value = 0;
for (let i = 0; i < str.length; i += 1) {
const current = str.charCodeAt(i);
value += current << (i % 16);
}
return value.toString(16).padStart(16, '0');
@yuriuliam
yuriuliam / createValidator.ts
Created December 7, 2023 07:12
create a simple, message-based abstract validator
type ValidateRuleFn<T> = (data: T) => string[] | null
type Priority = 'WARN' | 'ERROR'
type RuleTuple<T> = [priority: Priority, validateRule: ValidateRuleFn<T>]
type ValidationResult = { errors: string[], warnings: string[] }
const RESULT_KEYS: Record<Priority, string> = {
ERROR: 'errors',
@yuriuliam
yuriuliam / promiseAllWithProgress.ts
Created July 5, 2024 17:54
run Promise.all while being aware of the current progress
type OnPromiseProgressCallback = (resolvedPromises: number) => void;
/**
* Run `Promise.all` for all given promises inside an array.
* For each item complete it will call the given `onProgress` callback.
*
* NOTE: It will call the `onProgress` at the end as well, since asynchronous
* code is a mess to deal with in Javascript.
*/
const promiseAllWithProgress = async <TResult>(