Skip to content

Instantly share code, notes, and snippets.

@lysender
lysender / rust-sum-of-left-leaves.rs
Created October 6, 2023 04:00
Rust - Leetcode - Easy - Sum of left leaves
// Leetcode problem: https://leetcode.com/problems/sum-of-left-leaves/
// Easy but difficult when Rust's Rc<RefCell<T>> is used for rust beginners
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
@lysender
lysender / leetcode-rust-sum-of-square-numbers.rs
Last active October 5, 2023 06:13
Leetcode - Rust - Sum of square numbers using binary search
use std::{time::Instant};
fn main() {
solution_sum_of_square_numbers_bs();
}
// Problem: https://leetcode.com/problems/sum-of-square-numbers/
// Solution: https://leetcode.com/problems/sum-of-square-numbers/solutions/4132153/rust-solution-using-binary-search/
// Sources:
// - https://www.geeksforgeeks.org/check-whether-number-can-represented-sum-two-squares/
@lysender
lysender / sharp-resize-portait-to-landscape-but-it-rotates-instead.md
Created August 21, 2023 03:42
sharp - resize portrait into landscape but it rotates the image instead

I have a bunch of photos from my old iPad. I have a script that create thumbnail photos from these images.

The goal is to resize the image into 150x125 and crop if necessary to achieve the result.

const options: ResizeOptions = {
  width: 150,
  height: 125,
  fit: 'cover',
}
@lysender
lysender / htmx-allow-400-errors-swap-for-validation-errors.md
Created August 19, 2023 13:49
HTMX - Allow HTTP error 400 and 422 swap content to enable showing validation errors

Still a newbie to HTMX.

I was writing some form submit interaction and realized that HTMX won't swap/render if the HTTP status code is not 2xx.

For 404, 500 or 503 errors, I think it's fair enough.

For my use case, I'm returning error 400 or 422 for validation errors. I'm returning the form back with some error decorations for example.

According to the docs, I can change that behavior.

@lysender
lysender / nodejs-minify-bundle-css-files-using-lightningcss.md
Created August 19, 2023 11:59
NodeJS - minify and bundle css files using lightningcss

Been doing some HTMX lately using ExpressJS backend.

Unfortunately, my CSS files are not automatically minified and bundled for the browser.

Using lightningcss, a blazingly fast tool written in rust, we can achieve some minifications and combining using some NodeJS scripts.

Install lightningcss in your project

npm i -D lightningcss lightningcss-cli
@lysender
lysender / nodejs-minify-bundle-js-files-using-swc.md
Last active August 19, 2023 11:50
NodeJS - minify and bundle js files for browser using swc

Been doing some HTMX lately using ExpressJS backend.

Unfortunately, my JS files are not automatically minified and bundled for the browser.

Using swc - Speedy Web Compiler, a blazingly fast tool written in rust, we can achieve some minifications and combining using some NodeJS scripts.

Install swc in your project

npm i -D @swc/cli @swc/core
@lysender
lysender / hyperscript-toggle-burger-menu.md
Created August 17, 2023 16:41
hyperscript - Toggle Hamburger Menu for mobile browsers

How to toggle hamburger menu on/off in hyperscript?

I used this JavaScript solution earlier - https://gist.github.com/lysender/bc9800e6350e1ccfab3c6ce6934b1cd2

However, I realized it is way more simple to just use hyperscript.

What it does is toggle the is-active class on two divs related to the navigation menu.

No need to write the complicated JavaScript code but we do need to include the hyperscript JavaScript file in our page.

@lysender
lysender / htmx-load-content-with-loading-indicator.md
Created August 17, 2023 05:53
HTMX - Load content with loading indicator

Problem: I want blazingly fast page load but my content may take some time to load.

The page may actually be fast but the content may include calling a service that may take some time to respond or worst, fail.

Solution: Load the full page (HTML, CSS, JS) without the slow content first. Load the slow content later on page load.

<div
  class="albums-container"
 hx-get="/album-listing"
@lysender
lysender / no-htmx-toggle-burger-menu.md
Last active August 17, 2023 05:57
HTMX - Toggle Hamburger Menu for mobile browsers

How to toggle hamburger menu on/off in HTMX?

Why bother using HTMX, just use JavaScript!

Here is my navigation template, written in EJS using Bulma CSS framework. It shows a hamburger menu when viewed in a mobile browser. Without JavaScript, it won't expand or collapse the menu.

<nav class="navbar is-dark" role="navigation" aria-label="main navigation">
@lysender
lysender / bulk-rename-files.md
Created August 14, 2023 08:37
Bulk rename files in linux

Rename all files in current directory and sub-directories in certain pattern

I have several photos compiled in directories. However, these photos were processed by some old application and don't have file name extensions.

I would just assume that they are all JPG files so I want to rename all files to have the .jpg extension.

Will use the find and exec combination.

First, let's test it with a safer echo command. The command simply echos for original filename and the desired filename.