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 / 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))
})
@nash403
nash403 / Readme.md
Created May 16, 2019 15:03
How to list packages that are listed in the global package.json in Yarn config

cat $(yarn global dir)/package.json

@nash403
nash403 / webpack.config.js
Created February 9, 2018 13:22
Webpack base config to publish libs to production with modern code
const path = require("path");
const webpack = require("webpack");
const UglifyJSPlugin = require("uglifyjs-webpack-plugin");
const PROD = process.env.NODE_ENV === "production";
const configureBabelLoader = browserlist => {
return {
test: /\.js$/,
use: {
@nash403
nash403 / viewport-sizes.js
Created January 26, 2018 09:15
Get viewport exact width and height
// From this SO answer https://stackoverflow.com/a/2035211
function getViewport() {
var viewPortWidth;
var viewPortHeight;
// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
if (typeof window.innerWidth != 'undefined') {
viewPortWidth = window.innerWidth,
@nash403
nash403 / get-attributes.js
Created January 25, 2018 21:10
Get attributes of an HTML element as a JSON Object (not as NamedNodeMap)
function getAttributes (el) {
return Array.from(el.attributes)
.map(a => [a.name, a.value])
.reduce((acc, attr) => {
acc[attr[0]] = attr[1]
return acc
}, {})
}
@nash403
nash403 / scrollbar.scss
Created January 5, 2018 14:41
Scss mixins for custom scrollbar and no scrollbar
// custom scroll-bar
@mixin custom-scroll-bar() {
&::-webkit-scrollbar {
border-radius: 10px;
height: 10px;
width: 8px;
}
&::-webkit-scrollbar-thumb {
background: #999;