Skip to content

Instantly share code, notes, and snippets.

@astromac
astromac / isEmpty.js
Last active April 20, 2021 14:15
isEmpty
/** Determines if passed value is empty or not.
* Works on multiple data types.
**/
const isEmpty = data => {
let count = 0;
if (typeof (data) === 'number' || typeof (data) === 'boolean') { return false; }
if (typeof (data) === 'undefined' || data === null) { return true; }
if (typeof (data.length) !== 'undefined') { return data.length === 0; }
Object.keys(data).forEach(() => { count += 1; });
return count === 0;
@astromac
astromac / findParentElement.js
Last active March 4, 2020 14:00
Traverse through DOM
/* Locates the first parent element
* that has a given attribute.
*/
const findParentElement = (node, attribute) => {
if (isEmpty(node) || isEmpty(attribute)) return null;
let element = node;
/* eslint-disable no-cond-assign, no-empty */
while (
(element = element.parentElement) &&
/* Returns the absolute page position
* of a given element.
*/
const absoluteElementPosition = node => {
if (isEmpty(node)) return null
let element = node
let left = 0
let top = 0
do {
@astromac
astromac / binarytreesearch.js
Last active July 17, 2020 19:49
Binary Tree Search
/**
see https://codepen.io/chrisbfusion/pen/oNbyppO
sample tree visual
30
|
-------
| |
8 52
|