Skip to content

Instantly share code, notes, and snippets.

View alshdavid's full-sized avatar

David Alsh alshdavid

View GitHub Profile
@alshdavid
alshdavid / calculate_tax.js
Created September 27, 2024 23:46
Calculate tax from brackets
const MAX = Number.MAX_SAFE_INTEGER
const BRACKETS = {
AU_2025: [
[ 0, 18200, 0.00 ],
[ 18201, 45000, 0.16 ],
[ 45001, 135000, 0.30 ],
[ 135001, 190000, 0.37 ],
[ 190001, MAX, 0.45 ],
],
AU_2024: [
@alshdavid
alshdavid / index.js
Created March 5, 2024 09:21
Deno in rust
function echo(value) {
Deno.core.print("called with " + value)
}
Deno.core.ops.op_register_func(echo)
@alshdavid
alshdavid / install-zenith.bash
Created February 28, 2024 22:28
Install Zenith Binary
set -ev
rm -rf $HOME/.local/zenith
mkdir -p $HOME/.local/zenith/bin
wget https://github.com/bvaisvil/zenith/releases/latest/download/zenith.x86_64-unknown-linux-musl.tgz -O $HOME/.local/zenith/bin/zenith.tgz
tar -C $HOME/.local/zenith/bin -xzf $HOME/.local/zenith/bin/zenith.tgz
rm -rf $HOME/.local/zenith/bin/zenith.tgz
chmod +x $HOME/.local/zenith/bin/zenith
@alshdavid
alshdavid / increase-thai-font-size.mjs
Last active January 15, 2024 03:40
increase thai font size
(() => {
// Styles to apply
const styles = {
fontSize: '30px',
fontFamily: '"Noto Serif Thai", serif',
}
// Add fonts if they don't exist
const font_url = "https://fonts.googleapis.com/css2?family=Noto+Serif+Thai&display=swap";
if (!document.head.querySelector(`link[href="${font_url}"]`)) {
@alshdavid
alshdavid / tsconfig.strictest.json
Created July 12, 2023 23:41
Strictest TypeScript config posible
{
"compilerOptions": {
// Resolution (adjust to use case)
"target": "ESNext",
"module": "CommonJS",
"moduleResolution": "Node16",
"lib": ["ESNext"],
"types": ["node"],
// Super strict

Jest Dynamic Mock

Used to recursively dynamically generate an object where the properties are jest.fn() functions.

Behind the scenes it uses a JavaScript Proxy to detect if a property on the created object is being accessed or being invoked. If it's being invoked it generates a jest.fn() and uses that, if it's being accessed it creates a new mock proxy. This allows for mocks to be generated for objects dynamically and deeply.

// Source of https://www.npmjs.com/package/physical-cpu-count
'use strict'
const os = require('os')
const childProcess = require('child_process')
function exec (command) {
const output = childProcess.execSync(command, {encoding: 'utf8'})
return output
@alshdavid
alshdavid / local-storage-async.js
Created February 24, 2023 11:02
LocalStorage API powered by Indexeddb
class LocalStorageAsync {
static DatabaseKey = 'LocalStorageAsync.DatabaseKey'
static DatabaseStoreKey = 'LocalStorageAsync.DatabaseStoreKey'
#hasLoaded
constructor() {
this.#hasLoaded = new Promise((resolve, reject) => {
let dRequest = indexedDB.deleteDatabase(LocalStorageAsync.DatabaseKey)
dRequest.onerror = function() {
@alshdavid
alshdavid / README.md
Created November 4, 2022 07:13
Simple argument parsing function for Node.js

Usage

const args = parseArgs(process.argv.splice(2))
const args = parseArgs(['--foo', 'one'])

console.log(args.foo) // ['one']
@alshdavid
alshdavid / Readme.md
Last active December 27, 2021 05:20
Debian Sid Instructions
  1. Run sudo nano /etc/apt/sources.list
  2. Add contrib non-free to each deb and deb-src source
deb [...] bullseye main contrib non-free
deb-src [...] bullseye main contrib non-free

deb [...] bullseye-security main contrib non-free
deb-src [...] bullseye-security main contrib non-free

deb [...] bullseye-updates main contrib non-free