Skip to content

Instantly share code, notes, and snippets.

View bidiu's full-sized avatar

Henry bidiu

  • Archon Systems Inc.
  • Toronto, ON
  • 03:42 (UTC -04:00)
View GitHub Profile
@bidiu
bidiu / get-distance-to-viewport.js
Last active April 4, 2019 02:26
Calculate the distance of a give HTML element to viewport
// could be replaced by getBoundingClientRect()
function getDistanceToViewport(element: HTMLElement) {
let left = 0, top = 0, current: HTMLElement
current = element
while (current) {
left += current.offsetLeft
top += current.offsetTop
current = current.offsetParent as HTMLElement
}
function Promise(func) {
var state = 'pending';
var deferred = null; // the "then" handler
var value;
function resolve(newValue) {
state = 'resolved';
value = newValue;
if (deferred) {
handle(deferred);
@bidiu
bidiu / generator-iterator.js
Created March 12, 2019 17:45
implementing range using iterator and generator
class RangeIterable {
constructor(start, end, step) {
this.start = start;
this.end = end;
this.step = step;
this.cnt = start;
}
[Symbol.iterator]() {
return this;
@bidiu
bidiu / postcode.js
Last active December 9, 2018 20:53
Convert post code
/**
* @param {string} postCode post code
* @return {string|null}
* post code after an attempt converting to canonical format,
* in case conversion cannot be made, null will be returned.
*/
function convertPostCode(postCode) {
if (!postCode || typeof postCode !== 'string') {
// postCode is null, undefined, or non string
return null;

ETags: a pretty sweet feature of HTTP 1.1

HTTP caching review

HTTP provides two ways for servers to control client-side caching of page components:

  • freshness may be based on a date or a token whose meaning is app-specific
  • whether or not the client needs to confirm the cached version is up-to-date with the server

This breaks down as follows:

  • Cache locally and don't check before using.
@bidiu
bidiu / es-backup.py
Last active February 21, 2018 04:48
Back up Elasticsearch indices.
import time
import datetime
from elasticsearch import Elasticsearch, helpers
# the underlying Elasticsearch client
es = Elasticsearch(['localhost:9200'],
sniff_onz_start=True,
sniff_on_connection_fail=True,
sniffer_timeout=60)
@bidiu
bidiu / note-on-osx-pingfang.md
Created January 3, 2018 14:43 — forked from bitinn/note-on-osx-pingfang.md
A few notes on using OS X 10.11 (El Capitan)'s new Chinese font: PingFang (苹方/萍方).

What's this about?

OS X 10.11, aka El Capitan, comes with a new system font for Chinese users, named PingFang, it includes 6 weights for both Simplified and Traditional Chinese. The same font also appear on iOS 9 as the default UI font, though Apple didn't mention it explicitly.

How to get it?

If you are in Apple Developer Program (costs 99 USD a year), then you can get them now at their developer resource site, otherwise you can wait for their public beta to come out in July or wait for the public release this fall (a free upgrade like previous release).

Or you can get PingFang.ttc from your developer friends, though you are probably violating its font license one way or another, but I am not a lawyer so freedom to you.

@bidiu
bidiu / regex-match-last-word-of-url.md
Last active November 1, 2017 02:26
Regex matching last word of a url
const { execSync } = require('child_process');
const path = require('path');
const fs = require('fs');
const SVN_DIR_PATH = path.join(__dirname, '..', 'svn');
if (!fs.existsSync(SVN_DIR_PATH)) {
fs.mkdirSync(SVN_DIR_PATH);
}
const { exec } = require('child_process');
module.exports = function execCmd(cmd, { fromDir = '.' } = {}) {
return new Promise((resolve, reject) => {
exec(`cd ${fromDir} & ${cmd}`, (error, stdout, stderr) => {
if (!error) {
resolve(stdout);
} else {
reject({ error: error, stderr: stderr });
}