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
@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
Created June 30, 2021 09:13
Vue component that render raw SVG content like with `v-html` but without the need of an extra root element.
<script lang="ts">
import { VNodeData } from 'vue'
import { defineComponent } from '@vue/composition-api'
/**
* Use this component to render raw SVG content
* without the need to use the `v-html` directive
* which requires a parent node in Vue 2.x (ex: `<div v-html="..."></div>`).
* `<NSvgFragment :src="..." />` will directly render the svg tag with its content.
* */
@nash403
nash403 / decorators.js
Created June 20, 2019 07:05
JS Decorators How to example
// Prop/Method Decorator with no arguments
function logDecorator(t, n, descriptor) {
const original = descriptor.value;
if (typeof original === 'function') {
descriptor.value = function(...args) {
console.log(`Arguments: ${args}`);
try {
const result = original.apply(this, args);
console.log(`Result: ${result}`);
return result;
@nash403
nash403 / normalizeChar.mjs
Last active February 15, 2023 10:39
Normalize a character/string to remove accents. (For comparison)
/*
Two things will happen here:
1) normalize()ing to NFD Unicode normal form decomposes combined graphemes into the combination of simple ones. The è of Crème ends up expressed as e + ̀.
2) Using a regex character class to match the U+0300 → U+036F range, it is now trivial to globally get rid of the diacritics, which the Unicode standard conveniently groups as the Combining Diacritical Marks Unicode block.
*/
export const normalizeChar = char => char.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
export const normalizeStr = str => str.split('').map(normalizeChar).join('')
@nash403
nash403 / list_files.js
Created May 27, 2019 14:31
List file names in current directory (without extension)
const fs = require('fs')
const path = require('path')
const files = []
fs.readdirSync(__dirname).forEach(file => {
const i = file.indexOf(path.basename(file))
const j = file.indexOf(path.extname(file))
files.push(file.substring(i, j))
})