Skip to content

Instantly share code, notes, and snippets.

View SamuelHiroyuki's full-sized avatar
🧿
You'll see more with your eyes closed.

Samuel Hiroyuki SamuelHiroyuki

🧿
You'll see more with your eyes closed.
View GitHub Profile
@SamuelHiroyuki
SamuelHiroyuki / server.js
Created September 12, 2019 21:11
Run React production build
const express = require('express')
// First of all you will create a package.json file
// So need to run NPM i or YARN ADD to install Express dependencies
// After that, you will put the generated build folder in the same folder as this server
// So you just run the serve :)
const app = express()
const baseDir = `${__dirname}/build/`
@SamuelHiroyuki
SamuelHiroyuki / calculateTotal.js
Created May 10, 2020 19:56
Some good practices in JavaScript.
const TAX_RATE = 1.1;
const DEFAULT_SHIPPING = 5;
function calculateTotal(items, {shipping = DEFAULT_SHIPPING, discount = 0} = {}) {
if (!Array.isArray(items) || !items.length) return 0;
const itemsCost = items.reduce((total, item) => (total += item.quantity * item.price), 0);
const discountRate = (discount / 100) || 1;
return itemsCost * discountRate * TAX_RATE + shipping;
@SamuelHiroyuki
SamuelHiroyuki / Program.cs
Created July 20, 2020 19:36
C# - Windows Registry key creation
namespace CustomURLProtocol
{
class Program
{
static void Main(string[] args)
{
string appName = "CustomURLProtocol";
string appPath = @"C:\AppToOpen.exe";
RegistryKey key = Registry.ClassesRoot.OpenSubKey(appName); //open appName protocol's subkey
@SamuelHiroyuki
SamuelHiroyuki / DocumentValidator.js
Last active October 19, 2020 19:00
A validator for Brazilian CPF and CNPJ documents
// This validator does not check the formatting of the documents.
// Documents can be passed with or without formatting characters.
const processes = {
cpf: {
length: 11,
validator: [11, 10, 9, 8, 7, 6, 5, 4, 3, 2],
validationRegex: (d1, d2) => `([0-9]{9}${d1}${d2})`,
format: [/(\d{3})(\d{3})(\d{3})(\d{2})/, "$1.$2.$3-$4"],
},
@SamuelHiroyuki
SamuelHiroyuki / HandleValue.js
Created October 21, 2020 16:24
A handler for unfilled objects using Lodash
import _ from 'lodash';
// interface OptionsTypes {
// defaultReturn?: string;
// truthyOnly?: boolean;
// }
// interface HandleValueParams {
// obj: object;
// properties: string[];
@SamuelHiroyuki
SamuelHiroyuki / wt.reg
Created February 23, 2021 17:47
Add custom split panel 'Windows Terminal' in Windows context menu
Windows Registry Editor Version 5.00
; Define a custom message
[HKEY_CLASSES_ROOT\Directory\Background\shell\Windows Terminal]
@="Open with Windows Terminal"
; Define a custom icon, remove the following two lines to omit the icon
[HKEY_CLASSES_ROOT\Directory\Background\shell\Windows Terminal]
"Icon"="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
@SamuelHiroyuki
SamuelHiroyuki / localStorage.ts
Created March 8, 2021 20:18
Typescript useLocalStorage hook
import { useState } from 'react'
export function useLocalStorage<T>(
key: string,
initialValue: T
): [T, (params: T | ((prevValue: T) => T)) => void, () => void] {
const [storedValue, setStoredValue] = useState(() => {
try {
const item = window.localStorage.getItem(key)