This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 + ''; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** 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))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]'; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum WebStatus { | |
| Loading, | |
| Error, | |
| Success, | |
| NotCalled, | |
| } | |
| interface SuccessState<S> { | |
| status: WebStatus.Success; | |
| payload: S; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ], |
OlderNewer