Skip to content

Instantly share code, notes, and snippets.

@pirey
pirey / helpers.ts
Created December 28, 2023 11:05
some helper functions
function randomBoolean() {
return Math.random() < 0.5;
}
function chunkArray<T>(array: T[], chunkSize: number): T[][] {
return Array.from(
{ length: Math.ceil(array.length / chunkSize) },
(_, index) => array.slice(index * chunkSize, (index + 1) * chunkSize),
);
}
@pirey
pirey / withMeasure.ts
Created December 28, 2023 10:57
print simple perf measurement
async function withMeasure<T>(label: string = 'Measurement', fn: () => Promise<T>) {
function bytes2MB(bytes: number): number {
return bytes / (1024 * 1024)
}
const pstart = performance.now()
const mstart = process.memoryUsage().heapUsed
const result = await fn()
@pirey
pirey / tmux-cheatsheet.md
Created October 31, 2023 00:46
tmux cheatsheet

<prefix>-space change layout

<prefix>-} <prefix>-{ move pane

-; move between panes

@pirey
pirey / multilinecontent.jsx
Last active October 28, 2023 10:58
react display multiline text content
function Comp({ content }) {
return content.split('\r\n').map((line, i) =>
<p key={i}>{line.length ? line : <br />}</p>)
}
@pirey
pirey / notes.md
Created October 27, 2023 16:56
Personal laravel notes

laravel version: 10

authorization

policy

  • -> authorization logic for model
  • -> auto detected if following naming convention, User -> UserPolicy
  • $request->user()->can('update', $post)
  • $request->user()->cannot('create', Post::class)
  • $this-&gt;authorize('update', $post);
@pirey
pirey / useOutsideAlerter.js
Created October 25, 2023 03:57
event handler outside element
function useOutsideAlerter(ref, eventType, callback, childSelector) {
useEffect(() => {
function handler(event) {
if (!ref.current) return;
const clickedOutside = childSelector
? !Array.from(ref.current.querySelectorAll(childSelector)).some(
(node) => node.contains(event.target),
)
: !ref.current.contains(event.target);
set -g default-terminal "screen-256color"
set-option -sa terminal-overrides ",xterm-256color:RGB"
@pirey
pirey / sample.nix
Created June 5, 2023 17:39
create service in nixos configuration
# https://www.reddit.com/r/NixOS/comments/mwbr8t/comment/gvhm2mh/?utm_source=share&utm_medium=web2x&context=3
systemd.services.touchegg = {
enable = true;
description = "Touchégg. The daemon.";
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "simple";
Group = "input";
Restart = "on-failure";
RestartSec = 5;
@pirey
pirey / profile
Created May 16, 2023 07:33
powershell goodies
# nvim $profile - to edit the file
Import-Module PSFZF # enable fzf keybinding
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete # enable autocomplete
@pirey
pirey / CapsLockRemapV1.ahk
Last active May 15, 2023 04:23
autohotkey script to remap capslock as both control and escape key.
; Change Caps Lock to Control when held down; otherwise, Escape
;
; Originally based on the answer provided in
; [this](https://superuser.com/questions/581692/remap-caps-lock-in-windows-escape-and-control)
; StackExchange SuperUser question.
;
; A shortcut should be created for this script and placed in the Windows 10
; user's startup folder to automatically enable the feature on boot/startup.
; The user's startup folder can be found using the following steps:
;