Skip to content

Instantly share code, notes, and snippets.

@alanthai
alanthai / input.scss
Created May 18, 2023 14:33
Generated by SassMeister.com.
:root {
--blitz-color-content-primary: #000000;
}
$blitz-color-content-primary: var(--blitz-color-content-primary, #000000);
$blitz-color-content-secondary: #000000;
.test {
background-color: $blitz-color-content-primary;
background-color: $blitz-color-content-secondary;
@alanthai
alanthai / setter.ts
Created June 4, 2020 17:06
Typesafe getter and setter
type Key = string | number | symbol
const PathSymbol = Symbol('path');
function pathProxy(path: Key[] = []) {
return new Proxy({
get [PathSymbol]() {
return path;
},
}, {
@alanthai
alanthai / useReducer.ts
Created April 22, 2020 02:50
A more performant useReducer that only re-renders when the modified part of state is being read.
// example: https://stackblitz.com/edit/react-ts-hesbky?file=use-reducer.ts
// inspired by https://github.com/zeit/swr/pull/186/files
import {useState, useRef} from "react";
type StateDependencies<T> = {
[K in keyof T]?: boolean;
};
function equals(compareA, compareB) {
const INCOME = 100_000;
// ----- 2022 ----- //
// https://www.canada.ca/en/revenue-agency/services/forms-publications/payroll/t4032-payroll-deductions-tables/t4032on-jan/t4032on-january-general-information.html
const CPP_BRACKETS = [
[ 3_500, 0 ], // exemption
[ 61_400, 0.0570], // cap
[Infinity, 0 ],
enum WebStatus {
Loading,
Error,
Success,
NotCalled,
}
interface SuccessState<S> {
status: WebStatus.Success;
payload: S;
import { assocPath, flip, path, init, last, toPairs } from 'ramda';
function isObject(value: any): boolean {
return Object.prototype.toString.call(value) === '[object Object]';
}
function isFunction(value: any): boolean {
return Object.prototype.toString.call(value) === '[object Function]';
}
@alanthai
alanthai / asyncGenerator.js
Last active October 28, 2015 23:49
Async using Generators and Promises
/** ES 2015 naive implementation of https://github.com/tj/co */
function coAsync(generator) {
var gen = generator();
return new Promise(function(resolve, reject) {
(function async({value, done}) {
if (done) return resolve(value);
if (Array.isArray(value)) {
Promise.all(value).then(values => async(gen.next(values)));
@alanthai
alanthai / objectpath.js
Created February 3, 2015 16:30
Object Path
// Gets value of object given a string path
// Example: objectPathGet({a: {b: {c: {d: "hello"}}}}, "a.b.c.d") // returns "hello"
function objectPathGet(obj, path, _default) {
try {
var keys = path.split(".");
return (function _get(obj) {
var child = obj[keys.shift()];
return keys.length ? _get(child) : child;
})(obj);
} catch(err) {
@alanthai
alanthai / changeCase.js
Created December 4, 2014 18:40
Change snake to camel, camel to snake, uppercase and lowercase functions
function snakeToCamel(str) {
return str.replace(/(\_[a-z])/g, function(char){return char[1].toUpperCase();});
}
function camelToSnake(str) {
return str.replace(/([A-Z])/g, function(char){return "_" + char.toLowerCase();});
}
function upperCaseFirst(str) {
return str[0].toUpperCase() + str.slice(1);
@alanthai
alanthai / nested_dict.py
Last active August 29, 2015 14:10
Creates a nested dictionary with and without a specified depth level
# Unlimited depth nested dictionary
def nested_dict():
return defaultdict(nested_dict)
# Specify max depth of nested dictionary
def nested_dict(instance_type, max_depth):
def _nested_dict(depth):
if depth < max_depth:
return defaultdict(partial(_nested_dict, depth + 1))