Skip to content

Instantly share code, notes, and snippets.

View paulohfev's full-sized avatar
👊

Paulo Henrique Evangelista paulohfev

👊
View GitHub Profile
@paulohfev
paulohfev / find_max.js
Created April 4, 2023 00:28
Function for finding the highest number in a list of numbers
function find_max(nums) {
let max_num = Number.NEGATIVE_INFINITY; // smaller than all other numbers
for (let num of nums) {
if (num > max_num) {
max_num = num
}
}
return max_num;
}
@paulohfev
paulohfev / minimum-webpack.config.js
Last active October 8, 2022 20:03
Minimum webpack config for React-Typescript project
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: "./src/index.tsx",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bundle.js",
},
resolve: {
@paulohfev
paulohfev / formatCamelCase.js
Last active September 17, 2022 16:45
Format string in camel case
const NON_SPECIAL_CHARS_REGEX = /\W+|[_]+/;
const WHITE_SPACE_REGEX = /\s+/;
const formatCamelCase = (text) => {
const formatCase = (word, index) => {
const formattedNonFirstWord = word.charAt(0).toUpperCase() + word.slice(1);
return index === 0 ? word.toLowerCase() : formattedNonFirstWord
};
return text
.replace(NON_SPECIAL_CHARS_REGEX, ' ')
@paulohfev
paulohfev / html-regexes
Created July 19, 2022 17:05
List of regexes for matching HTML related tags, attributes, properties, etc.
const FIRST_PARAGRAPH_TAG_REGEX = /<p>(.*?)<\/p>/;
const INLINE_STYLE_REGEX = /style="[^"]*"/g;
@paulohfev
paulohfev / file-type-regexs.js
Created July 16, 2022 22:11
File type RegExs
const VIDEO_FILE_REGEX = /video\/\w+/gm;
const AUDIO_FILE_REGEX = /audio\/\w+/gm;
const IMAGE_FILE_REGEX = /image\/\w+/gm;
@paulohfev
paulohfev / useKeyDown.ts
Last active September 25, 2023 15:33
useKeyDown hook w/ TypeScript
import { useEffect } from 'react';
import { KeyboardKey } from 'enums/keyboardKey';
export const useKeyPress = (callback: (T?: any) => void, keys: KeyboardKey[]) => {
const onKeyDown = (event: KeyboardEvent) => {
const wasAnyKeyPressed = keys.some((key) => event.key === key);
if (wasAnyKeyPressed) {
event.preventDefault();
callback();
@paulohfev
paulohfev / useWindowDimensions.js
Created July 1, 2022 16:51
Custom React hook for getting the current screen's dimensions
import { useState, useEffect } from 'react';
const getWindowDimensions = () => {
const { innerWidth: width, innerHeight: height } = window;
return { width, height };
}
const useWindowDimensions = () => {
const [windowDimensions, setWindowDimensions] = useState(getWindowDimensions());
@paulohfev
paulohfev / useOutsideClick.ts
Last active March 12, 2024 11:27
useOutsideClick React Hook w/ TypeScript
import { useEffect } from 'react';
export const useOutsideClick = (callback: (T?: any) => void, ref: React.RefObject<HTMLDivElement>) => {
const handleClick = (e: Event) => {
if (ref.current && !ref.current.contains(<HTMLElement>e.target)) {
callback();
}
};
useEffect(() => {
@paulohfev
paulohfev / createContentExcerpt
Last active July 1, 2022 02:59
Create a content excerpt
const createContentExcerpt = (content, maxNumberOfWords, trailingIndicator = '...') => {
const listOfWords = content.trim().split(' ');
const truncatedContent = listOfWords.slice(0, maxNumberOfWords).join(' ');
const excerpt = truncatedContent + trailingIndicator;
const result = listOfWords.length > maxNumberOfWords ? excerpt : content;
return result;
};
// Example:
@paulohfev
paulohfev / html-head-boilerplate.html
Created July 2, 2021 20:53 — forked from nunosans/html-head-boilerplate.html
HTML Head Boilerplate & Reference
<!doctype html>
<html>
<head>
<!-- Priority tags. These must come first. -->
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge; chrome=1"> <!-- Render Chrome if available or using latest version of Internet Explorer (Recommended). -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<!-- Document Title -->
<title>Page Title</title>
<!-- Allows control over where resources are loaded from. Place as early in the document as possible, only applies to content below this tag. -->