Skip to content

Instantly share code, notes, and snippets.

View alexdemers's full-sized avatar
🏠
Working from home

Alex Demers alexdemers

🏠
Working from home
View GitHub Profile
@alexdemers
alexdemers / use-debounced-state.ts
Created April 27, 2024 20:08
useDebouncedState
import { useEffect, useState } from 'react';
export const useDebouncedState = <T>(initialValue: T, delay: number) => {
const [value, setValue] = useState(initialValue);
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
@alexdemers
alexdemers / String.pluralization.js
Created March 28, 2017 15:46
Laravel's pluralization logic in JavaScript
// replace occurences of :vars in the string.
// "This :thing is great!".format
String.prototype.format = function(replacements) {
var str = this;
for (var key in replacements) {
if (replacements.hasOwnProperty(key)) {
str = str.replace(':' + key, replacements[key]);
}
}