Skip to content

Instantly share code, notes, and snippets.

View drianoaz's full-sized avatar

Adriano de Azevedo drianoaz

View GitHub Profile
@drianoaz
drianoaz / localstorage.js
Created August 16, 2023 21:25
Breakpoint localStorage
/**
* @see https://stackoverflow.com/questions/49092423/how-to-break-on-localstorage-changes
*/
Object.defineProperty(window, 'localStorage', {
configurable: true,
enumerable: true,
value: new Proxy(localStorage, {
set: function (ls, prop, value) {
console.log(`direct assignment: ${prop} = ${value}`);
debugger;
@drianoaz
drianoaz / debounce.test.ts
Created June 23, 2023 14:20
Simple debounce function using typescript
import { debounce } from './debounce';
describe('debounce', () => {
test('should debounce function calls', () => {
jest.useFakeTimers();
const mockFunction = jest.fn();
const delay = 500;
const debouncedFunction = debounce(mockFunction, delay);
@drianoaz
drianoaz / exampleUsage.ts
Created March 18, 2022 22:19
A simple use react hook that makes create throttled functions easy
const MyComponent = () => {
const callbackWithThrottle = useThrottle(() => {
console.log("this fires after 3 seconds after last click")
}, 3000);
return <button onClick={callbackWithThrottle}>Click me</button>
}
```
@drianoaz
drianoaz / overrides_enabled.js
Created November 19, 2021 20:58
A simple script to show hen the overrides extension is enabled
$("document").ready(() => {
$("body").append(`
<div style='width: 100%; background: red; position: absolute; bottom: 0; z-index: 9999; padding: 0; font-size: 14px; color: white; font-weight: bold; text-align: center;'>
Overrides ON
</div>
`);
});
@drianoaz
drianoaz / Header.jsx
Created August 9, 2021 13:06
Exemplo de uso do react-query
import styled from 'styled-components';
import { Typography } from '@/fairplay';
import { useCart } from './services/carrinho';
const StyledHeader = styled.div`
width: 100%;
height: 80rem;
background: red;
display: grid;
place-items: center;
@drianoaz
drianoaz / useWindowResize.spec.ts
Created June 2, 2021 22:38
An optimized react hook for window resize
import { renderHook } from '@testing-library/react-hooks';
import { useWindowResize } from './useWindowResize';
jest.useFakeTimers();
describe('useWindowResize', () => {
it('should run the callback when the window is resized', () => {
const mock = jest.fn();
renderHook(() => useWindowResize(mock));
@drianoaz
drianoaz / MyController.js
Created December 11, 2020 14:01
Mock setTimeout jest
exports.foo = (req, res) => {
const isSuccess = Math.random() < 0.5;
setTimeout(() => {
if (isSuccess) {
return res.send(201);
}
res.send(403);
}, 3000);
@drianoaz
drianoaz / old_helper.php
Created July 20, 2018 12:56
Laravel old helper for database models
if (! function_exists('old_helper')) {
/**
* Retrieve an old input item and set a
* default value with helper
*
* @param $oldKey
* @param null $target
* @param null $targetKey
* @param callable|null $callback