Skip to content

Instantly share code, notes, and snippets.

View gruhh's full-sized avatar

Christhian Gruhh gruhh

View GitHub Profile
@gruhh
gruhh / validate-cpf.ts
Created January 26, 2023 00:53
Validate Brazilian CPF number with Typescript
/*
* Validate Brazilian CPF number with Typescript
*
* @param {string} cpf - A string because can start with 0
*/
export const validateCpf: (cpf: string) => (boolean) = (cpf) => {
const _cpf = cpf.replace(/\D/g, '');
if (_cpf.length < 11) {
return false;
@gruhh
gruhh / middle-truncate-text.ts
Created January 28, 2023 11:59
Truncate a string based on the number of characters before and after
/*
* Middle Truncate Text
*
* Example: middleTruncateText('123456789', 3), returns "123...789"
*
* @param {string} text - The text to be truncated
* @param {number} visibleSize - Number of chars on each side (default 5)
* @param {string} ellipsis - The string to place on truncated text (default '...')
*/
export const middleTruncateText: (
@gruhh
gruhh / array-shuffle.js
Created May 25, 2024 13:54
Function to shuffle the values of an array (Use case: simulating dice rolls for games without an RNG)
/**
* Shuffles an array and optionally returns a specified number of elements from the shuffled array.
*
* @param {Array} arr - The array to be shuffled.
* @param {number} [size=0] - The number of elements to return from the shuffled array.
* If 0, the entire shuffled array is returned. Defaults to 0.
* @returns {Array} A new array containing the shuffled elements. If `size` is specified,
* the array will contain only the first `size` elements of the shuffled array.
*
* @example
@gruhh
gruhh / Circumference.cs
Created May 25, 2024 13:58
Zero-One Circumference Clamp in CSharp
/// <summary>
/// Class Circumference
/// </summary>
public static class Circumference
{
/// <summary>
/// Clamps the circumference to a [0..1] float
/// <example>For example:
/// <code>
/// float f = ZeroOneClamp(1.2f);
@gruhh
gruhh / UISafeArea.cs
Created May 25, 2024 14:00
Unity UI safe area to canvas
using UnityEngine;
/// <summary>
/// This is a simple implementation of applying the screen's safe area to the UI.
///
/// To use it, create a Panel as child of your Canvas object,
/// configure it to cover the entire Canvas (center pivot and stretched in x and y);
/// and add UISafeArea as a component of the Panel.
///
/// If the project does not use screen rotation, set screenCanRotate to false;