Skip to content

Instantly share code, notes, and snippets.

@alanthai
alanthai / colorConvert.js
Last active August 26, 2019 07:46
Converts color hex to decimal
function toHex(n: number) {
return ('0' + n.toString(16)).slice(-2);
}
// ES6
function hexToRGB(code) {
const [r, g, b] = code.replace('#', '')
.match(/../g).map((hex) => parseInt(hex, 16));
return {r, g, b};
@alanthai
alanthai / times.js
Last active August 29, 2015 14:07
Useful date functions and statements
// midnight today
var today = new Date(new Date().setHours(0,0,0,0));
// midnight tomorrow
var tomorrow = new Date(new Date().setHours(24,0,0,0));
// date format: yyyy-mm-dd HH:MM:SS
function pad(n, width, z) {
z = z || '0';
n = n + '';
@alanthai
alanthai / ordinals.js
Created November 17, 2014 15:46
Adds ordinal suffix to an integer
// assume positive integers as parameters
function getDigit(n, d) {
return parseInt( n / Math.pow(10, d - 1), 10 ) % 10;
}
function nth(n) {
var d1 = getDigit(n, 1);
var d2 = getDigit(n, 2);
n = "" + n;
@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))
@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 / 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 / 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)));
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]';
}
enum WebStatus {
Loading,
Error,
Success,
NotCalled,
}
interface SuccessState<S> {
status: WebStatus.Success;
payload: S;
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 ],