Skip to content

Instantly share code, notes, and snippets.

Henry bidiu

Block or report user

Report or block bidiu

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@bidiu
bidiu / get-distance-to-viewport.js
Last active Apr 4, 2019
Calculate the distance of a give HTML element to viewport
View get-distance-to-viewport.js
// 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
}
View promise_impl.js
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 Mar 12, 2019
implementing range using iterator and generator
View generator-iterator.js
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 Dec 9, 2018
Convert post code
View postcode.js
/**
* @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;
View gist:67d45d4956f2723d97b717dad70cdb8b

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 Feb 21, 2018
Back up Elasticsearch indices.
View es-backup.py
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 Jan 3, 2018 — forked from bitinn/note-on-osx-pingfang.md
A few notes on using OS X 10.11 (El Capitan)'s new Chinese font: PingFang (苹方/萍方).
View note-on-osx-pingfang.md

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 Nov 1, 2017
Regex matching last word of a url
View svn.js
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);
}
View exec-cmd.js
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 });
}
You can’t perform that action at this time.