Skip to content

Instantly share code, notes, and snippets.

@ahallora
ahallora / ABTest.tsx
Last active March 27, 2024 08:06
Basic React ABTesting Component - POC
import React, { ReactNode, useState } from "react";
type Experiment = {
exposure: number;
render: ({ success }: { success: any }) => any;
name?: string;
};
enum TestEvent {
"VIEW" = "View",
@ahallora
ahallora / decimalStringToCento.ts
Created March 24, 2024 12:08
Decimal string to cento number
export const decimalStringToCento = (decimalString: string): number => {
// Replace comma with dot to make it a valid float
const cleanedString = decimalString.replace(",", ".");
// Parse the string as a floating-point number
const floatValue = parseFloat(cleanedString);
// Multiply by 100 to get the cento number
const centoNumber = Math.round(floatValue * 100);
@ahallora
ahallora / csvToJson.ts
Created March 24, 2024 12:07
Convert csv to JSON
const parseCSVLine = (line: string): string[] => {
const parts = line.split(",");
const result: string[] = [];
parts.reduce(
(accumulator, currentPart) => {
if (!accumulator.insideQuotes) {
if (currentPart.startsWith('"') && currentPart.endsWith('"')) {
// Handle the case when the value is enclosed in double quotes
result.push(currentPart.slice(1, -1));
@ahallora
ahallora / useScrollableMask.ts
Last active February 21, 2024 07:56
useScrollableMask
/*
HOW TO USE:
import React from "react";
import useScrollableMask from "./useScrollableMask";
const Example = () => {
const { scrollDiv, scrollDivStyles } = useScrollableMask();
return (
@ahallora
ahallora / useAudioContext.js
Created February 6, 2024 15:29
Web Audio API react hook to easily stream music with fade in and out (working on Safari iOS too)
import { useState, useEffect } from "react";
export const useAudioContext = () => {
const [audioContext, setAudioContext] = useState(null);
const [currentSourceNode, setCurrentSourceNode] = useState(null);
const [gainNode, setGainNode] = useState(null);
useEffect(() => {
const context = new AudioContext();
setAudioContext(context);
@ahallora
ahallora / AdvancedForm.jsx
Last active February 3, 2024 21:48
React form where form state is saved in querystring using react-hook-form
"use client";
import { useForm } from "react-hook-form";
import { useEffect } from "react";
const AdvancedForm = () => {
const { register, handleSubmit, setValue, watch } = useForm();
const watchedValues = watch();
// Function to update the query string
const updateQueryString = () => {
@ahallora
ahallora / readme.md
Created January 31, 2024 08:51
LocalAI and StableDiffusion on Ubuntu (WSL2)

Making LocalAI and StableDiffusion work on Ubuntu (via WSL2)

Prerequisites:

  • Docker running
  • nvidia-smi running

Print out the GPUs available in docker:

docker run -it --rm --gpus all ubuntu nvidia-smi -L
@ahallora
ahallora / README.md
Last active January 16, 2024 10:38
Convert MP4 to GIF free and open-source using FFMPEG and Windows Context menu

Convert MP4 to GIF free and easy directly from the Windows context menu

Previously I've been using GifCam, but it doesn't play well with my retina display, so I had to come up with something. There's strong options for Mac but no good free options for Windows.

I can use the native Windows snipping tool to record my screen, but I'm unable to save the recording as a gif, resulting in pretty big MP4 files. I don't want that. So I created this little script that can convert the recorded MP4 file to GIF for a great reduction in file size.

Installation

  1. Install ffmpeg from www.ffmpeg.org
  2. Update the path to ffmpeg in the bat file
@ahallora
ahallora / README.md
Last active December 29, 2023 13:54
CloudFlare Email Forwarding - Support for Gmail alias

CloudFlare Email Routing with alias

Steps to make it work

  1. Create a new Email Worker with this script (modify to your needs)
  2. Enable catch-all in CloudFlare Email Routing and send all catch-all mail to this worker
  3. Make sure the forwarded emails are in fact verified to receive email forwarding
  4. You should still keep the "plain" custom mapping without alias enabled; like dad => dads-email@gmail.com
  5. ??
  6. Profit
@ahallora
ahallora / addwish-to-csv.js
Created May 25, 2022 06:50
Gem dine ønskelister fra AddWish.com til CSV
/*
Sådan gemmer du dine ønskelister fra AddWish.com til CSV
--------------------------------------------------------
1) Log ind på din ønskeliste på www.addwish.com
2) Start console i din browser (fx. tryk [ctrl+shift+i] i Chrome)
3) Copy/paste dette script ind i console og tryk [enter] for at køre det
4) Kopier CSV-teksten som vises i din browser prompt ind i Google sheets og tryk "Split text to columns" og vælg "Semicolon"
5) Din ønskeliste er nu gemt i Sheets