Skip to content

Instantly share code, notes, and snippets.

View AnsonH's full-sized avatar
👾
Doing great

Anson Heung AnsonH

👾
Doing great
View GitHub Profile
@AnsonH
AnsonH / init.vim
Last active September 6, 2021 13:01
Vim Tip #3 - Move lines up and down with <A-k> and <A-j>
nnoremap <silent> <A-j> :m .+1<CR>==
nnoremap <silent> <A-k> :m .-2<CR>==
inoremap <silent> <A-j> <Esc>:m .+1<CR>==gi
inoremap <silent> <A-k> <Esc>:m .-2<CR>==gi
vnoremap <silent> <A-j> :m '>+1<CR>gv=gv
vnoremap <silent> <A-k> :m '<-2<CR>gv=gv
@AnsonH
AnsonH / js-tip1.js
Last active September 7, 2021 10:38
Javascript Tip #1 - Directly invoke arrow functions
/* Tweet: https://twitter.com/AnsonH_/status/1435189821740175363?s=20 */
/* Example 1 */
(() => {
console.log("Hello!");
})();
// Equivalent to...
(function() {
console.log("Hello!");
@AnsonH
AnsonH / js-tip2.js
Last active September 8, 2021 11:17
Javascript Tip #2 - 3 ways to iterate through object keys
/* Tweet: https://twitter.com/AnsonH_/status/1435562848399228936?s=20 */
const student = { name: "Tom", age: 20, gender: "male" };
// Method 1: for-in loop
for (const key in student) {
console.log(key + ": " + student[key]);
}
// Method 2: Object.keys()
@AnsonH
AnsonH / js-tip3.js
Last active September 9, 2021 12:38
Javascript Tip #3 - Class private properties and methods
/* Tweet: https://twitter.com/AnsonH_/status/1435945599367479303 */
class Student {
#gpa; // 1. Declare private property using the # sign
constructor(gpa) {
this.#gpa = gpa; // 2. Add this private property to class instance
}
// Private method
@AnsonH
AnsonH / js-tip4.js
Last active September 11, 2021 12:36
Javascript Tip #4 - Measure function execution time
/* Tweet: https://twitter.com/AnsonH_/status/1436669886055415816?s=20 */
function slowFunction() {
for (let i = 0; i < 999999999; ++i) {
// do something...
}
}
/***** Browser example *****/
@AnsonH
AnsonH / js-tip5.js
Last active September 13, 2021 12:52
Javascript Tip #5 - Sleep
/* Tweet: https://twitter.com/AnsonH_/status/1437398568055181314?s=20 */
/* Sleep function */
async function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/* Example */
async function takeNap() {
@AnsonH
AnsonH / js-tip6.js
Last active September 19, 2021 11:08
Javascript Tip #6 - Rest parameters
/* Tweet: https://twitter.com/AnsonH_/status/1439546876718551049?s=20 */
/* First photo */
function myFunc(a, b, ...args) {
// Remaining arguments are stored in `args` array
console.log("a: ", a);
console.log("b: ", b);
console.log("args: ", args); // This is an array!
}
@AnsonH
AnsonH / p10k_classic.omp.json
Last active September 27, 2024 17:45
p10k_classic - Custom Oh My Posh theme
{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"blocks": [
{
"alignment": "left",
"newline": true,
"segments": [
{
"foreground": "lightYellow",
"style": "plain",
@AnsonH
AnsonH / pipsize.py
Created March 5, 2022 04:20
Lists size of pip packages
# Run `python pipsize.py` in Terminal to show size of pip packages
# Credits: https://stackoverflow.com/a/67914559/11067496
sort_in_descending = True # Show packages in descending order
import os
import pkg_resources
def calc_container(path):
total_size = 0
{
"vim.smartRelativeLine": true,
"editor.cursorSurroundingLines": 8,
"editor.wordSeparators": "`~!@#$%^&*()=+[{]}\\|;:'\",.<>/?",
"vim.leader": "<space>",
"vim.normalModeKeyBindings": [
{
"before": ["<leader>", "e"],
"commands": ["workbench.view.explorer"]
},