Skip to content

Instantly share code, notes, and snippets.

@neatshell
neatshell / useStateWithCallback.ts
Created July 6, 2023 14:27
useStateWithCallback like old setState callback behaviour
// https://betterprogramming.pub/synchronous-state-in-react-using-hooks-dc77f43d8521
import { useState } from 'react';
export function useStateWithCallback<T>(initialValue: T) {
const [value, setValue] = useState(initialValue);
const setValueAndCallback = (newValue: T, callback: (prevValue: T, newValue: T) => void) => {
setValue((prevValue) => {
if (callback) {
callback(prevValue, newValue);
@neatshell
neatshell / brew-dnsmasq.md
Created February 20, 2023 10:25 — forked from davebarnwell/brew-dnsmasq.md
install dnsmasq with brew

Install dnsmasq and configure for *.dev.local domains

$ brew install dnsmasq
$ vim /usr/local/etc/dnsmasq.conf

Reload configuration and clear cache

# Copy the daemon configuration file into place.
$ sudo cp $(brew list dnsmasq | grep /homebrew.mxcl.dnsmasq.plist$) /Library/LaunchDaemons/

$ sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

@neatshell
neatshell / yo_non_root
Last active January 2, 2021 18:18 — forked from Jay991/solution.text
Solution for Yoeman Error: EACCES, permission denied '/root/.config/configstore/insight-yo.json' You don't have access to this file.
You need to create a new user and give it sudo privleges:
$adduser username
Set password prompts:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for username
Enter the new value, or press ENTER for the default
Full Name []:
(function(org, notificationType) {
// org:
// the organizazione name
// notificationType:
// 'included' = participating and @mentions,
// 'subscribed' = all activity,
// 'ignore' = ignore
let qx = $x;
let unwatch = function(org, notificationType) {
@neatshell
neatshell / ngx_brotli_osx.md
Last active April 26, 2020 14:48
Add dynamic nginx-brotli-module to nginx

Pre-requisites:

nginx installed with homebrew from the core tap

git installed and all the software needed to build something from source

Getting sources:

First of all, you need to get your currently installed nginx version simply by executing:

import path from 'path';
import fs from 'fs';
const testAgainstRegex = (patterns, string) => {
if (typeof patterns === 'string') {
return new RegExp(patterns).test(string);
} else if (patterns instanceof Array) {
return new RegExp(patterns.join('|')).test(string);
} else {
return patterns;
# Retrieve the arguments passed to an npm/yarn script
ARGV=$(echo "console.log(JSON.parse(process.argv[2]).original.splice(1, Infinity).join(' '))" | node - ${npm_config_argv})
@neatshell
neatshell / document.querySelectorCheatsheet
Last active March 4, 2020 20:23
document.querySelector* cheatsheet
// Get the canonical url of a page
document.querySelector("link[rel='canonical']").href;
document.querySelector("link[rel='canonical']").getAttribute("href");
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
module.exports = (millis) => {
let seconds = (millis / 1000).toFixed(0);
let minutes = Math.floor(seconds / 60);
let hours = '';
if (minutes > 59) {
hours = Math.floor(minutes / 60);
hours = (hours >= 10) ? hours : '0' + hours;
minutes = minutes - (hours * 60);
minutes = (minutes >= 10) ? minutes : '0' + minutes;