Skip to content

Instantly share code, notes, and snippets.

View gladchinda's full-sized avatar

Glad Chinda gladchinda

View GitHub Profile
gladchinda / ListQueue.js
Created October 19, 2022 17:01
Configurable LL-based queue for JavaScript
var ListQueue = (() => {
const _enumerable_ = value => ({ value, enumerable: true });
const _writable_ = value => ({ ..._enumerable_(value), writable: true });
function ListQueue(config) {
if (!(this instanceof ListQueue)) {
return new ListQueue(config);
const { hash, fifo, maxsize, reorder } =
gladchinda / ArrayQueue.js
Last active October 18, 2022 21:56
Configurable array-based queue for JavaScript
const ArrayQueue = (() => {
const __enumerable__ = value => ({ value, enumerable: true });
function ArrayQueue(config) {
if (!(this instanceof ArrayQueue)) {
return new ArrayQueue(config);
const { equal, fifo, maxsize, reorder } = === "[object Object]"
gladchinda / heap.js
Last active October 8, 2022 08:47
Binary heap implementation for JavaScript
const swap = (arr, index1, index2) => {
const tmp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = tmp;
const sift_down = (heap, cmp, index) => {
const item = heap[index];
const size = heap.length;
gladchinda /
Last active September 13, 2022 13:34
Hierarchy of Headings

Hierarchy of Headings

Markdown has become quite the standard for static contents like blog posts, notes, readmes, etc as popularized by static site generators (SSGs), with a plethora of packages that can parse and transform markdown content into HTML for rendering. Most content written in markdown have multiple layers of headings, hence it is often required that they be rendered alongside a table of contents (hierarchy of headings) to ease navigation through the content.

Consider a blog being developed using one of the popular SSGs for which all blog posts are written in markdown and a parser is used to transform all markdown content to flat (no nested elements) HTML markup for rendering. Given a markdown that has been transformed to HTML, you are required to build a hierarchy of headings that can be used to render a table of contents alongside the content of the markdown.

Flat list of headings

Since the HTML markup has a flat structure, it is easy to get all the heading elements (`h1

gladchinda / oneHourFromNow.js
Last active June 2, 2022 08:43
Get human readable time string for an hour from current time (with optional offset).
const oneHourFromNow = (() => {
const pad = num => `0${num}`.slice(-2);
const parse = num => Math.max(~~parseFloat(num), 0);
const meridian = hour => ["AM", "PM"][+(hour >= 12)];
const tomorrow = date => date.getDate() > new Date().getDate();
const timezone = date => `GMT+${-(date.getTimezoneOffset() / 60)}`
// .replace(/\+?([-+])/, "$1");
.replace(/\+?([-+])(\d+)/, (_, $1, $2) => +$2 ? `${$1}${$2}` : "");
return (offset) => {
gladchinda / threewaypartition.js
Last active May 13, 2022 10:28
Three-way partitioning algorithm with ranking function (for JavaScript)
const __valueranking__ = (minvalue, maxvalue = minvalue) => {
if (maxvalue < minvalue) {
const temp = minvalue;
minvalue = maxvalue;
maxvalue = temp;
return (value) => +(value >= minvalue ? value > maxvalue : -1);
gladchinda / determineBrowsingContext.js
Last active May 12, 2022 14:42
Get useful browsing context information about a window.
function determineBrowsingContext(win = window) {
* ---------------------------------------------------------------------------
* The source browsing context of the specified window object or subframe.
* ---------------------------------------------------------------------------
* It can be a reference to the parent browsing context (for frames and other
* embedded windows), or the opener browsing context (for windows opened from
* another window).
* For windows opened from another window, it is possible not to have access
gladchinda / deep-link-from-browser.js
Created August 21, 2021 13:17 — forked from diachedelic/deep-link-from-browser.js
Deep link to a native app from a browser, with a fallback

JS Data Structures — Maps and Sets

The way, in which data is structured, plays a vital role in being able to efficiently perform certain operations on the data or solve certain problems in relation to the data. For example you can delete any item from a doubly linked list in constant time, whereas that could take linear time if the list is represented as an array. Also, searching for the presence of a key in an array of keys can be done more efficiently (in logarithmic time) when the array is sorted than when not sorted.

Some very popular programming languages like Java and Python provide lots of useful data structure implementations out of the box, as part of their standard library; whereas the ubiquitous "JavaScript" programming language appears to be pretty lean in that regard. However, like most programming languages, JavaScript ships with some very basic data types — such as arrays, strings, objects, sets, maps, etc.

Keyed Collections

Prior to the ECMAScript 2015 specification updates (_

displayPrice: (function() {
const PRICE_CACHE = [];
return function _displayPrice(eventIndex) {
if (!PRICE_CACHE[eventIndex]) {
const tickets = this.ticketsByEvent[eventIndex];
if (tickets && tickets.length > 0) {
tickets.sort((a, b) => a.price - b.price);