Skip to content

Instantly share code, notes, and snippets.

View nash403's full-sized avatar
🦄
want a cookie ?

Honoré Nintunze nash403

🦄
want a cookie ?
View GitHub Profile
export const formatFileSize = (bytes, fSExt = ['Bytes', 'KB', 'MB', 'GB']) => {
// fSExt is an Array of unit labels
let i = 0 // Index to track which unit to use
while (bytes > 900) { // While the size is bigger than 900 in the current unit
bytes /= 1024 // Convert to the next bigger unit
i++ // Move to the next unit in the array
}
return (Math.round(bytes * 100) / 100) + ' ' + fSExt[i] // Round to 2 decimal places and append unit
}
@nash403
nash403 / roundTo.js
Created August 7, 2025 14:15
Round a number in Javascript with specified number of decimals
function roundTo(n, digits) {
const factor = Math.pow(10, digits);
return Math.round(n * factor) / factor;
}
// roundTo(3.14159, 3); // 3.142
// roundTo(3.14159, 2); // 3.14
// roundTo(3.14159, 1); // 3.1
// roundTo(3.14159, 0); // 3
@nash403
nash403 / tailwind.css
Last active June 9, 2025 21:20
Tailwind CSS v4+ utility to create a responsive column layout
/*
* Create a fully responsive column layout while defining the max width of the columns.
* On small screens, columns width will be the min between 100% and the defined width.
*
* Add `--col-size` and / or `--col-size-*` variables to your tailwind theme for customization
* Ex:
* ```
* --col-size: 320px;
* --col-size-xl: 448px;
* --col-size-wathever: 1024px;
@nash403
nash403 / useStepperWithAsync.ts
Last active March 28, 2025 11:45
My helper for building a multi-step linear wizard interface. Base on https://vueuse.org/core/useStepper/#usestepper
import { useStepper } from '@vueuse/core'
import { ref, toValue } from 'vue'
/**
* A wrapper around VueUse's `useStepper` that adds async-aware navigation methods.
* It tracks direction of navigation (forward/backward) and only transitions between steps
* if the provided async action succeeds.
*
* The transition functions (`goXXX`) return a tuple `[success, error, result]`:
* - `success`: Boolean indicating whether the async action succeeded.
@nash403
nash403 / 1.trycatch.ts
Last active March 26, 2025 21:27 — forked from t3dotgg/try-catch.ts
My preferred way of handling try/catch in TypeScript
// Types for the result object with discriminated union
type Success<T> = {
data: T;
error: null;
};
type Failure<E> = {
data: null;
error: E;
};
@nash403
nash403 / auth-middleware.js
Created February 13, 2025 15:44
Basic Vue3 router middleware system
// auth middleware
import { useAuthStore } from "@/stores/auth";
export default async function auth({ next }) {
const authStore = useAuthStore();
const user = await authStore.checkAuth();
if (!( 'id' in user)) {
console.log("Not logged in");
@nash403
nash403 / Readme.md
Last active January 14, 2025 03:29
Vue 3 composable to create a reactive Map to use as an in memory (default) database. Extended with ability to standardize custom key generation.

useReactiveMap composable

Vue 3 composable to create a reactive Map to use as a small local database.

In memory by default, users can pass a custom map object to override that behavior.

The returned reactive Map is extended with ability to standardize custom key generation.

@nash403
nash403 / Readme.md
Created January 8, 2025 14:22
Dynamic composable (or effect scope) loading with Vue 3

Dynamic composable loading with Vue 3

Reactive watcher to load effect scopes dynamically depending on the watched value. Typically, the effect scope you load is a function that contains reactive logic in the same way you would have in a component setup function.

Example use:

const watchSource = ref(...) // anything
 
@nash403
nash403 / create-simple-state-machine.js
Last active November 4, 2024 17:37
The simplest state machine in JS. You don't always need bulletproof solutions like XState. Copied from a Kent C. Dodds' article
// Copied from this article https://kentcdodds.com/blog/implementing-a-simple-state-machine-library-in-javascript
function createMachine(stateMachineDefinition) {
const machine = {
value: stateMachineDefinition.initialState,
transition(currentState, event) {
const currentStateDefinition = stateMachineDefinition[currentState]
const destinationTransition = currentStateDefinition.transitions[event]
if (!destinationTransition) {
return
}
@nash403
nash403 / SvgFragment.vue
Last active June 9, 2025 21:31
Vue component that render raw SVG content like with `v-html` but without the need of an extra root element.
<script lang="ts">
import { h, VNodeData, mergeProps, defineComponent } from 'vue'
/**
* Use this component to render raw SVG content
* without the need to use the `v-html` directive
* which requires a parent node in Vue (ex: `<div v-html="..."></div>`).
* `<NSvgFragment :content="..." />` will directly render the svg tag with its content.
* */
export default defineComponent({