Skip to content

Instantly share code, notes, and snippets.

View bidiu's full-sized avatar

Henry bidiu

  • Archon Systems Inc.
  • Toronto, ON
  • 09:41 (UTC -04:00)
View GitHub Profile
@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;
@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;
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 / 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
}